blob: cca95902449a3e3b73cd5b1c6734aa64498377ec [file] [log] [blame]
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301/*
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +05302 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05303 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#define LOG_TAG "audio_hw_qaf"
31/*#define LOG_NDEBUG 0*/
32/*#define VERY_VERY_VERBOSE_LOGGING*/
33#ifdef VERY_VERY_VERBOSE_LOGGING
34#define ALOGVV ALOGV
35#else
36#define ALOGVV(a...) do { } while(0)
37#endif
38
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +053039#define DEBUG_MSG_VV(arg,...) ALOGVV("%s: %d: " arg, __func__, __LINE__, ##__VA_ARGS__)
Harsh Bansal28718c52017-04-20 13:47:12 +053040#define DEBUG_MSG(arg,...) ALOGV("%s: %d: " arg, __func__, __LINE__, ##__VA_ARGS__)
41#define ERROR_MSG(arg,...) ALOGE("%s: %d: " arg, __func__, __LINE__, ##__VA_ARGS__)
42
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053043#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 2
Harsh Bansal28718c52017-04-20 13:47:12 +053044#define COMPRESS_PASSTHROUGH_DDP_FRAGMENT_SIZE 4608
45
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053046#define QAF_DEFAULT_COMPR_AUDIO_HANDLE 1001
47#define QAF_DEFAULT_COMPR_PASSTHROUGH_HANDLE 1002
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053048#define QAF_DEFAULT_PASSTHROUGH_HANDLE 1003
Ben Rombergerd771a7c2017-02-22 18:05:17 -080049
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053050#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 300
Varun B34da7a42017-02-13 16:16:53 +053051
Varun B34da7a42017-02-13 16:16:53 +053052#define MIN_PCM_OFFLOAD_FRAGMENT_SIZE 512
53#define MAX_PCM_OFFLOAD_FRAGMENT_SIZE (240 * 1024)
54
55#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
56#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
57
58/* Pcm input node buffer size is 6144 bytes, i.e, 32msec for 48000 samplerate */
Harsh Bansal28718c52017-04-20 13:47:12 +053059#define QAF_MODULE_PCM_INPUT_BUFFER_LATENCY 32
Varun B34da7a42017-02-13 16:16:53 +053060
Harsh Bansal28718c52017-04-20 13:47:12 +053061#define MS12_PCM_OUT_FRAGMENT_SIZE 1536 //samples
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +053062#define MS12_PCM_IN_FRAGMENT_SIZE 1536 //samples
Varun B34da7a42017-02-13 16:16:53 +053063
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053064#define DD_FRAME_SIZE 1536
Harsh Bansal28718c52017-04-20 13:47:12 +053065#define DDP_FRAME_SIZE DD_FRAME_SIZE
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053066/*
Harsh Bansal28718c52017-04-20 13:47:12 +053067 * DD encoder output size for one frame.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053068 */
69#define DD_ENCODER_OUTPUT_SIZE 2560
70/*
Harsh Bansal28718c52017-04-20 13:47:12 +053071 * DDP encoder output size for one frame.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053072 */
73#define DDP_ENCODER_OUTPUT_SIZE 4608
74
Harsh Bansal28718c52017-04-20 13:47:12 +053075/*********TODO Need to get correct values.*************************/
76
77#define DTS_PCM_OUT_FRAGMENT_SIZE 1024 //samples
78
79#define DTS_FRAME_SIZE 1536
80#define DTSHD_FRAME_SIZE DTS_FRAME_SIZE
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053081/*
Harsh Bansal28718c52017-04-20 13:47:12 +053082 * DTS encoder output size for one frame.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053083 */
Harsh Bansal28718c52017-04-20 13:47:12 +053084#define DTS_ENCODER_OUTPUT_SIZE 2560
85/*
86 * DTSHD encoder output size for one frame.
87 */
88#define DTSHD_ENCODER_OUTPUT_SIZE 4608
89/******************************************************************/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053090
Bharath Gopal01310bb2016-12-05 15:39:32 +053091/*
92 * QAF Latency to process buffers since out_write from primary HAL
93 */
94#define QAF_COMPRESS_OFFLOAD_PROCESSING_LATENCY 18
95#define QAF_PCM_OFFLOAD_PROCESSING_LATENCY 48
96
Harsh Bansal28718c52017-04-20 13:47:12 +053097//TODO: Need to handle for DTS
Varun B34da7a42017-02-13 16:16:53 +053098#define QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1536
99
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530100#include <stdlib.h>
101#include <pthread.h>
102#include <errno.h>
103#include <dlfcn.h>
104#include <sys/resource.h>
105#include <sys/prctl.h>
106#include <cutils/properties.h>
107#include <cutils/str_parms.h>
108#include <cutils/log.h>
109#include <cutils/atomic.h>
110#include "audio_utils/primitives.h"
111#include "audio_hw.h"
112#include "platform_api.h"
113#include <platform.h>
114#include <system/thread_defs.h>
115#include <cutils/sched_policy.h>
116#include "audio_extn.h"
117#include <qti_audio.h>
118#include "sound/compress_params.h"
119
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +0530120#ifdef DYNAMIC_LOG_ENABLED
121#include <log_xml_parser.h>
122#define LOG_MASK HAL_MOD_FILE_QAF
123#include <log_utils.h>
124#endif
125
Harsh Bansal28718c52017-04-20 13:47:12 +0530126//TODO: Need to remove this.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530127#define QAF_OUTPUT_SAMPLING_RATE 48000
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530128
129#ifdef QAF_DUMP_ENABLED
130FILE *fp_output_writer_hdmi = NULL;
131#endif
132
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800133struct qaf_adsp_hdlr_config_state {
134 struct audio_adsp_event event_params;
135 /* For holding client audio_adsp_event payload */
136 uint8_t event_payload[AUDIO_MAX_ADSP_STREAM_CMD_PAYLOAD_LEN];
137 bool adsp_hdlr_config_valid;
138};
139
Harsh Bansal28718c52017-04-20 13:47:12 +0530140//Types of MM module, currently supported by QAF.
141typedef enum {
142 MS12,
143 DTS_M8,
144 MAX_MM_MODULE_TYPE,
145 INVALID_MM_MODULE
146} mm_module_type;
147
148typedef enum {
149 QAF_OUT_TRANSCODE_PASSTHROUGH = 0, /* Transcode passthrough via MM module*/
150 QAF_OUT_OFFLOAD_MCH, /* Multi-channel PCM offload*/
151 QAF_OUT_OFFLOAD, /* PCM offload */
152
153 MAX_QAF_MODULE_OUT
154} mm_module_output_type;
155
156typedef enum {
157 QAF_IN_MAIN = 0, /* Single PID Main/Primary or Dual-PID stream */
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530158 QAF_IN_ASSOC, /* Associated/Secondary stream */
159 QAF_IN_PCM, /* PCM stream. */
160 QAF_IN_MAIN_2, /* Single PID Main2 stream */
Harsh Bansal28718c52017-04-20 13:47:12 +0530161 MAX_QAF_MODULE_IN
162} mm_module_input_type;
163
164struct qaf_module {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530165 audio_session_handle_t session_handle;
166 void *qaf_lib;
Harsh Bansal28718c52017-04-20 13:47:12 +0530167 int (*qaf_audio_session_open)(audio_session_handle_t* session_handle,
Sri Karri1fbfef52017-06-29 19:45:47 +0530168 audio_session_type_t s_type,
Harsh Bansal28718c52017-04-20 13:47:12 +0530169 void *p_data,
170 void* license_data);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530171 int (*qaf_audio_session_close)(audio_session_handle_t session_handle);
Harsh Bansal28718c52017-04-20 13:47:12 +0530172 int (*qaf_audio_stream_open)(audio_session_handle_t session_handle,
173 audio_stream_handle_t* stream_handle,
174 audio_stream_config_t input_config,
175 audio_devices_t devices,
176 stream_type_t flags);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530177 int (*qaf_audio_stream_close)(audio_stream_handle_t stream_handle);
178 int (*qaf_audio_stream_set_param)(audio_stream_handle_t stream_handle, const char* kv_pairs);
179 int (*qaf_audio_session_set_param)(audio_session_handle_t handle, const char* kv_pairs);
180 char* (*qaf_audio_stream_get_param)(audio_stream_handle_t stream_handle, const char* key);
181 char* (*qaf_audio_session_get_param)(audio_session_handle_t handle, const char* key);
182 int (*qaf_audio_stream_start)(audio_stream_handle_t handle);
183 int (*qaf_audio_stream_stop)(audio_stream_handle_t stream_handle);
184 int (*qaf_audio_stream_pause)(audio_stream_handle_t stream_handle);
185 int (*qaf_audio_stream_flush)(audio_stream_handle_t stream_handle);
186 int (*qaf_audio_stream_write)(audio_stream_handle_t stream_handle, const void* buf, int size);
Harsh Bansal28718c52017-04-20 13:47:12 +0530187 void (*qaf_register_event_callback)(audio_session_handle_t session_handle,
188 void *priv_data,
189 notify_event_callback_t event_callback,
190 audio_event_id_t event_id);
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800191
Harsh Bansal28718c52017-04-20 13:47:12 +0530192 /*Input stream of MM module */
193 struct stream_out *stream_in[MAX_QAF_MODULE_IN];
194 /*Output Stream from MM module */
195 struct stream_out *stream_out[MAX_QAF_MODULE_OUT];
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800196
Harsh Bansal431eede2017-04-28 21:22:30 +0530197 /*Media format associated with each output id raised by mm module. */
198 audio_qaf_media_format_t out_stream_fmt[MAX_QAF_MODULE_OUT];
199 /*Flag is set if media format is changed for an mm module output. */
200 bool is_media_fmt_changed[MAX_QAF_MODULE_OUT];
201 /*Index to be updated in out_stream_fmt array for a new mm module output. */
202 int new_out_format_index;
203
Harsh Bansal28718c52017-04-20 13:47:12 +0530204 struct qaf_adsp_hdlr_config_state adsp_hdlr_config[MAX_QAF_MODULE_IN];
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800205
Harsh Bansal28718c52017-04-20 13:47:12 +0530206 //BT session handle.
Bharath Gopal01310bb2016-12-05 15:39:32 +0530207 void *bt_hdl;
Harsh Bansal28718c52017-04-20 13:47:12 +0530208
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530209 float vol_left;
210 float vol_right;
Harsh Bansal28718c52017-04-20 13:47:12 +0530211 bool is_vol_set;
212 bool drain_received[MAX_QAF_MODULE_IN];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530213};
214
Harsh Bansal28718c52017-04-20 13:47:12 +0530215struct qaf {
216 struct audio_device *adev;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530217
Harsh Bansal28718c52017-04-20 13:47:12 +0530218 pthread_mutex_t lock;
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800219
Harsh Bansal28718c52017-04-20 13:47:12 +0530220 bool hdmi_connect;
221 int hdmi_sink_channels;
222
223 //Flag to indicate if QAF transcode output stream is enabled from any mm module.
224 bool passthrough_enabled;
225 //Flag to indicate if QAF mch pcm output stream is enabled from any mm module.
226 bool mch_pcm_hdmi_enabled;
227
228 //Flag to indicate if msmd is supported.
229 bool qaf_msmd_enabled;
230
231 //Handle of QAF input stream, which is routed as QAF passthrough.
232 struct stream_out *passthrough_in;
233 //Handle of QAF passthrough stream.
234 struct stream_out *passthrough_out;
235
236 struct qaf_module qaf_mod[MAX_MM_MODULE_TYPE];
237};
238
239static int qaf_out_pause(struct audio_stream_out* stream);
240static int qaf_out_flush(struct audio_stream_out* stream);
241static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type);
242
243//Global handle of QAF. Access to this should be protected by mutex lock.
244static struct qaf *p_qaf = NULL;
245
246/* Gets the pointer to qaf module for the qaf input stream. */
247static struct qaf_module* get_qaf_module_for_input_stream(struct stream_out *out)
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530248{
Harsh Bansal28718c52017-04-20 13:47:12 +0530249 struct qaf_module *qaf_mod = NULL;
250 int i, j;
251 if (!p_qaf) return NULL;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530252
Harsh Bansal28718c52017-04-20 13:47:12 +0530253 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
254 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
255 if (p_qaf->qaf_mod[i].stream_in[j] == out) {
256 qaf_mod = &(p_qaf->qaf_mod[i]);
257 break;
258 }
259 }
260 }
261
262 return qaf_mod;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530263}
264
Harsh Bansal28718c52017-04-20 13:47:12 +0530265/* Finds the mm module input stream index for the QAF input stream. */
266static int get_input_stream_index(struct stream_out *out)
267{
268 int index = -1, j;
269 struct qaf_module* qaf_mod = NULL;
270
271 qaf_mod = get_qaf_module_for_input_stream(out);
272 if (!qaf_mod) return index;
273
274 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
275 if (qaf_mod->stream_in[j] == out) {
276 index = j;
277 break;
278 }
279 }
280
281 return index;
282}
283
284/* Finds the right mm module for the QAF input stream format. */
285static mm_module_type get_mm_module_for_format(audio_format_t format)
286{
287 int j;
288
289 DEBUG_MSG("Format 0x%x", format);
290
291 if (format == AUDIO_FORMAT_PCM_16_BIT) {
292 //If dts is not supported then alway support pcm with MS12
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700293 if (!property_get_bool("vendor.audio.qaf.dts_m8", false)) { //TODO: Need to add this property for DTS.
Harsh Bansal28718c52017-04-20 13:47:12 +0530294 return MS12;
295 }
296
297 //If QAF passthrough is active then send the PCM stream to primary HAL.
298 if (!p_qaf->passthrough_out) {
299 /* Iff any stream is active in MS12 module then route PCM stream to it. */
300 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
301 if (p_qaf->qaf_mod[MS12].stream_in[j]) {
302 return MS12;
303 }
304 }
305 }
306 return INVALID_MM_MODULE;
307 }
308
309 switch (format & AUDIO_FORMAT_MAIN_MASK) {
310 case AUDIO_FORMAT_AC3:
311 case AUDIO_FORMAT_E_AC3:
312 case AUDIO_FORMAT_AAC:
313 case AUDIO_FORMAT_AAC_ADTS:
314 case AUDIO_FORMAT_AC4:
315 return MS12;
316 case AUDIO_FORMAT_DTS:
317 case AUDIO_FORMAT_DTS_HD:
318 return DTS_M8;
319 default:
320 return INVALID_MM_MODULE;
321 }
322}
323
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530324static bool is_main_active(struct qaf_module* qaf_mod)
325{
326 return (qaf_mod->stream_in[QAF_IN_MAIN] || qaf_mod->stream_in[QAF_IN_MAIN_2]);
327}
328
329static bool is_dual_main_active(struct qaf_module* qaf_mod)
330{
331 return (qaf_mod->stream_in[QAF_IN_MAIN] && qaf_mod->stream_in[QAF_IN_MAIN_2]);
332}
333
Harsh Bansal28718c52017-04-20 13:47:12 +0530334/* Gets the pcm output buffer size(in samples) for the mm module. */
335static uint32_t get_pcm_output_buffer_size_samples(struct qaf_module *qaf_mod)
336{
337 uint32_t pcm_output_buffer_size = 0;
338
339 if (qaf_mod == &p_qaf->qaf_mod[MS12]) {
340 pcm_output_buffer_size = MS12_PCM_OUT_FRAGMENT_SIZE;
341 } else if (qaf_mod == &p_qaf->qaf_mod[DTS_M8]) {
342 pcm_output_buffer_size = DTS_PCM_OUT_FRAGMENT_SIZE;
343 }
344
345 return pcm_output_buffer_size;
346}
347
Harsh Bansal431eede2017-04-28 21:22:30 +0530348static int get_media_fmt_array_index_for_output_id(
349 struct qaf_module* qaf_mod,
350 int output_id)
351{
352 int i;
353 for (i = 0; i < MAX_QAF_MODULE_OUT; i++) {
354 if (qaf_mod->out_stream_fmt[i].output_id == output_id) {
355 return i;
356 }
357 }
358 return -1;
359}
360
Harsh Bansal28718c52017-04-20 13:47:12 +0530361/* Acquire Mutex lock on output stream */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530362static void lock_output_stream(struct stream_out *out)
363{
364 pthread_mutex_lock(&out->pre_lock);
365 pthread_mutex_lock(&out->lock);
366 pthread_mutex_unlock(&out->pre_lock);
367}
368
Harsh Bansal28718c52017-04-20 13:47:12 +0530369/* Release Mutex lock on output stream */
370static void unlock_output_stream(struct stream_out *out)
371{
372 pthread_mutex_unlock(&out->lock);
373}
374
375/* Checks if stream can be routed as QAF passthrough or not. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530376static bool audio_extn_qaf_passthrough_enabled(struct stream_out *out)
377{
Harsh Bansal28718c52017-04-20 13:47:12 +0530378 DEBUG_MSG("Format 0x%x", out->format);
379 bool is_enabled = false;
380
381 if (!p_qaf) return false;
382
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700383 if ((!property_get_bool("vendor.audio.qaf.reencode", false))
384 && property_get_bool("vendor.audio.qaf.passthrough", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530385
386 if ((out->format == AUDIO_FORMAT_PCM_16_BIT) && (popcount(out->channel_mask) > 2)) {
387 is_enabled = true;
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700388 } else if (property_get_bool("vendor.audio.offload.passthrough", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530389 switch (out->format) {
390 case AUDIO_FORMAT_AC3:
391 case AUDIO_FORMAT_E_AC3:
392 case AUDIO_FORMAT_DTS:
Ben Romberger1aaaf862017-04-06 17:49:46 -0700393 case AUDIO_FORMAT_DTS_HD:
Naresh Tanniru928f0862017-04-07 16:44:23 -0700394 case AUDIO_FORMAT_DOLBY_TRUEHD:
395 case AUDIO_FORMAT_IEC61937: {
Harsh Bansal28718c52017-04-20 13:47:12 +0530396 is_enabled = true;
397 break;
398 }
399 default:
400 is_enabled = false;
401 break;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530402 }
403 }
404 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530405
406 return is_enabled;
407}
408
409/*Closes all pcm hdmi output from QAF. */
410static void close_all_pcm_hdmi_output()
411{
412 int i;
413 //Closing all the PCM HDMI output stream from QAF.
414 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
415 if (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]) {
416 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
417 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]));
418 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH] = NULL;
419 }
420
421 if ((p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD])
422 && (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
423 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
424 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]));
425 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD] = NULL;
426 }
427 }
428
429 p_qaf->mch_pcm_hdmi_enabled = 0;
430}
431
432static void close_all_hdmi_output()
433{
434 int k;
435 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
436 if (p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
437 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
438 (struct audio_stream_out *)(p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]));
439 p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] = NULL;
440 }
441 }
442 p_qaf->passthrough_enabled = 0;
443
444 close_all_pcm_hdmi_output();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530445}
446
447static int qaf_out_callback(stream_callback_event_t event, void *param __unused, void *cookie)
448{
449 struct stream_out *out = (struct stream_out *)cookie;
450
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800451 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530452 return 0;
453}
454
Harsh Bansal28718c52017-04-20 13:47:12 +0530455/* Creates the QAF passthrough output stream. */
456static int create_qaf_passthrough_stream()
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530457{
Harsh Bansal28718c52017-04-20 13:47:12 +0530458 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530459
Harsh Bansal28718c52017-04-20 13:47:12 +0530460 int ret = 0, k;
461 struct stream_out *out = p_qaf->passthrough_in;
462
463 if (!out) return -EINVAL;
464
465 pthread_mutex_lock(&p_qaf->lock);
466 lock_output_stream(out);
467
468 //Creating QAF passthrough output stream.
469 if (NULL == p_qaf->passthrough_out) {
470 audio_output_flags_t flags;
471 struct audio_config config;
472 audio_devices_t devices;
473
474 config.sample_rate = config.offload_info.sample_rate = out->sample_rate;
475 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
476 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
477 config.offload_info.format = out->format;
478 config.offload_info.bit_width = out->bit_width;
479 config.format = out->format;
480 config.offload_info.channel_mask = config.channel_mask = out->channel_mask;
481
482 //Device is copied from the QAF passthrough input stream.
483 devices = out->devices;
484 flags = out->flags;
485
486 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
487 QAF_DEFAULT_PASSTHROUGH_HANDLE,
488 devices,
489 flags,
490 &config,
491 (struct audio_stream_out **)&(p_qaf->passthrough_out),
492 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530493 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530494 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
495 unlock_output_stream(out);
496 return ret;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530497 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530498 p_qaf->passthrough_in = out;
499 p_qaf->passthrough_out->stream.set_callback((struct audio_stream_out *)p_qaf->passthrough_out,
500 (stream_callback_t) qaf_out_callback, out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530501 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530502
503 unlock_output_stream(out);
504
505 //Since QAF-Passthrough is created, close other HDMI outputs.
506 close_all_hdmi_output();
507
508 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530509 return ret;
510}
511
Harsh Bansal28718c52017-04-20 13:47:12 +0530512/* Closes the QAF passthrough output stream. */
513static void close_qaf_passthrough_stream()
514{
515 if (p_qaf->passthrough_out != NULL) { //QAF pasthroug is enabled. Close it.
516 pthread_mutex_lock(&p_qaf->lock);
517 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
518 (struct audio_stream_out *)(p_qaf->passthrough_out));
519 p_qaf->passthrough_out = NULL;
520 pthread_mutex_unlock(&p_qaf->lock);
521
522 if (p_qaf->passthrough_in->qaf_stream_handle) {
523 qaf_out_pause((struct audio_stream_out*)p_qaf->passthrough_in);
524 qaf_out_flush((struct audio_stream_out*)p_qaf->passthrough_in);
525 qaf_out_drain((struct audio_stream_out*)p_qaf->passthrough_in,
526 (audio_drain_type_t)STREAM_CBK_EVENT_DRAIN_READY);
527 }
528 }
529}
530
531/* Sends a command to output stream offload thread. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530532static int qaf_send_offload_cmd_l(struct stream_out* out, int command)
533{
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530534 DEBUG_MSG_VV("command is %d", command);
Harsh Bansal28718c52017-04-20 13:47:12 +0530535
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530536 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
537
538 if (!cmd) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530539 ERROR_MSG("failed to allocate mem for command 0x%x", command);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530540 return -ENOMEM;
541 }
542
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530543 cmd->cmd = command;
Harsh Bansal28718c52017-04-20 13:47:12 +0530544
545 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530546 list_add_tail(&out->qaf_offload_cmd_list, &cmd->node);
547 pthread_cond_signal(&out->qaf_offload_cond);
Harsh Bansal28718c52017-04-20 13:47:12 +0530548 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530549 return 0;
550}
551
Harsh Bansal28718c52017-04-20 13:47:12 +0530552/* Stops a QAF module stream.*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530553static int audio_extn_qaf_stream_stop(struct stream_out *out)
554{
Harsh Bansal28718c52017-04-20 13:47:12 +0530555 int ret = 0;
556 DEBUG_MSG("Output Stream 0x%x", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530557
Harsh Bansal28718c52017-04-20 13:47:12 +0530558 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
559 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_stop)) {
560 return ret;
561 }
562
563 if (out->qaf_stream_handle) {
564 ret = qaf_mod->qaf_audio_stream_stop(out->qaf_stream_handle);
565 }
566
567 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530568}
569
Harsh Bansal28718c52017-04-20 13:47:12 +0530570/* Puts a QAF module stream in standby. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530571static int qaf_out_standby(struct audio_stream *stream)
572{
573 struct stream_out *out = (struct stream_out *)stream;
574 int status = 0;
575
576 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
577 stream, out->usecase, use_case_table[out->usecase]);
578
579 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530580
581 //If QAF passthrough is active then block standby on all the input streams of QAF mm modules.
582 if (p_qaf->passthrough_out) {
583 //If standby is received on QAF passthrough stream then forward it to primary HAL.
584 if (p_qaf->passthrough_in == out) {
585 status = p_qaf->passthrough_out->stream.common.standby(
586 (struct audio_stream *)p_qaf->passthrough_out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530587 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530588 } else {
589 //If QAF passthrough stream is not active then stop the QAF module stream.
590 status = audio_extn_qaf_stream_stop(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530591 }
592
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530593 if (!out->standby) {
594 out->standby = true;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530595 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530596
597 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530598 return status;
599}
600
Harsh Bansal28718c52017-04-20 13:47:12 +0530601/* Sets the volume to PCM output stream. */
602static int qaf_out_set_volume(struct audio_stream_out *stream, float left, float right)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530603{
604 int ret = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530605 struct stream_out *out = (struct stream_out *)stream;
606 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530607
Harsh Bansal28718c52017-04-20 13:47:12 +0530608 DEBUG_MSG("Left %f, Right %f", left, right);
609
610 qaf_mod = get_qaf_module_for_input_stream(out);
611 if (!qaf_mod) {
612 return -EINVAL;
613 }
614
615 pthread_mutex_lock(&p_qaf->lock);
616 qaf_mod->vol_left = left;
617 qaf_mod->vol_right = right;
618 qaf_mod->is_vol_set = true;
619 pthread_mutex_unlock(&p_qaf->lock);
620
621 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL) {
622 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
623 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], left, right);
624 }
625
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530626 return ret;
627}
628
Harsh Bansal28718c52017-04-20 13:47:12 +0530629/* Starts a QAF module stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530630static int qaf_stream_start(struct stream_out *out)
631{
Harsh Bansal28718c52017-04-20 13:47:12 +0530632 int ret = -EINVAL;
633 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530634
Harsh Bansal28718c52017-04-20 13:47:12 +0530635 DEBUG_MSG("Output Stream = %p", out);
636
637 qaf_mod = get_qaf_module_for_input_stream(out);
638 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_start)) {
639 return -EINVAL;
640 }
641
642 if (out->qaf_stream_handle) {
643 ret = qaf_mod->qaf_audio_stream_start(out->qaf_stream_handle);
644 }
645
646 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530647}
648
649static int qaf_start_output_stream(struct stream_out *out)
650{
651 int ret = 0;
652 struct audio_device *adev = out->dev;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530653
654 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
655 ret = -EINVAL;
656 usleep(50000);
657 return ret;
658 }
659
660 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
661 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
662 out->devices);
663
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530664 if (CARD_STATUS_OFFLINE == out->card_status ||
665 CARD_STATUS_OFFLINE == adev->card_status) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530666 ALOGE("%s: sound card is not active/SSR returning error", __func__);
667 ret = -EIO;
668 usleep(50000);
669 return ret;
670 }
671
672 return qaf_stream_start(out);
673}
674
Harsh Bansal28718c52017-04-20 13:47:12 +0530675/* Sends input buffer to the QAF MM module. */
676static int qaf_module_write_input_buffer(struct stream_out *out, const void *buffer, int bytes)
677{
678 int ret = -EINVAL;
679 struct qaf_module *qaf_mod = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +0530680
681 qaf_mod = get_qaf_module_for_input_stream(out);
682 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_write)) {
683 return ret;
684 }
685
686 if (out->qaf_stream_handle) {
687 ret = qaf_mod->qaf_audio_stream_write(out->qaf_stream_handle, buffer, bytes);
688 }
689 return ret;
690}
691
692/* Writes buffer to QAF input stream. */
693static ssize_t qaf_out_write(struct audio_stream_out *stream, const void *buffer, size_t bytes)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530694{
695 struct stream_out *out = (struct stream_out *)stream;
696 struct audio_device *adev = out->dev;
697 ssize_t ret = 0;
698
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530699 DEBUG_MSG_VV("bytes = %d, usecase[%d] and flags[%x] for handle[%p]",
Harsh Bansal28718c52017-04-20 13:47:12 +0530700 (int)bytes, out->usecase, out->flags, out);
701
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530702 lock_output_stream(out);
703
Harsh Bansal28718c52017-04-20 13:47:12 +0530704 // If QAF passthrough is active then block writing data to QAF mm module.
705 if (p_qaf->passthrough_out) {
706 //If write is received for the QAF passthrough stream then send the buffer to primary HAL.
707 if (p_qaf->passthrough_in == out) {
708 ret = p_qaf->passthrough_out->stream.write(
709 (struct audio_stream_out *)(p_qaf->passthrough_out),
710 buffer,
711 bytes);
712 if (ret > 0) out->standby = false;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530713 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530714 unlock_output_stream(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530715 return ret;
Harsh Bansal28718c52017-04-20 13:47:12 +0530716 } else if (out->standby) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530717 pthread_mutex_lock(&adev->lock);
718 ret = qaf_start_output_stream(out);
719 pthread_mutex_unlock(&adev->lock);
Harsh Bansal28718c52017-04-20 13:47:12 +0530720 if (ret == 0) {
721 out->standby = false;
722 } else {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530723 goto exit;
724 }
725 }
726
Harsh Bansal28718c52017-04-20 13:47:12 +0530727 if ((adev->is_channel_status_set == false) && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530728 audio_utils_set_hdmi_channel_status(out, (char *)buffer, bytes);
729 adev->is_channel_status_set = true;
730 }
731
Harsh Bansal28718c52017-04-20 13:47:12 +0530732 ret = qaf_module_write_input_buffer(out, buffer, bytes);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530733 DEBUG_MSG_VV("ret [%d]", (int)ret);
Harsh Bansal28718c52017-04-20 13:47:12 +0530734
735 if (ret >= 0) {
736 bytes = ret;
737 out->written += bytes / ((popcount(out->channel_mask) * sizeof(short)));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530738 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530739
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530740
741exit:
Harsh Bansal28718c52017-04-20 13:47:12 +0530742 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530743
744 if (ret < 0) {
745 if (ret == -EAGAIN) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530746 DEBUG_MSG_VV("No space available in mm module, post msg to cb thread");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530747 ret = qaf_send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530748 bytes = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530749 } else if (ret == -ENOMEM || ret == -EPERM) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530750 if (out->pcm)
Harsh Bansal28718c52017-04-20 13:47:12 +0530751 ERROR_MSG("error %d, %s", (int)ret, pcm_get_error(out->pcm));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530752 qaf_out_standby(&out->stream.common);
Harsh Bansal28718c52017-04-20 13:47:12 +0530753 usleep(bytes * 1000000
754 / audio_stream_out_frame_size(stream)
755 / out->stream.common.get_sample_rate(&out->stream.common));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530756 }
757 }
758 return bytes;
759}
760
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530761/* Gets PCM offload buffer size for a given config. */
762static uint32_t qaf_get_pcm_offload_buffer_size(audio_offload_info_t* info,
763 uint32_t samples_per_frame)
Varun B34da7a42017-02-13 16:16:53 +0530764{
765 uint32_t fragment_size = 0;
Varun B34da7a42017-02-13 16:16:53 +0530766
Harsh Bansal28718c52017-04-20 13:47:12 +0530767 fragment_size = (samples_per_frame * (info->bit_width >> 3) * popcount(info->channel_mask));
768
769 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530770 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530771 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530772 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530773
Varun B34da7a42017-02-13 16:16:53 +0530774 // To have same PCM samples for all channels, the buffer size requires to
775 // be multiple of (number of channels * bytes per sample)
776 // For writes to succeed, the buffer must be written at address which is multiple of 32
Harsh Bansal28718c52017-04-20 13:47:12 +0530777 fragment_size = ALIGN(fragment_size,
778 ((info->bit_width >> 3) * popcount(info->channel_mask) * 32));
Varun B34da7a42017-02-13 16:16:53 +0530779
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530780 ALOGI("Qaf PCM offload Fragment size is %d bytes", fragment_size);
Varun B34da7a42017-02-13 16:16:53 +0530781
782 return fragment_size;
783}
784
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530785static uint32_t qaf_get_pcm_offload_input_buffer_size(info)
786{
787 return qaf_get_pcm_offload_buffer_size(info, MS12_PCM_IN_FRAGMENT_SIZE);
788}
789
790static uint32_t qaf_get_pcm_offload_output_buffer_size(struct qaf_module *qaf_mod,
791 audio_offload_info_t* info)
792{
793 return qaf_get_pcm_offload_buffer_size(info, get_pcm_output_buffer_size_samples(qaf_mod));
794}
795
Harsh Bansal28718c52017-04-20 13:47:12 +0530796/* Gets buffer latency in samples. */
797static int get_buffer_latency(struct stream_out *out, uint32_t buffer_size, uint32_t *latency)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530798{
Harsh Bansal28718c52017-04-20 13:47:12 +0530799 unsigned long int samples_in_one_encoded_frame;
800 unsigned long int size_of_one_encoded_frame;
801
802 switch (out->format) {
803 case AUDIO_FORMAT_AC3:
804 samples_in_one_encoded_frame = DD_FRAME_SIZE;
805 size_of_one_encoded_frame = DD_ENCODER_OUTPUT_SIZE;
806 break;
807 case AUDIO_FORMAT_E_AC3:
808 samples_in_one_encoded_frame = DDP_FRAME_SIZE;
809 size_of_one_encoded_frame = DDP_ENCODER_OUTPUT_SIZE;
810 break;
811 case AUDIO_FORMAT_DTS:
812 samples_in_one_encoded_frame = DTS_FRAME_SIZE;
813 size_of_one_encoded_frame = DTS_ENCODER_OUTPUT_SIZE;
814 break;
815 case AUDIO_FORMAT_DTS_HD:
816 samples_in_one_encoded_frame = DTSHD_FRAME_SIZE;
817 size_of_one_encoded_frame = DTSHD_ENCODER_OUTPUT_SIZE;
818 break;
819 case AUDIO_FORMAT_PCM_16_BIT:
820 samples_in_one_encoded_frame = 1;
821 size_of_one_encoded_frame = ((out->bit_width) >> 3) * popcount(out->channel_mask);
822 break;
823 default:
824 *latency = 0;
825 return (-EINVAL);
826 }
827
828 *latency = ((buffer_size * samples_in_one_encoded_frame) / size_of_one_encoded_frame);
829 return 0;
830}
831
832/* Returns the number of frames rendered to outside observer. */
833static int qaf_get_rendered_frames(struct stream_out *out, uint64_t *frames)
834{
835 int ret = 0, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530836 struct str_parms *parms;
837 int value = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530838 int module_latency = 0;
839 uint32_t kernel_latency = 0;
840 uint32_t dsp_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530841 int signed_frames = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530842 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +0530843 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530844
Harsh Bansal28718c52017-04-20 13:47:12 +0530845 DEBUG_MSG("Output Format %d", out->format);
846
847 qaf_mod = get_qaf_module_for_input_stream(out);
848 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_get_param)) {
849 return -EINVAL;
850 }
851
852 //Get MM module latency.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530853 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "get_latency");
854 if (kvpairs) {
855 parms = str_parms_create_str(kvpairs);
Harsh Bansal28718c52017-04-20 13:47:12 +0530856 ret = str_parms_get_int(parms, "get_latency", &module_latency);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530857 if (ret >= 0) {
858 str_parms_destroy(parms);
859 parms = NULL;
860 }
861 free(kvpairs);
862 kvpairs = NULL;
863 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530864
865 //Get kernel Latency
866 for (i = MAX_QAF_MODULE_OUT - 1; i >= 0; i--) {
867 if (qaf_mod->stream_out[i] == NULL) {
868 continue;
869 } else {
870 unsigned int num_fragments = qaf_mod->stream_out[i]->compr_config.fragments;
871 uint32_t fragment_size = qaf_mod->stream_out[i]->compr_config.fragment_size;
872 uint32_t kernel_buffer_size = num_fragments * fragment_size;
873 get_buffer_latency(qaf_mod->stream_out[i], kernel_buffer_size, &kernel_latency);
874 break;
875 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530876 }
877
Harsh Bansal28718c52017-04-20 13:47:12 +0530878 //Get DSP latency
879 if ((qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL)
880 || (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH] != NULL)) {
881 unsigned int sample_rate = 0;
882
883 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
884 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
885 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
886 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
887
888 audio_usecase_t platform_latency =
889 platform_render_latency(qaf_mod->stream_out[QAF_OUT_OFFLOAD]->usecase);
890 dsp_latency = (platform_latency * sample_rate) / 1000000LL;
891 } else if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] != NULL) {
892 unsigned int sample_rate = 0;
893
894 sample_rate = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->sample_rate; //TODO: How this sample rate can be used?
895 dsp_latency = (COMPRESS_OFFLOAD_PLAYBACK_LATENCY * sample_rate) / 1000;
896 }
897
898 // MM Module Latency + Kernel Latency + DSP Latency
899 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
900 out->platform_latency = module_latency + audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl);
901 } else {
902 out->platform_latency = (uint32_t)module_latency + kernel_latency + dsp_latency;
903 }
904
905 if (out->format & AUDIO_FORMAT_PCM_16_BIT) {
906 *frames = 0;
907 signed_frames = out->written - out->platform_latency;
908 // It would be unusual for this value to be negative, but check just in case ...
909 if (signed_frames >= 0) {
910 *frames = signed_frames;
911 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530912 } else if (qaf_mod->qaf_audio_stream_get_param) {
913 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "position");
914 if (kvpairs) {
915 parms = str_parms_create_str(kvpairs);
916 ret = str_parms_get_int(parms, "position", &value);
917 if (ret >= 0) {
918 *frames = value;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530919 signed_frames = value - out->platform_latency;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530920 // It would be unusual for this value to be negative, but check just in case ...
921 if (signed_frames >= 0) {
922 *frames = signed_frames;
923 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530924 }
925 str_parms_destroy(parms);
926 }
927 } else {
928 ret = -EINVAL;
929 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530930
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530931 return ret;
932}
933
934static int qaf_out_get_presentation_position(const struct audio_stream_out *stream,
Harsh Bansal28718c52017-04-20 13:47:12 +0530935 uint64_t *frames,
936 struct timespec *timestamp)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530937{
938 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530939 int ret = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530940
Harsh Bansal28718c52017-04-20 13:47:12 +0530941 DEBUG_MSG("Output Stream %p", stream);
942
943 //If QAF passthorugh output stream is active.
944 if (p_qaf->passthrough_out) {
945 if (p_qaf->passthrough_in == out) {
946 //If api is called for QAF passthorugh stream then call the primary HAL api to get the position.
947 pthread_mutex_lock(&p_qaf->lock);
948 ret = p_qaf->passthrough_out->stream.get_presentation_position(
949 (struct audio_stream_out *)p_qaf->passthrough_out,
950 frames,
951 timestamp);
952 pthread_mutex_unlock(&p_qaf->lock);
953 } else {
954 //If api is called for other stream then return zero frames.
955 *frames = 0;
956 clock_gettime(CLOCK_MONOTONIC, timestamp);
957 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530958 return ret;
959 }
960
Harsh Bansal28718c52017-04-20 13:47:12 +0530961 ret = qaf_get_rendered_frames(out, frames);
962 clock_gettime(CLOCK_MONOTONIC, timestamp);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530963
964 return ret;
965}
966
Harsh Bansal28718c52017-04-20 13:47:12 +0530967/* Pause the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530968static int qaf_stream_pause(struct stream_out *out)
969{
Harsh Bansal28718c52017-04-20 13:47:12 +0530970 struct qaf_module *qaf_mod = NULL;
971 int ret = -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530972
Harsh Bansal28718c52017-04-20 13:47:12 +0530973 qaf_mod = get_qaf_module_for_input_stream(out);
974 if (!qaf_mod || !qaf_mod->qaf_audio_stream_pause) {
975 return -EINVAL;
976 }
977
978 if (out->qaf_stream_handle)
979 ret = qaf_mod->qaf_audio_stream_pause(out->qaf_stream_handle);
980
981 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530982}
983
Harsh Bansal28718c52017-04-20 13:47:12 +0530984/* Pause a QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530985static int qaf_out_pause(struct audio_stream_out* stream)
986{
987 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530988 int status = 0;
989 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530990
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530991 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530992
993 //If QAF passthrough is enabled then block the pause on module stream.
994 if (p_qaf->passthrough_out) {
995 pthread_mutex_lock(&p_qaf->lock);
996 //If pause is received for QAF passthorugh stream then call the primary HAL api.
997 if (p_qaf->passthrough_in == out) {
998 status = p_qaf->passthrough_out->stream.pause(
999 (struct audio_stream_out *)p_qaf->passthrough_out);
1000 out->offload_state = OFFLOAD_STATE_PAUSED;
1001 }
1002 pthread_mutex_unlock(&p_qaf->lock);
1003 } else {
1004 //Pause the module input stream.
1005 status = qaf_stream_pause(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301006 }
1007
Harsh Bansal28718c52017-04-20 13:47:12 +05301008 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301009 return status;
1010}
1011
Harsh Bansal28718c52017-04-20 13:47:12 +05301012/* Drains a qaf input stream. */
1013static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301014{
1015 struct stream_out *out = (struct stream_out *)stream;
1016 int status = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05301017 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301018
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301019 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301020
1021 //If QAF passthrough is enabled then block the drain on module stream.
1022 if (p_qaf->passthrough_out) {
1023 pthread_mutex_lock(&p_qaf->lock);
1024 //If drain is received for QAF passthorugh stream then call the primary HAL api.
1025 if (p_qaf->passthrough_in == out) {
1026 status = p_qaf->passthrough_out->stream.drain(
1027 (struct audio_stream_out *)p_qaf->passthrough_out, type);
1028 }
1029 pthread_mutex_unlock(&p_qaf->lock);
1030 } else {
1031 //Drain the module input stream.
1032 /* Stream stop will trigger EOS and on EOS_EVENT received
1033 from callback DRAIN_READY command is sent */
1034 status = audio_extn_qaf_stream_stop(out);
1035
1036 if (status == 0) {
1037 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
1038 int index = get_input_stream_index(out);
1039 if (qaf_mod && index >= 0) qaf_mod->drain_received[index] = true;
1040 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301041 }
1042
Harsh Bansal28718c52017-04-20 13:47:12 +05301043 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301044 return status;
1045}
1046
Harsh Bansal28718c52017-04-20 13:47:12 +05301047/* Flush the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301048static int audio_extn_qaf_stream_flush(struct stream_out *out)
1049{
Harsh Bansal28718c52017-04-20 13:47:12 +05301050 DEBUG_MSG("Output Stream %p", out);
1051 int ret = -EINVAL;
1052 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301053
Harsh Bansal28718c52017-04-20 13:47:12 +05301054 qaf_mod = get_qaf_module_for_input_stream(out);
1055 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_flush)) {
1056 return -EINVAL;
1057 }
1058
1059 if (out->qaf_stream_handle)
1060 ret = qaf_mod->qaf_audio_stream_flush(out->qaf_stream_handle);
1061
1062 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301063}
1064
Harsh Bansal28718c52017-04-20 13:47:12 +05301065/* Flush the QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301066static int qaf_out_flush(struct audio_stream_out* stream)
1067{
1068 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05301069 int status = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301070
Harsh Bansal28718c52017-04-20 13:47:12 +05301071 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301072 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301073
1074 //If QAF passthrough is active then block the flush on module input streams.
1075 if (p_qaf->passthrough_out) {
1076 pthread_mutex_lock(&p_qaf->lock);
1077 //If flush is received for the QAF passthrough stream then call the primary HAL api.
1078 if (p_qaf->passthrough_in == out) {
1079 status = p_qaf->passthrough_out->stream.flush(
1080 (struct audio_stream_out *)p_qaf->passthrough_out);
1081 out->offload_state = OFFLOAD_STATE_IDLE;
1082 }
1083 pthread_mutex_unlock(&p_qaf->lock);
1084 } else {
1085 //Flush the module input stream.
1086 status = audio_extn_qaf_stream_flush(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301087 }
1088
Harsh Bansal28718c52017-04-20 13:47:12 +05301089 unlock_output_stream(out);
1090 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301091 return status;
1092}
1093
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301094static uint32_t qaf_out_get_latency(const struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301095{
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301096 struct stream_out *out = (struct stream_out *)stream;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301097 uint32_t latency = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05301098 struct qaf_module *qaf_mod = NULL;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301099 DEBUG_MSG_VV("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301100
Harsh Bansal28718c52017-04-20 13:47:12 +05301101 qaf_mod = get_qaf_module_for_input_stream(out);
1102 if (!qaf_mod) {
1103 return 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301104 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301105
Harsh Bansal28718c52017-04-20 13:47:12 +05301106 //If QAF passthrough is active then block the get latency on module input streams.
1107 if (p_qaf->passthrough_out) {
1108 pthread_mutex_lock(&p_qaf->lock);
1109 //If get latency is called for the QAF passthrough stream then call the primary HAL api.
1110 if (p_qaf->passthrough_in == out) {
1111 latency = p_qaf->passthrough_out->stream.get_latency(
1112 (struct audio_stream_out *)p_qaf->passthrough_out);
1113 }
1114 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301115 } else {
Bharath Gopal01310bb2016-12-05 15:39:32 +05301116 if (is_offload_usecase(out->usecase)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301117 latency = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301118 } else {
Harsh Bansal28718c52017-04-20 13:47:12 +05301119 uint32_t sample_rate = 0;
1120 latency = QAF_MODULE_PCM_INPUT_BUFFER_LATENCY; //Input latency
1121
1122 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
1123 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
1124 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
1125 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
1126
1127 if (sample_rate) {
1128 latency += (get_pcm_output_buffer_size_samples(qaf_mod) * 1000) / out->sample_rate;
1129 }
1130 }
1131
1132 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
1133 if (is_offload_usecase(out->usecase)) {
1134 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1135 QAF_COMPRESS_OFFLOAD_PROCESSING_LATENCY;
1136 } else {
1137 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1138 QAF_PCM_OFFLOAD_PROCESSING_LATENCY;
1139 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05301140 }
1141 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301142
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301143 DEBUG_MSG_VV("Latency %d", latency);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301144 return latency;
1145}
1146
Harsh Bansal28718c52017-04-20 13:47:12 +05301147static bool check_and_get_compressed_device_format(int device, int *format)
1148{
1149 switch (device) {
1150 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DD):
1151 *format = AUDIO_FORMAT_AC3;
1152 return true;
1153 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DDP):
1154 *format = AUDIO_FORMAT_E_AC3;
1155 return true;
1156 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_FORMAT_DTS):
1157 *format = AUDIO_FORMAT_DTS;
1158 return true;
Harsh Bansal28718c52017-04-20 13:47:12 +05301159 default:
1160 return false;
1161 }
1162}
1163
1164/* Call back function for mm module. */
1165static void notify_event_callback(audio_session_handle_t session_handle /*__unused*/,
1166 void *prv_data,
1167 void *buf,
1168 audio_event_id_t event_id,
1169 int size,
1170 int device) //TODO: add media format as well.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301171{
1172
Harsh Bansal28718c52017-04-20 13:47:12 +05301173 /*
1174 For SPKR:
1175 1. Open pcm device if device_id passed to it SPKR and write the data to
1176 pcm device
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301177
Harsh Bansal28718c52017-04-20 13:47:12 +05301178 For HDMI
1179 1.Open compress device for HDMI(PCM or AC3) based on current hdmi o/p format and write
1180 data to the HDMI device.
1181 */
1182 int ret, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301183 audio_output_flags_t flags;
Harsh Bansal28718c52017-04-20 13:47:12 +05301184 struct qaf_module* qaf_mod = (struct qaf_module*)prv_data;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301185 struct audio_stream_out *bt_stream = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301186 int format;
Harsh Bansal431eede2017-04-28 21:22:30 +05301187 int8_t *data_buffer_p = NULL;
1188 uint32_t buffer_size = 0;
1189 bool need_to_recreate_stream = false;
1190 struct audio_config config;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301191
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301192 DEBUG_MSG_VV("Device 0x%X, Event = 0x%X, Bytes to write %d", device, event_id, size);
Harsh Bansal28718c52017-04-20 13:47:12 +05301193 pthread_mutex_lock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301194
Harsh Bansal431eede2017-04-28 21:22:30 +05301195 /* Default config initialization. */
1196 config.sample_rate = config.offload_info.sample_rate = QAF_OUTPUT_SAMPLING_RATE;
1197 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1198 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
1199 config.format = config.offload_info.format = AUDIO_FORMAT_PCM_16_BIT;
1200 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
1201 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1202
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301203 if (event_id == AUDIO_DATA_EVENT) {
Harsh Bansal431eede2017-04-28 21:22:30 +05301204 data_buffer_p = (int8_t*)buf;
1205 buffer_size = size;
1206 } else if (event_id == AUDIO_DATA_EVENT_V2) {
1207 audio_qaf_out_buffer_t *buf_payload = (audio_qaf_out_buffer_t*)buf;
1208 int index = -1;
1209 audio_qaf_media_format_t *p_cached_fmt = NULL;
1210
1211 if (size < sizeof(audio_qaf_out_buffer_t)) {
1212 ERROR_MSG("AUDIO_DATA_EVENT_V2 payload size is not sufficient.");
1213 return;
1214 }
1215
1216 data_buffer_p = (int8_t*)buf_payload->data + buf_payload->offset;
1217 buffer_size = buf_payload->size - buf_payload->offset;
1218
1219 index = get_media_fmt_array_index_for_output_id(qaf_mod, buf_payload->output_id);
1220
1221 if (index < 0) {
1222 /*If media format is not received then switch to default values.*/
1223 event_id = AUDIO_DATA_EVENT;
1224 } else {
1225 p_cached_fmt = &qaf_mod->out_stream_fmt[index];
1226 need_to_recreate_stream = qaf_mod->is_media_fmt_changed[index];
1227 qaf_mod->is_media_fmt_changed[index] = false;
1228
1229 config.sample_rate = config.offload_info.sample_rate = p_cached_fmt->sample_rate;
1230 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1231 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
1232 config.format = config.offload_info.format = p_cached_fmt->format;
1233 config.offload_info.bit_width = p_cached_fmt->bit_width;
1234
1235 if (p_cached_fmt->format == AUDIO_FORMAT_PCM) {
1236 if (p_cached_fmt->bit_width == 16)
1237 config.format = config.offload_info.format = AUDIO_FORMAT_PCM_16_BIT;
1238 else if (p_cached_fmt->bit_width == 24)
1239 config.format = config.offload_info.format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1240 else
1241 config.format = config.offload_info.format = AUDIO_FORMAT_PCM_32_BIT;
1242 }
1243
1244 device |= (p_cached_fmt->format & AUDIO_FORMAT_MAIN_MASK);
1245
1246 switch (p_cached_fmt->channels) {
1247 case 1:
1248 config.offload_info.channel_mask = config.channel_mask =
1249 AUDIO_CHANNEL_OUT_MONO;
1250 break;
1251 case 2:
1252 config.offload_info.channel_mask = config.channel_mask =
1253 AUDIO_CHANNEL_OUT_STEREO;
1254 break;
1255 case 6:
1256 config.offload_info.channel_mask = config.channel_mask =
1257 AUDIO_CHANNEL_OUT_5POINT1;
1258 break;
1259 case 8:
1260 config.offload_info.channel_mask = config.channel_mask =
1261 AUDIO_CHANNEL_OUT_7POINT1;
1262 break;
1263 default:
1264 config.offload_info.channel_mask = config.channel_mask =
1265 AUDIO_CHANNEL_OUT_5POINT1;
1266 break;
1267 }
1268 }
1269 }
1270
1271 if (event_id == AUDIO_OUTPUT_MEDIA_FORMAT_EVENT) {
1272 audio_qaf_media_format_t *p_fmt = (audio_qaf_media_format_t*)buf;
1273 audio_qaf_media_format_t *p_cached_fmt = NULL;
1274 int index = -1;
1275
1276 if (size < sizeof(audio_qaf_media_format_t)) {
1277 ERROR_MSG("Size is not proper for the event AUDIO_OUTPUT_MEDIA_FORMAT_EVENT.");
1278 return ;
1279 }
1280
1281 index = get_media_fmt_array_index_for_output_id(qaf_mod, p_fmt->output_id);
1282
1283 if (index >= 0) {
1284 p_cached_fmt = &qaf_mod->out_stream_fmt[index];
1285 } else if (index < 0 && qaf_mod->new_out_format_index < MAX_QAF_MODULE_OUT) {
1286 index = qaf_mod->new_out_format_index;
1287 p_cached_fmt = &qaf_mod->out_stream_fmt[index];
1288 qaf_mod->new_out_format_index++;
1289 }
1290
1291 if (p_cached_fmt == NULL) {
1292 ERROR_MSG("Maximum output from a QAF module is reached. Can not process new output.");
1293 return ;
1294 }
1295
1296 if (memcmp(p_cached_fmt, p_fmt, sizeof(audio_qaf_media_format_t)) != 0) {
1297 memcpy(p_cached_fmt, p_fmt, sizeof(audio_qaf_media_format_t));
1298 qaf_mod->is_media_fmt_changed[index] = true;
1299 }
1300 } else if (event_id == AUDIO_DATA_EVENT || event_id == AUDIO_DATA_EVENT_V2) {
1301
Harsh Bansal28718c52017-04-20 13:47:12 +05301302 if (p_qaf->passthrough_out != NULL) {
1303 //If QAF passthrough is active then all the module output will be dropped.
1304 pthread_mutex_unlock(&p_qaf->lock);
1305 DEBUG_MSG("QAF-PSTH is active, DROPPING DATA!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301306 return;
Harsh Bansal28718c52017-04-20 13:47:12 +05301307 }
1308
1309 if (check_and_get_compressed_device_format(device, &format)) {
1310 /*
1311 * CASE 1: Transcoded output of mm module.
1312 * If HDMI is not connected then drop the data.
1313 * Only one HDMI output can be supported from all the mm modules of QAF.
1314 * Multi-Channel PCM HDMI output streams will be closed from all the mm modules.
1315 * If transcoded output of other module is already enabled then this data will be dropped.
1316 */
1317
1318 if (!p_qaf->hdmi_connect) {
1319 DEBUG_MSG("HDMI not connected, DROPPING DATA!");
1320 pthread_mutex_unlock(&p_qaf->lock);
1321 return;
1322 }
1323
1324 //Closing all the PCM HDMI output stream from QAF.
1325 close_all_pcm_hdmi_output();
1326
Harsh Bansal431eede2017-04-28 21:22:30 +05301327 /* If Media format was changed for this stream then need to re-create the stream. */
1328 if (need_to_recreate_stream && qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
1329 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1330 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]));
1331 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] = NULL;
1332 p_qaf->passthrough_enabled = false;
1333 }
1334
Harsh Bansal28718c52017-04-20 13:47:12 +05301335 if (!p_qaf->passthrough_enabled
1336 && !(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH])) {
1337
Harsh Bansal28718c52017-04-20 13:47:12 +05301338 audio_devices_t devices;
1339
Harsh Bansal28718c52017-04-20 13:47:12 +05301340 config.format = config.offload_info.format = format;
Harsh Bansal28718c52017-04-20 13:47:12 +05301341 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1342
1343 flags = (AUDIO_OUTPUT_FLAG_NON_BLOCKING
1344 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD
1345 | AUDIO_OUTPUT_FLAG_DIRECT);
1346 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1347
1348 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1349 QAF_DEFAULT_COMPR_PASSTHROUGH_HANDLE,
1350 devices,
1351 flags,
1352 &config,
1353 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]),
1354 NULL);
1355 if (ret < 0) {
1356 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1357 pthread_mutex_unlock(&p_qaf->lock);
1358 return;
1359 }
1360
Harsh Bansal431eede2017-04-28 21:22:30 +05301361 if (format & AUDIO_FORMAT_E_AC3) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301362 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragment_size =
1363 COMPRESS_PASSTHROUGH_DDP_FRAGMENT_SIZE;
1364 }
1365 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragments =
1366 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1367
1368 p_qaf->passthrough_enabled = true;
1369 }
1370
1371 if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
1372 ret = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->stream.write(
1373 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH],
Harsh Bansal431eede2017-04-28 21:22:30 +05301374 data_buffer_p,
1375 buffer_size);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301376 }
1377 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301378 else if ((device & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1379 && (p_qaf->hdmi_connect)
1380 && (p_qaf->hdmi_sink_channels > 2)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301381
Harsh Bansal28718c52017-04-20 13:47:12 +05301382 /* CASE 2: Multi-Channel PCM output to HDMI.
1383 * If any other HDMI output is already enabled then this has to be dropped.
1384 */
1385 bool create_mch_out_stream = false;
1386
1387 if (p_qaf->passthrough_enabled) {
1388 //Closing all the multi-Channel PCM HDMI output stream from QAF.
1389 close_all_pcm_hdmi_output();
1390
1391 //If passthrough is active then pcm hdmi output has to be dropped.
1392 pthread_mutex_unlock(&p_qaf->lock);
1393 DEBUG_MSG("Compressed passthrough enabled, DROPPING DATA!");
1394 return;
1395 }
1396
Harsh Bansal431eede2017-04-28 21:22:30 +05301397 /* If Media format was changed for this stream then need to re-create the stream. */
1398 if (need_to_recreate_stream && qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) {
1399 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1400 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]));
1401 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH] = NULL;
1402 p_qaf->mch_pcm_hdmi_enabled = false;
1403 }
1404
Harsh Bansal28718c52017-04-20 13:47:12 +05301405 if (!p_qaf->mch_pcm_hdmi_enabled && !(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301406 audio_devices_t devices;
1407
Harsh Bansal431eede2017-04-28 21:22:30 +05301408 if (event_id == AUDIO_DATA_EVENT) {
1409 config.offload_info.format = config.format = AUDIO_FORMAT_PCM_16_BIT;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301410
Harsh Bansal431eede2017-04-28 21:22:30 +05301411 if (p_qaf->hdmi_sink_channels == 8) {
1412 config.offload_info.channel_mask = config.channel_mask =
1413 AUDIO_CHANNEL_OUT_7POINT1;
1414 } else if (p_qaf->hdmi_sink_channels == 6) {
1415 config.offload_info.channel_mask = config.channel_mask =
1416 AUDIO_CHANNEL_OUT_5POINT1;
1417 } else {
1418 config.offload_info.channel_mask = config.channel_mask =
1419 AUDIO_CHANNEL_OUT_STEREO;
1420 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301421 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301422
Harsh Bansal28718c52017-04-20 13:47:12 +05301423 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1424 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
1425
1426 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1427 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1428 devices,
1429 flags,
1430 &config,
1431 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]),
1432 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301433 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301434 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1435 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301436 return;
1437 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001438
Harsh Bansal28718c52017-04-20 13:47:12 +05301439 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragments =
1440 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1441 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragment_size =
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301442 qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info);
Harsh Bansal28718c52017-04-20 13:47:12 +05301443
1444 p_qaf->mch_pcm_hdmi_enabled = true;
1445
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301446 if ((qaf_mod->stream_in[QAF_IN_MAIN]
1447 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) ||
1448 (qaf_mod->stream_in[QAF_IN_MAIN_2]
1449 && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301450
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301451 if (qaf_mod->stream_in[QAF_IN_MAIN]) {
1452 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
Harsh Bansal28718c52017-04-20 13:47:12 +05301453 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1454 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1455 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301456 }
1457 if (qaf_mod->stream_in[QAF_IN_MAIN_2]) {
1458 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1459 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1460 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback,
1461 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie);
1462 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301463 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1464 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1465
1466 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1467 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1468 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1469 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1470 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001471
1472 int index = -1;
1473 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301474 index = (int) QAF_IN_MAIN;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301475 else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid)
1476 index = (int) QAF_IN_MAIN_2;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001477 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301478 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001479
Harsh Bansal28718c52017-04-20 13:47:12 +05301480 if (index >= 0) {
1481 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->standby)
1482 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1483 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], NULL, 0);
1484
1485 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001486 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301487 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1488 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1489 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1490 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001491
1492 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301493 }
1494
Harsh Bansal28718c52017-04-20 13:47:12 +05301495 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) {
1496 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1497 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
Harsh Bansal431eede2017-04-28 21:22:30 +05301498 data_buffer_p,
1499 buffer_size);
Varun B34da7a42017-02-13 16:16:53 +05301500 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301501 }
1502 else {
1503 /* CASE 3: PCM output.
1504 */
Harsh Bansal431eede2017-04-28 21:22:30 +05301505
1506 /* If Media format was changed for this stream then need to re-create the stream. */
1507 if (need_to_recreate_stream && qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1508 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1509 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD]));
1510 qaf_mod->stream_out[QAF_OUT_OFFLOAD] = NULL;
1511 }
1512
Bharath Gopal01310bb2016-12-05 15:39:32 +05301513 bt_stream = audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl);
1514 if (bt_stream != NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301515 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1516 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1517 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD]));
1518 qaf_mod->stream_out[QAF_OUT_OFFLOAD] = NULL;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301519 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301520
Harsh Bansal431eede2017-04-28 21:22:30 +05301521 audio_extn_bt_hal_out_write(p_qaf->bt_hdl, data_buffer_p, buffer_size);
Harsh Bansal28718c52017-04-20 13:47:12 +05301522 } else if (NULL == qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301523 audio_devices_t devices;
1524
Harsh Bansal28718c52017-04-20 13:47:12 +05301525 if (qaf_mod->stream_in[QAF_IN_MAIN])
1526 devices = qaf_mod->stream_in[QAF_IN_MAIN]->devices;
1527 else
1528 devices = qaf_mod->stream_in[QAF_IN_PCM]->devices;
1529
1530 //If multi channel pcm or passthrough is already enabled then remove the hdmi flag from device.
1531 if (p_qaf->mch_pcm_hdmi_enabled || p_qaf->passthrough_enabled) {
1532 if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1533 devices ^= AUDIO_DEVICE_OUT_AUX_DIGITAL;
1534 }
1535 if (devices == 0) {
1536 devices = device;
1537 }
1538
1539 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001540
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301541 /* TODO:: Need to Propagate errors to framework */
Harsh Bansal28718c52017-04-20 13:47:12 +05301542 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1543 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1544 devices,
1545 flags,
1546 &config,
1547 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD]),
1548 NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301549 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301550 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1551 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301552 return;
1553 }
Deepak Agarwal1e42b852017-02-11 17:57:04 +05301554
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301555 if ((qaf_mod->stream_in[QAF_IN_MAIN]
1556 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) ||
1557 (qaf_mod->stream_in[QAF_IN_MAIN_2]
1558 && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001559
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301560 if (qaf_mod->stream_in[QAF_IN_MAIN]) {
1561 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
Harsh Bansal28718c52017-04-20 13:47:12 +05301562 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1563 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1564 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301565 }
1566 if (qaf_mod->stream_in[QAF_IN_MAIN_2]) {
1567 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1568 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1569 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback,
1570 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie);
1571 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301572 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1573 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1574
1575 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1576 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1577 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1578 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1579 }
1580
1581 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragments =
1582 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1583 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragment_size =
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301584 qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info);
Harsh Bansal28718c52017-04-20 13:47:12 +05301585
1586 if (qaf_mod->is_vol_set) {
1587 DEBUG_MSG("Setting Volume Left[%f], Right[%f]", qaf_mod->vol_left, qaf_mod->vol_right);
1588 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
1589 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1590 qaf_mod->vol_left,
1591 qaf_mod->vol_right);
1592 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001593
1594 int index = -1;
1595 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301596 index = (int) QAF_IN_MAIN;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301597 else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid)
1598 index = (int) QAF_IN_MAIN_2;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001599 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301600 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001601 if (index >= 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301602 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]->standby) {
1603 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1604 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], NULL, 0);
1605 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001606
Harsh Bansal28718c52017-04-20 13:47:12 +05301607 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001608 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301609 qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1610 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1611 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1612 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301613 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301614 }
1615
1616 /*
1617 * TODO:: Since this is mixed data,
1618 * need to identify to which stream the error should be sent
1619 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301620 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1621 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1622 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
Harsh Bansal431eede2017-04-28 21:22:30 +05301623 data_buffer_p,
1624 buffer_size);
Varun B34da7a42017-02-13 16:16:53 +05301625 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301626 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301627 DEBUG_MSG_VV("Bytes written = %d", ret);
Harsh Bansal28718c52017-04-20 13:47:12 +05301628 }
1629 else if (event_id == AUDIO_EOS_MAIN_DD_DDP_EVENT
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301630 || event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT
Varun B34da7a42017-02-13 16:16:53 +05301631 || event_id == AUDIO_EOS_MAIN_AAC_EVENT
1632 || event_id == AUDIO_EOS_MAIN_AC4_EVENT
1633 || event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301634 struct stream_out *out = qaf_mod->stream_in[QAF_IN_MAIN];
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301635 struct stream_out *out_main2 = qaf_mod->stream_in[QAF_IN_MAIN_2];
Harsh Bansal28718c52017-04-20 13:47:12 +05301636 struct stream_out *out_assoc = qaf_mod->stream_in[QAF_IN_ASSOC];
1637 bool *main_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301638 bool *main2_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN_2];
Harsh Bansal28718c52017-04-20 13:47:12 +05301639 bool *assoc_drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
Varun B34da7a42017-02-13 16:16:53 +05301640
1641 /**
1642 * TODO:: Only DD/DDP Associate Eos is handled, need to add support
1643 * for other formats.
1644 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301645 if (event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT
1646 && (out_assoc != NULL)
1647 && (*assoc_drain_received)) {
1648
Varun B34da7a42017-02-13 16:16:53 +05301649 lock_output_stream(out_assoc);
Harsh Bansal28718c52017-04-20 13:47:12 +05301650 out_assoc->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_assoc->client_cookie);
1651 *assoc_drain_received = false;
1652 unlock_output_stream(out_assoc);
1653 DEBUG_MSG("sent associated DRAIN_READY");
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301654 } else if (event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT
1655 && (out_main2 != NULL)
1656 && (*main2_drain_received)) {
1657
1658 lock_output_stream(out_main2);
1659 out_main2->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_main2->client_cookie);
1660 *main2_drain_received = false;
1661 unlock_output_stream(out_main2);
1662 DEBUG_MSG("sent main2 DRAIN_READY");
Harsh Bansal28718c52017-04-20 13:47:12 +05301663 } else if ((out != NULL) && (*main_drain_received)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301664 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001665 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
Harsh Bansal28718c52017-04-20 13:47:12 +05301666 *main_drain_received = false;
1667 unlock_output_stream(out);
1668 DEBUG_MSG("sent main DRAIN_READY");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301669 }
1670 }
Harsh Bansal431eede2017-04-28 21:22:30 +05301671 else if (event_id == AUDIO_MAIN_EOS_EVENT || event_id == AUDIO_ASSOC_EOS_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301672 struct stream_out *out = NULL;
1673 bool *drain_received = NULL;
1674
1675 if (event_id == AUDIO_MAIN_EOS_EVENT) {
1676 out = qaf_mod->stream_in[QAF_IN_MAIN];
1677 drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
1678 } else {
1679 out = qaf_mod->stream_in[QAF_IN_ASSOC];
1680 drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
1681 }
1682
1683 if ((out != NULL) && (*drain_received)) {
1684 lock_output_stream(out);
1685 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
1686 *drain_received = false;
1687 unlock_output_stream(out);
1688 DEBUG_MSG("sent DRAIN_READY");
1689 }
1690 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301691
1692 pthread_mutex_unlock(&p_qaf->lock);
1693 return;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301694}
1695
Harsh Bansal28718c52017-04-20 13:47:12 +05301696/* Close the mm module session. */
1697static int qaf_session_close(struct qaf_module* qaf_mod)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301698{
Harsh Bansal28718c52017-04-20 13:47:12 +05301699 int j;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301700
Harsh Bansal28718c52017-04-20 13:47:12 +05301701 //Check if all streams are closed or not.
1702 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
1703 if (qaf_mod->stream_in[j] != NULL) {
1704 break;
1705 }
1706 }
1707 if (j != MAX_QAF_MODULE_IN) {
1708 return 0; //Some stream is already active, Can not close session.
1709 }
1710
1711 if (qaf_mod->session_handle != NULL && qaf_mod->qaf_audio_session_close) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301712 qaf_mod->qaf_audio_session_close(qaf_mod->session_handle);
1713 qaf_mod->session_handle = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301714 qaf_mod->is_vol_set = false;
1715 memset(qaf_mod->drain_received, 0, sizeof(qaf_mod->drain_received));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301716 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301717
1718 for (j = 0; j < MAX_QAF_MODULE_OUT; j++) {
1719 if (qaf_mod->stream_out[j]) {
1720 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1721 (struct audio_stream_out *)(qaf_mod->stream_out[j]));
1722 qaf_mod->stream_out[j] = NULL;
1723 }
Harsh Bansal431eede2017-04-28 21:22:30 +05301724 memset(&qaf_mod->out_stream_fmt[j], 0, sizeof(audio_qaf_media_format_t));
1725 qaf_mod->is_media_fmt_changed[j] = false;
Harsh Bansal28718c52017-04-20 13:47:12 +05301726 }
Harsh Bansal431eede2017-04-28 21:22:30 +05301727 qaf_mod->new_out_format_index = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05301728
1729 DEBUG_MSG("Session Closed.");
1730
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301731 return 0;
1732}
1733
Harsh Bansal28718c52017-04-20 13:47:12 +05301734/* Close the stream of QAF module. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301735static int qaf_stream_close(struct stream_out *out)
1736{
Harsh Bansal28718c52017-04-20 13:47:12 +05301737 int ret = -EINVAL;
1738 struct qaf_module *qaf_mod = NULL;
1739 int index = -1;
1740 DEBUG_MSG("Flag [0x%x], Stream handle [%p]", out->flags, out->qaf_stream_handle);
1741
1742 qaf_mod = get_qaf_module_for_input_stream(out);
1743 index = get_input_stream_index(out);
1744
1745 if (!qaf_mod || !qaf_mod->qaf_audio_stream_close || index < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301746 return -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301747 }
1748
1749 qaf_mod->stream_in[index] = NULL;
1750 memset(&qaf_mod->adsp_hdlr_config[index], 0, sizeof(struct qaf_adsp_hdlr_config_state));
1751
1752 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301753 if (out->qaf_stream_handle) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301754 ret = qaf_mod->qaf_audio_stream_close(out->qaf_stream_handle);
1755 out->qaf_stream_handle = NULL;
1756 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301757 unlock_output_stream(out);
1758
1759 //If all streams are closed then close the session.
1760 qaf_session_close(qaf_mod);
1761
1762 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301763 return ret;
1764}
1765
Harsh Bansal28718c52017-04-20 13:47:12 +05301766/* Open a MM module session with QAF. */
1767static int audio_extn_qaf_session_open(mm_module_type mod_type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301768{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301769 ALOGV("%s %d", __func__, __LINE__);
Harsh Bansal28718c52017-04-20 13:47:12 +05301770 unsigned char* license_data = NULL;
1771 device_license_config_t lic_config = {0};
1772 int ret = -ENOSYS, size = 0;
1773 char value[PROPERTY_VALUE_MAX] = {0};
1774 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301775
Harsh Bansal28718c52017-04-20 13:47:12 +05301776 if (mod_type >= MAX_MM_MODULE_TYPE || !(p_qaf->qaf_mod[mod_type].qaf_audio_session_open))
1777 return -ENOTSUP; //Not supported by QAF module.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301778
Harsh Bansal28718c52017-04-20 13:47:12 +05301779 pthread_mutex_lock(&p_qaf->lock);
1780
1781 qaf_mod = &(p_qaf->qaf_mod[mod_type]);
1782
1783 //If session is already opened then return.
1784 if (qaf_mod->session_handle) {
1785 DEBUG_MSG("Session is already opened.");
1786 pthread_mutex_unlock(&p_qaf->lock);
1787 return 0;
1788 }
1789
1790 if (mod_type == MS12) {
1791 //Getting the license
1792 license_data = platform_get_license((struct audio_hw_device *)(p_qaf->adev->platform),
1793 &size);
1794 if (!license_data) {
1795 ERROR_MSG("License data is not present.");
1796 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301797 return -EINVAL;
1798 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301799
1800 lic_config.p_license = (unsigned char*)calloc(1, size);
1801 if (lic_config.p_license == NULL) {
1802 ERROR_MSG("Out of Memory");
1803 ret = -ENOMEM;
1804 goto exit;
1805 }
1806
1807 lic_config.l_size = size;
1808 memcpy(lic_config.p_license, license_data, size);
1809
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001810 if (property_get("vendor.audio.qaf.manufacturer", value, "") && atoi(value)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301811 lic_config.manufacturer_id = (unsigned long)atoi(value);
1812 } else {
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001813 ERROR_MSG("vendor.audio.qaf.manufacturer id is not set");
Harsh Bansal28718c52017-04-20 13:47:12 +05301814 ret = -EINVAL;
1815 goto exit;
1816 }
1817 }
1818
1819 ret = qaf_mod->qaf_audio_session_open(&qaf_mod->session_handle,
Sri Karri1fbfef52017-06-29 19:45:47 +05301820 AUDIO_SESSION_BROADCAST,
Harsh Bansal28718c52017-04-20 13:47:12 +05301821 (void *)(qaf_mod),
1822 (void *)&lic_config);
1823 if (ret < 0) {
1824 ERROR_MSG("Error in session open %d", ret);
1825 goto exit;
1826 }
1827
1828 if (qaf_mod->session_handle == NULL) {
1829 ERROR_MSG("Session handle is NULL.");
1830 ret = -ENOMEM;
1831 goto exit;
1832 }
1833
1834 if (qaf_mod->qaf_register_event_callback)
1835 qaf_mod->qaf_register_event_callback(qaf_mod->session_handle,
1836 qaf_mod,
1837 &notify_event_callback,
Harsh Bansal431eede2017-04-28 21:22:30 +05301838 AUDIO_DATA_EVENT_V2);
Harsh Bansal28718c52017-04-20 13:47:12 +05301839
1840 set_hdmi_configuration_to_module();
1841
1842exit:
1843 if (license_data != NULL) {
1844 free(license_data);
1845 license_data = NULL;
1846 }
1847 if (lic_config.p_license != NULL) {
1848 free(lic_config.p_license);
1849 lic_config.p_license = NULL;
1850 }
1851
1852 pthread_mutex_unlock(&p_qaf->lock);
1853 return ret;
1854}
1855
1856/* opens a stream in QAF module. */
1857static int qaf_stream_open(struct stream_out *out,
1858 struct audio_config *config,
1859 audio_output_flags_t flags,
1860 audio_devices_t devices)
1861{
1862 int status = -EINVAL;
1863 mm_module_type mmtype = get_mm_module_for_format(config->format);
1864 struct qaf_module* qaf_mod = NULL;
1865 DEBUG_MSG("Flags 0x%x, Device 0x%x", flags, devices);
1866
1867 if (mmtype >= MAX_MM_MODULE_TYPE
1868 || p_qaf->qaf_mod[mmtype].qaf_audio_session_open == NULL
1869 || p_qaf->qaf_mod[mmtype].qaf_audio_stream_open == NULL) {
1870 ERROR_MSG("Unsupported Stream");
1871 return -ENOTSUP;
1872 }
1873
1874 //Open the module session, if not opened already.
1875 status = audio_extn_qaf_session_open(mmtype);
1876 qaf_mod = &(p_qaf->qaf_mod[mmtype]);
1877
1878 if ((status != 0) || (qaf_mod->session_handle == NULL)) {
1879 ERROR_MSG("Failed to open session.");
1880 return status;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301881 }
1882
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301883 audio_stream_config_t input_config;
1884 input_config.sample_rate = config->sample_rate;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301885 input_config.channels = popcount(config->channel_mask);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301886 input_config.format = config->format;
1887
Harsh Bansal28718c52017-04-20 13:47:12 +05301888 if (input_config.format != AUDIO_FORMAT_PCM_16_BIT) {
1889 input_config.format &= AUDIO_FORMAT_MAIN_MASK;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301890 }
1891
Harsh Bansal28718c52017-04-20 13:47:12 +05301892 DEBUG_MSG("stream_open sample_rate(%d) channels(%d) devices(%#x) flags(%#x) format(%#x)",
1893 input_config.sample_rate, input_config.channels, devices, flags, input_config.format);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301894
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301895 if (input_config.format == AUDIO_FORMAT_PCM_16_BIT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301896 //If PCM stream is already opened then fail this stream open.
1897 if (qaf_mod->stream_in[QAF_IN_PCM]) {
1898 ERROR_MSG("PCM input is already active.");
1899 return -ENOTSUP;
1900 }
1901
1902 //TODO: Flag can be system tone or external associated PCM.
1903 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle,
1904 &out->qaf_stream_handle,
1905 input_config,
1906 devices,
1907 AUDIO_STREAM_SYSTEM_TONE);
1908 qaf_mod->stream_in[QAF_IN_PCM] = out;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301909 } else if ((flags & AUDIO_OUTPUT_FLAG_MAIN) && (flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
1910 if (is_main_active(qaf_mod) || is_dual_main_active(qaf_mod)) {
1911 ERROR_MSG("Dual Main or Main already active. So, Cannot open main and associated stream");
1912 return -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301913 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301914 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN);
1915 if (status == 0) {
1916 DEBUG_MSG("Open stream for Input with both Main and Associated stream contents with flag(%x) and stream_handle(%p)", flags, out->qaf_stream_handle);
1917 qaf_mod->stream_in[QAF_IN_MAIN] = out;
1918 } else {
1919 ERROR_MSG("Stream Open FAILED !!!");
1920 }
1921 }
Naresh Tanniru908d9a02017-05-17 14:12:48 +05301922 } else if ((flags & AUDIO_OUTPUT_FLAG_MAIN) || ((!(flags & AUDIO_OUTPUT_FLAG_MAIN)) && (!(flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)))) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301923 /* Assume Main if no flag is set */
1924 if (is_dual_main_active(qaf_mod)) {
1925 ERROR_MSG("Dual Main already active. So, Cannot open main stream");
1926 return -EINVAL;
1927 } else if (is_main_active(qaf_mod) && qaf_mod->stream_in[QAF_IN_ASSOC]) {
1928 ERROR_MSG("Main and Associated already active. So, Cannot open main stream");
1929 return -EINVAL;
1930 } else if (is_main_active(qaf_mod) && (mmtype != MS12)) {
1931 ERROR_MSG("Main already active and Not an MS12 format. So, Cannot open another main stream");
1932 return -EINVAL;
1933 } else {
1934 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN);
1935 if (status == 0) {
1936 DEBUG_MSG("Open stream for Input with only Main flag(%x) stream_handle(%p)", flags, out->qaf_stream_handle);
1937 if(qaf_mod->stream_in[QAF_IN_MAIN]) {
1938 qaf_mod->stream_in[QAF_IN_MAIN_2] = out;
1939 } else {
1940 qaf_mod->stream_in[QAF_IN_MAIN] = out;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301941 }
1942 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301943 ERROR_MSG("Stream Open FAILED !!!");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301944 }
1945 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301946 } else if ((flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
1947 if (is_dual_main_active(qaf_mod)) {
1948 ERROR_MSG("Dual Main already active. So, Cannot open associated stream");
1949 return -EINVAL;
1950 } else if (!is_main_active(qaf_mod)) {
1951 ERROR_MSG("Main not active. So, Cannot open associated stream");
1952 return -EINVAL;
1953 } else if (qaf_mod->stream_in[QAF_IN_ASSOC]) {
1954 ERROR_MSG("Associated already active. So, Cannot open associated stream");
1955 return -EINVAL;
1956 }
1957 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_ASSOCIATED);
1958 if (status == 0) {
1959 DEBUG_MSG("Open stream for Input with only Associated flag(%x) stream handle(%p)", flags, out->qaf_stream_handle);
1960 qaf_mod->stream_in[QAF_IN_ASSOC] = out;
1961 } else {
1962 ERROR_MSG("Stream Open FAILED !!!");
1963 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301964 }
1965
Harsh Bansal28718c52017-04-20 13:47:12 +05301966 if (status != 0) {
1967 //If no stream is active then close the session.
1968 qaf_session_close(qaf_mod);
1969 return status;
1970 }
1971
1972 //If Device is HDMI, QAF passthrough is enabled and there is no previous QAF passthrough input stream.
1973 if ((!p_qaf->passthrough_in)
1974 && (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1975 && audio_extn_qaf_passthrough_enabled(out)) {
1976 //Assign the QAF passthrough input stream.
1977 p_qaf->passthrough_in = out;
1978
1979 //If HDMI is connected and format is supported by HDMI then create QAF passthrough output stream.
1980 if (p_qaf->hdmi_connect
1981 && platform_is_edid_supported_format(p_qaf->adev->platform, out->format)) {
1982 status = create_qaf_passthrough_stream();
1983 if (status < 0) {
1984 qaf_stream_close(out);
1985 ERROR_MSG("QAF passthrough stream creation failed with error %d", status);
1986 return status;
1987 }
1988 }
1989 /*Else: since QAF passthrough input stream is already initialized,
1990 * when hdmi is connected
1991 * then qaf passthrough output stream will be created.
1992 */
1993 }
1994
1995 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301996 return status;
1997}
1998
Harsh Bansal28718c52017-04-20 13:47:12 +05301999/* Resume a QAF stream. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302000static int qaf_out_resume(struct audio_stream_out* stream)
2001{
2002 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05302003 int status = 0;
2004 DEBUG_MSG("Output Stream %p", out);
2005
2006
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302007 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05302008
2009 //If QAF passthrough is active then block the resume on module input streams.
2010 if (p_qaf->passthrough_out) {
2011 //If resume is received for the QAF passthrough stream then call the primary HAL api.
2012 pthread_mutex_lock(&p_qaf->lock);
2013 if (p_qaf->passthrough_in == out) {
2014 status = p_qaf->passthrough_out->stream.resume(
2015 (struct audio_stream_out*)p_qaf->passthrough_out);
2016 if (!status) out->offload_state = OFFLOAD_STATE_PLAYING;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302017 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302018 pthread_mutex_unlock(&p_qaf->lock);
2019 } else {
2020 //Flush the module input stream.
2021 status = qaf_stream_start(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302022 }
2023
Harsh Bansal28718c52017-04-20 13:47:12 +05302024 unlock_output_stream(out);
2025
2026 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302027 return status;
2028}
2029
Harsh Bansal28718c52017-04-20 13:47:12 +05302030/* Offload thread for QAF output streams. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302031static void *qaf_offload_thread_loop(void *context)
2032{
Harsh Bansal28718c52017-04-20 13:47:12 +05302033 struct stream_out *out = (struct stream_out *)context;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302034 struct listnode *item;
2035 int ret = 0;
2036 struct str_parms *parms = NULL;
2037 int value = 0;
2038 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05302039 struct qaf_module *qaf_mod = NULL;
2040
2041 qaf_mod = get_qaf_module_for_input_stream(out);
2042
2043 if (!qaf_mod) return NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302044
2045 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
2046 set_sched_policy(0, SP_FOREGROUND);
2047 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
2048
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302049 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05302050
2051 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302052 for (;;) {
2053 struct offload_cmd *cmd = NULL;
2054 stream_callback_event_t event;
2055 bool send_callback = false;
2056
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302057 DEBUG_MSG_VV("List Empty %d (1:TRUE, 0:FALSE)", list_empty(&out->qaf_offload_cmd_list));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302058 if (list_empty(&out->qaf_offload_cmd_list)) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302059 DEBUG_MSG_VV("SLEEPING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302060 pthread_cond_wait(&out->qaf_offload_cond, &out->lock);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302061 DEBUG_MSG_VV("RUNNING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302062 continue;
2063 }
2064
2065 item = list_head(&out->qaf_offload_cmd_list);
2066 cmd = node_to_item(item, struct offload_cmd, node);
2067 list_remove(item);
2068
2069 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
2070 free(cmd);
2071 break;
2072 }
2073
Harsh Bansal28718c52017-04-20 13:47:12 +05302074 unlock_output_stream(out);
2075
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302076 send_callback = false;
Harsh Bansal28718c52017-04-20 13:47:12 +05302077 switch (cmd->cmd) {
2078 case OFFLOAD_CMD_WAIT_FOR_BUFFER: {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302079 DEBUG_MSG_VV("wait for buffer availability");
Harsh Bansal28718c52017-04-20 13:47:12 +05302080
2081 while (1) {
2082 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle,
2083 "buf_available");
2084 if (kvpairs) {
2085 parms = str_parms_create_str(kvpairs);
2086 ret = str_parms_get_int(parms, "buf_available", &value);
2087 if (ret >= 0) {
2088 if (value >= (int)out->compr_config.fragment_size) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302089 DEBUG_MSG_VV("buffer available");
Harsh Bansal28718c52017-04-20 13:47:12 +05302090 str_parms_destroy(parms);
2091 parms = NULL;
2092 break;
2093 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302094 DEBUG_MSG_VV("sleep");
Harsh Bansal28718c52017-04-20 13:47:12 +05302095 str_parms_destroy(parms);
2096 parms = NULL;
2097 usleep(10000);
2098 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302099 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302100 free(kvpairs);
2101 kvpairs = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302102 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302103 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302104 send_callback = true;
2105 event = STREAM_CBK_EVENT_WRITE_READY;
2106 break;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302107 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302108 default:
2109 DEBUG_MSG("unknown command received: %d", cmd->cmd);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302110 break;
2111 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302112
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302113 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05302114
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002115 if (send_callback && out->client_callback) {
2116 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302117 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302118
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302119 free(cmd);
2120 }
2121
2122 while (!list_empty(&out->qaf_offload_cmd_list)) {
2123 item = list_head(&out->qaf_offload_cmd_list);
2124 list_remove(item);
Harsh Bansal28718c52017-04-20 13:47:12 +05302125 free (node_to_item( item, struct offload_cmd, node));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302126 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302127 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302128
2129 return NULL;
2130}
2131
Harsh Bansal28718c52017-04-20 13:47:12 +05302132/* Create the offload callback thread for QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302133static int qaf_create_offload_callback_thread(struct stream_out *out)
2134{
Harsh Bansal28718c52017-04-20 13:47:12 +05302135 DEBUG_MSG("Output Stream %p", out);
2136 lock_output_stream(out);
2137 pthread_cond_init(&out->qaf_offload_cond, (const pthread_condattr_t *)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302138 list_init(&out->qaf_offload_cmd_list);
Harsh Bansal28718c52017-04-20 13:47:12 +05302139 pthread_create(&out->qaf_offload_thread,
2140 (const pthread_attr_t *)NULL,
2141 qaf_offload_thread_loop,
2142 out);
2143 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302144 return 0;
2145}
2146
Harsh Bansal28718c52017-04-20 13:47:12 +05302147/* Destroy the offload callback thread of QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302148static int qaf_destroy_offload_callback_thread(struct stream_out *out)
2149{
Harsh Bansal28718c52017-04-20 13:47:12 +05302150 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302151 qaf_send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302152
Harsh Bansal28718c52017-04-20 13:47:12 +05302153 pthread_join(out->qaf_offload_thread, (void **)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302154 pthread_cond_destroy(&out->qaf_offload_cond);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302155 return 0;
2156}
2157
Harsh Bansal28718c52017-04-20 13:47:12 +05302158/* Sets the stream set parameters (device routing information). */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302159static int qaf_out_set_parameters(struct audio_stream *stream, const char *kvpairs)
2160{
2161 struct str_parms *parms, *new_parms;
2162 char value[32];
2163 char *new_kv_pairs;
2164 int val = 0;
2165 struct stream_out *out = (struct stream_out *)stream;
2166 int ret = 0;
2167 int err = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05302168 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302169
Harsh Bansal28718c52017-04-20 13:47:12 +05302170 DEBUG_MSG("usecase(%d: %s) kvpairs: %s", out->usecase, use_case_table[out->usecase], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302171
Harsh Bansal28718c52017-04-20 13:47:12 +05302172 parms = str_parms_create_str(kvpairs);
2173 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2174 if (err < 0)
2175 return err;
2176 val = atoi(value);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302177
Harsh Bansal28718c52017-04-20 13:47:12 +05302178 qaf_mod = get_qaf_module_for_input_stream(out);
2179 if (!qaf_mod) return (-EINVAL);
2180
2181 //TODO: HDMI is connected but user doesn't want HDMI output, close both HDMI outputs.
2182
2183 /* Setting new device information to the mm module input streams.
2184 * This is needed if QAF module output streams are not created yet.
2185 */
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302186 out->devices = val;
Harsh Bansal28718c52017-04-20 13:47:12 +05302187
2188 if (val == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
2189 //If device is BT then open the BT stream if not already opened.
2190 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) == NULL
2191 && audio_extn_bt_hal_get_device(qaf_mod->bt_hdl) != NULL) {
2192 ret = audio_extn_bt_hal_open_output_stream(qaf_mod->bt_hdl,
2193 QAF_OUTPUT_SAMPLING_RATE,
2194 AUDIO_CHANNEL_OUT_STEREO,
2195 CODEC_BACKEND_DEFAULT_BIT_WIDTH);
2196 if (ret != 0) {
2197 ERROR_MSG("BT Output stream open failure!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302198 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302199 }
2200 } else if (val != 0) {
2201 //If device is not BT then close the BT stream if already opened.
2202 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
2203 audio_extn_bt_hal_close_output_stream(qaf_mod->bt_hdl);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302204 }
2205 }
2206
Harsh Bansal28718c52017-04-20 13:47:12 +05302207 if (p_qaf->passthrough_in == out) { //Device routing is received for QAF passthrough stream.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302208
Harsh Bansal28718c52017-04-20 13:47:12 +05302209 if (!(val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) { //HDMI route is disabled.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302210
Harsh Bansal28718c52017-04-20 13:47:12 +05302211 //If QAF pasthrough output is enabled. Close it.
2212 close_qaf_passthrough_stream();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302213
Harsh Bansal28718c52017-04-20 13:47:12 +05302214 //Send the routing information to mm module pcm output.
2215 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2216 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2217 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302218 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302219 //else: device info is updated in the input streams.
2220 } else { //HDMI route is enabled.
2221
2222 //create the QAf passthrough stream, if not created already.
2223 ret = create_qaf_passthrough_stream();
2224
2225 if (p_qaf->passthrough_out != NULL) { //If QAF passthrough out is enabled then send routing information.
2226 ret = p_qaf->passthrough_out->stream.common.set_parameters(
2227 (struct audio_stream *)p_qaf->passthrough_out, kvpairs);
2228 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302229 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302230 } else {
2231 //Send the routing information to mm module pcm output.
2232 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2233 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2234 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
2235 }
2236 //else: device info is updated in the input streams.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302237 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302238 str_parms_destroy(parms);
2239
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002240 return ret;
2241}
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302242
Harsh Bansal28718c52017-04-20 13:47:12 +05302243/* Checks if a stream is QAF stream or not. */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002244bool audio_extn_is_qaf_stream(struct stream_out *out)
2245{
Harsh Bansal28718c52017-04-20 13:47:12 +05302246 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002247
Harsh Bansal28718c52017-04-20 13:47:12 +05302248 if (qaf_mod) {
2249 return true;
2250 }
2251 return false;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002252}
2253
2254/* API to send playback stream specific config parameters */
2255int audio_extn_qaf_out_set_param_data(struct stream_out *out,
Harsh Bansal28718c52017-04-20 13:47:12 +05302256 audio_extn_param_id param_id,
2257 audio_extn_param_payload *payload)
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002258{
2259 int ret = -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05302260 int index;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002261 struct stream_out *new_out = NULL;
2262 struct audio_adsp_event *adsp_event;
Harsh Bansal28718c52017-04-20 13:47:12 +05302263 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002264
Harsh Bansal28718c52017-04-20 13:47:12 +05302265 if (!out || !qaf_mod || !payload) {
2266 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002267 return ret;
2268 }
2269
2270 /* In qaf output render session may not be opened at this time.
Harsh Bansal28718c52017-04-20 13:47:12 +05302271 to handle it store adsp_hdlr param info so that it can be
2272 applied later after opening render session from ms12 callback
2273 */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002274 if (param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302275 index = get_input_stream_index(out);
2276 if (index < 0) {
2277 ERROR_MSG("Invalid stream");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002278 return ret;
2279 }
2280 adsp_event = (struct audio_adsp_event *)payload;
2281
Harsh Bansal28718c52017-04-20 13:47:12 +05302282 if (payload->adsp_event_params.payload_length <= AUDIO_MAX_ADSP_STREAM_CMD_PAYLOAD_LEN) {
2283 pthread_mutex_lock(&p_qaf->lock);
2284 memcpy(qaf_mod->adsp_hdlr_config[index].event_payload,
2285 adsp_event->payload,
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002286 adsp_event->payload_length);
Harsh Bansal28718c52017-04-20 13:47:12 +05302287 qaf_mod->adsp_hdlr_config[index].event_params.payload =
2288 qaf_mod->adsp_hdlr_config[index].event_payload;
2289 qaf_mod->adsp_hdlr_config[index].event_params.payload_length =
2290 adsp_event->payload_length;
2291 qaf_mod->adsp_hdlr_config[index].adsp_hdlr_config_valid = true;
2292 pthread_mutex_unlock(&p_qaf->lock);
2293 } else {
2294 ERROR_MSG("Invalid adsp event length %d", adsp_event->payload_length);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002295 return ret;
2296 }
2297 ret = 0;
2298 }
2299
Harsh Bansal28718c52017-04-20 13:47:12 +05302300 /* apply param for all active out sessions */
2301 for (index = 0; index < MAX_QAF_MODULE_OUT; index++) {
2302 new_out = qaf_mod->stream_out[index];
2303 if (!new_out) continue;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002304
Harsh Bansal28718c52017-04-20 13:47:12 +05302305 /*ADSP event is not supported for passthrough*/
2306 if ((param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD)
2307 && !(new_out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) continue;
2308 if (new_out->standby)
2309 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
2310 lock_output_stream(new_out);
2311 ret = audio_extn_out_set_param_data(new_out, param_id, payload);
2312 if (ret)
2313 ERROR_MSG("audio_extn_out_set_param_data error %d", ret);
2314 unlock_output_stream(new_out);
2315 }
2316 return ret;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002317}
2318
2319int audio_extn_qaf_out_get_param_data(struct stream_out *out,
2320 audio_extn_param_id param_id,
2321 audio_extn_param_payload *payload)
2322{
Harsh Bansal28718c52017-04-20 13:47:12 +05302323 int ret = -EINVAL, i;
2324 struct stream_out *new_out;
2325 struct audio_usecase *uc_info;
2326 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002327
Harsh Bansal28718c52017-04-20 13:47:12 +05302328 if (!out || !qaf_mod || !payload) {
2329 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002330 return ret;
2331 }
2332
Harsh Bansal28718c52017-04-20 13:47:12 +05302333 if (!p_qaf->hdmi_connect) {
2334 ERROR_MSG("hdmi not connected");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002335 return ret;
2336 }
2337
2338 /* get session which is routed to hdmi*/
Harsh Bansal28718c52017-04-20 13:47:12 +05302339 if (p_qaf->passthrough_out)
2340 new_out = p_qaf->passthrough_out;
2341 else {
2342 for (i = 0; i < MAX_QAF_MODULE_OUT; i++) {
2343 if (qaf_mod->stream_out[i]) {
2344 new_out = qaf_mod->stream_out[i];
2345 break;
2346 }
2347 }
2348 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002349
2350 if (!new_out) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302351 ERROR_MSG("No stream active.");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002352 return ret;
2353 }
2354
2355 if (new_out->standby)
Harsh Bansal28718c52017-04-20 13:47:12 +05302356 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002357
2358 lock_output_stream(new_out);
2359 ret = audio_extn_out_get_param_data(new_out, param_id, payload);
Harsh Bansal28718c52017-04-20 13:47:12 +05302360 if (ret)
2361 ERROR_MSG("audio_extn_out_get_param_data error %d", ret);
2362 unlock_output_stream(new_out);
2363
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302364 return ret;
2365}
2366
Harsh Bansal28718c52017-04-20 13:47:12 +05302367/* To open a stream with QAF. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302368int audio_extn_qaf_open_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302369 audio_io_handle_t handle,
2370 audio_devices_t devices,
2371 audio_output_flags_t flags,
2372 struct audio_config *config,
2373 struct audio_stream_out **stream_out,
2374 const char *address)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302375{
2376 int ret = 0;
2377 struct stream_out *out;
2378
2379 ret = adev_open_output_stream(dev, handle, devices, flags, config, stream_out, address);
2380 if (*stream_out == NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302381 ERROR_MSG("Stream open failed %d", ret);
Deepak Agarwal1e42b852017-02-11 17:57:04 +05302382 return ret;
2383 }
2384
Harsh Bansal28718c52017-04-20 13:47:12 +05302385 out = (struct stream_out *)*stream_out;
2386
2387 ret = qaf_stream_open(out, config, flags, devices);
2388 if (ret == -ENOTSUP) {
2389 //Stream not supported by QAF, Bypass QAF.
2390 return 0;
2391 } else if (ret < 0) {
2392 ERROR_MSG("Error opening QAF stream err[%d]!", ret);
2393 adev_close_output_stream(dev, *stream_out);
2394 return ret;
2395 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302396
2397 /* Override function pointers based on qaf definitions */
2398 out->stream.set_volume = qaf_out_set_volume;
2399 out->stream.pause = qaf_out_pause;
2400 out->stream.resume = qaf_out_resume;
2401 out->stream.drain = qaf_out_drain;
2402 out->stream.flush = qaf_out_flush;
2403
2404 out->stream.common.standby = qaf_out_standby;
2405 out->stream.common.set_parameters = qaf_out_set_parameters;
2406 out->stream.get_latency = qaf_out_get_latency;
2407 out->stream.write = qaf_out_write;
2408 out->stream.get_presentation_position = qaf_out_get_presentation_position;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302409 out->platform_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302410
Harsh Bansal28718c52017-04-20 13:47:12 +05302411 /*TODO: Need to handle this for DTS*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302412 if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) {
2413 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
Varun B34da7a42017-02-13 16:16:53 +05302414 out->config.period_size = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302415 out->config.period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT;
Varun B34da7a42017-02-13 16:16:53 +05302416 out->config.start_threshold = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
2417 out->config.avail_min = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302418 } else if(out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
2419 out->compr_config.fragment_size = qaf_get_pcm_offload_input_buffer_size(&(config->offload_info));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302420 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302421
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302422 *stream_out = &out->stream;
2423 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2424 qaf_create_offload_callback_thread(out);
2425 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302426
2427 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302428 return 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302429}
2430
Harsh Bansal28718c52017-04-20 13:47:12 +05302431/* Close a QAF stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302432void audio_extn_qaf_close_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302433 struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302434{
2435 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05302436 struct qaf_module* qaf_mod = get_qaf_module_for_input_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302437
Harsh Bansal28718c52017-04-20 13:47:12 +05302438 if (!qaf_mod) return;
2439
2440 DEBUG_MSG("stream_handle(%p) format = %x", out, out->format);
2441
2442 //If close is received for QAF passthrough stream then close the QAF passthrough output.
2443 if (p_qaf->passthrough_in == out) {
2444 if (p_qaf->passthrough_out) {
2445 ALOGD("%s %d closing stream handle %p", __func__, __LINE__, p_qaf->passthrough_out);
2446 pthread_mutex_lock(&p_qaf->lock);
2447 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2448 (struct audio_stream_out *)(p_qaf->passthrough_out));
2449 pthread_mutex_unlock(&p_qaf->lock);
2450 p_qaf->passthrough_out = NULL;
2451 }
2452
2453 p_qaf->passthrough_in = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302454 }
2455
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302456 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2457 qaf_destroy_offload_callback_thread(out);
2458 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002459
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302460 qaf_stream_close(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302461
2462 adev_close_output_stream(dev, stream);
Harsh Bansal28718c52017-04-20 13:47:12 +05302463
2464 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302465}
2466
Harsh Bansal28718c52017-04-20 13:47:12 +05302467/* Check if QAF is supported or not. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302468bool audio_extn_qaf_is_enabled()
2469{
2470 bool prop_enabled = false;
2471 char value[PROPERTY_VALUE_MAX] = {0};
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002472 property_get("vendor.audio.qaf.enabled", value, NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302473 prop_enabled = atoi(value) || !strncmp("true", value, 4);
2474 return (prop_enabled);
2475}
2476
Harsh Bansal28718c52017-04-20 13:47:12 +05302477/* Query HDMI EDID and sets module output accordingly.*/
2478void set_hdmi_configuration_to_module()
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302479{
Harsh Bansal28718c52017-04-20 13:47:12 +05302480 int channels = 0;
2481 char *format_params;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302482 struct str_parms *qaf_params;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302483 char prop_value[PROPERTY_VALUE_MAX];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302484 bool passth_support = false;
2485
Harsh Bansal28718c52017-04-20 13:47:12 +05302486 DEBUG_MSG("Entry");
2487
2488 if (!p_qaf) {
2489 return;
2490 }
2491
2492 if (!p_qaf->hdmi_connect) {
2493 DEBUG_MSG("HDMI is not connected.");
2494 return;
2495 }
2496
2497 p_qaf->hdmi_sink_channels = 0;
2498
2499 //QAF re-encoding and DSP offload passthrough is supported.
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002500 if (property_get_bool("vendor.audio.offload.passthrough", false)
2501 && property_get_bool("vendor.audio.qaf.reencode", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302502
2503 //If MS12 session is active.
2504 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2505
2506 bool do_setparam = false;
2507 qaf_params = str_parms_create();
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002508 property_get("vendor.audio.qaf.hdmi.out", prop_value, NULL);
Harsh Bansal28718c52017-04-20 13:47:12 +05302509
2510 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_E_AC3)
2511 && (strncmp(prop_value, "ddp", 3) == 0)) {
2512 do_setparam = true;
2513 if (qaf_params) {
2514 str_parms_add_str(qaf_params,
2515 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2516 AUDIO_QAF_PARAMETER_VALUE_REENCODE_EAC3);
2517 }
2518 } else if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_AC3)) {
2519 do_setparam = true;
2520 if (qaf_params) {
2521 str_parms_add_str(qaf_params,
2522 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2523 AUDIO_QAF_PARAMETER_VALUE_REENCODE_AC3);
2524 }
2525 }
2526
2527 if (do_setparam) {
2528 if (p_qaf->qaf_msmd_enabled) {
2529 str_parms_add_str(qaf_params,
2530 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2531 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2532 } else {
2533 str_parms_add_str(qaf_params,
2534 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2535 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2536 }
2537 format_params = str_parms_to_str(qaf_params);
2538
2539 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2540 format_params);
2541
2542 passth_support = true;
2543 }
2544 str_parms_destroy(qaf_params);
2545 }
2546
2547 //DTS_M8 session is active.
2548 if (p_qaf->qaf_mod[DTS_M8].session_handle
2549 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2550
2551 bool do_setparam = false;
2552 qaf_params = str_parms_create();
Harsh Bansal28718c52017-04-20 13:47:12 +05302553 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_DTS)) {
2554 do_setparam = true;
2555 if (qaf_params) {
2556 str_parms_add_str(qaf_params,
2557 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2558 AUDIO_QAF_PARAMETER_VALUE_REENCODE_DTS);
2559 }
2560 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302561
2562 if (do_setparam) {
2563 if (p_qaf->qaf_msmd_enabled) {
2564 str_parms_add_str(qaf_params,
2565 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2566 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2567 } else {
2568 str_parms_add_str(qaf_params,
2569 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2570 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2571 }
2572 format_params = str_parms_to_str(qaf_params);
2573
2574 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2575 format_params);
2576
2577 passth_support = true;
2578 }
2579 str_parms_destroy(qaf_params);
2580 }
2581 }
2582
2583 //Compressed passthrough is not enabled.
2584 if (!passth_support) {
2585
2586 channels = platform_edid_get_max_channels(p_qaf->adev->platform);
2587
2588 qaf_params = str_parms_create();
Harsh Bansal431eede2017-04-28 21:22:30 +05302589
2590 str_parms_add_str(qaf_params,
2591 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2592 AUDIO_QAF_PARAMETER_VALUE_PCM);
Harsh Bansal28718c52017-04-20 13:47:12 +05302593 switch (channels) {
2594 case 8:
2595 DEBUG_MSG("Switching Qaf output to 7.1 channels");
2596 str_parms_add_str(qaf_params,
2597 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2598 AUDIO_QAF_PARAMETER_VALUE_8_CHANNELS);
2599 if (p_qaf->qaf_msmd_enabled) {
2600 str_parms_add_str(qaf_params,
2601 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2602 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2603 } else {
2604 str_parms_add_str(qaf_params,
2605 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2606 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2607 }
2608 p_qaf->hdmi_sink_channels = channels;
2609 break;
2610 case 6:
2611 DEBUG_MSG("Switching Qaf output to 5.1 channels");
2612 str_parms_add_str(qaf_params,
2613 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2614 AUDIO_QAF_PARAMETER_VALUE_6_CHANNELS);
2615 if (p_qaf->qaf_msmd_enabled) {
2616 str_parms_add_str(qaf_params,
2617 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2618 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2619 } else {
2620 str_parms_add_str(qaf_params,
2621 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2622 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2623 }
2624 p_qaf->hdmi_sink_channels = channels;
2625 break;
2626 default:
2627 DEBUG_MSG("Switching Qaf output to default channels");
2628 str_parms_add_str(qaf_params,
2629 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2630 AUDIO_QAF_PARAMETER_VALUE_DEFAULT_CHANNELS);
2631 if (p_qaf->qaf_msmd_enabled) {
2632 str_parms_add_str(qaf_params,
2633 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2634 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2635 } else {
2636 str_parms_add_str(qaf_params,
2637 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2638 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2639 }
2640 p_qaf->hdmi_sink_channels = 2;
2641 break;
2642 }
2643
2644 format_params = str_parms_to_str(qaf_params);
2645
2646 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2647 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2648 format_params);
2649 }
2650 if (p_qaf->qaf_mod[DTS_M8].session_handle
2651 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2652 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2653 format_params);
2654 }
2655
2656 str_parms_destroy(qaf_params);
2657 }
2658 DEBUG_MSG("Exit");
2659}
2660
2661/* QAF set parameter function. For Device connect and disconnect. */
2662int audio_extn_qaf_set_parameters(struct audio_device *adev, struct str_parms *parms)
2663{
2664 int status = 0, val = 0, k;
2665 char *format_params, *kv_parirs;
2666 struct str_parms *qaf_params;
2667 char value[32];
2668
2669 DEBUG_MSG("Entry");
2670
2671 if (!p_qaf) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302672 return -EINVAL;
2673 }
2674
2675 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
Harsh Bansal28718c52017-04-20 13:47:12 +05302676
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302677 if (status >= 0) {
2678 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302679 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is connected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302680
Harsh Bansal28718c52017-04-20 13:47:12 +05302681 p_qaf->hdmi_connect = 1;
2682 p_qaf->hdmi_sink_channels = 0;
2683
2684 if (p_qaf->passthrough_in) { //If QAF passthrough is already initialized.
2685 lock_output_stream(p_qaf->passthrough_in);
2686 if (platform_is_edid_supported_format(adev->platform,
2687 p_qaf->passthrough_in->format)) {
2688 //If passthrough format is supported by HDMI then create the QAF passthrough output if not created already.
2689 create_qaf_passthrough_stream();
2690 //Ignoring the returned error, If error then QAF passthrough is disabled.
2691 } else {
2692 //If passthrough format is not supported by HDMI then close the QAF passthrough output if already created.
2693 close_qaf_passthrough_stream();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302694 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302695 unlock_output_stream(p_qaf->passthrough_in);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302696 }
2697
Harsh Bansal28718c52017-04-20 13:47:12 +05302698 set_hdmi_configuration_to_module();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302699
Bharath Gopal01310bb2016-12-05 15:39:32 +05302700 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302701 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2702 if (!p_qaf->qaf_mod[k].bt_hdl) {
2703 DEBUG_MSG("Opening a2dp output...");
2704 status = audio_extn_bt_hal_load(&p_qaf->qaf_mod[k].bt_hdl);
2705 if (status != 0) {
2706 ERROR_MSG("Error opening BT module");
2707 return status;
2708 }
2709 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05302710 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302711 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302712 //TODO else if: Need to consider other devices.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302713 }
2714
2715 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
2716 if (status >= 0) {
2717 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302718 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is disconnected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302719
Harsh Bansal28718c52017-04-20 13:47:12 +05302720 qaf_params = str_parms_create();
2721 str_parms_add_str(qaf_params,
2722 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2723 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2724 str_parms_add_str(qaf_params,
2725 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2726 AUDIO_QAF_PARAMETER_VALUE_PCM);
2727 p_qaf->hdmi_sink_channels = 0;
2728
2729 p_qaf->passthrough_enabled = 0;
2730 p_qaf->mch_pcm_hdmi_enabled = 0;
2731 p_qaf->hdmi_connect = 0;
2732
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302733 format_params = str_parms_to_str(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302734
2735 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2736 if (p_qaf->qaf_mod[k].session_handle
2737 && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2738 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2739 p_qaf->qaf_mod[k].session_handle, format_params);
2740 }
2741 }
2742 close_all_hdmi_output();
2743
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302744 str_parms_destroy(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302745 close_qaf_passthrough_stream();
Bharath Gopal01310bb2016-12-05 15:39:32 +05302746 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302747 DEBUG_MSG("Closing a2dp output...");
2748 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2749 if (p_qaf->qaf_mod[k].bt_hdl) {
2750 audio_extn_bt_hal_unload(p_qaf->qaf_mod[k].bt_hdl);
2751 p_qaf->qaf_mod[k].bt_hdl = NULL;
2752 }
2753 }
2754 }
2755 //TODO else if: Need to consider other devices.
2756 }
2757
2758 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2759 kv_parirs = str_parms_to_str(parms);
2760 if (p_qaf->qaf_mod[k].session_handle && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2761 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2762 p_qaf->qaf_mod[k].session_handle, kv_parirs);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302763 }
2764 }
2765
Harsh Bansal28718c52017-04-20 13:47:12 +05302766 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302767 return status;
2768}
2769
Harsh Bansal28718c52017-04-20 13:47:12 +05302770/* Create the QAF. */
2771int audio_extn_qaf_init(struct audio_device *adev)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302772{
Harsh Bansal28718c52017-04-20 13:47:12 +05302773 DEBUG_MSG("Entry");
2774
2775 p_qaf = calloc(1, sizeof(struct qaf));
2776 if (p_qaf == NULL) {
2777 ERROR_MSG("Out of memory");
2778 return -ENOMEM;
2779 }
2780
2781 p_qaf->adev = adev;
2782
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002783 if (property_get_bool("vendor.audio.qaf.msmd", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302784 p_qaf->qaf_msmd_enabled = 1;
2785 }
2786 pthread_mutex_init(&p_qaf->lock, (const pthread_mutexattr_t *) NULL);
2787
2788 int i = 0;
2789
2790 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2791 char value[PROPERTY_VALUE_MAX] = {0};
2792 char lib_name[PROPERTY_VALUE_MAX] = {0};
2793 struct qaf_module *qaf_mod = &(p_qaf->qaf_mod[i]);
2794
2795 if (i == MS12) {
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002796 property_get("vendor.audio.qaf.library", value, NULL);
Harsh Bansal28718c52017-04-20 13:47:12 +05302797 } else if (i == DTS_M8) {
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002798 property_get("vendor.audio.qaf.m8.library", value, NULL);
Harsh Bansal28718c52017-04-20 13:47:12 +05302799 } else {
2800 continue;
2801 }
2802
2803 snprintf(lib_name, PROPERTY_VALUE_MAX, "%s", value);
2804
2805 qaf_mod->qaf_lib = dlopen(lib_name, RTLD_NOW);
2806 if (qaf_mod->qaf_lib == NULL) {
2807 ERROR_MSG("DLOPEN failed for %s", lib_name);
2808 continue;
2809 }
2810
2811 DEBUG_MSG("DLOPEN successful for %s", lib_name);
2812 qaf_mod->qaf_audio_session_open =
Sri Karri1fbfef52017-06-29 19:45:47 +05302813 (int (*)(audio_session_handle_t* session_handle, audio_session_type_t s_type,
2814 void *p_data, void* license_data))dlsym(qaf_mod->qaf_lib,
Harsh Bansal28718c52017-04-20 13:47:12 +05302815 "audio_session_open");
2816 qaf_mod->qaf_audio_session_close =
2817 (int (*)(audio_session_handle_t session_handle))dlsym(qaf_mod->qaf_lib,
2818 "audio_session_close");
2819 qaf_mod->qaf_audio_stream_open =
2820 (int (*)(audio_session_handle_t session_handle, audio_stream_handle_t* stream_handle,
2821 audio_stream_config_t input_config, audio_devices_t devices, stream_type_t flags))dlsym(qaf_mod->qaf_lib,
2822 "audio_stream_open");
2823 qaf_mod->qaf_audio_stream_close =
2824 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2825 "audio_stream_close");
2826 qaf_mod->qaf_audio_stream_set_param =
2827 (int (*)(audio_stream_handle_t stream_handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2828 "audio_stream_set_param");
2829 qaf_mod->qaf_audio_session_set_param =
2830 (int (*)(audio_session_handle_t handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2831 "audio_session_set_param");
2832 qaf_mod->qaf_audio_stream_get_param =
2833 (char* (*)(audio_stream_handle_t stream_handle, const char* key))dlsym(qaf_mod->qaf_lib,
2834 "audio_stream_get_param");
2835 qaf_mod->qaf_audio_session_get_param =
2836 (char* (*)(audio_session_handle_t handle, const char* key))dlsym(qaf_mod->qaf_lib,
2837 "audio_session_get_param");
2838 qaf_mod->qaf_audio_stream_start =
2839 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2840 "audio_stream_start");
2841 qaf_mod->qaf_audio_stream_stop =
2842 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2843 "audio_stream_stop");
2844 qaf_mod->qaf_audio_stream_pause =
2845 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2846 "audio_stream_pause");
2847 qaf_mod->qaf_audio_stream_flush =
2848 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2849 "audio_stream_flush");
2850 qaf_mod->qaf_audio_stream_write =
2851 (int (*)(audio_stream_handle_t stream_handle, const void* buf, int size))dlsym(qaf_mod->qaf_lib,
2852 "audio_stream_write");
2853 qaf_mod->qaf_register_event_callback =
2854 (void (*)(audio_session_handle_t session_handle, void *priv_data, notify_event_callback_t event_callback,
2855 audio_event_id_t event_id))dlsym(qaf_mod->qaf_lib,
2856 "register_event_callback");
2857 }
2858
2859 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302860 return 0;
2861}
2862
Harsh Bansal28718c52017-04-20 13:47:12 +05302863/* Tear down the qaf extension. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302864void audio_extn_qaf_deinit()
2865{
Harsh Bansal28718c52017-04-20 13:47:12 +05302866 int i;
2867 DEBUG_MSG("Entry");
2868
2869 if (p_qaf != NULL) {
2870 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2871 qaf_session_close(&p_qaf->qaf_mod[i]);
2872
2873 if (p_qaf->qaf_mod[i].qaf_lib != NULL) {
2874 dlclose(p_qaf->qaf_mod[i].qaf_lib);
2875 p_qaf->qaf_mod[i].qaf_lib = NULL;
2876 }
2877 }
2878 if (p_qaf->passthrough_out) {
2879 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2880 (struct audio_stream_out *)(p_qaf->passthrough_out));
2881 p_qaf->passthrough_out = NULL;
2882 }
2883
2884 pthread_mutex_destroy(&p_qaf->lock);
2885 free(p_qaf);
2886 p_qaf = NULL;
2887 }
2888 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302889}