blob: c0dad9c80a81e09f83b5589d356526925ac2f8a6 [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,
168 void *p_data,
169 void* license_data);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530170 int (*qaf_audio_session_close)(audio_session_handle_t session_handle);
Harsh Bansal28718c52017-04-20 13:47:12 +0530171 int (*qaf_audio_stream_open)(audio_session_handle_t session_handle,
172 audio_stream_handle_t* stream_handle,
173 audio_stream_config_t input_config,
174 audio_devices_t devices,
175 stream_type_t flags);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530176 int (*qaf_audio_stream_close)(audio_stream_handle_t stream_handle);
177 int (*qaf_audio_stream_set_param)(audio_stream_handle_t stream_handle, const char* kv_pairs);
178 int (*qaf_audio_session_set_param)(audio_session_handle_t handle, const char* kv_pairs);
179 char* (*qaf_audio_stream_get_param)(audio_stream_handle_t stream_handle, const char* key);
180 char* (*qaf_audio_session_get_param)(audio_session_handle_t handle, const char* key);
181 int (*qaf_audio_stream_start)(audio_stream_handle_t handle);
182 int (*qaf_audio_stream_stop)(audio_stream_handle_t stream_handle);
183 int (*qaf_audio_stream_pause)(audio_stream_handle_t stream_handle);
184 int (*qaf_audio_stream_flush)(audio_stream_handle_t stream_handle);
185 int (*qaf_audio_stream_write)(audio_stream_handle_t stream_handle, const void* buf, int size);
Harsh Bansal28718c52017-04-20 13:47:12 +0530186 void (*qaf_register_event_callback)(audio_session_handle_t session_handle,
187 void *priv_data,
188 notify_event_callback_t event_callback,
189 audio_event_id_t event_id);
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800190
Harsh Bansal28718c52017-04-20 13:47:12 +0530191 /*Input stream of MM module */
192 struct stream_out *stream_in[MAX_QAF_MODULE_IN];
193 /*Output Stream from MM module */
194 struct stream_out *stream_out[MAX_QAF_MODULE_OUT];
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800195
Harsh Bansal28718c52017-04-20 13:47:12 +0530196 struct qaf_adsp_hdlr_config_state adsp_hdlr_config[MAX_QAF_MODULE_IN];
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800197
Harsh Bansal28718c52017-04-20 13:47:12 +0530198 //BT session handle.
Bharath Gopal01310bb2016-12-05 15:39:32 +0530199 void *bt_hdl;
Harsh Bansal28718c52017-04-20 13:47:12 +0530200
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530201 float vol_left;
202 float vol_right;
Harsh Bansal28718c52017-04-20 13:47:12 +0530203 bool is_vol_set;
204 bool drain_received[MAX_QAF_MODULE_IN];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530205};
206
Harsh Bansal28718c52017-04-20 13:47:12 +0530207struct qaf {
208 struct audio_device *adev;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530209
Harsh Bansal28718c52017-04-20 13:47:12 +0530210 pthread_mutex_t lock;
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800211
Harsh Bansal28718c52017-04-20 13:47:12 +0530212 bool hdmi_connect;
213 int hdmi_sink_channels;
214
215 //Flag to indicate if QAF transcode output stream is enabled from any mm module.
216 bool passthrough_enabled;
217 //Flag to indicate if QAF mch pcm output stream is enabled from any mm module.
218 bool mch_pcm_hdmi_enabled;
219
220 //Flag to indicate if msmd is supported.
221 bool qaf_msmd_enabled;
222
223 //Handle of QAF input stream, which is routed as QAF passthrough.
224 struct stream_out *passthrough_in;
225 //Handle of QAF passthrough stream.
226 struct stream_out *passthrough_out;
227
228 struct qaf_module qaf_mod[MAX_MM_MODULE_TYPE];
229};
230
231static int qaf_out_pause(struct audio_stream_out* stream);
232static int qaf_out_flush(struct audio_stream_out* stream);
233static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type);
234
235//Global handle of QAF. Access to this should be protected by mutex lock.
236static struct qaf *p_qaf = NULL;
237
238/* Gets the pointer to qaf module for the qaf input stream. */
239static struct qaf_module* get_qaf_module_for_input_stream(struct stream_out *out)
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530240{
Harsh Bansal28718c52017-04-20 13:47:12 +0530241 struct qaf_module *qaf_mod = NULL;
242 int i, j;
243 if (!p_qaf) return NULL;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530244
Harsh Bansal28718c52017-04-20 13:47:12 +0530245 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
246 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
247 if (p_qaf->qaf_mod[i].stream_in[j] == out) {
248 qaf_mod = &(p_qaf->qaf_mod[i]);
249 break;
250 }
251 }
252 }
253
254 return qaf_mod;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530255}
256
Harsh Bansal28718c52017-04-20 13:47:12 +0530257/* Finds the mm module input stream index for the QAF input stream. */
258static int get_input_stream_index(struct stream_out *out)
259{
260 int index = -1, j;
261 struct qaf_module* qaf_mod = NULL;
262
263 qaf_mod = get_qaf_module_for_input_stream(out);
264 if (!qaf_mod) return index;
265
266 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
267 if (qaf_mod->stream_in[j] == out) {
268 index = j;
269 break;
270 }
271 }
272
273 return index;
274}
275
276/* Finds the right mm module for the QAF input stream format. */
277static mm_module_type get_mm_module_for_format(audio_format_t format)
278{
279 int j;
280
281 DEBUG_MSG("Format 0x%x", format);
282
283 if (format == AUDIO_FORMAT_PCM_16_BIT) {
284 //If dts is not supported then alway support pcm with MS12
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700285 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 +0530286 return MS12;
287 }
288
289 //If QAF passthrough is active then send the PCM stream to primary HAL.
290 if (!p_qaf->passthrough_out) {
291 /* Iff any stream is active in MS12 module then route PCM stream to it. */
292 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
293 if (p_qaf->qaf_mod[MS12].stream_in[j]) {
294 return MS12;
295 }
296 }
297 }
298 return INVALID_MM_MODULE;
299 }
300
301 switch (format & AUDIO_FORMAT_MAIN_MASK) {
302 case AUDIO_FORMAT_AC3:
303 case AUDIO_FORMAT_E_AC3:
304 case AUDIO_FORMAT_AAC:
305 case AUDIO_FORMAT_AAC_ADTS:
306 case AUDIO_FORMAT_AC4:
307 return MS12;
308 case AUDIO_FORMAT_DTS:
309 case AUDIO_FORMAT_DTS_HD:
310 return DTS_M8;
311 default:
312 return INVALID_MM_MODULE;
313 }
314}
315
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530316static bool is_main_active(struct qaf_module* qaf_mod)
317{
318 return (qaf_mod->stream_in[QAF_IN_MAIN] || qaf_mod->stream_in[QAF_IN_MAIN_2]);
319}
320
321static bool is_dual_main_active(struct qaf_module* qaf_mod)
322{
323 return (qaf_mod->stream_in[QAF_IN_MAIN] && qaf_mod->stream_in[QAF_IN_MAIN_2]);
324}
325
Harsh Bansal28718c52017-04-20 13:47:12 +0530326/* Gets the pcm output buffer size(in samples) for the mm module. */
327static uint32_t get_pcm_output_buffer_size_samples(struct qaf_module *qaf_mod)
328{
329 uint32_t pcm_output_buffer_size = 0;
330
331 if (qaf_mod == &p_qaf->qaf_mod[MS12]) {
332 pcm_output_buffer_size = MS12_PCM_OUT_FRAGMENT_SIZE;
333 } else if (qaf_mod == &p_qaf->qaf_mod[DTS_M8]) {
334 pcm_output_buffer_size = DTS_PCM_OUT_FRAGMENT_SIZE;
335 }
336
337 return pcm_output_buffer_size;
338}
339
340/* Acquire Mutex lock on output stream */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530341static void lock_output_stream(struct stream_out *out)
342{
343 pthread_mutex_lock(&out->pre_lock);
344 pthread_mutex_lock(&out->lock);
345 pthread_mutex_unlock(&out->pre_lock);
346}
347
Harsh Bansal28718c52017-04-20 13:47:12 +0530348/* Release Mutex lock on output stream */
349static void unlock_output_stream(struct stream_out *out)
350{
351 pthread_mutex_unlock(&out->lock);
352}
353
354/* Checks if stream can be routed as QAF passthrough or not. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530355static bool audio_extn_qaf_passthrough_enabled(struct stream_out *out)
356{
Harsh Bansal28718c52017-04-20 13:47:12 +0530357 DEBUG_MSG("Format 0x%x", out->format);
358 bool is_enabled = false;
359
360 if (!p_qaf) return false;
361
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700362 if ((!property_get_bool("vendor.audio.qaf.reencode", false))
363 && property_get_bool("vendor.audio.qaf.passthrough", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530364
365 if ((out->format == AUDIO_FORMAT_PCM_16_BIT) && (popcount(out->channel_mask) > 2)) {
366 is_enabled = true;
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700367 } else if (property_get_bool("vendor.audio.offload.passthrough", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530368 switch (out->format) {
369 case AUDIO_FORMAT_AC3:
370 case AUDIO_FORMAT_E_AC3:
371 case AUDIO_FORMAT_DTS:
Ben Romberger1aaaf862017-04-06 17:49:46 -0700372 case AUDIO_FORMAT_DTS_HD:
Naresh Tanniru928f0862017-04-07 16:44:23 -0700373 case AUDIO_FORMAT_DOLBY_TRUEHD:
374 case AUDIO_FORMAT_IEC61937: {
Harsh Bansal28718c52017-04-20 13:47:12 +0530375 is_enabled = true;
376 break;
377 }
378 default:
379 is_enabled = false;
380 break;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530381 }
382 }
383 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530384
385 return is_enabled;
386}
387
388/*Closes all pcm hdmi output from QAF. */
389static void close_all_pcm_hdmi_output()
390{
391 int i;
392 //Closing all the PCM HDMI output stream from QAF.
393 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
394 if (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]) {
395 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
396 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]));
397 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH] = NULL;
398 }
399
400 if ((p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD])
401 && (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
402 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
403 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]));
404 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD] = NULL;
405 }
406 }
407
408 p_qaf->mch_pcm_hdmi_enabled = 0;
409}
410
411static void close_all_hdmi_output()
412{
413 int k;
414 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
415 if (p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
416 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
417 (struct audio_stream_out *)(p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]));
418 p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] = NULL;
419 }
420 }
421 p_qaf->passthrough_enabled = 0;
422
423 close_all_pcm_hdmi_output();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530424}
425
426static int qaf_out_callback(stream_callback_event_t event, void *param __unused, void *cookie)
427{
428 struct stream_out *out = (struct stream_out *)cookie;
429
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800430 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530431 return 0;
432}
433
Harsh Bansal28718c52017-04-20 13:47:12 +0530434/* Creates the QAF passthrough output stream. */
435static int create_qaf_passthrough_stream()
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530436{
Harsh Bansal28718c52017-04-20 13:47:12 +0530437 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530438
Harsh Bansal28718c52017-04-20 13:47:12 +0530439 int ret = 0, k;
440 struct stream_out *out = p_qaf->passthrough_in;
441
442 if (!out) return -EINVAL;
443
444 pthread_mutex_lock(&p_qaf->lock);
445 lock_output_stream(out);
446
447 //Creating QAF passthrough output stream.
448 if (NULL == p_qaf->passthrough_out) {
449 audio_output_flags_t flags;
450 struct audio_config config;
451 audio_devices_t devices;
452
453 config.sample_rate = config.offload_info.sample_rate = out->sample_rate;
454 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
455 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
456 config.offload_info.format = out->format;
457 config.offload_info.bit_width = out->bit_width;
458 config.format = out->format;
459 config.offload_info.channel_mask = config.channel_mask = out->channel_mask;
460
461 //Device is copied from the QAF passthrough input stream.
462 devices = out->devices;
463 flags = out->flags;
464
465 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
466 QAF_DEFAULT_PASSTHROUGH_HANDLE,
467 devices,
468 flags,
469 &config,
470 (struct audio_stream_out **)&(p_qaf->passthrough_out),
471 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530472 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530473 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
474 unlock_output_stream(out);
475 return ret;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530476 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530477 p_qaf->passthrough_in = out;
478 p_qaf->passthrough_out->stream.set_callback((struct audio_stream_out *)p_qaf->passthrough_out,
479 (stream_callback_t) qaf_out_callback, out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530480 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530481
482 unlock_output_stream(out);
483
484 //Since QAF-Passthrough is created, close other HDMI outputs.
485 close_all_hdmi_output();
486
487 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530488 return ret;
489}
490
Harsh Bansal28718c52017-04-20 13:47:12 +0530491/* Closes the QAF passthrough output stream. */
492static void close_qaf_passthrough_stream()
493{
494 if (p_qaf->passthrough_out != NULL) { //QAF pasthroug is enabled. Close it.
495 pthread_mutex_lock(&p_qaf->lock);
496 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
497 (struct audio_stream_out *)(p_qaf->passthrough_out));
498 p_qaf->passthrough_out = NULL;
499 pthread_mutex_unlock(&p_qaf->lock);
500
501 if (p_qaf->passthrough_in->qaf_stream_handle) {
502 qaf_out_pause((struct audio_stream_out*)p_qaf->passthrough_in);
503 qaf_out_flush((struct audio_stream_out*)p_qaf->passthrough_in);
504 qaf_out_drain((struct audio_stream_out*)p_qaf->passthrough_in,
505 (audio_drain_type_t)STREAM_CBK_EVENT_DRAIN_READY);
506 }
507 }
508}
509
510/* Sends a command to output stream offload thread. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530511static int qaf_send_offload_cmd_l(struct stream_out* out, int command)
512{
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530513 DEBUG_MSG_VV("command is %d", command);
Harsh Bansal28718c52017-04-20 13:47:12 +0530514
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530515 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
516
517 if (!cmd) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530518 ERROR_MSG("failed to allocate mem for command 0x%x", command);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530519 return -ENOMEM;
520 }
521
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530522 cmd->cmd = command;
Harsh Bansal28718c52017-04-20 13:47:12 +0530523
524 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530525 list_add_tail(&out->qaf_offload_cmd_list, &cmd->node);
526 pthread_cond_signal(&out->qaf_offload_cond);
Harsh Bansal28718c52017-04-20 13:47:12 +0530527 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530528 return 0;
529}
530
Harsh Bansal28718c52017-04-20 13:47:12 +0530531/* Stops a QAF module stream.*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530532static int audio_extn_qaf_stream_stop(struct stream_out *out)
533{
Harsh Bansal28718c52017-04-20 13:47:12 +0530534 int ret = 0;
535 DEBUG_MSG("Output Stream 0x%x", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530536
Harsh Bansal28718c52017-04-20 13:47:12 +0530537 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
538 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_stop)) {
539 return ret;
540 }
541
542 if (out->qaf_stream_handle) {
543 ret = qaf_mod->qaf_audio_stream_stop(out->qaf_stream_handle);
544 }
545
546 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530547}
548
Harsh Bansal28718c52017-04-20 13:47:12 +0530549/* Puts a QAF module stream in standby. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530550static int qaf_out_standby(struct audio_stream *stream)
551{
552 struct stream_out *out = (struct stream_out *)stream;
553 int status = 0;
554
555 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
556 stream, out->usecase, use_case_table[out->usecase]);
557
558 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530559
560 //If QAF passthrough is active then block standby on all the input streams of QAF mm modules.
561 if (p_qaf->passthrough_out) {
562 //If standby is received on QAF passthrough stream then forward it to primary HAL.
563 if (p_qaf->passthrough_in == out) {
564 status = p_qaf->passthrough_out->stream.common.standby(
565 (struct audio_stream *)p_qaf->passthrough_out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530566 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530567 } else {
568 //If QAF passthrough stream is not active then stop the QAF module stream.
569 status = audio_extn_qaf_stream_stop(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530570 }
571
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530572 if (!out->standby) {
573 out->standby = true;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530574 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530575
576 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530577 return status;
578}
579
Harsh Bansal28718c52017-04-20 13:47:12 +0530580/* Sets the volume to PCM output stream. */
581static int qaf_out_set_volume(struct audio_stream_out *stream, float left, float right)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530582{
583 int ret = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530584 struct stream_out *out = (struct stream_out *)stream;
585 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530586
Harsh Bansal28718c52017-04-20 13:47:12 +0530587 DEBUG_MSG("Left %f, Right %f", left, right);
588
589 qaf_mod = get_qaf_module_for_input_stream(out);
590 if (!qaf_mod) {
591 return -EINVAL;
592 }
593
594 pthread_mutex_lock(&p_qaf->lock);
595 qaf_mod->vol_left = left;
596 qaf_mod->vol_right = right;
597 qaf_mod->is_vol_set = true;
598 pthread_mutex_unlock(&p_qaf->lock);
599
600 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL) {
601 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
602 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], left, right);
603 }
604
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530605 return ret;
606}
607
Harsh Bansal28718c52017-04-20 13:47:12 +0530608/* Starts a QAF module stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530609static int qaf_stream_start(struct stream_out *out)
610{
Harsh Bansal28718c52017-04-20 13:47:12 +0530611 int ret = -EINVAL;
612 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530613
Harsh Bansal28718c52017-04-20 13:47:12 +0530614 DEBUG_MSG("Output Stream = %p", out);
615
616 qaf_mod = get_qaf_module_for_input_stream(out);
617 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_start)) {
618 return -EINVAL;
619 }
620
621 if (out->qaf_stream_handle) {
622 ret = qaf_mod->qaf_audio_stream_start(out->qaf_stream_handle);
623 }
624
625 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530626}
627
628static int qaf_start_output_stream(struct stream_out *out)
629{
630 int ret = 0;
631 struct audio_device *adev = out->dev;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530632
633 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
634 ret = -EINVAL;
635 usleep(50000);
636 return ret;
637 }
638
639 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
640 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
641 out->devices);
642
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530643 if (CARD_STATUS_OFFLINE == out->card_status ||
644 CARD_STATUS_OFFLINE == adev->card_status) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530645 ALOGE("%s: sound card is not active/SSR returning error", __func__);
646 ret = -EIO;
647 usleep(50000);
648 return ret;
649 }
650
651 return qaf_stream_start(out);
652}
653
Harsh Bansal28718c52017-04-20 13:47:12 +0530654/* Sends input buffer to the QAF MM module. */
655static int qaf_module_write_input_buffer(struct stream_out *out, const void *buffer, int bytes)
656{
657 int ret = -EINVAL;
658 struct qaf_module *qaf_mod = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +0530659
660 qaf_mod = get_qaf_module_for_input_stream(out);
661 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_write)) {
662 return ret;
663 }
664
665 if (out->qaf_stream_handle) {
666 ret = qaf_mod->qaf_audio_stream_write(out->qaf_stream_handle, buffer, bytes);
667 }
668 return ret;
669}
670
671/* Writes buffer to QAF input stream. */
672static ssize_t qaf_out_write(struct audio_stream_out *stream, const void *buffer, size_t bytes)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530673{
674 struct stream_out *out = (struct stream_out *)stream;
675 struct audio_device *adev = out->dev;
676 ssize_t ret = 0;
677
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530678 DEBUG_MSG_VV("bytes = %d, usecase[%d] and flags[%x] for handle[%p]",
Harsh Bansal28718c52017-04-20 13:47:12 +0530679 (int)bytes, out->usecase, out->flags, out);
680
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530681 lock_output_stream(out);
682
Harsh Bansal28718c52017-04-20 13:47:12 +0530683 // If QAF passthrough is active then block writing data to QAF mm module.
684 if (p_qaf->passthrough_out) {
685 //If write is received for the QAF passthrough stream then send the buffer to primary HAL.
686 if (p_qaf->passthrough_in == out) {
687 ret = p_qaf->passthrough_out->stream.write(
688 (struct audio_stream_out *)(p_qaf->passthrough_out),
689 buffer,
690 bytes);
691 if (ret > 0) out->standby = false;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530692 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530693 unlock_output_stream(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530694 return ret;
Harsh Bansal28718c52017-04-20 13:47:12 +0530695 } else if (out->standby) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530696 pthread_mutex_lock(&adev->lock);
697 ret = qaf_start_output_stream(out);
698 pthread_mutex_unlock(&adev->lock);
Harsh Bansal28718c52017-04-20 13:47:12 +0530699 if (ret == 0) {
700 out->standby = false;
701 } else {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530702 goto exit;
703 }
704 }
705
Harsh Bansal28718c52017-04-20 13:47:12 +0530706 if ((adev->is_channel_status_set == false) && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530707 audio_utils_set_hdmi_channel_status(out, (char *)buffer, bytes);
708 adev->is_channel_status_set = true;
709 }
710
Harsh Bansal28718c52017-04-20 13:47:12 +0530711 ret = qaf_module_write_input_buffer(out, buffer, bytes);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530712 DEBUG_MSG_VV("ret [%d]", (int)ret);
Harsh Bansal28718c52017-04-20 13:47:12 +0530713
714 if (ret >= 0) {
715 bytes = ret;
716 out->written += bytes / ((popcount(out->channel_mask) * sizeof(short)));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530717 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530718
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530719
720exit:
Harsh Bansal28718c52017-04-20 13:47:12 +0530721 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530722
723 if (ret < 0) {
724 if (ret == -EAGAIN) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530725 DEBUG_MSG_VV("No space available in mm module, post msg to cb thread");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530726 ret = qaf_send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530727 bytes = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530728 } else if (ret == -ENOMEM || ret == -EPERM) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530729 if (out->pcm)
Harsh Bansal28718c52017-04-20 13:47:12 +0530730 ERROR_MSG("error %d, %s", (int)ret, pcm_get_error(out->pcm));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530731 qaf_out_standby(&out->stream.common);
Harsh Bansal28718c52017-04-20 13:47:12 +0530732 usleep(bytes * 1000000
733 / audio_stream_out_frame_size(stream)
734 / out->stream.common.get_sample_rate(&out->stream.common));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530735 }
736 }
737 return bytes;
738}
739
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530740/* Gets PCM offload buffer size for a given config. */
741static uint32_t qaf_get_pcm_offload_buffer_size(audio_offload_info_t* info,
742 uint32_t samples_per_frame)
Varun B34da7a42017-02-13 16:16:53 +0530743{
744 uint32_t fragment_size = 0;
Varun B34da7a42017-02-13 16:16:53 +0530745
Harsh Bansal28718c52017-04-20 13:47:12 +0530746 fragment_size = (samples_per_frame * (info->bit_width >> 3) * popcount(info->channel_mask));
747
748 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530749 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530750 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530751 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530752
Varun B34da7a42017-02-13 16:16:53 +0530753 // To have same PCM samples for all channels, the buffer size requires to
754 // be multiple of (number of channels * bytes per sample)
755 // For writes to succeed, the buffer must be written at address which is multiple of 32
Harsh Bansal28718c52017-04-20 13:47:12 +0530756 fragment_size = ALIGN(fragment_size,
757 ((info->bit_width >> 3) * popcount(info->channel_mask) * 32));
Varun B34da7a42017-02-13 16:16:53 +0530758
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530759 ALOGI("Qaf PCM offload Fragment size is %d bytes", fragment_size);
Varun B34da7a42017-02-13 16:16:53 +0530760
761 return fragment_size;
762}
763
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530764static uint32_t qaf_get_pcm_offload_input_buffer_size(info)
765{
766 return qaf_get_pcm_offload_buffer_size(info, MS12_PCM_IN_FRAGMENT_SIZE);
767}
768
769static uint32_t qaf_get_pcm_offload_output_buffer_size(struct qaf_module *qaf_mod,
770 audio_offload_info_t* info)
771{
772 return qaf_get_pcm_offload_buffer_size(info, get_pcm_output_buffer_size_samples(qaf_mod));
773}
774
Harsh Bansal28718c52017-04-20 13:47:12 +0530775/* Gets buffer latency in samples. */
776static int get_buffer_latency(struct stream_out *out, uint32_t buffer_size, uint32_t *latency)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530777{
Harsh Bansal28718c52017-04-20 13:47:12 +0530778 unsigned long int samples_in_one_encoded_frame;
779 unsigned long int size_of_one_encoded_frame;
780
781 switch (out->format) {
782 case AUDIO_FORMAT_AC3:
783 samples_in_one_encoded_frame = DD_FRAME_SIZE;
784 size_of_one_encoded_frame = DD_ENCODER_OUTPUT_SIZE;
785 break;
786 case AUDIO_FORMAT_E_AC3:
787 samples_in_one_encoded_frame = DDP_FRAME_SIZE;
788 size_of_one_encoded_frame = DDP_ENCODER_OUTPUT_SIZE;
789 break;
790 case AUDIO_FORMAT_DTS:
791 samples_in_one_encoded_frame = DTS_FRAME_SIZE;
792 size_of_one_encoded_frame = DTS_ENCODER_OUTPUT_SIZE;
793 break;
794 case AUDIO_FORMAT_DTS_HD:
795 samples_in_one_encoded_frame = DTSHD_FRAME_SIZE;
796 size_of_one_encoded_frame = DTSHD_ENCODER_OUTPUT_SIZE;
797 break;
798 case AUDIO_FORMAT_PCM_16_BIT:
799 samples_in_one_encoded_frame = 1;
800 size_of_one_encoded_frame = ((out->bit_width) >> 3) * popcount(out->channel_mask);
801 break;
802 default:
803 *latency = 0;
804 return (-EINVAL);
805 }
806
807 *latency = ((buffer_size * samples_in_one_encoded_frame) / size_of_one_encoded_frame);
808 return 0;
809}
810
811/* Returns the number of frames rendered to outside observer. */
812static int qaf_get_rendered_frames(struct stream_out *out, uint64_t *frames)
813{
814 int ret = 0, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530815 struct str_parms *parms;
816 int value = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530817 int module_latency = 0;
818 uint32_t kernel_latency = 0;
819 uint32_t dsp_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530820 int signed_frames = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530821 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +0530822 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530823
Harsh Bansal28718c52017-04-20 13:47:12 +0530824 DEBUG_MSG("Output Format %d", out->format);
825
826 qaf_mod = get_qaf_module_for_input_stream(out);
827 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_get_param)) {
828 return -EINVAL;
829 }
830
831 //Get MM module latency.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530832 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "get_latency");
833 if (kvpairs) {
834 parms = str_parms_create_str(kvpairs);
Harsh Bansal28718c52017-04-20 13:47:12 +0530835 ret = str_parms_get_int(parms, "get_latency", &module_latency);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530836 if (ret >= 0) {
837 str_parms_destroy(parms);
838 parms = NULL;
839 }
840 free(kvpairs);
841 kvpairs = NULL;
842 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530843
844 //Get kernel Latency
845 for (i = MAX_QAF_MODULE_OUT - 1; i >= 0; i--) {
846 if (qaf_mod->stream_out[i] == NULL) {
847 continue;
848 } else {
849 unsigned int num_fragments = qaf_mod->stream_out[i]->compr_config.fragments;
850 uint32_t fragment_size = qaf_mod->stream_out[i]->compr_config.fragment_size;
851 uint32_t kernel_buffer_size = num_fragments * fragment_size;
852 get_buffer_latency(qaf_mod->stream_out[i], kernel_buffer_size, &kernel_latency);
853 break;
854 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530855 }
856
Harsh Bansal28718c52017-04-20 13:47:12 +0530857 //Get DSP latency
858 if ((qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL)
859 || (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH] != NULL)) {
860 unsigned int sample_rate = 0;
861
862 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
863 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
864 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
865 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
866
867 audio_usecase_t platform_latency =
868 platform_render_latency(qaf_mod->stream_out[QAF_OUT_OFFLOAD]->usecase);
869 dsp_latency = (platform_latency * sample_rate) / 1000000LL;
870 } else if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] != NULL) {
871 unsigned int sample_rate = 0;
872
873 sample_rate = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->sample_rate; //TODO: How this sample rate can be used?
874 dsp_latency = (COMPRESS_OFFLOAD_PLAYBACK_LATENCY * sample_rate) / 1000;
875 }
876
877 // MM Module Latency + Kernel Latency + DSP Latency
878 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
879 out->platform_latency = module_latency + audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl);
880 } else {
881 out->platform_latency = (uint32_t)module_latency + kernel_latency + dsp_latency;
882 }
883
884 if (out->format & AUDIO_FORMAT_PCM_16_BIT) {
885 *frames = 0;
886 signed_frames = out->written - out->platform_latency;
887 // It would be unusual for this value to be negative, but check just in case ...
888 if (signed_frames >= 0) {
889 *frames = signed_frames;
890 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530891 } else if (qaf_mod->qaf_audio_stream_get_param) {
892 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "position");
893 if (kvpairs) {
894 parms = str_parms_create_str(kvpairs);
895 ret = str_parms_get_int(parms, "position", &value);
896 if (ret >= 0) {
897 *frames = value;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530898 signed_frames = value - out->platform_latency;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530899 // It would be unusual for this value to be negative, but check just in case ...
900 if (signed_frames >= 0) {
901 *frames = signed_frames;
902 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530903 }
904 str_parms_destroy(parms);
905 }
906 } else {
907 ret = -EINVAL;
908 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530909
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530910 return ret;
911}
912
913static int qaf_out_get_presentation_position(const struct audio_stream_out *stream,
Harsh Bansal28718c52017-04-20 13:47:12 +0530914 uint64_t *frames,
915 struct timespec *timestamp)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530916{
917 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530918 int ret = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530919
Harsh Bansal28718c52017-04-20 13:47:12 +0530920 DEBUG_MSG("Output Stream %p", stream);
921
922 //If QAF passthorugh output stream is active.
923 if (p_qaf->passthrough_out) {
924 if (p_qaf->passthrough_in == out) {
925 //If api is called for QAF passthorugh stream then call the primary HAL api to get the position.
926 pthread_mutex_lock(&p_qaf->lock);
927 ret = p_qaf->passthrough_out->stream.get_presentation_position(
928 (struct audio_stream_out *)p_qaf->passthrough_out,
929 frames,
930 timestamp);
931 pthread_mutex_unlock(&p_qaf->lock);
932 } else {
933 //If api is called for other stream then return zero frames.
934 *frames = 0;
935 clock_gettime(CLOCK_MONOTONIC, timestamp);
936 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530937 return ret;
938 }
939
Harsh Bansal28718c52017-04-20 13:47:12 +0530940 ret = qaf_get_rendered_frames(out, frames);
941 clock_gettime(CLOCK_MONOTONIC, timestamp);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530942
943 return ret;
944}
945
Harsh Bansal28718c52017-04-20 13:47:12 +0530946/* Pause the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530947static int qaf_stream_pause(struct stream_out *out)
948{
Harsh Bansal28718c52017-04-20 13:47:12 +0530949 struct qaf_module *qaf_mod = NULL;
950 int ret = -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530951
Harsh Bansal28718c52017-04-20 13:47:12 +0530952 qaf_mod = get_qaf_module_for_input_stream(out);
953 if (!qaf_mod || !qaf_mod->qaf_audio_stream_pause) {
954 return -EINVAL;
955 }
956
957 if (out->qaf_stream_handle)
958 ret = qaf_mod->qaf_audio_stream_pause(out->qaf_stream_handle);
959
960 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530961}
962
Harsh Bansal28718c52017-04-20 13:47:12 +0530963/* Pause a QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530964static int qaf_out_pause(struct audio_stream_out* stream)
965{
966 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530967 int status = 0;
968 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530969
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530970 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530971
972 //If QAF passthrough is enabled then block the pause on module stream.
973 if (p_qaf->passthrough_out) {
974 pthread_mutex_lock(&p_qaf->lock);
975 //If pause is received for QAF passthorugh stream then call the primary HAL api.
976 if (p_qaf->passthrough_in == out) {
977 status = p_qaf->passthrough_out->stream.pause(
978 (struct audio_stream_out *)p_qaf->passthrough_out);
979 out->offload_state = OFFLOAD_STATE_PAUSED;
980 }
981 pthread_mutex_unlock(&p_qaf->lock);
982 } else {
983 //Pause the module input stream.
984 status = qaf_stream_pause(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530985 }
986
Harsh Bansal28718c52017-04-20 13:47:12 +0530987 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530988 return status;
989}
990
Harsh Bansal28718c52017-04-20 13:47:12 +0530991/* Drains a qaf input stream. */
992static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530993{
994 struct stream_out *out = (struct stream_out *)stream;
995 int status = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530996 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530997
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530998 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530999
1000 //If QAF passthrough is enabled then block the drain on module stream.
1001 if (p_qaf->passthrough_out) {
1002 pthread_mutex_lock(&p_qaf->lock);
1003 //If drain is received for QAF passthorugh stream then call the primary HAL api.
1004 if (p_qaf->passthrough_in == out) {
1005 status = p_qaf->passthrough_out->stream.drain(
1006 (struct audio_stream_out *)p_qaf->passthrough_out, type);
1007 }
1008 pthread_mutex_unlock(&p_qaf->lock);
1009 } else {
1010 //Drain the module input stream.
1011 /* Stream stop will trigger EOS and on EOS_EVENT received
1012 from callback DRAIN_READY command is sent */
1013 status = audio_extn_qaf_stream_stop(out);
1014
1015 if (status == 0) {
1016 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
1017 int index = get_input_stream_index(out);
1018 if (qaf_mod && index >= 0) qaf_mod->drain_received[index] = true;
1019 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301020 }
1021
Harsh Bansal28718c52017-04-20 13:47:12 +05301022 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301023 return status;
1024}
1025
Harsh Bansal28718c52017-04-20 13:47:12 +05301026/* Flush the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301027static int audio_extn_qaf_stream_flush(struct stream_out *out)
1028{
Harsh Bansal28718c52017-04-20 13:47:12 +05301029 DEBUG_MSG("Output Stream %p", out);
1030 int ret = -EINVAL;
1031 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301032
Harsh Bansal28718c52017-04-20 13:47:12 +05301033 qaf_mod = get_qaf_module_for_input_stream(out);
1034 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_flush)) {
1035 return -EINVAL;
1036 }
1037
1038 if (out->qaf_stream_handle)
1039 ret = qaf_mod->qaf_audio_stream_flush(out->qaf_stream_handle);
1040
1041 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301042}
1043
Harsh Bansal28718c52017-04-20 13:47:12 +05301044/* Flush the QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301045static int qaf_out_flush(struct audio_stream_out* stream)
1046{
1047 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05301048 int status = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301049
Harsh Bansal28718c52017-04-20 13:47:12 +05301050 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301051 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301052
1053 //If QAF passthrough is active then block the flush on module input streams.
1054 if (p_qaf->passthrough_out) {
1055 pthread_mutex_lock(&p_qaf->lock);
1056 //If flush is received for the QAF passthrough stream then call the primary HAL api.
1057 if (p_qaf->passthrough_in == out) {
1058 status = p_qaf->passthrough_out->stream.flush(
1059 (struct audio_stream_out *)p_qaf->passthrough_out);
1060 out->offload_state = OFFLOAD_STATE_IDLE;
1061 }
1062 pthread_mutex_unlock(&p_qaf->lock);
1063 } else {
1064 //Flush the module input stream.
1065 status = audio_extn_qaf_stream_flush(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301066 }
1067
Harsh Bansal28718c52017-04-20 13:47:12 +05301068 unlock_output_stream(out);
1069 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301070 return status;
1071}
1072
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301073static uint32_t qaf_out_get_latency(const struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301074{
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301075 struct stream_out *out = (struct stream_out *)stream;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301076 uint32_t latency = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05301077 struct qaf_module *qaf_mod = NULL;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301078 DEBUG_MSG_VV("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301079
Harsh Bansal28718c52017-04-20 13:47:12 +05301080 qaf_mod = get_qaf_module_for_input_stream(out);
1081 if (!qaf_mod) {
1082 return 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301083 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301084
Harsh Bansal28718c52017-04-20 13:47:12 +05301085 //If QAF passthrough is active then block the get latency on module input streams.
1086 if (p_qaf->passthrough_out) {
1087 pthread_mutex_lock(&p_qaf->lock);
1088 //If get latency is called for the QAF passthrough stream then call the primary HAL api.
1089 if (p_qaf->passthrough_in == out) {
1090 latency = p_qaf->passthrough_out->stream.get_latency(
1091 (struct audio_stream_out *)p_qaf->passthrough_out);
1092 }
1093 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301094 } else {
Bharath Gopal01310bb2016-12-05 15:39:32 +05301095 if (is_offload_usecase(out->usecase)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301096 latency = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301097 } else {
Harsh Bansal28718c52017-04-20 13:47:12 +05301098 uint32_t sample_rate = 0;
1099 latency = QAF_MODULE_PCM_INPUT_BUFFER_LATENCY; //Input latency
1100
1101 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
1102 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
1103 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
1104 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
1105
1106 if (sample_rate) {
1107 latency += (get_pcm_output_buffer_size_samples(qaf_mod) * 1000) / out->sample_rate;
1108 }
1109 }
1110
1111 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
1112 if (is_offload_usecase(out->usecase)) {
1113 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1114 QAF_COMPRESS_OFFLOAD_PROCESSING_LATENCY;
1115 } else {
1116 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1117 QAF_PCM_OFFLOAD_PROCESSING_LATENCY;
1118 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05301119 }
1120 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301121
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301122 DEBUG_MSG_VV("Latency %d", latency);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301123 return latency;
1124}
1125
Harsh Bansal28718c52017-04-20 13:47:12 +05301126static bool check_and_get_compressed_device_format(int device, int *format)
1127{
1128 switch (device) {
1129 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DD):
1130 *format = AUDIO_FORMAT_AC3;
1131 return true;
1132 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DDP):
1133 *format = AUDIO_FORMAT_E_AC3;
1134 return true;
1135 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_FORMAT_DTS):
1136 *format = AUDIO_FORMAT_DTS;
1137 return true;
1138 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_FORMAT_DTS_HD):
1139 *format = AUDIO_FORMAT_DTS_HD;
1140 return true;
1141 default:
1142 return false;
1143 }
1144}
1145
1146/* Call back function for mm module. */
1147static void notify_event_callback(audio_session_handle_t session_handle /*__unused*/,
1148 void *prv_data,
1149 void *buf,
1150 audio_event_id_t event_id,
1151 int size,
1152 int device) //TODO: add media format as well.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301153{
1154
Harsh Bansal28718c52017-04-20 13:47:12 +05301155 /*
1156 For SPKR:
1157 1. Open pcm device if device_id passed to it SPKR and write the data to
1158 pcm device
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301159
Harsh Bansal28718c52017-04-20 13:47:12 +05301160 For HDMI
1161 1.Open compress device for HDMI(PCM or AC3) based on current hdmi o/p format and write
1162 data to the HDMI device.
1163 */
1164 int ret, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301165 audio_output_flags_t flags;
Harsh Bansal28718c52017-04-20 13:47:12 +05301166 struct qaf_module* qaf_mod = (struct qaf_module*)prv_data;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301167 struct audio_stream_out *bt_stream = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301168 int format;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301169
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301170 DEBUG_MSG_VV("Device 0x%X, Event = 0x%X, Bytes to write %d", device, event_id, size);
Harsh Bansal28718c52017-04-20 13:47:12 +05301171 pthread_mutex_lock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301172
1173 if (event_id == AUDIO_DATA_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301174 if (p_qaf->passthrough_out != NULL) {
1175 //If QAF passthrough is active then all the module output will be dropped.
1176 pthread_mutex_unlock(&p_qaf->lock);
1177 DEBUG_MSG("QAF-PSTH is active, DROPPING DATA!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301178 return;
Harsh Bansal28718c52017-04-20 13:47:12 +05301179 }
1180
1181 if (check_and_get_compressed_device_format(device, &format)) {
1182 /*
1183 * CASE 1: Transcoded output of mm module.
1184 * If HDMI is not connected then drop the data.
1185 * Only one HDMI output can be supported from all the mm modules of QAF.
1186 * Multi-Channel PCM HDMI output streams will be closed from all the mm modules.
1187 * If transcoded output of other module is already enabled then this data will be dropped.
1188 */
1189
1190 if (!p_qaf->hdmi_connect) {
1191 DEBUG_MSG("HDMI not connected, DROPPING DATA!");
1192 pthread_mutex_unlock(&p_qaf->lock);
1193 return;
1194 }
1195
1196 //Closing all the PCM HDMI output stream from QAF.
1197 close_all_pcm_hdmi_output();
1198
1199 if (!p_qaf->passthrough_enabled
1200 && !(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH])) {
1201
1202 struct audio_config config;
1203 audio_devices_t devices;
1204
1205 //TODO: need to update the actual sample rate.
1206 config.sample_rate = config.offload_info.sample_rate =
1207 QAF_OUTPUT_SAMPLING_RATE;
1208 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1209 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
1210 config.format = config.offload_info.format = format;
1211 //TODO: need to update the actual bit width.
1212 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
1213 //TODO: What is the relevance of num of channels.
1214 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1215
1216 flags = (AUDIO_OUTPUT_FLAG_NON_BLOCKING
1217 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD
1218 | AUDIO_OUTPUT_FLAG_DIRECT);
1219 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1220
1221 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1222 QAF_DEFAULT_COMPR_PASSTHROUGH_HANDLE,
1223 devices,
1224 flags,
1225 &config,
1226 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]),
1227 NULL);
1228 if (ret < 0) {
1229 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1230 pthread_mutex_unlock(&p_qaf->lock);
1231 return;
1232 }
1233
1234 if (format & AUDIO_COMPRESSED_OUT_DDP) {
1235 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragment_size =
1236 COMPRESS_PASSTHROUGH_DDP_FRAGMENT_SIZE;
1237 }
1238 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragments =
1239 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1240
1241 p_qaf->passthrough_enabled = true;
1242 }
1243
1244 if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
1245 ret = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->stream.write(
1246 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH],
1247 buf,
1248 size);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301249 }
1250 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301251 else if ((device & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1252 && (p_qaf->hdmi_connect)
1253 && (p_qaf->hdmi_sink_channels > 2)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301254
Harsh Bansal28718c52017-04-20 13:47:12 +05301255 /* CASE 2: Multi-Channel PCM output to HDMI.
1256 * If any other HDMI output is already enabled then this has to be dropped.
1257 */
1258 bool create_mch_out_stream = false;
1259
1260 if (p_qaf->passthrough_enabled) {
1261 //Closing all the multi-Channel PCM HDMI output stream from QAF.
1262 close_all_pcm_hdmi_output();
1263
1264 //If passthrough is active then pcm hdmi output has to be dropped.
1265 pthread_mutex_unlock(&p_qaf->lock);
1266 DEBUG_MSG("Compressed passthrough enabled, DROPPING DATA!");
1267 return;
1268 }
1269
1270 if (!p_qaf->mch_pcm_hdmi_enabled && !(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301271 struct audio_config config;
1272 audio_devices_t devices;
1273
Harsh Bansal28718c52017-04-20 13:47:12 +05301274 //TODO: need to update the actual sample rate.
Varun B34da7a42017-02-13 16:16:53 +05301275 config.sample_rate = config.offload_info.sample_rate =
Harsh Bansal28718c52017-04-20 13:47:12 +05301276 QAF_OUTPUT_SAMPLING_RATE;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301277 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1278 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
Harsh Bansal28718c52017-04-20 13:47:12 +05301279 config.offload_info.format = config.format = AUDIO_FORMAT_PCM_16_BIT;
1280 //TODO: need to update the actual bit width.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301281 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301282
Harsh Bansal28718c52017-04-20 13:47:12 +05301283 if (p_qaf->hdmi_sink_channels == 8) {
Varun B34da7a42017-02-13 16:16:53 +05301284 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301285 AUDIO_CHANNEL_OUT_7POINT1;
1286 } else if (p_qaf->hdmi_sink_channels == 6) {
Varun B34da7a42017-02-13 16:16:53 +05301287 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301288 AUDIO_CHANNEL_OUT_5POINT1;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301289 } else {
Varun B34da7a42017-02-13 16:16:53 +05301290 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301291 AUDIO_CHANNEL_OUT_STEREO;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301292 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301293
Harsh Bansal28718c52017-04-20 13:47:12 +05301294 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1295 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
1296
1297 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1298 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1299 devices,
1300 flags,
1301 &config,
1302 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]),
1303 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301304 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301305 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1306 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301307 return;
1308 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001309
Harsh Bansal28718c52017-04-20 13:47:12 +05301310 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragments =
1311 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1312 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragment_size =
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301313 qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info);
Harsh Bansal28718c52017-04-20 13:47:12 +05301314
1315 p_qaf->mch_pcm_hdmi_enabled = true;
1316
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301317 if ((qaf_mod->stream_in[QAF_IN_MAIN]
1318 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) ||
1319 (qaf_mod->stream_in[QAF_IN_MAIN_2]
1320 && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301321
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301322 if (qaf_mod->stream_in[QAF_IN_MAIN]) {
1323 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
Harsh Bansal28718c52017-04-20 13:47:12 +05301324 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1325 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1326 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301327 }
1328 if (qaf_mod->stream_in[QAF_IN_MAIN_2]) {
1329 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1330 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1331 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback,
1332 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie);
1333 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301334 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1335 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1336
1337 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1338 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1339 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1340 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1341 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001342
1343 int index = -1;
1344 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301345 index = (int) QAF_IN_MAIN;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301346 else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid)
1347 index = (int) QAF_IN_MAIN_2;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001348 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301349 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001350
Harsh Bansal28718c52017-04-20 13:47:12 +05301351 if (index >= 0) {
1352 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->standby)
1353 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1354 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], NULL, 0);
1355
1356 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001357 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301358 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1359 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1360 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1361 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001362
1363 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301364 }
1365
Harsh Bansal28718c52017-04-20 13:47:12 +05301366 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) {
1367 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1368 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1369 buf,
1370 size);
Varun B34da7a42017-02-13 16:16:53 +05301371 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301372 }
1373 else {
1374 /* CASE 3: PCM output.
1375 */
Bharath Gopal01310bb2016-12-05 15:39:32 +05301376 bt_stream = audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl);
1377 if (bt_stream != NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301378 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1379 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1380 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD]));
1381 qaf_mod->stream_out[QAF_OUT_OFFLOAD] = NULL;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301382 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301383
Harsh Bansal28718c52017-04-20 13:47:12 +05301384 audio_extn_bt_hal_out_write(p_qaf->bt_hdl, buf, size);
1385 } else if (NULL == qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301386 struct audio_config config;
1387 audio_devices_t devices;
1388
Harsh Bansal28718c52017-04-20 13:47:12 +05301389 //TODO: need to update the actual sample rate.
Varun B34da7a42017-02-13 16:16:53 +05301390 config.sample_rate = config.offload_info.sample_rate =
Harsh Bansal28718c52017-04-20 13:47:12 +05301391 QAF_OUTPUT_SAMPLING_RATE;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301392 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1393 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
Harsh Bansal28718c52017-04-20 13:47:12 +05301394 config.offload_info.format = config.format = AUDIO_FORMAT_PCM_16_BIT;
1395 //TODO: need to update the actual bit width.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301396 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Harsh Bansal28718c52017-04-20 13:47:12 +05301397 //TODO: need to update the actual num channels.
1398 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301399
Harsh Bansal28718c52017-04-20 13:47:12 +05301400 if (qaf_mod->stream_in[QAF_IN_MAIN])
1401 devices = qaf_mod->stream_in[QAF_IN_MAIN]->devices;
1402 else
1403 devices = qaf_mod->stream_in[QAF_IN_PCM]->devices;
1404
1405 //If multi channel pcm or passthrough is already enabled then remove the hdmi flag from device.
1406 if (p_qaf->mch_pcm_hdmi_enabled || p_qaf->passthrough_enabled) {
1407 if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1408 devices ^= AUDIO_DEVICE_OUT_AUX_DIGITAL;
1409 }
1410 if (devices == 0) {
1411 devices = device;
1412 }
1413
1414 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001415
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301416 /* TODO:: Need to Propagate errors to framework */
Harsh Bansal28718c52017-04-20 13:47:12 +05301417 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1418 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1419 devices,
1420 flags,
1421 &config,
1422 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD]),
1423 NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301424 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301425 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1426 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301427 return;
1428 }
Deepak Agarwal1e42b852017-02-11 17:57:04 +05301429
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301430 if ((qaf_mod->stream_in[QAF_IN_MAIN]
1431 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) ||
1432 (qaf_mod->stream_in[QAF_IN_MAIN_2]
1433 && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001434
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301435 if (qaf_mod->stream_in[QAF_IN_MAIN]) {
1436 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
Harsh Bansal28718c52017-04-20 13:47:12 +05301437 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1438 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1439 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301440 }
1441 if (qaf_mod->stream_in[QAF_IN_MAIN_2]) {
1442 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1443 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1444 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback,
1445 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie);
1446 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301447 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1448 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1449
1450 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1451 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1452 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1453 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1454 }
1455
1456 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragments =
1457 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1458 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragment_size =
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301459 qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info);
Harsh Bansal28718c52017-04-20 13:47:12 +05301460
1461 if (qaf_mod->is_vol_set) {
1462 DEBUG_MSG("Setting Volume Left[%f], Right[%f]", qaf_mod->vol_left, qaf_mod->vol_right);
1463 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
1464 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1465 qaf_mod->vol_left,
1466 qaf_mod->vol_right);
1467 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001468
1469 int index = -1;
1470 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301471 index = (int) QAF_IN_MAIN;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301472 else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid)
1473 index = (int) QAF_IN_MAIN_2;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001474 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301475 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001476 if (index >= 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301477 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]->standby) {
1478 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1479 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], NULL, 0);
1480 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001481
Harsh Bansal28718c52017-04-20 13:47:12 +05301482 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001483 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301484 qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1485 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1486 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1487 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301488 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301489 }
1490
1491 /*
1492 * TODO:: Since this is mixed data,
1493 * need to identify to which stream the error should be sent
1494 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301495 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1496 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1497 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1498 buf,
1499 size);
Varun B34da7a42017-02-13 16:16:53 +05301500 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301501 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301502 DEBUG_MSG_VV("Bytes written = %d", ret);
Harsh Bansal28718c52017-04-20 13:47:12 +05301503 }
1504 else if (event_id == AUDIO_EOS_MAIN_DD_DDP_EVENT
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301505 || event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT
Varun B34da7a42017-02-13 16:16:53 +05301506 || event_id == AUDIO_EOS_MAIN_AAC_EVENT
1507 || event_id == AUDIO_EOS_MAIN_AC4_EVENT
1508 || event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301509 struct stream_out *out = qaf_mod->stream_in[QAF_IN_MAIN];
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301510 struct stream_out *out_main2 = qaf_mod->stream_in[QAF_IN_MAIN_2];
Harsh Bansal28718c52017-04-20 13:47:12 +05301511 struct stream_out *out_assoc = qaf_mod->stream_in[QAF_IN_ASSOC];
1512 bool *main_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301513 bool *main2_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN_2];
Harsh Bansal28718c52017-04-20 13:47:12 +05301514 bool *assoc_drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
Varun B34da7a42017-02-13 16:16:53 +05301515
1516 /**
1517 * TODO:: Only DD/DDP Associate Eos is handled, need to add support
1518 * for other formats.
1519 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301520 if (event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT
1521 && (out_assoc != NULL)
1522 && (*assoc_drain_received)) {
1523
Varun B34da7a42017-02-13 16:16:53 +05301524 lock_output_stream(out_assoc);
Harsh Bansal28718c52017-04-20 13:47:12 +05301525 out_assoc->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_assoc->client_cookie);
1526 *assoc_drain_received = false;
1527 unlock_output_stream(out_assoc);
1528 DEBUG_MSG("sent associated DRAIN_READY");
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301529 } else if (event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT
1530 && (out_main2 != NULL)
1531 && (*main2_drain_received)) {
1532
1533 lock_output_stream(out_main2);
1534 out_main2->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_main2->client_cookie);
1535 *main2_drain_received = false;
1536 unlock_output_stream(out_main2);
1537 DEBUG_MSG("sent main2 DRAIN_READY");
Harsh Bansal28718c52017-04-20 13:47:12 +05301538 } else if ((out != NULL) && (*main_drain_received)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301539 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001540 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
Harsh Bansal28718c52017-04-20 13:47:12 +05301541 *main_drain_received = false;
1542 unlock_output_stream(out);
1543 DEBUG_MSG("sent main DRAIN_READY");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301544 }
1545 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301546#if 0
1547 else if (event_id == AUDIO_MAIN_EOS_EVENT || event_id == AUDIO_ASSOC_EOS_EVENT) { //TODO: For DTS
1548 struct stream_out *out = NULL;
1549 bool *drain_received = NULL;
1550
1551 if (event_id == AUDIO_MAIN_EOS_EVENT) {
1552 out = qaf_mod->stream_in[QAF_IN_MAIN];
1553 drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
1554 } else {
1555 out = qaf_mod->stream_in[QAF_IN_ASSOC];
1556 drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
1557 }
1558
1559 if ((out != NULL) && (*drain_received)) {
1560 lock_output_stream(out);
1561 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
1562 *drain_received = false;
1563 unlock_output_stream(out);
1564 DEBUG_MSG("sent DRAIN_READY");
1565 }
1566 }
1567#endif
1568
1569 pthread_mutex_unlock(&p_qaf->lock);
1570 return;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301571}
1572
Harsh Bansal28718c52017-04-20 13:47:12 +05301573/* Close the mm module session. */
1574static int qaf_session_close(struct qaf_module* qaf_mod)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301575{
Harsh Bansal28718c52017-04-20 13:47:12 +05301576 int j;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301577
Harsh Bansal28718c52017-04-20 13:47:12 +05301578 //Check if all streams are closed or not.
1579 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
1580 if (qaf_mod->stream_in[j] != NULL) {
1581 break;
1582 }
1583 }
1584 if (j != MAX_QAF_MODULE_IN) {
1585 return 0; //Some stream is already active, Can not close session.
1586 }
1587
1588 if (qaf_mod->session_handle != NULL && qaf_mod->qaf_audio_session_close) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301589 qaf_mod->qaf_audio_session_close(qaf_mod->session_handle);
1590 qaf_mod->session_handle = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301591 qaf_mod->is_vol_set = false;
1592 memset(qaf_mod->drain_received, 0, sizeof(qaf_mod->drain_received));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301593 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301594
1595 for (j = 0; j < MAX_QAF_MODULE_OUT; j++) {
1596 if (qaf_mod->stream_out[j]) {
1597 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1598 (struct audio_stream_out *)(qaf_mod->stream_out[j]));
1599 qaf_mod->stream_out[j] = NULL;
1600 }
1601 }
1602
1603 DEBUG_MSG("Session Closed.");
1604
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301605 return 0;
1606}
1607
Harsh Bansal28718c52017-04-20 13:47:12 +05301608/* Close the stream of QAF module. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301609static int qaf_stream_close(struct stream_out *out)
1610{
Harsh Bansal28718c52017-04-20 13:47:12 +05301611 int ret = -EINVAL;
1612 struct qaf_module *qaf_mod = NULL;
1613 int index = -1;
1614 DEBUG_MSG("Flag [0x%x], Stream handle [%p]", out->flags, out->qaf_stream_handle);
1615
1616 qaf_mod = get_qaf_module_for_input_stream(out);
1617 index = get_input_stream_index(out);
1618
1619 if (!qaf_mod || !qaf_mod->qaf_audio_stream_close || index < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301620 return -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301621 }
1622
1623 qaf_mod->stream_in[index] = NULL;
1624 memset(&qaf_mod->adsp_hdlr_config[index], 0, sizeof(struct qaf_adsp_hdlr_config_state));
1625
1626 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301627 if (out->qaf_stream_handle) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301628 ret = qaf_mod->qaf_audio_stream_close(out->qaf_stream_handle);
1629 out->qaf_stream_handle = NULL;
1630 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301631 unlock_output_stream(out);
1632
1633 //If all streams are closed then close the session.
1634 qaf_session_close(qaf_mod);
1635
1636 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301637 return ret;
1638}
1639
Harsh Bansal28718c52017-04-20 13:47:12 +05301640/* Open a MM module session with QAF. */
1641static int audio_extn_qaf_session_open(mm_module_type mod_type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301642{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301643 ALOGV("%s %d", __func__, __LINE__);
Harsh Bansal28718c52017-04-20 13:47:12 +05301644 unsigned char* license_data = NULL;
1645 device_license_config_t lic_config = {0};
1646 int ret = -ENOSYS, size = 0;
1647 char value[PROPERTY_VALUE_MAX] = {0};
1648 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301649
Harsh Bansal28718c52017-04-20 13:47:12 +05301650 if (mod_type >= MAX_MM_MODULE_TYPE || !(p_qaf->qaf_mod[mod_type].qaf_audio_session_open))
1651 return -ENOTSUP; //Not supported by QAF module.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301652
Harsh Bansal28718c52017-04-20 13:47:12 +05301653 pthread_mutex_lock(&p_qaf->lock);
1654
1655 qaf_mod = &(p_qaf->qaf_mod[mod_type]);
1656
1657 //If session is already opened then return.
1658 if (qaf_mod->session_handle) {
1659 DEBUG_MSG("Session is already opened.");
1660 pthread_mutex_unlock(&p_qaf->lock);
1661 return 0;
1662 }
1663
1664 if (mod_type == MS12) {
1665 //Getting the license
1666 license_data = platform_get_license((struct audio_hw_device *)(p_qaf->adev->platform),
1667 &size);
1668 if (!license_data) {
1669 ERROR_MSG("License data is not present.");
1670 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301671 return -EINVAL;
1672 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301673
1674 lic_config.p_license = (unsigned char*)calloc(1, size);
1675 if (lic_config.p_license == NULL) {
1676 ERROR_MSG("Out of Memory");
1677 ret = -ENOMEM;
1678 goto exit;
1679 }
1680
1681 lic_config.l_size = size;
1682 memcpy(lic_config.p_license, license_data, size);
1683
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001684 if (property_get("vendor.audio.qaf.manufacturer", value, "") && atoi(value)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301685 lic_config.manufacturer_id = (unsigned long)atoi(value);
1686 } else {
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001687 ERROR_MSG("vendor.audio.qaf.manufacturer id is not set");
Harsh Bansal28718c52017-04-20 13:47:12 +05301688 ret = -EINVAL;
1689 goto exit;
1690 }
1691 }
1692
1693 ret = qaf_mod->qaf_audio_session_open(&qaf_mod->session_handle,
1694 (void *)(qaf_mod),
1695 (void *)&lic_config);
1696 if (ret < 0) {
1697 ERROR_MSG("Error in session open %d", ret);
1698 goto exit;
1699 }
1700
1701 if (qaf_mod->session_handle == NULL) {
1702 ERROR_MSG("Session handle is NULL.");
1703 ret = -ENOMEM;
1704 goto exit;
1705 }
1706
1707 if (qaf_mod->qaf_register_event_callback)
1708 qaf_mod->qaf_register_event_callback(qaf_mod->session_handle,
1709 qaf_mod,
1710 &notify_event_callback,
1711 AUDIO_DATA_EVENT);
1712
1713 set_hdmi_configuration_to_module();
1714
1715exit:
1716 if (license_data != NULL) {
1717 free(license_data);
1718 license_data = NULL;
1719 }
1720 if (lic_config.p_license != NULL) {
1721 free(lic_config.p_license);
1722 lic_config.p_license = NULL;
1723 }
1724
1725 pthread_mutex_unlock(&p_qaf->lock);
1726 return ret;
1727}
1728
1729/* opens a stream in QAF module. */
1730static int qaf_stream_open(struct stream_out *out,
1731 struct audio_config *config,
1732 audio_output_flags_t flags,
1733 audio_devices_t devices)
1734{
1735 int status = -EINVAL;
1736 mm_module_type mmtype = get_mm_module_for_format(config->format);
1737 struct qaf_module* qaf_mod = NULL;
1738 DEBUG_MSG("Flags 0x%x, Device 0x%x", flags, devices);
1739
1740 if (mmtype >= MAX_MM_MODULE_TYPE
1741 || p_qaf->qaf_mod[mmtype].qaf_audio_session_open == NULL
1742 || p_qaf->qaf_mod[mmtype].qaf_audio_stream_open == NULL) {
1743 ERROR_MSG("Unsupported Stream");
1744 return -ENOTSUP;
1745 }
1746
1747 //Open the module session, if not opened already.
1748 status = audio_extn_qaf_session_open(mmtype);
1749 qaf_mod = &(p_qaf->qaf_mod[mmtype]);
1750
1751 if ((status != 0) || (qaf_mod->session_handle == NULL)) {
1752 ERROR_MSG("Failed to open session.");
1753 return status;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301754 }
1755
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301756 audio_stream_config_t input_config;
1757 input_config.sample_rate = config->sample_rate;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301758 input_config.channels = popcount(config->channel_mask);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301759 input_config.format = config->format;
1760
Harsh Bansal28718c52017-04-20 13:47:12 +05301761 if (input_config.format != AUDIO_FORMAT_PCM_16_BIT) {
1762 input_config.format &= AUDIO_FORMAT_MAIN_MASK;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301763 }
1764
Harsh Bansal28718c52017-04-20 13:47:12 +05301765 DEBUG_MSG("stream_open sample_rate(%d) channels(%d) devices(%#x) flags(%#x) format(%#x)",
1766 input_config.sample_rate, input_config.channels, devices, flags, input_config.format);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301767
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301768 if (input_config.format == AUDIO_FORMAT_PCM_16_BIT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301769 //If PCM stream is already opened then fail this stream open.
1770 if (qaf_mod->stream_in[QAF_IN_PCM]) {
1771 ERROR_MSG("PCM input is already active.");
1772 return -ENOTSUP;
1773 }
1774
1775 //TODO: Flag can be system tone or external associated PCM.
1776 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle,
1777 &out->qaf_stream_handle,
1778 input_config,
1779 devices,
1780 AUDIO_STREAM_SYSTEM_TONE);
1781 qaf_mod->stream_in[QAF_IN_PCM] = out;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301782 } else if ((flags & AUDIO_OUTPUT_FLAG_MAIN) && (flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
1783 if (is_main_active(qaf_mod) || is_dual_main_active(qaf_mod)) {
1784 ERROR_MSG("Dual Main or Main already active. So, Cannot open main and associated stream");
1785 return -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301786 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301787 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN);
1788 if (status == 0) {
1789 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);
1790 qaf_mod->stream_in[QAF_IN_MAIN] = out;
1791 } else {
1792 ERROR_MSG("Stream Open FAILED !!!");
1793 }
1794 }
Naresh Tanniru908d9a02017-05-17 14:12:48 +05301795 } 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 +05301796 /* Assume Main if no flag is set */
1797 if (is_dual_main_active(qaf_mod)) {
1798 ERROR_MSG("Dual Main already active. So, Cannot open main stream");
1799 return -EINVAL;
1800 } else if (is_main_active(qaf_mod) && qaf_mod->stream_in[QAF_IN_ASSOC]) {
1801 ERROR_MSG("Main and Associated already active. So, Cannot open main stream");
1802 return -EINVAL;
1803 } else if (is_main_active(qaf_mod) && (mmtype != MS12)) {
1804 ERROR_MSG("Main already active and Not an MS12 format. So, Cannot open another main stream");
1805 return -EINVAL;
1806 } else {
1807 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN);
1808 if (status == 0) {
1809 DEBUG_MSG("Open stream for Input with only Main flag(%x) stream_handle(%p)", flags, out->qaf_stream_handle);
1810 if(qaf_mod->stream_in[QAF_IN_MAIN]) {
1811 qaf_mod->stream_in[QAF_IN_MAIN_2] = out;
1812 } else {
1813 qaf_mod->stream_in[QAF_IN_MAIN] = out;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301814 }
1815 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301816 ERROR_MSG("Stream Open FAILED !!!");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301817 }
1818 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301819 } else if ((flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
1820 if (is_dual_main_active(qaf_mod)) {
1821 ERROR_MSG("Dual Main already active. So, Cannot open associated stream");
1822 return -EINVAL;
1823 } else if (!is_main_active(qaf_mod)) {
1824 ERROR_MSG("Main not active. So, Cannot open associated stream");
1825 return -EINVAL;
1826 } else if (qaf_mod->stream_in[QAF_IN_ASSOC]) {
1827 ERROR_MSG("Associated already active. So, Cannot open associated stream");
1828 return -EINVAL;
1829 }
1830 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_ASSOCIATED);
1831 if (status == 0) {
1832 DEBUG_MSG("Open stream for Input with only Associated flag(%x) stream handle(%p)", flags, out->qaf_stream_handle);
1833 qaf_mod->stream_in[QAF_IN_ASSOC] = out;
1834 } else {
1835 ERROR_MSG("Stream Open FAILED !!!");
1836 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301837 }
1838
Harsh Bansal28718c52017-04-20 13:47:12 +05301839 if (status != 0) {
1840 //If no stream is active then close the session.
1841 qaf_session_close(qaf_mod);
1842 return status;
1843 }
1844
1845 //If Device is HDMI, QAF passthrough is enabled and there is no previous QAF passthrough input stream.
1846 if ((!p_qaf->passthrough_in)
1847 && (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1848 && audio_extn_qaf_passthrough_enabled(out)) {
1849 //Assign the QAF passthrough input stream.
1850 p_qaf->passthrough_in = out;
1851
1852 //If HDMI is connected and format is supported by HDMI then create QAF passthrough output stream.
1853 if (p_qaf->hdmi_connect
1854 && platform_is_edid_supported_format(p_qaf->adev->platform, out->format)) {
1855 status = create_qaf_passthrough_stream();
1856 if (status < 0) {
1857 qaf_stream_close(out);
1858 ERROR_MSG("QAF passthrough stream creation failed with error %d", status);
1859 return status;
1860 }
1861 }
1862 /*Else: since QAF passthrough input stream is already initialized,
1863 * when hdmi is connected
1864 * then qaf passthrough output stream will be created.
1865 */
1866 }
1867
1868 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301869 return status;
1870}
1871
Harsh Bansal28718c52017-04-20 13:47:12 +05301872/* Resume a QAF stream. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301873static int qaf_out_resume(struct audio_stream_out* stream)
1874{
1875 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05301876 int status = 0;
1877 DEBUG_MSG("Output Stream %p", out);
1878
1879
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301880 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301881
1882 //If QAF passthrough is active then block the resume on module input streams.
1883 if (p_qaf->passthrough_out) {
1884 //If resume is received for the QAF passthrough stream then call the primary HAL api.
1885 pthread_mutex_lock(&p_qaf->lock);
1886 if (p_qaf->passthrough_in == out) {
1887 status = p_qaf->passthrough_out->stream.resume(
1888 (struct audio_stream_out*)p_qaf->passthrough_out);
1889 if (!status) out->offload_state = OFFLOAD_STATE_PLAYING;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301890 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301891 pthread_mutex_unlock(&p_qaf->lock);
1892 } else {
1893 //Flush the module input stream.
1894 status = qaf_stream_start(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301895 }
1896
Harsh Bansal28718c52017-04-20 13:47:12 +05301897 unlock_output_stream(out);
1898
1899 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301900 return status;
1901}
1902
Harsh Bansal28718c52017-04-20 13:47:12 +05301903/* Offload thread for QAF output streams. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301904static void *qaf_offload_thread_loop(void *context)
1905{
Harsh Bansal28718c52017-04-20 13:47:12 +05301906 struct stream_out *out = (struct stream_out *)context;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301907 struct listnode *item;
1908 int ret = 0;
1909 struct str_parms *parms = NULL;
1910 int value = 0;
1911 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301912 struct qaf_module *qaf_mod = NULL;
1913
1914 qaf_mod = get_qaf_module_for_input_stream(out);
1915
1916 if (!qaf_mod) return NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301917
1918 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1919 set_sched_policy(0, SP_FOREGROUND);
1920 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1921
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301922 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301923
1924 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301925 for (;;) {
1926 struct offload_cmd *cmd = NULL;
1927 stream_callback_event_t event;
1928 bool send_callback = false;
1929
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301930 DEBUG_MSG_VV("List Empty %d (1:TRUE, 0:FALSE)", list_empty(&out->qaf_offload_cmd_list));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301931 if (list_empty(&out->qaf_offload_cmd_list)) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301932 DEBUG_MSG_VV("SLEEPING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301933 pthread_cond_wait(&out->qaf_offload_cond, &out->lock);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301934 DEBUG_MSG_VV("RUNNING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301935 continue;
1936 }
1937
1938 item = list_head(&out->qaf_offload_cmd_list);
1939 cmd = node_to_item(item, struct offload_cmd, node);
1940 list_remove(item);
1941
1942 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1943 free(cmd);
1944 break;
1945 }
1946
Harsh Bansal28718c52017-04-20 13:47:12 +05301947 unlock_output_stream(out);
1948
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301949 send_callback = false;
Harsh Bansal28718c52017-04-20 13:47:12 +05301950 switch (cmd->cmd) {
1951 case OFFLOAD_CMD_WAIT_FOR_BUFFER: {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301952 DEBUG_MSG_VV("wait for buffer availability");
Harsh Bansal28718c52017-04-20 13:47:12 +05301953
1954 while (1) {
1955 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle,
1956 "buf_available");
1957 if (kvpairs) {
1958 parms = str_parms_create_str(kvpairs);
1959 ret = str_parms_get_int(parms, "buf_available", &value);
1960 if (ret >= 0) {
1961 if (value >= (int)out->compr_config.fragment_size) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301962 DEBUG_MSG_VV("buffer available");
Harsh Bansal28718c52017-04-20 13:47:12 +05301963 str_parms_destroy(parms);
1964 parms = NULL;
1965 break;
1966 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301967 DEBUG_MSG_VV("sleep");
Harsh Bansal28718c52017-04-20 13:47:12 +05301968 str_parms_destroy(parms);
1969 parms = NULL;
1970 usleep(10000);
1971 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301972 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301973 free(kvpairs);
1974 kvpairs = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301975 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301976 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301977 send_callback = true;
1978 event = STREAM_CBK_EVENT_WRITE_READY;
1979 break;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301980 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301981 default:
1982 DEBUG_MSG("unknown command received: %d", cmd->cmd);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301983 break;
1984 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301985
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301986 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301987
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001988 if (send_callback && out->client_callback) {
1989 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301990 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301991
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301992 free(cmd);
1993 }
1994
1995 while (!list_empty(&out->qaf_offload_cmd_list)) {
1996 item = list_head(&out->qaf_offload_cmd_list);
1997 list_remove(item);
Harsh Bansal28718c52017-04-20 13:47:12 +05301998 free (node_to_item( item, struct offload_cmd, node));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301999 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302000 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302001
2002 return NULL;
2003}
2004
Harsh Bansal28718c52017-04-20 13:47:12 +05302005/* Create the offload callback thread for QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302006static int qaf_create_offload_callback_thread(struct stream_out *out)
2007{
Harsh Bansal28718c52017-04-20 13:47:12 +05302008 DEBUG_MSG("Output Stream %p", out);
2009 lock_output_stream(out);
2010 pthread_cond_init(&out->qaf_offload_cond, (const pthread_condattr_t *)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302011 list_init(&out->qaf_offload_cmd_list);
Harsh Bansal28718c52017-04-20 13:47:12 +05302012 pthread_create(&out->qaf_offload_thread,
2013 (const pthread_attr_t *)NULL,
2014 qaf_offload_thread_loop,
2015 out);
2016 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302017 return 0;
2018}
2019
Harsh Bansal28718c52017-04-20 13:47:12 +05302020/* Destroy the offload callback thread of QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302021static int qaf_destroy_offload_callback_thread(struct stream_out *out)
2022{
Harsh Bansal28718c52017-04-20 13:47:12 +05302023 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302024 qaf_send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302025
Harsh Bansal28718c52017-04-20 13:47:12 +05302026 pthread_join(out->qaf_offload_thread, (void **)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302027 pthread_cond_destroy(&out->qaf_offload_cond);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302028 return 0;
2029}
2030
Harsh Bansal28718c52017-04-20 13:47:12 +05302031/* Sets the stream set parameters (device routing information). */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302032static int qaf_out_set_parameters(struct audio_stream *stream, const char *kvpairs)
2033{
2034 struct str_parms *parms, *new_parms;
2035 char value[32];
2036 char *new_kv_pairs;
2037 int val = 0;
2038 struct stream_out *out = (struct stream_out *)stream;
2039 int ret = 0;
2040 int err = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05302041 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302042
Harsh Bansal28718c52017-04-20 13:47:12 +05302043 DEBUG_MSG("usecase(%d: %s) kvpairs: %s", out->usecase, use_case_table[out->usecase], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302044
Harsh Bansal28718c52017-04-20 13:47:12 +05302045 parms = str_parms_create_str(kvpairs);
2046 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2047 if (err < 0)
2048 return err;
2049 val = atoi(value);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302050
Harsh Bansal28718c52017-04-20 13:47:12 +05302051 qaf_mod = get_qaf_module_for_input_stream(out);
2052 if (!qaf_mod) return (-EINVAL);
2053
2054 //TODO: HDMI is connected but user doesn't want HDMI output, close both HDMI outputs.
2055
2056 /* Setting new device information to the mm module input streams.
2057 * This is needed if QAF module output streams are not created yet.
2058 */
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302059 out->devices = val;
Harsh Bansal28718c52017-04-20 13:47:12 +05302060
2061 if (val == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
2062 //If device is BT then open the BT stream if not already opened.
2063 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) == NULL
2064 && audio_extn_bt_hal_get_device(qaf_mod->bt_hdl) != NULL) {
2065 ret = audio_extn_bt_hal_open_output_stream(qaf_mod->bt_hdl,
2066 QAF_OUTPUT_SAMPLING_RATE,
2067 AUDIO_CHANNEL_OUT_STEREO,
2068 CODEC_BACKEND_DEFAULT_BIT_WIDTH);
2069 if (ret != 0) {
2070 ERROR_MSG("BT Output stream open failure!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302071 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302072 }
2073 } else if (val != 0) {
2074 //If device is not BT then close the BT stream if already opened.
2075 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
2076 audio_extn_bt_hal_close_output_stream(qaf_mod->bt_hdl);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302077 }
2078 }
2079
Harsh Bansal28718c52017-04-20 13:47:12 +05302080 if (p_qaf->passthrough_in == out) { //Device routing is received for QAF passthrough stream.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302081
Harsh Bansal28718c52017-04-20 13:47:12 +05302082 if (!(val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) { //HDMI route is disabled.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302083
Harsh Bansal28718c52017-04-20 13:47:12 +05302084 //If QAF pasthrough output is enabled. Close it.
2085 close_qaf_passthrough_stream();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302086
Harsh Bansal28718c52017-04-20 13:47:12 +05302087 //Send the routing information to mm module pcm output.
2088 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2089 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2090 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302091 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302092 //else: device info is updated in the input streams.
2093 } else { //HDMI route is enabled.
2094
2095 //create the QAf passthrough stream, if not created already.
2096 ret = create_qaf_passthrough_stream();
2097
2098 if (p_qaf->passthrough_out != NULL) { //If QAF passthrough out is enabled then send routing information.
2099 ret = p_qaf->passthrough_out->stream.common.set_parameters(
2100 (struct audio_stream *)p_qaf->passthrough_out, kvpairs);
2101 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302102 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302103 } else {
2104 //Send the routing information to mm module pcm output.
2105 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2106 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2107 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
2108 }
2109 //else: device info is updated in the input streams.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302110 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302111 str_parms_destroy(parms);
2112
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002113 return ret;
2114}
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302115
Harsh Bansal28718c52017-04-20 13:47:12 +05302116/* Checks if a stream is QAF stream or not. */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002117bool audio_extn_is_qaf_stream(struct stream_out *out)
2118{
Harsh Bansal28718c52017-04-20 13:47:12 +05302119 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002120
Harsh Bansal28718c52017-04-20 13:47:12 +05302121 if (qaf_mod) {
2122 return true;
2123 }
2124 return false;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002125}
2126
2127/* API to send playback stream specific config parameters */
2128int audio_extn_qaf_out_set_param_data(struct stream_out *out,
Harsh Bansal28718c52017-04-20 13:47:12 +05302129 audio_extn_param_id param_id,
2130 audio_extn_param_payload *payload)
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002131{
2132 int ret = -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05302133 int index;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002134 struct stream_out *new_out = NULL;
2135 struct audio_adsp_event *adsp_event;
Harsh Bansal28718c52017-04-20 13:47:12 +05302136 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002137
Harsh Bansal28718c52017-04-20 13:47:12 +05302138 if (!out || !qaf_mod || !payload) {
2139 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002140 return ret;
2141 }
2142
2143 /* In qaf output render session may not be opened at this time.
Harsh Bansal28718c52017-04-20 13:47:12 +05302144 to handle it store adsp_hdlr param info so that it can be
2145 applied later after opening render session from ms12 callback
2146 */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002147 if (param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302148 index = get_input_stream_index(out);
2149 if (index < 0) {
2150 ERROR_MSG("Invalid stream");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002151 return ret;
2152 }
2153 adsp_event = (struct audio_adsp_event *)payload;
2154
Harsh Bansal28718c52017-04-20 13:47:12 +05302155 if (payload->adsp_event_params.payload_length <= AUDIO_MAX_ADSP_STREAM_CMD_PAYLOAD_LEN) {
2156 pthread_mutex_lock(&p_qaf->lock);
2157 memcpy(qaf_mod->adsp_hdlr_config[index].event_payload,
2158 adsp_event->payload,
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002159 adsp_event->payload_length);
Harsh Bansal28718c52017-04-20 13:47:12 +05302160 qaf_mod->adsp_hdlr_config[index].event_params.payload =
2161 qaf_mod->adsp_hdlr_config[index].event_payload;
2162 qaf_mod->adsp_hdlr_config[index].event_params.payload_length =
2163 adsp_event->payload_length;
2164 qaf_mod->adsp_hdlr_config[index].adsp_hdlr_config_valid = true;
2165 pthread_mutex_unlock(&p_qaf->lock);
2166 } else {
2167 ERROR_MSG("Invalid adsp event length %d", adsp_event->payload_length);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002168 return ret;
2169 }
2170 ret = 0;
2171 }
2172
Harsh Bansal28718c52017-04-20 13:47:12 +05302173 /* apply param for all active out sessions */
2174 for (index = 0; index < MAX_QAF_MODULE_OUT; index++) {
2175 new_out = qaf_mod->stream_out[index];
2176 if (!new_out) continue;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002177
Harsh Bansal28718c52017-04-20 13:47:12 +05302178 /*ADSP event is not supported for passthrough*/
2179 if ((param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD)
2180 && !(new_out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) continue;
2181 if (new_out->standby)
2182 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
2183 lock_output_stream(new_out);
2184 ret = audio_extn_out_set_param_data(new_out, param_id, payload);
2185 if (ret)
2186 ERROR_MSG("audio_extn_out_set_param_data error %d", ret);
2187 unlock_output_stream(new_out);
2188 }
2189 return ret;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002190}
2191
2192int audio_extn_qaf_out_get_param_data(struct stream_out *out,
2193 audio_extn_param_id param_id,
2194 audio_extn_param_payload *payload)
2195{
Harsh Bansal28718c52017-04-20 13:47:12 +05302196 int ret = -EINVAL, i;
2197 struct stream_out *new_out;
2198 struct audio_usecase *uc_info;
2199 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002200
Harsh Bansal28718c52017-04-20 13:47:12 +05302201 if (!out || !qaf_mod || !payload) {
2202 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002203 return ret;
2204 }
2205
Harsh Bansal28718c52017-04-20 13:47:12 +05302206 if (!p_qaf->hdmi_connect) {
2207 ERROR_MSG("hdmi not connected");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002208 return ret;
2209 }
2210
2211 /* get session which is routed to hdmi*/
Harsh Bansal28718c52017-04-20 13:47:12 +05302212 if (p_qaf->passthrough_out)
2213 new_out = p_qaf->passthrough_out;
2214 else {
2215 for (i = 0; i < MAX_QAF_MODULE_OUT; i++) {
2216 if (qaf_mod->stream_out[i]) {
2217 new_out = qaf_mod->stream_out[i];
2218 break;
2219 }
2220 }
2221 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002222
2223 if (!new_out) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302224 ERROR_MSG("No stream active.");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002225 return ret;
2226 }
2227
2228 if (new_out->standby)
Harsh Bansal28718c52017-04-20 13:47:12 +05302229 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002230
2231 lock_output_stream(new_out);
2232 ret = audio_extn_out_get_param_data(new_out, param_id, payload);
Harsh Bansal28718c52017-04-20 13:47:12 +05302233 if (ret)
2234 ERROR_MSG("audio_extn_out_get_param_data error %d", ret);
2235 unlock_output_stream(new_out);
2236
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302237 return ret;
2238}
2239
Harsh Bansal28718c52017-04-20 13:47:12 +05302240/* To open a stream with QAF. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302241int audio_extn_qaf_open_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302242 audio_io_handle_t handle,
2243 audio_devices_t devices,
2244 audio_output_flags_t flags,
2245 struct audio_config *config,
2246 struct audio_stream_out **stream_out,
2247 const char *address)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302248{
2249 int ret = 0;
2250 struct stream_out *out;
2251
2252 ret = adev_open_output_stream(dev, handle, devices, flags, config, stream_out, address);
2253 if (*stream_out == NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302254 ERROR_MSG("Stream open failed %d", ret);
Deepak Agarwal1e42b852017-02-11 17:57:04 +05302255 return ret;
2256 }
2257
Harsh Bansal28718c52017-04-20 13:47:12 +05302258 out = (struct stream_out *)*stream_out;
2259
2260 ret = qaf_stream_open(out, config, flags, devices);
2261 if (ret == -ENOTSUP) {
2262 //Stream not supported by QAF, Bypass QAF.
2263 return 0;
2264 } else if (ret < 0) {
2265 ERROR_MSG("Error opening QAF stream err[%d]!", ret);
2266 adev_close_output_stream(dev, *stream_out);
2267 return ret;
2268 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302269
2270 /* Override function pointers based on qaf definitions */
2271 out->stream.set_volume = qaf_out_set_volume;
2272 out->stream.pause = qaf_out_pause;
2273 out->stream.resume = qaf_out_resume;
2274 out->stream.drain = qaf_out_drain;
2275 out->stream.flush = qaf_out_flush;
2276
2277 out->stream.common.standby = qaf_out_standby;
2278 out->stream.common.set_parameters = qaf_out_set_parameters;
2279 out->stream.get_latency = qaf_out_get_latency;
2280 out->stream.write = qaf_out_write;
2281 out->stream.get_presentation_position = qaf_out_get_presentation_position;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302282 out->platform_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302283
Harsh Bansal28718c52017-04-20 13:47:12 +05302284 /*TODO: Need to handle this for DTS*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302285 if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) {
2286 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
Varun B34da7a42017-02-13 16:16:53 +05302287 out->config.period_size = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302288 out->config.period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT;
Varun B34da7a42017-02-13 16:16:53 +05302289 out->config.start_threshold = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
2290 out->config.avail_min = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302291 } else if(out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
2292 out->compr_config.fragment_size = qaf_get_pcm_offload_input_buffer_size(&(config->offload_info));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302293 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302294
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302295 *stream_out = &out->stream;
2296 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2297 qaf_create_offload_callback_thread(out);
2298 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302299
2300 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302301 return 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302302}
2303
Harsh Bansal28718c52017-04-20 13:47:12 +05302304/* Close a QAF stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302305void audio_extn_qaf_close_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302306 struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302307{
2308 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05302309 struct qaf_module* qaf_mod = get_qaf_module_for_input_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302310
Harsh Bansal28718c52017-04-20 13:47:12 +05302311 if (!qaf_mod) return;
2312
2313 DEBUG_MSG("stream_handle(%p) format = %x", out, out->format);
2314
2315 //If close is received for QAF passthrough stream then close the QAF passthrough output.
2316 if (p_qaf->passthrough_in == out) {
2317 if (p_qaf->passthrough_out) {
2318 ALOGD("%s %d closing stream handle %p", __func__, __LINE__, p_qaf->passthrough_out);
2319 pthread_mutex_lock(&p_qaf->lock);
2320 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2321 (struct audio_stream_out *)(p_qaf->passthrough_out));
2322 pthread_mutex_unlock(&p_qaf->lock);
2323 p_qaf->passthrough_out = NULL;
2324 }
2325
2326 p_qaf->passthrough_in = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302327 }
2328
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302329 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2330 qaf_destroy_offload_callback_thread(out);
2331 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002332
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302333 qaf_stream_close(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302334
2335 adev_close_output_stream(dev, stream);
Harsh Bansal28718c52017-04-20 13:47:12 +05302336
2337 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302338}
2339
Harsh Bansal28718c52017-04-20 13:47:12 +05302340/* Check if QAF is supported or not. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302341bool audio_extn_qaf_is_enabled()
2342{
2343 bool prop_enabled = false;
2344 char value[PROPERTY_VALUE_MAX] = {0};
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002345 property_get("vendor.audio.qaf.enabled", value, NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302346 prop_enabled = atoi(value) || !strncmp("true", value, 4);
2347 return (prop_enabled);
2348}
2349
Harsh Bansal28718c52017-04-20 13:47:12 +05302350/* Query HDMI EDID and sets module output accordingly.*/
2351void set_hdmi_configuration_to_module()
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302352{
Harsh Bansal28718c52017-04-20 13:47:12 +05302353 int channels = 0;
2354 char *format_params;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302355 struct str_parms *qaf_params;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302356 char prop_value[PROPERTY_VALUE_MAX];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302357 bool passth_support = false;
2358
Harsh Bansal28718c52017-04-20 13:47:12 +05302359 DEBUG_MSG("Entry");
2360
2361 if (!p_qaf) {
2362 return;
2363 }
2364
2365 if (!p_qaf->hdmi_connect) {
2366 DEBUG_MSG("HDMI is not connected.");
2367 return;
2368 }
2369
2370 p_qaf->hdmi_sink_channels = 0;
2371
2372 //QAF re-encoding and DSP offload passthrough is supported.
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002373 if (property_get_bool("vendor.audio.offload.passthrough", false)
2374 && property_get_bool("vendor.audio.qaf.reencode", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302375
2376 //If MS12 session is active.
2377 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2378
2379 bool do_setparam = false;
2380 qaf_params = str_parms_create();
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002381 property_get("vendor.audio.qaf.hdmi.out", prop_value, NULL);
Harsh Bansal28718c52017-04-20 13:47:12 +05302382
2383 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_E_AC3)
2384 && (strncmp(prop_value, "ddp", 3) == 0)) {
2385 do_setparam = true;
2386 if (qaf_params) {
2387 str_parms_add_str(qaf_params,
2388 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2389 AUDIO_QAF_PARAMETER_VALUE_REENCODE_EAC3);
2390 }
2391 } else if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_AC3)) {
2392 do_setparam = true;
2393 if (qaf_params) {
2394 str_parms_add_str(qaf_params,
2395 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2396 AUDIO_QAF_PARAMETER_VALUE_REENCODE_AC3);
2397 }
2398 }
2399
2400 if (do_setparam) {
2401 if (p_qaf->qaf_msmd_enabled) {
2402 str_parms_add_str(qaf_params,
2403 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2404 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2405 } else {
2406 str_parms_add_str(qaf_params,
2407 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2408 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2409 }
2410 format_params = str_parms_to_str(qaf_params);
2411
2412 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2413 format_params);
2414
2415 passth_support = true;
2416 }
2417 str_parms_destroy(qaf_params);
2418 }
2419
2420 //DTS_M8 session is active.
2421 if (p_qaf->qaf_mod[DTS_M8].session_handle
2422 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2423
2424 bool do_setparam = false;
2425 qaf_params = str_parms_create();
2426#if 0 //TODO: Need to enable with DTS_M8 wrapper.
2427 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_DTS)) {
2428 do_setparam = true;
2429 if (qaf_params) {
2430 str_parms_add_str(qaf_params,
2431 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2432 AUDIO_QAF_PARAMETER_VALUE_REENCODE_DTS);
2433 }
2434 }
2435#endif
2436
2437 if (do_setparam) {
2438 if (p_qaf->qaf_msmd_enabled) {
2439 str_parms_add_str(qaf_params,
2440 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2441 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2442 } else {
2443 str_parms_add_str(qaf_params,
2444 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2445 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2446 }
2447 format_params = str_parms_to_str(qaf_params);
2448
2449 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2450 format_params);
2451
2452 passth_support = true;
2453 }
2454 str_parms_destroy(qaf_params);
2455 }
2456 }
2457
2458 //Compressed passthrough is not enabled.
2459 if (!passth_support) {
2460
2461 channels = platform_edid_get_max_channels(p_qaf->adev->platform);
2462
2463 qaf_params = str_parms_create();
2464 switch (channels) {
2465 case 8:
2466 DEBUG_MSG("Switching Qaf output to 7.1 channels");
2467 str_parms_add_str(qaf_params,
2468 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2469 AUDIO_QAF_PARAMETER_VALUE_8_CHANNELS);
2470 if (p_qaf->qaf_msmd_enabled) {
2471 str_parms_add_str(qaf_params,
2472 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2473 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2474 } else {
2475 str_parms_add_str(qaf_params,
2476 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2477 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2478 }
2479 p_qaf->hdmi_sink_channels = channels;
2480 break;
2481 case 6:
2482 DEBUG_MSG("Switching Qaf output to 5.1 channels");
2483 str_parms_add_str(qaf_params,
2484 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2485 AUDIO_QAF_PARAMETER_VALUE_6_CHANNELS);
2486 if (p_qaf->qaf_msmd_enabled) {
2487 str_parms_add_str(qaf_params,
2488 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2489 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2490 } else {
2491 str_parms_add_str(qaf_params,
2492 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2493 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2494 }
2495 p_qaf->hdmi_sink_channels = channels;
2496 break;
2497 default:
2498 DEBUG_MSG("Switching Qaf output to default channels");
2499 str_parms_add_str(qaf_params,
2500 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2501 AUDIO_QAF_PARAMETER_VALUE_DEFAULT_CHANNELS);
2502 if (p_qaf->qaf_msmd_enabled) {
2503 str_parms_add_str(qaf_params,
2504 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2505 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2506 } else {
2507 str_parms_add_str(qaf_params,
2508 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2509 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2510 }
2511 p_qaf->hdmi_sink_channels = 2;
2512 break;
2513 }
2514
2515 format_params = str_parms_to_str(qaf_params);
2516
2517 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2518 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2519 format_params);
2520 }
2521 if (p_qaf->qaf_mod[DTS_M8].session_handle
2522 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2523 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2524 format_params);
2525 }
2526
2527 str_parms_destroy(qaf_params);
2528 }
2529 DEBUG_MSG("Exit");
2530}
2531
2532/* QAF set parameter function. For Device connect and disconnect. */
2533int audio_extn_qaf_set_parameters(struct audio_device *adev, struct str_parms *parms)
2534{
2535 int status = 0, val = 0, k;
2536 char *format_params, *kv_parirs;
2537 struct str_parms *qaf_params;
2538 char value[32];
2539
2540 DEBUG_MSG("Entry");
2541
2542 if (!p_qaf) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302543 return -EINVAL;
2544 }
2545
2546 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
Harsh Bansal28718c52017-04-20 13:47:12 +05302547
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302548 if (status >= 0) {
2549 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302550 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is connected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302551
Harsh Bansal28718c52017-04-20 13:47:12 +05302552 p_qaf->hdmi_connect = 1;
2553 p_qaf->hdmi_sink_channels = 0;
2554
2555 if (p_qaf->passthrough_in) { //If QAF passthrough is already initialized.
2556 lock_output_stream(p_qaf->passthrough_in);
2557 if (platform_is_edid_supported_format(adev->platform,
2558 p_qaf->passthrough_in->format)) {
2559 //If passthrough format is supported by HDMI then create the QAF passthrough output if not created already.
2560 create_qaf_passthrough_stream();
2561 //Ignoring the returned error, If error then QAF passthrough is disabled.
2562 } else {
2563 //If passthrough format is not supported by HDMI then close the QAF passthrough output if already created.
2564 close_qaf_passthrough_stream();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302565 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302566 unlock_output_stream(p_qaf->passthrough_in);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302567 }
2568
Harsh Bansal28718c52017-04-20 13:47:12 +05302569 set_hdmi_configuration_to_module();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302570
Bharath Gopal01310bb2016-12-05 15:39:32 +05302571 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302572 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2573 if (!p_qaf->qaf_mod[k].bt_hdl) {
2574 DEBUG_MSG("Opening a2dp output...");
2575 status = audio_extn_bt_hal_load(&p_qaf->qaf_mod[k].bt_hdl);
2576 if (status != 0) {
2577 ERROR_MSG("Error opening BT module");
2578 return status;
2579 }
2580 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05302581 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302582 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302583 //TODO else if: Need to consider other devices.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302584 }
2585
2586 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
2587 if (status >= 0) {
2588 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302589 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is disconnected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302590
Harsh Bansal28718c52017-04-20 13:47:12 +05302591 qaf_params = str_parms_create();
2592 str_parms_add_str(qaf_params,
2593 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2594 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2595 str_parms_add_str(qaf_params,
2596 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2597 AUDIO_QAF_PARAMETER_VALUE_PCM);
2598 p_qaf->hdmi_sink_channels = 0;
2599
2600 p_qaf->passthrough_enabled = 0;
2601 p_qaf->mch_pcm_hdmi_enabled = 0;
2602 p_qaf->hdmi_connect = 0;
2603
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302604 format_params = str_parms_to_str(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302605
2606 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2607 if (p_qaf->qaf_mod[k].session_handle
2608 && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2609 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2610 p_qaf->qaf_mod[k].session_handle, format_params);
2611 }
2612 }
2613 close_all_hdmi_output();
2614
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302615 str_parms_destroy(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302616 close_qaf_passthrough_stream();
Bharath Gopal01310bb2016-12-05 15:39:32 +05302617 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302618 DEBUG_MSG("Closing a2dp output...");
2619 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2620 if (p_qaf->qaf_mod[k].bt_hdl) {
2621 audio_extn_bt_hal_unload(p_qaf->qaf_mod[k].bt_hdl);
2622 p_qaf->qaf_mod[k].bt_hdl = NULL;
2623 }
2624 }
2625 }
2626 //TODO else if: Need to consider other devices.
2627 }
2628
2629 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2630 kv_parirs = str_parms_to_str(parms);
2631 if (p_qaf->qaf_mod[k].session_handle && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2632 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2633 p_qaf->qaf_mod[k].session_handle, kv_parirs);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302634 }
2635 }
2636
Harsh Bansal28718c52017-04-20 13:47:12 +05302637 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302638 return status;
2639}
2640
Harsh Bansal28718c52017-04-20 13:47:12 +05302641/* Create the QAF. */
2642int audio_extn_qaf_init(struct audio_device *adev)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302643{
Harsh Bansal28718c52017-04-20 13:47:12 +05302644 DEBUG_MSG("Entry");
2645
2646 p_qaf = calloc(1, sizeof(struct qaf));
2647 if (p_qaf == NULL) {
2648 ERROR_MSG("Out of memory");
2649 return -ENOMEM;
2650 }
2651
2652 p_qaf->adev = adev;
2653
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002654 if (property_get_bool("vendor.audio.qaf.msmd", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302655 p_qaf->qaf_msmd_enabled = 1;
2656 }
2657 pthread_mutex_init(&p_qaf->lock, (const pthread_mutexattr_t *) NULL);
2658
2659 int i = 0;
2660
2661 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2662 char value[PROPERTY_VALUE_MAX] = {0};
2663 char lib_name[PROPERTY_VALUE_MAX] = {0};
2664 struct qaf_module *qaf_mod = &(p_qaf->qaf_mod[i]);
2665
2666 if (i == MS12) {
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002667 property_get("vendor.audio.qaf.library", value, NULL);
Harsh Bansal28718c52017-04-20 13:47:12 +05302668 } else if (i == DTS_M8) {
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002669 property_get("vendor.audio.qaf.m8.library", value, NULL);
Harsh Bansal28718c52017-04-20 13:47:12 +05302670 } else {
2671 continue;
2672 }
2673
2674 snprintf(lib_name, PROPERTY_VALUE_MAX, "%s", value);
2675
2676 qaf_mod->qaf_lib = dlopen(lib_name, RTLD_NOW);
2677 if (qaf_mod->qaf_lib == NULL) {
2678 ERROR_MSG("DLOPEN failed for %s", lib_name);
2679 continue;
2680 }
2681
2682 DEBUG_MSG("DLOPEN successful for %s", lib_name);
2683 qaf_mod->qaf_audio_session_open =
2684 (int (*)(audio_session_handle_t* session_handle, void *p_data, void* license_data))dlsym(qaf_mod->qaf_lib,
2685 "audio_session_open");
2686 qaf_mod->qaf_audio_session_close =
2687 (int (*)(audio_session_handle_t session_handle))dlsym(qaf_mod->qaf_lib,
2688 "audio_session_close");
2689 qaf_mod->qaf_audio_stream_open =
2690 (int (*)(audio_session_handle_t session_handle, audio_stream_handle_t* stream_handle,
2691 audio_stream_config_t input_config, audio_devices_t devices, stream_type_t flags))dlsym(qaf_mod->qaf_lib,
2692 "audio_stream_open");
2693 qaf_mod->qaf_audio_stream_close =
2694 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2695 "audio_stream_close");
2696 qaf_mod->qaf_audio_stream_set_param =
2697 (int (*)(audio_stream_handle_t stream_handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2698 "audio_stream_set_param");
2699 qaf_mod->qaf_audio_session_set_param =
2700 (int (*)(audio_session_handle_t handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2701 "audio_session_set_param");
2702 qaf_mod->qaf_audio_stream_get_param =
2703 (char* (*)(audio_stream_handle_t stream_handle, const char* key))dlsym(qaf_mod->qaf_lib,
2704 "audio_stream_get_param");
2705 qaf_mod->qaf_audio_session_get_param =
2706 (char* (*)(audio_session_handle_t handle, const char* key))dlsym(qaf_mod->qaf_lib,
2707 "audio_session_get_param");
2708 qaf_mod->qaf_audio_stream_start =
2709 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2710 "audio_stream_start");
2711 qaf_mod->qaf_audio_stream_stop =
2712 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2713 "audio_stream_stop");
2714 qaf_mod->qaf_audio_stream_pause =
2715 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2716 "audio_stream_pause");
2717 qaf_mod->qaf_audio_stream_flush =
2718 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2719 "audio_stream_flush");
2720 qaf_mod->qaf_audio_stream_write =
2721 (int (*)(audio_stream_handle_t stream_handle, const void* buf, int size))dlsym(qaf_mod->qaf_lib,
2722 "audio_stream_write");
2723 qaf_mod->qaf_register_event_callback =
2724 (void (*)(audio_session_handle_t session_handle, void *priv_data, notify_event_callback_t event_callback,
2725 audio_event_id_t event_id))dlsym(qaf_mod->qaf_lib,
2726 "register_event_callback");
2727 }
2728
2729 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302730 return 0;
2731}
2732
Harsh Bansal28718c52017-04-20 13:47:12 +05302733/* Tear down the qaf extension. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302734void audio_extn_qaf_deinit()
2735{
Harsh Bansal28718c52017-04-20 13:47:12 +05302736 int i;
2737 DEBUG_MSG("Entry");
2738
2739 if (p_qaf != NULL) {
2740 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2741 qaf_session_close(&p_qaf->qaf_mod[i]);
2742
2743 if (p_qaf->qaf_mod[i].qaf_lib != NULL) {
2744 dlclose(p_qaf->qaf_mod[i].qaf_lib);
2745 p_qaf->qaf_mod[i].qaf_lib = NULL;
2746 }
2747 }
2748 if (p_qaf->passthrough_out) {
2749 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2750 (struct audio_stream_out *)(p_qaf->passthrough_out));
2751 p_qaf->passthrough_out = NULL;
2752 }
2753
2754 pthread_mutex_destroy(&p_qaf->lock);
2755 free(p_qaf);
2756 p_qaf = NULL;
2757 }
2758 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302759}