blob: 2174364fc7e2d6292d904f193c644fefc2731462 [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 Bansal431eede2017-04-28 21:22:30 +0530196 /*Media format associated with each output id raised by mm module. */
197 audio_qaf_media_format_t out_stream_fmt[MAX_QAF_MODULE_OUT];
198 /*Flag is set if media format is changed for an mm module output. */
199 bool is_media_fmt_changed[MAX_QAF_MODULE_OUT];
200 /*Index to be updated in out_stream_fmt array for a new mm module output. */
201 int new_out_format_index;
202
Harsh Bansal28718c52017-04-20 13:47:12 +0530203 struct qaf_adsp_hdlr_config_state adsp_hdlr_config[MAX_QAF_MODULE_IN];
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800204
Harsh Bansal28718c52017-04-20 13:47:12 +0530205 //BT session handle.
Bharath Gopal01310bb2016-12-05 15:39:32 +0530206 void *bt_hdl;
Harsh Bansal28718c52017-04-20 13:47:12 +0530207
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530208 float vol_left;
209 float vol_right;
Harsh Bansal28718c52017-04-20 13:47:12 +0530210 bool is_vol_set;
211 bool drain_received[MAX_QAF_MODULE_IN];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530212};
213
Harsh Bansal28718c52017-04-20 13:47:12 +0530214struct qaf {
215 struct audio_device *adev;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530216
Harsh Bansal28718c52017-04-20 13:47:12 +0530217 pthread_mutex_t lock;
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800218
Harsh Bansal28718c52017-04-20 13:47:12 +0530219 bool hdmi_connect;
220 int hdmi_sink_channels;
221
222 //Flag to indicate if QAF transcode output stream is enabled from any mm module.
223 bool passthrough_enabled;
224 //Flag to indicate if QAF mch pcm output stream is enabled from any mm module.
225 bool mch_pcm_hdmi_enabled;
226
227 //Flag to indicate if msmd is supported.
228 bool qaf_msmd_enabled;
229
230 //Handle of QAF input stream, which is routed as QAF passthrough.
231 struct stream_out *passthrough_in;
232 //Handle of QAF passthrough stream.
233 struct stream_out *passthrough_out;
234
235 struct qaf_module qaf_mod[MAX_MM_MODULE_TYPE];
236};
237
238static int qaf_out_pause(struct audio_stream_out* stream);
239static int qaf_out_flush(struct audio_stream_out* stream);
240static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type);
241
242//Global handle of QAF. Access to this should be protected by mutex lock.
243static struct qaf *p_qaf = NULL;
244
245/* Gets the pointer to qaf module for the qaf input stream. */
246static struct qaf_module* get_qaf_module_for_input_stream(struct stream_out *out)
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530247{
Harsh Bansal28718c52017-04-20 13:47:12 +0530248 struct qaf_module *qaf_mod = NULL;
249 int i, j;
250 if (!p_qaf) return NULL;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530251
Harsh Bansal28718c52017-04-20 13:47:12 +0530252 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
253 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
254 if (p_qaf->qaf_mod[i].stream_in[j] == out) {
255 qaf_mod = &(p_qaf->qaf_mod[i]);
256 break;
257 }
258 }
259 }
260
261 return qaf_mod;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530262}
263
Harsh Bansal28718c52017-04-20 13:47:12 +0530264/* Finds the mm module input stream index for the QAF input stream. */
265static int get_input_stream_index(struct stream_out *out)
266{
267 int index = -1, j;
268 struct qaf_module* qaf_mod = NULL;
269
270 qaf_mod = get_qaf_module_for_input_stream(out);
271 if (!qaf_mod) return index;
272
273 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
274 if (qaf_mod->stream_in[j] == out) {
275 index = j;
276 break;
277 }
278 }
279
280 return index;
281}
282
283/* Finds the right mm module for the QAF input stream format. */
284static mm_module_type get_mm_module_for_format(audio_format_t format)
285{
286 int j;
287
288 DEBUG_MSG("Format 0x%x", format);
289
290 if (format == AUDIO_FORMAT_PCM_16_BIT) {
291 //If dts is not supported then alway support pcm with MS12
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700292 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 +0530293 return MS12;
294 }
295
296 //If QAF passthrough is active then send the PCM stream to primary HAL.
297 if (!p_qaf->passthrough_out) {
298 /* Iff any stream is active in MS12 module then route PCM stream to it. */
299 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
300 if (p_qaf->qaf_mod[MS12].stream_in[j]) {
301 return MS12;
302 }
303 }
304 }
305 return INVALID_MM_MODULE;
306 }
307
308 switch (format & AUDIO_FORMAT_MAIN_MASK) {
309 case AUDIO_FORMAT_AC3:
310 case AUDIO_FORMAT_E_AC3:
311 case AUDIO_FORMAT_AAC:
312 case AUDIO_FORMAT_AAC_ADTS:
313 case AUDIO_FORMAT_AC4:
314 return MS12;
315 case AUDIO_FORMAT_DTS:
316 case AUDIO_FORMAT_DTS_HD:
317 return DTS_M8;
318 default:
319 return INVALID_MM_MODULE;
320 }
321}
322
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530323static bool is_main_active(struct qaf_module* qaf_mod)
324{
325 return (qaf_mod->stream_in[QAF_IN_MAIN] || qaf_mod->stream_in[QAF_IN_MAIN_2]);
326}
327
328static bool is_dual_main_active(struct qaf_module* qaf_mod)
329{
330 return (qaf_mod->stream_in[QAF_IN_MAIN] && qaf_mod->stream_in[QAF_IN_MAIN_2]);
331}
332
Harsh Bansal28718c52017-04-20 13:47:12 +0530333/* Gets the pcm output buffer size(in samples) for the mm module. */
334static uint32_t get_pcm_output_buffer_size_samples(struct qaf_module *qaf_mod)
335{
336 uint32_t pcm_output_buffer_size = 0;
337
338 if (qaf_mod == &p_qaf->qaf_mod[MS12]) {
339 pcm_output_buffer_size = MS12_PCM_OUT_FRAGMENT_SIZE;
340 } else if (qaf_mod == &p_qaf->qaf_mod[DTS_M8]) {
341 pcm_output_buffer_size = DTS_PCM_OUT_FRAGMENT_SIZE;
342 }
343
344 return pcm_output_buffer_size;
345}
346
Harsh Bansal431eede2017-04-28 21:22:30 +0530347static int get_media_fmt_array_index_for_output_id(
348 struct qaf_module* qaf_mod,
349 int output_id)
350{
351 int i;
352 for (i = 0; i < MAX_QAF_MODULE_OUT; i++) {
353 if (qaf_mod->out_stream_fmt[i].output_id == output_id) {
354 return i;
355 }
356 }
357 return -1;
358}
359
Harsh Bansal28718c52017-04-20 13:47:12 +0530360/* Acquire Mutex lock on output stream */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530361static void lock_output_stream(struct stream_out *out)
362{
363 pthread_mutex_lock(&out->pre_lock);
364 pthread_mutex_lock(&out->lock);
365 pthread_mutex_unlock(&out->pre_lock);
366}
367
Harsh Bansal28718c52017-04-20 13:47:12 +0530368/* Release Mutex lock on output stream */
369static void unlock_output_stream(struct stream_out *out)
370{
371 pthread_mutex_unlock(&out->lock);
372}
373
374/* Checks if stream can be routed as QAF passthrough or not. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530375static bool audio_extn_qaf_passthrough_enabled(struct stream_out *out)
376{
Harsh Bansal28718c52017-04-20 13:47:12 +0530377 DEBUG_MSG("Format 0x%x", out->format);
378 bool is_enabled = false;
379
380 if (!p_qaf) return false;
381
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700382 if ((!property_get_bool("vendor.audio.qaf.reencode", false))
383 && property_get_bool("vendor.audio.qaf.passthrough", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530384
385 if ((out->format == AUDIO_FORMAT_PCM_16_BIT) && (popcount(out->channel_mask) > 2)) {
386 is_enabled = true;
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700387 } else if (property_get_bool("vendor.audio.offload.passthrough", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530388 switch (out->format) {
389 case AUDIO_FORMAT_AC3:
390 case AUDIO_FORMAT_E_AC3:
391 case AUDIO_FORMAT_DTS:
Ben Romberger1aaaf862017-04-06 17:49:46 -0700392 case AUDIO_FORMAT_DTS_HD:
Naresh Tanniru928f0862017-04-07 16:44:23 -0700393 case AUDIO_FORMAT_DOLBY_TRUEHD:
394 case AUDIO_FORMAT_IEC61937: {
Harsh Bansal28718c52017-04-20 13:47:12 +0530395 is_enabled = true;
396 break;
397 }
398 default:
399 is_enabled = false;
400 break;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530401 }
402 }
403 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530404
405 return is_enabled;
406}
407
408/*Closes all pcm hdmi output from QAF. */
409static void close_all_pcm_hdmi_output()
410{
411 int i;
412 //Closing all the PCM HDMI output stream from QAF.
413 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
414 if (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]) {
415 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
416 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]));
417 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH] = NULL;
418 }
419
420 if ((p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD])
421 && (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
422 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
423 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]));
424 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD] = NULL;
425 }
426 }
427
428 p_qaf->mch_pcm_hdmi_enabled = 0;
429}
430
431static void close_all_hdmi_output()
432{
433 int k;
434 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
435 if (p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
436 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
437 (struct audio_stream_out *)(p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]));
438 p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] = NULL;
439 }
440 }
441 p_qaf->passthrough_enabled = 0;
442
443 close_all_pcm_hdmi_output();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530444}
445
446static int qaf_out_callback(stream_callback_event_t event, void *param __unused, void *cookie)
447{
448 struct stream_out *out = (struct stream_out *)cookie;
449
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800450 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530451 return 0;
452}
453
Harsh Bansal28718c52017-04-20 13:47:12 +0530454/* Creates the QAF passthrough output stream. */
455static int create_qaf_passthrough_stream()
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530456{
Harsh Bansal28718c52017-04-20 13:47:12 +0530457 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530458
Harsh Bansal28718c52017-04-20 13:47:12 +0530459 int ret = 0, k;
460 struct stream_out *out = p_qaf->passthrough_in;
461
462 if (!out) return -EINVAL;
463
464 pthread_mutex_lock(&p_qaf->lock);
465 lock_output_stream(out);
466
467 //Creating QAF passthrough output stream.
468 if (NULL == p_qaf->passthrough_out) {
469 audio_output_flags_t flags;
470 struct audio_config config;
471 audio_devices_t devices;
472
473 config.sample_rate = config.offload_info.sample_rate = out->sample_rate;
474 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
475 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
476 config.offload_info.format = out->format;
477 config.offload_info.bit_width = out->bit_width;
478 config.format = out->format;
479 config.offload_info.channel_mask = config.channel_mask = out->channel_mask;
480
481 //Device is copied from the QAF passthrough input stream.
482 devices = out->devices;
483 flags = out->flags;
484
485 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
486 QAF_DEFAULT_PASSTHROUGH_HANDLE,
487 devices,
488 flags,
489 &config,
490 (struct audio_stream_out **)&(p_qaf->passthrough_out),
491 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530492 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530493 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
494 unlock_output_stream(out);
495 return ret;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530496 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530497 p_qaf->passthrough_in = out;
498 p_qaf->passthrough_out->stream.set_callback((struct audio_stream_out *)p_qaf->passthrough_out,
499 (stream_callback_t) qaf_out_callback, out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530500 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530501
502 unlock_output_stream(out);
503
504 //Since QAF-Passthrough is created, close other HDMI outputs.
505 close_all_hdmi_output();
506
507 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530508 return ret;
509}
510
Harsh Bansal28718c52017-04-20 13:47:12 +0530511/* Closes the QAF passthrough output stream. */
512static void close_qaf_passthrough_stream()
513{
514 if (p_qaf->passthrough_out != NULL) { //QAF pasthroug is enabled. Close it.
515 pthread_mutex_lock(&p_qaf->lock);
516 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
517 (struct audio_stream_out *)(p_qaf->passthrough_out));
518 p_qaf->passthrough_out = NULL;
519 pthread_mutex_unlock(&p_qaf->lock);
520
521 if (p_qaf->passthrough_in->qaf_stream_handle) {
522 qaf_out_pause((struct audio_stream_out*)p_qaf->passthrough_in);
523 qaf_out_flush((struct audio_stream_out*)p_qaf->passthrough_in);
524 qaf_out_drain((struct audio_stream_out*)p_qaf->passthrough_in,
525 (audio_drain_type_t)STREAM_CBK_EVENT_DRAIN_READY);
526 }
527 }
528}
529
530/* Sends a command to output stream offload thread. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530531static int qaf_send_offload_cmd_l(struct stream_out* out, int command)
532{
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530533 DEBUG_MSG_VV("command is %d", command);
Harsh Bansal28718c52017-04-20 13:47:12 +0530534
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530535 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
536
537 if (!cmd) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530538 ERROR_MSG("failed to allocate mem for command 0x%x", command);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530539 return -ENOMEM;
540 }
541
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530542 cmd->cmd = command;
Harsh Bansal28718c52017-04-20 13:47:12 +0530543
544 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530545 list_add_tail(&out->qaf_offload_cmd_list, &cmd->node);
546 pthread_cond_signal(&out->qaf_offload_cond);
Harsh Bansal28718c52017-04-20 13:47:12 +0530547 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530548 return 0;
549}
550
Harsh Bansal28718c52017-04-20 13:47:12 +0530551/* Stops a QAF module stream.*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530552static int audio_extn_qaf_stream_stop(struct stream_out *out)
553{
Harsh Bansal28718c52017-04-20 13:47:12 +0530554 int ret = 0;
555 DEBUG_MSG("Output Stream 0x%x", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530556
Harsh Bansal28718c52017-04-20 13:47:12 +0530557 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
558 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_stop)) {
559 return ret;
560 }
561
562 if (out->qaf_stream_handle) {
563 ret = qaf_mod->qaf_audio_stream_stop(out->qaf_stream_handle);
564 }
565
566 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530567}
568
Harsh Bansal28718c52017-04-20 13:47:12 +0530569/* Puts a QAF module stream in standby. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530570static int qaf_out_standby(struct audio_stream *stream)
571{
572 struct stream_out *out = (struct stream_out *)stream;
573 int status = 0;
574
575 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
576 stream, out->usecase, use_case_table[out->usecase]);
577
578 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530579
580 //If QAF passthrough is active then block standby on all the input streams of QAF mm modules.
581 if (p_qaf->passthrough_out) {
582 //If standby is received on QAF passthrough stream then forward it to primary HAL.
583 if (p_qaf->passthrough_in == out) {
584 status = p_qaf->passthrough_out->stream.common.standby(
585 (struct audio_stream *)p_qaf->passthrough_out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530586 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530587 } else {
588 //If QAF passthrough stream is not active then stop the QAF module stream.
589 status = audio_extn_qaf_stream_stop(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530590 }
591
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530592 if (!out->standby) {
593 out->standby = true;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530594 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530595
596 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530597 return status;
598}
599
Harsh Bansal28718c52017-04-20 13:47:12 +0530600/* Sets the volume to PCM output stream. */
601static int qaf_out_set_volume(struct audio_stream_out *stream, float left, float right)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530602{
603 int ret = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530604 struct stream_out *out = (struct stream_out *)stream;
605 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530606
Harsh Bansal28718c52017-04-20 13:47:12 +0530607 DEBUG_MSG("Left %f, Right %f", left, right);
608
609 qaf_mod = get_qaf_module_for_input_stream(out);
610 if (!qaf_mod) {
611 return -EINVAL;
612 }
613
614 pthread_mutex_lock(&p_qaf->lock);
615 qaf_mod->vol_left = left;
616 qaf_mod->vol_right = right;
617 qaf_mod->is_vol_set = true;
618 pthread_mutex_unlock(&p_qaf->lock);
619
620 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL) {
621 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
622 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], left, right);
623 }
624
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530625 return ret;
626}
627
Harsh Bansal28718c52017-04-20 13:47:12 +0530628/* Starts a QAF module stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530629static int qaf_stream_start(struct stream_out *out)
630{
Harsh Bansal28718c52017-04-20 13:47:12 +0530631 int ret = -EINVAL;
632 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530633
Harsh Bansal28718c52017-04-20 13:47:12 +0530634 DEBUG_MSG("Output Stream = %p", out);
635
636 qaf_mod = get_qaf_module_for_input_stream(out);
637 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_start)) {
638 return -EINVAL;
639 }
640
641 if (out->qaf_stream_handle) {
642 ret = qaf_mod->qaf_audio_stream_start(out->qaf_stream_handle);
643 }
644
645 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530646}
647
648static int qaf_start_output_stream(struct stream_out *out)
649{
650 int ret = 0;
651 struct audio_device *adev = out->dev;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530652
653 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
654 ret = -EINVAL;
655 usleep(50000);
656 return ret;
657 }
658
659 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
660 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
661 out->devices);
662
Dhananjay Kumare6293dd2017-05-25 17:25:30 +0530663 if (CARD_STATUS_OFFLINE == out->card_status ||
664 CARD_STATUS_OFFLINE == adev->card_status) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530665 ALOGE("%s: sound card is not active/SSR returning error", __func__);
666 ret = -EIO;
667 usleep(50000);
668 return ret;
669 }
670
671 return qaf_stream_start(out);
672}
673
Harsh Bansal28718c52017-04-20 13:47:12 +0530674/* Sends input buffer to the QAF MM module. */
675static int qaf_module_write_input_buffer(struct stream_out *out, const void *buffer, int bytes)
676{
677 int ret = -EINVAL;
678 struct qaf_module *qaf_mod = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +0530679
680 qaf_mod = get_qaf_module_for_input_stream(out);
681 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_write)) {
682 return ret;
683 }
684
685 if (out->qaf_stream_handle) {
686 ret = qaf_mod->qaf_audio_stream_write(out->qaf_stream_handle, buffer, bytes);
687 }
688 return ret;
689}
690
691/* Writes buffer to QAF input stream. */
692static ssize_t qaf_out_write(struct audio_stream_out *stream, const void *buffer, size_t bytes)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530693{
694 struct stream_out *out = (struct stream_out *)stream;
695 struct audio_device *adev = out->dev;
696 ssize_t ret = 0;
697
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530698 DEBUG_MSG_VV("bytes = %d, usecase[%d] and flags[%x] for handle[%p]",
Harsh Bansal28718c52017-04-20 13:47:12 +0530699 (int)bytes, out->usecase, out->flags, out);
700
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530701 lock_output_stream(out);
702
Harsh Bansal28718c52017-04-20 13:47:12 +0530703 // If QAF passthrough is active then block writing data to QAF mm module.
704 if (p_qaf->passthrough_out) {
705 //If write is received for the QAF passthrough stream then send the buffer to primary HAL.
706 if (p_qaf->passthrough_in == out) {
707 ret = p_qaf->passthrough_out->stream.write(
708 (struct audio_stream_out *)(p_qaf->passthrough_out),
709 buffer,
710 bytes);
711 if (ret > 0) out->standby = false;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530712 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530713 unlock_output_stream(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530714 return ret;
Harsh Bansal28718c52017-04-20 13:47:12 +0530715 } else if (out->standby) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530716 pthread_mutex_lock(&adev->lock);
717 ret = qaf_start_output_stream(out);
718 pthread_mutex_unlock(&adev->lock);
Harsh Bansal28718c52017-04-20 13:47:12 +0530719 if (ret == 0) {
720 out->standby = false;
721 } else {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530722 goto exit;
723 }
724 }
725
Harsh Bansal28718c52017-04-20 13:47:12 +0530726 if ((adev->is_channel_status_set == false) && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530727 audio_utils_set_hdmi_channel_status(out, (char *)buffer, bytes);
728 adev->is_channel_status_set = true;
729 }
730
Harsh Bansal28718c52017-04-20 13:47:12 +0530731 ret = qaf_module_write_input_buffer(out, buffer, bytes);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530732 DEBUG_MSG_VV("ret [%d]", (int)ret);
Harsh Bansal28718c52017-04-20 13:47:12 +0530733
734 if (ret >= 0) {
735 bytes = ret;
736 out->written += bytes / ((popcount(out->channel_mask) * sizeof(short)));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530737 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530738
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530739
740exit:
Harsh Bansal28718c52017-04-20 13:47:12 +0530741 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530742
743 if (ret < 0) {
744 if (ret == -EAGAIN) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530745 DEBUG_MSG_VV("No space available in mm module, post msg to cb thread");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530746 ret = qaf_send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530747 bytes = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530748 } else if (ret == -ENOMEM || ret == -EPERM) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530749 if (out->pcm)
Harsh Bansal28718c52017-04-20 13:47:12 +0530750 ERROR_MSG("error %d, %s", (int)ret, pcm_get_error(out->pcm));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530751 qaf_out_standby(&out->stream.common);
Harsh Bansal28718c52017-04-20 13:47:12 +0530752 usleep(bytes * 1000000
753 / audio_stream_out_frame_size(stream)
754 / out->stream.common.get_sample_rate(&out->stream.common));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530755 }
756 }
757 return bytes;
758}
759
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530760/* Gets PCM offload buffer size for a given config. */
761static uint32_t qaf_get_pcm_offload_buffer_size(audio_offload_info_t* info,
762 uint32_t samples_per_frame)
Varun B34da7a42017-02-13 16:16:53 +0530763{
764 uint32_t fragment_size = 0;
Varun B34da7a42017-02-13 16:16:53 +0530765
Harsh Bansal28718c52017-04-20 13:47:12 +0530766 fragment_size = (samples_per_frame * (info->bit_width >> 3) * popcount(info->channel_mask));
767
768 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530769 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530770 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530771 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530772
Varun B34da7a42017-02-13 16:16:53 +0530773 // To have same PCM samples for all channels, the buffer size requires to
774 // be multiple of (number of channels * bytes per sample)
775 // For writes to succeed, the buffer must be written at address which is multiple of 32
Harsh Bansal28718c52017-04-20 13:47:12 +0530776 fragment_size = ALIGN(fragment_size,
777 ((info->bit_width >> 3) * popcount(info->channel_mask) * 32));
Varun B34da7a42017-02-13 16:16:53 +0530778
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530779 ALOGI("Qaf PCM offload Fragment size is %d bytes", fragment_size);
Varun B34da7a42017-02-13 16:16:53 +0530780
781 return fragment_size;
782}
783
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530784static uint32_t qaf_get_pcm_offload_input_buffer_size(info)
785{
786 return qaf_get_pcm_offload_buffer_size(info, MS12_PCM_IN_FRAGMENT_SIZE);
787}
788
789static uint32_t qaf_get_pcm_offload_output_buffer_size(struct qaf_module *qaf_mod,
790 audio_offload_info_t* info)
791{
792 return qaf_get_pcm_offload_buffer_size(info, get_pcm_output_buffer_size_samples(qaf_mod));
793}
794
Harsh Bansal28718c52017-04-20 13:47:12 +0530795/* Gets buffer latency in samples. */
796static int get_buffer_latency(struct stream_out *out, uint32_t buffer_size, uint32_t *latency)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530797{
Harsh Bansal28718c52017-04-20 13:47:12 +0530798 unsigned long int samples_in_one_encoded_frame;
799 unsigned long int size_of_one_encoded_frame;
800
801 switch (out->format) {
802 case AUDIO_FORMAT_AC3:
803 samples_in_one_encoded_frame = DD_FRAME_SIZE;
804 size_of_one_encoded_frame = DD_ENCODER_OUTPUT_SIZE;
805 break;
806 case AUDIO_FORMAT_E_AC3:
807 samples_in_one_encoded_frame = DDP_FRAME_SIZE;
808 size_of_one_encoded_frame = DDP_ENCODER_OUTPUT_SIZE;
809 break;
810 case AUDIO_FORMAT_DTS:
811 samples_in_one_encoded_frame = DTS_FRAME_SIZE;
812 size_of_one_encoded_frame = DTS_ENCODER_OUTPUT_SIZE;
813 break;
814 case AUDIO_FORMAT_DTS_HD:
815 samples_in_one_encoded_frame = DTSHD_FRAME_SIZE;
816 size_of_one_encoded_frame = DTSHD_ENCODER_OUTPUT_SIZE;
817 break;
818 case AUDIO_FORMAT_PCM_16_BIT:
819 samples_in_one_encoded_frame = 1;
820 size_of_one_encoded_frame = ((out->bit_width) >> 3) * popcount(out->channel_mask);
821 break;
822 default:
823 *latency = 0;
824 return (-EINVAL);
825 }
826
827 *latency = ((buffer_size * samples_in_one_encoded_frame) / size_of_one_encoded_frame);
828 return 0;
829}
830
831/* Returns the number of frames rendered to outside observer. */
832static int qaf_get_rendered_frames(struct stream_out *out, uint64_t *frames)
833{
834 int ret = 0, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530835 struct str_parms *parms;
836 int value = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530837 int module_latency = 0;
838 uint32_t kernel_latency = 0;
839 uint32_t dsp_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530840 int signed_frames = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530841 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +0530842 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530843
Harsh Bansal28718c52017-04-20 13:47:12 +0530844 DEBUG_MSG("Output Format %d", out->format);
845
846 qaf_mod = get_qaf_module_for_input_stream(out);
847 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_get_param)) {
848 return -EINVAL;
849 }
850
851 //Get MM module latency.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530852 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "get_latency");
853 if (kvpairs) {
854 parms = str_parms_create_str(kvpairs);
Harsh Bansal28718c52017-04-20 13:47:12 +0530855 ret = str_parms_get_int(parms, "get_latency", &module_latency);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530856 if (ret >= 0) {
857 str_parms_destroy(parms);
858 parms = NULL;
859 }
860 free(kvpairs);
861 kvpairs = NULL;
862 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530863
864 //Get kernel Latency
865 for (i = MAX_QAF_MODULE_OUT - 1; i >= 0; i--) {
866 if (qaf_mod->stream_out[i] == NULL) {
867 continue;
868 } else {
869 unsigned int num_fragments = qaf_mod->stream_out[i]->compr_config.fragments;
870 uint32_t fragment_size = qaf_mod->stream_out[i]->compr_config.fragment_size;
871 uint32_t kernel_buffer_size = num_fragments * fragment_size;
872 get_buffer_latency(qaf_mod->stream_out[i], kernel_buffer_size, &kernel_latency);
873 break;
874 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530875 }
876
Harsh Bansal28718c52017-04-20 13:47:12 +0530877 //Get DSP latency
878 if ((qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL)
879 || (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH] != NULL)) {
880 unsigned int sample_rate = 0;
881
882 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
883 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
884 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
885 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
886
887 audio_usecase_t platform_latency =
888 platform_render_latency(qaf_mod->stream_out[QAF_OUT_OFFLOAD]->usecase);
889 dsp_latency = (platform_latency * sample_rate) / 1000000LL;
890 } else if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] != NULL) {
891 unsigned int sample_rate = 0;
892
893 sample_rate = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->sample_rate; //TODO: How this sample rate can be used?
894 dsp_latency = (COMPRESS_OFFLOAD_PLAYBACK_LATENCY * sample_rate) / 1000;
895 }
896
897 // MM Module Latency + Kernel Latency + DSP Latency
898 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
899 out->platform_latency = module_latency + audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl);
900 } else {
901 out->platform_latency = (uint32_t)module_latency + kernel_latency + dsp_latency;
902 }
903
904 if (out->format & AUDIO_FORMAT_PCM_16_BIT) {
905 *frames = 0;
906 signed_frames = out->written - out->platform_latency;
907 // It would be unusual for this value to be negative, but check just in case ...
908 if (signed_frames >= 0) {
909 *frames = signed_frames;
910 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530911 } else if (qaf_mod->qaf_audio_stream_get_param) {
912 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "position");
913 if (kvpairs) {
914 parms = str_parms_create_str(kvpairs);
915 ret = str_parms_get_int(parms, "position", &value);
916 if (ret >= 0) {
917 *frames = value;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530918 signed_frames = value - out->platform_latency;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530919 // It would be unusual for this value to be negative, but check just in case ...
920 if (signed_frames >= 0) {
921 *frames = signed_frames;
922 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530923 }
924 str_parms_destroy(parms);
925 }
926 } else {
927 ret = -EINVAL;
928 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530929
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530930 return ret;
931}
932
933static int qaf_out_get_presentation_position(const struct audio_stream_out *stream,
Harsh Bansal28718c52017-04-20 13:47:12 +0530934 uint64_t *frames,
935 struct timespec *timestamp)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530936{
937 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530938 int ret = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530939
Harsh Bansal28718c52017-04-20 13:47:12 +0530940 DEBUG_MSG("Output Stream %p", stream);
941
942 //If QAF passthorugh output stream is active.
943 if (p_qaf->passthrough_out) {
944 if (p_qaf->passthrough_in == out) {
945 //If api is called for QAF passthorugh stream then call the primary HAL api to get the position.
946 pthread_mutex_lock(&p_qaf->lock);
947 ret = p_qaf->passthrough_out->stream.get_presentation_position(
948 (struct audio_stream_out *)p_qaf->passthrough_out,
949 frames,
950 timestamp);
951 pthread_mutex_unlock(&p_qaf->lock);
952 } else {
953 //If api is called for other stream then return zero frames.
954 *frames = 0;
955 clock_gettime(CLOCK_MONOTONIC, timestamp);
956 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530957 return ret;
958 }
959
Harsh Bansal28718c52017-04-20 13:47:12 +0530960 ret = qaf_get_rendered_frames(out, frames);
961 clock_gettime(CLOCK_MONOTONIC, timestamp);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530962
963 return ret;
964}
965
Harsh Bansal28718c52017-04-20 13:47:12 +0530966/* Pause the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530967static int qaf_stream_pause(struct stream_out *out)
968{
Harsh Bansal28718c52017-04-20 13:47:12 +0530969 struct qaf_module *qaf_mod = NULL;
970 int ret = -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530971
Harsh Bansal28718c52017-04-20 13:47:12 +0530972 qaf_mod = get_qaf_module_for_input_stream(out);
973 if (!qaf_mod || !qaf_mod->qaf_audio_stream_pause) {
974 return -EINVAL;
975 }
976
977 if (out->qaf_stream_handle)
978 ret = qaf_mod->qaf_audio_stream_pause(out->qaf_stream_handle);
979
980 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530981}
982
Harsh Bansal28718c52017-04-20 13:47:12 +0530983/* Pause a QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530984static int qaf_out_pause(struct audio_stream_out* stream)
985{
986 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530987 int status = 0;
988 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530989
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530990 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530991
992 //If QAF passthrough is enabled then block the pause on module stream.
993 if (p_qaf->passthrough_out) {
994 pthread_mutex_lock(&p_qaf->lock);
995 //If pause is received for QAF passthorugh stream then call the primary HAL api.
996 if (p_qaf->passthrough_in == out) {
997 status = p_qaf->passthrough_out->stream.pause(
998 (struct audio_stream_out *)p_qaf->passthrough_out);
999 out->offload_state = OFFLOAD_STATE_PAUSED;
1000 }
1001 pthread_mutex_unlock(&p_qaf->lock);
1002 } else {
1003 //Pause the module input stream.
1004 status = qaf_stream_pause(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301005 }
1006
Harsh Bansal28718c52017-04-20 13:47:12 +05301007 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301008 return status;
1009}
1010
Harsh Bansal28718c52017-04-20 13:47:12 +05301011/* Drains a qaf input stream. */
1012static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301013{
1014 struct stream_out *out = (struct stream_out *)stream;
1015 int status = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05301016 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301017
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301018 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301019
1020 //If QAF passthrough is enabled then block the drain on module stream.
1021 if (p_qaf->passthrough_out) {
1022 pthread_mutex_lock(&p_qaf->lock);
1023 //If drain is received for QAF passthorugh stream then call the primary HAL api.
1024 if (p_qaf->passthrough_in == out) {
1025 status = p_qaf->passthrough_out->stream.drain(
1026 (struct audio_stream_out *)p_qaf->passthrough_out, type);
1027 }
1028 pthread_mutex_unlock(&p_qaf->lock);
1029 } else {
1030 //Drain the module input stream.
1031 /* Stream stop will trigger EOS and on EOS_EVENT received
1032 from callback DRAIN_READY command is sent */
1033 status = audio_extn_qaf_stream_stop(out);
1034
1035 if (status == 0) {
1036 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
1037 int index = get_input_stream_index(out);
1038 if (qaf_mod && index >= 0) qaf_mod->drain_received[index] = true;
1039 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301040 }
1041
Harsh Bansal28718c52017-04-20 13:47:12 +05301042 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301043 return status;
1044}
1045
Harsh Bansal28718c52017-04-20 13:47:12 +05301046/* Flush the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301047static int audio_extn_qaf_stream_flush(struct stream_out *out)
1048{
Harsh Bansal28718c52017-04-20 13:47:12 +05301049 DEBUG_MSG("Output Stream %p", out);
1050 int ret = -EINVAL;
1051 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301052
Harsh Bansal28718c52017-04-20 13:47:12 +05301053 qaf_mod = get_qaf_module_for_input_stream(out);
1054 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_flush)) {
1055 return -EINVAL;
1056 }
1057
1058 if (out->qaf_stream_handle)
1059 ret = qaf_mod->qaf_audio_stream_flush(out->qaf_stream_handle);
1060
1061 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301062}
1063
Harsh Bansal28718c52017-04-20 13:47:12 +05301064/* Flush the QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301065static int qaf_out_flush(struct audio_stream_out* stream)
1066{
1067 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05301068 int status = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301069
Harsh Bansal28718c52017-04-20 13:47:12 +05301070 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301071 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301072
1073 //If QAF passthrough is active then block the flush on module input streams.
1074 if (p_qaf->passthrough_out) {
1075 pthread_mutex_lock(&p_qaf->lock);
1076 //If flush is received for the QAF passthrough stream then call the primary HAL api.
1077 if (p_qaf->passthrough_in == out) {
1078 status = p_qaf->passthrough_out->stream.flush(
1079 (struct audio_stream_out *)p_qaf->passthrough_out);
1080 out->offload_state = OFFLOAD_STATE_IDLE;
1081 }
1082 pthread_mutex_unlock(&p_qaf->lock);
1083 } else {
1084 //Flush the module input stream.
1085 status = audio_extn_qaf_stream_flush(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301086 }
1087
Harsh Bansal28718c52017-04-20 13:47:12 +05301088 unlock_output_stream(out);
1089 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301090 return status;
1091}
1092
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301093static uint32_t qaf_out_get_latency(const struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301094{
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301095 struct stream_out *out = (struct stream_out *)stream;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301096 uint32_t latency = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05301097 struct qaf_module *qaf_mod = NULL;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301098 DEBUG_MSG_VV("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301099
Harsh Bansal28718c52017-04-20 13:47:12 +05301100 qaf_mod = get_qaf_module_for_input_stream(out);
1101 if (!qaf_mod) {
1102 return 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301103 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301104
Harsh Bansal28718c52017-04-20 13:47:12 +05301105 //If QAF passthrough is active then block the get latency on module input streams.
1106 if (p_qaf->passthrough_out) {
1107 pthread_mutex_lock(&p_qaf->lock);
1108 //If get latency is called for the QAF passthrough stream then call the primary HAL api.
1109 if (p_qaf->passthrough_in == out) {
1110 latency = p_qaf->passthrough_out->stream.get_latency(
1111 (struct audio_stream_out *)p_qaf->passthrough_out);
1112 }
1113 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301114 } else {
Bharath Gopal01310bb2016-12-05 15:39:32 +05301115 if (is_offload_usecase(out->usecase)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301116 latency = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301117 } else {
Harsh Bansal28718c52017-04-20 13:47:12 +05301118 uint32_t sample_rate = 0;
1119 latency = QAF_MODULE_PCM_INPUT_BUFFER_LATENCY; //Input latency
1120
1121 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
1122 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
1123 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
1124 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
1125
1126 if (sample_rate) {
1127 latency += (get_pcm_output_buffer_size_samples(qaf_mod) * 1000) / out->sample_rate;
1128 }
1129 }
1130
1131 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
1132 if (is_offload_usecase(out->usecase)) {
1133 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1134 QAF_COMPRESS_OFFLOAD_PROCESSING_LATENCY;
1135 } else {
1136 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1137 QAF_PCM_OFFLOAD_PROCESSING_LATENCY;
1138 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05301139 }
1140 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301141
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301142 DEBUG_MSG_VV("Latency %d", latency);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301143 return latency;
1144}
1145
Harsh Bansal28718c52017-04-20 13:47:12 +05301146static bool check_and_get_compressed_device_format(int device, int *format)
1147{
1148 switch (device) {
1149 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DD):
1150 *format = AUDIO_FORMAT_AC3;
1151 return true;
1152 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DDP):
1153 *format = AUDIO_FORMAT_E_AC3;
1154 return true;
1155 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_FORMAT_DTS):
1156 *format = AUDIO_FORMAT_DTS;
1157 return true;
Harsh Bansal28718c52017-04-20 13:47:12 +05301158 default:
1159 return false;
1160 }
1161}
1162
1163/* Call back function for mm module. */
1164static void notify_event_callback(audio_session_handle_t session_handle /*__unused*/,
1165 void *prv_data,
1166 void *buf,
1167 audio_event_id_t event_id,
1168 int size,
1169 int device) //TODO: add media format as well.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301170{
1171
Harsh Bansal28718c52017-04-20 13:47:12 +05301172 /*
1173 For SPKR:
1174 1. Open pcm device if device_id passed to it SPKR and write the data to
1175 pcm device
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301176
Harsh Bansal28718c52017-04-20 13:47:12 +05301177 For HDMI
1178 1.Open compress device for HDMI(PCM or AC3) based on current hdmi o/p format and write
1179 data to the HDMI device.
1180 */
1181 int ret, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301182 audio_output_flags_t flags;
Harsh Bansal28718c52017-04-20 13:47:12 +05301183 struct qaf_module* qaf_mod = (struct qaf_module*)prv_data;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301184 struct audio_stream_out *bt_stream = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301185 int format;
Harsh Bansal431eede2017-04-28 21:22:30 +05301186 int8_t *data_buffer_p = NULL;
1187 uint32_t buffer_size = 0;
1188 bool need_to_recreate_stream = false;
1189 struct audio_config config;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301190
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301191 DEBUG_MSG_VV("Device 0x%X, Event = 0x%X, Bytes to write %d", device, event_id, size);
Harsh Bansal28718c52017-04-20 13:47:12 +05301192 pthread_mutex_lock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301193
Harsh Bansal431eede2017-04-28 21:22:30 +05301194 /* Default config initialization. */
1195 config.sample_rate = config.offload_info.sample_rate = QAF_OUTPUT_SAMPLING_RATE;
1196 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1197 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
1198 config.format = config.offload_info.format = AUDIO_FORMAT_PCM_16_BIT;
1199 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
1200 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1201
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301202 if (event_id == AUDIO_DATA_EVENT) {
Harsh Bansal431eede2017-04-28 21:22:30 +05301203 data_buffer_p = (int8_t*)buf;
1204 buffer_size = size;
1205 } else if (event_id == AUDIO_DATA_EVENT_V2) {
1206 audio_qaf_out_buffer_t *buf_payload = (audio_qaf_out_buffer_t*)buf;
1207 int index = -1;
1208 audio_qaf_media_format_t *p_cached_fmt = NULL;
1209
1210 if (size < sizeof(audio_qaf_out_buffer_t)) {
1211 ERROR_MSG("AUDIO_DATA_EVENT_V2 payload size is not sufficient.");
1212 return;
1213 }
1214
1215 data_buffer_p = (int8_t*)buf_payload->data + buf_payload->offset;
1216 buffer_size = buf_payload->size - buf_payload->offset;
1217
1218 index = get_media_fmt_array_index_for_output_id(qaf_mod, buf_payload->output_id);
1219
1220 if (index < 0) {
1221 /*If media format is not received then switch to default values.*/
1222 event_id = AUDIO_DATA_EVENT;
1223 } else {
1224 p_cached_fmt = &qaf_mod->out_stream_fmt[index];
1225 need_to_recreate_stream = qaf_mod->is_media_fmt_changed[index];
1226 qaf_mod->is_media_fmt_changed[index] = false;
1227
1228 config.sample_rate = config.offload_info.sample_rate = p_cached_fmt->sample_rate;
1229 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1230 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
1231 config.format = config.offload_info.format = p_cached_fmt->format;
1232 config.offload_info.bit_width = p_cached_fmt->bit_width;
1233
1234 if (p_cached_fmt->format == AUDIO_FORMAT_PCM) {
1235 if (p_cached_fmt->bit_width == 16)
1236 config.format = config.offload_info.format = AUDIO_FORMAT_PCM_16_BIT;
1237 else if (p_cached_fmt->bit_width == 24)
1238 config.format = config.offload_info.format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1239 else
1240 config.format = config.offload_info.format = AUDIO_FORMAT_PCM_32_BIT;
1241 }
1242
1243 device |= (p_cached_fmt->format & AUDIO_FORMAT_MAIN_MASK);
1244
1245 switch (p_cached_fmt->channels) {
1246 case 1:
1247 config.offload_info.channel_mask = config.channel_mask =
1248 AUDIO_CHANNEL_OUT_MONO;
1249 break;
1250 case 2:
1251 config.offload_info.channel_mask = config.channel_mask =
1252 AUDIO_CHANNEL_OUT_STEREO;
1253 break;
1254 case 6:
1255 config.offload_info.channel_mask = config.channel_mask =
1256 AUDIO_CHANNEL_OUT_5POINT1;
1257 break;
1258 case 8:
1259 config.offload_info.channel_mask = config.channel_mask =
1260 AUDIO_CHANNEL_OUT_7POINT1;
1261 break;
1262 default:
1263 config.offload_info.channel_mask = config.channel_mask =
1264 AUDIO_CHANNEL_OUT_5POINT1;
1265 break;
1266 }
1267 }
1268 }
1269
1270 if (event_id == AUDIO_OUTPUT_MEDIA_FORMAT_EVENT) {
1271 audio_qaf_media_format_t *p_fmt = (audio_qaf_media_format_t*)buf;
1272 audio_qaf_media_format_t *p_cached_fmt = NULL;
1273 int index = -1;
1274
1275 if (size < sizeof(audio_qaf_media_format_t)) {
1276 ERROR_MSG("Size is not proper for the event AUDIO_OUTPUT_MEDIA_FORMAT_EVENT.");
1277 return ;
1278 }
1279
1280 index = get_media_fmt_array_index_for_output_id(qaf_mod, p_fmt->output_id);
1281
1282 if (index >= 0) {
1283 p_cached_fmt = &qaf_mod->out_stream_fmt[index];
1284 } else if (index < 0 && qaf_mod->new_out_format_index < MAX_QAF_MODULE_OUT) {
1285 index = qaf_mod->new_out_format_index;
1286 p_cached_fmt = &qaf_mod->out_stream_fmt[index];
1287 qaf_mod->new_out_format_index++;
1288 }
1289
1290 if (p_cached_fmt == NULL) {
1291 ERROR_MSG("Maximum output from a QAF module is reached. Can not process new output.");
1292 return ;
1293 }
1294
1295 if (memcmp(p_cached_fmt, p_fmt, sizeof(audio_qaf_media_format_t)) != 0) {
1296 memcpy(p_cached_fmt, p_fmt, sizeof(audio_qaf_media_format_t));
1297 qaf_mod->is_media_fmt_changed[index] = true;
1298 }
1299 } else if (event_id == AUDIO_DATA_EVENT || event_id == AUDIO_DATA_EVENT_V2) {
1300
Harsh Bansal28718c52017-04-20 13:47:12 +05301301 if (p_qaf->passthrough_out != NULL) {
1302 //If QAF passthrough is active then all the module output will be dropped.
1303 pthread_mutex_unlock(&p_qaf->lock);
1304 DEBUG_MSG("QAF-PSTH is active, DROPPING DATA!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301305 return;
Harsh Bansal28718c52017-04-20 13:47:12 +05301306 }
1307
1308 if (check_and_get_compressed_device_format(device, &format)) {
1309 /*
1310 * CASE 1: Transcoded output of mm module.
1311 * If HDMI is not connected then drop the data.
1312 * Only one HDMI output can be supported from all the mm modules of QAF.
1313 * Multi-Channel PCM HDMI output streams will be closed from all the mm modules.
1314 * If transcoded output of other module is already enabled then this data will be dropped.
1315 */
1316
1317 if (!p_qaf->hdmi_connect) {
1318 DEBUG_MSG("HDMI not connected, DROPPING DATA!");
1319 pthread_mutex_unlock(&p_qaf->lock);
1320 return;
1321 }
1322
1323 //Closing all the PCM HDMI output stream from QAF.
1324 close_all_pcm_hdmi_output();
1325
Harsh Bansal431eede2017-04-28 21:22:30 +05301326 /* If Media format was changed for this stream then need to re-create the stream. */
1327 if (need_to_recreate_stream && qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
1328 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1329 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]));
1330 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] = NULL;
1331 p_qaf->passthrough_enabled = false;
1332 }
1333
Harsh Bansal28718c52017-04-20 13:47:12 +05301334 if (!p_qaf->passthrough_enabled
1335 && !(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH])) {
1336
Harsh Bansal28718c52017-04-20 13:47:12 +05301337 audio_devices_t devices;
1338
Harsh Bansal28718c52017-04-20 13:47:12 +05301339 config.format = config.offload_info.format = format;
Harsh Bansal28718c52017-04-20 13:47:12 +05301340 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1341
1342 flags = (AUDIO_OUTPUT_FLAG_NON_BLOCKING
1343 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD
1344 | AUDIO_OUTPUT_FLAG_DIRECT);
1345 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1346
1347 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1348 QAF_DEFAULT_COMPR_PASSTHROUGH_HANDLE,
1349 devices,
1350 flags,
1351 &config,
1352 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]),
1353 NULL);
1354 if (ret < 0) {
1355 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1356 pthread_mutex_unlock(&p_qaf->lock);
1357 return;
1358 }
1359
Harsh Bansal431eede2017-04-28 21:22:30 +05301360 if (format & AUDIO_FORMAT_E_AC3) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301361 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragment_size =
1362 COMPRESS_PASSTHROUGH_DDP_FRAGMENT_SIZE;
1363 }
1364 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragments =
1365 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1366
1367 p_qaf->passthrough_enabled = true;
1368 }
1369
1370 if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
1371 ret = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->stream.write(
1372 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH],
Harsh Bansal431eede2017-04-28 21:22:30 +05301373 data_buffer_p,
1374 buffer_size);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301375 }
1376 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301377 else if ((device & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1378 && (p_qaf->hdmi_connect)
1379 && (p_qaf->hdmi_sink_channels > 2)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301380
Harsh Bansal28718c52017-04-20 13:47:12 +05301381 /* CASE 2: Multi-Channel PCM output to HDMI.
1382 * If any other HDMI output is already enabled then this has to be dropped.
1383 */
1384 bool create_mch_out_stream = false;
1385
1386 if (p_qaf->passthrough_enabled) {
1387 //Closing all the multi-Channel PCM HDMI output stream from QAF.
1388 close_all_pcm_hdmi_output();
1389
1390 //If passthrough is active then pcm hdmi output has to be dropped.
1391 pthread_mutex_unlock(&p_qaf->lock);
1392 DEBUG_MSG("Compressed passthrough enabled, DROPPING DATA!");
1393 return;
1394 }
1395
Harsh Bansal431eede2017-04-28 21:22:30 +05301396 /* If Media format was changed for this stream then need to re-create the stream. */
1397 if (need_to_recreate_stream && qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) {
1398 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1399 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]));
1400 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH] = NULL;
1401 p_qaf->mch_pcm_hdmi_enabled = false;
1402 }
1403
Harsh Bansal28718c52017-04-20 13:47:12 +05301404 if (!p_qaf->mch_pcm_hdmi_enabled && !(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301405 audio_devices_t devices;
1406
Harsh Bansal431eede2017-04-28 21:22:30 +05301407 if (event_id == AUDIO_DATA_EVENT) {
1408 config.offload_info.format = config.format = AUDIO_FORMAT_PCM_16_BIT;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301409
Harsh Bansal431eede2017-04-28 21:22:30 +05301410 if (p_qaf->hdmi_sink_channels == 8) {
1411 config.offload_info.channel_mask = config.channel_mask =
1412 AUDIO_CHANNEL_OUT_7POINT1;
1413 } else if (p_qaf->hdmi_sink_channels == 6) {
1414 config.offload_info.channel_mask = config.channel_mask =
1415 AUDIO_CHANNEL_OUT_5POINT1;
1416 } else {
1417 config.offload_info.channel_mask = config.channel_mask =
1418 AUDIO_CHANNEL_OUT_STEREO;
1419 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301420 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301421
Harsh Bansal28718c52017-04-20 13:47:12 +05301422 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1423 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
1424
1425 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1426 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1427 devices,
1428 flags,
1429 &config,
1430 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]),
1431 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301432 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301433 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1434 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301435 return;
1436 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001437
Harsh Bansal28718c52017-04-20 13:47:12 +05301438 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragments =
1439 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1440 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragment_size =
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301441 qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info);
Harsh Bansal28718c52017-04-20 13:47:12 +05301442
1443 p_qaf->mch_pcm_hdmi_enabled = true;
1444
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301445 if ((qaf_mod->stream_in[QAF_IN_MAIN]
1446 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) ||
1447 (qaf_mod->stream_in[QAF_IN_MAIN_2]
1448 && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301449
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301450 if (qaf_mod->stream_in[QAF_IN_MAIN]) {
1451 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
Harsh Bansal28718c52017-04-20 13:47:12 +05301452 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1453 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1454 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301455 }
1456 if (qaf_mod->stream_in[QAF_IN_MAIN_2]) {
1457 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1458 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1459 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback,
1460 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie);
1461 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301462 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1463 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1464
1465 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1466 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1467 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1468 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1469 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001470
1471 int index = -1;
1472 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301473 index = (int) QAF_IN_MAIN;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301474 else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid)
1475 index = (int) QAF_IN_MAIN_2;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001476 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301477 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001478
Harsh Bansal28718c52017-04-20 13:47:12 +05301479 if (index >= 0) {
1480 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->standby)
1481 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1482 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], NULL, 0);
1483
1484 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001485 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301486 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1487 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1488 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1489 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001490
1491 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301492 }
1493
Harsh Bansal28718c52017-04-20 13:47:12 +05301494 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) {
1495 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1496 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
Harsh Bansal431eede2017-04-28 21:22:30 +05301497 data_buffer_p,
1498 buffer_size);
Varun B34da7a42017-02-13 16:16:53 +05301499 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301500 }
1501 else {
1502 /* CASE 3: PCM output.
1503 */
Harsh Bansal431eede2017-04-28 21:22:30 +05301504
1505 /* If Media format was changed for this stream then need to re-create the stream. */
1506 if (need_to_recreate_stream && qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1507 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1508 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD]));
1509 qaf_mod->stream_out[QAF_OUT_OFFLOAD] = NULL;
1510 }
1511
Bharath Gopal01310bb2016-12-05 15:39:32 +05301512 bt_stream = audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl);
1513 if (bt_stream != NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301514 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1515 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1516 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD]));
1517 qaf_mod->stream_out[QAF_OUT_OFFLOAD] = NULL;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301518 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301519
Harsh Bansal431eede2017-04-28 21:22:30 +05301520 audio_extn_bt_hal_out_write(p_qaf->bt_hdl, data_buffer_p, buffer_size);
Harsh Bansal28718c52017-04-20 13:47:12 +05301521 } else if (NULL == qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301522 audio_devices_t devices;
1523
Harsh Bansal28718c52017-04-20 13:47:12 +05301524 if (qaf_mod->stream_in[QAF_IN_MAIN])
1525 devices = qaf_mod->stream_in[QAF_IN_MAIN]->devices;
1526 else
1527 devices = qaf_mod->stream_in[QAF_IN_PCM]->devices;
1528
1529 //If multi channel pcm or passthrough is already enabled then remove the hdmi flag from device.
1530 if (p_qaf->mch_pcm_hdmi_enabled || p_qaf->passthrough_enabled) {
1531 if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1532 devices ^= AUDIO_DEVICE_OUT_AUX_DIGITAL;
1533 }
1534 if (devices == 0) {
1535 devices = device;
1536 }
1537
1538 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001539
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301540 /* TODO:: Need to Propagate errors to framework */
Harsh Bansal28718c52017-04-20 13:47:12 +05301541 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1542 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1543 devices,
1544 flags,
1545 &config,
1546 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD]),
1547 NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301548 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301549 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1550 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301551 return;
1552 }
Deepak Agarwal1e42b852017-02-11 17:57:04 +05301553
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301554 if ((qaf_mod->stream_in[QAF_IN_MAIN]
1555 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) ||
1556 (qaf_mod->stream_in[QAF_IN_MAIN_2]
1557 && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001558
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301559 if (qaf_mod->stream_in[QAF_IN_MAIN]) {
1560 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
Harsh Bansal28718c52017-04-20 13:47:12 +05301561 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1562 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1563 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301564 }
1565 if (qaf_mod->stream_in[QAF_IN_MAIN_2]) {
1566 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1567 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1568 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback,
1569 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie);
1570 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301571 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1572 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1573
1574 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1575 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1576 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1577 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1578 }
1579
1580 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragments =
1581 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1582 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragment_size =
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301583 qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info);
Harsh Bansal28718c52017-04-20 13:47:12 +05301584
1585 if (qaf_mod->is_vol_set) {
1586 DEBUG_MSG("Setting Volume Left[%f], Right[%f]", qaf_mod->vol_left, qaf_mod->vol_right);
1587 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
1588 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1589 qaf_mod->vol_left,
1590 qaf_mod->vol_right);
1591 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001592
1593 int index = -1;
1594 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301595 index = (int) QAF_IN_MAIN;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301596 else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid)
1597 index = (int) QAF_IN_MAIN_2;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001598 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301599 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001600 if (index >= 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301601 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]->standby) {
1602 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1603 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], NULL, 0);
1604 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001605
Harsh Bansal28718c52017-04-20 13:47:12 +05301606 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001607 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301608 qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1609 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1610 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1611 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301612 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301613 }
1614
1615 /*
1616 * TODO:: Since this is mixed data,
1617 * need to identify to which stream the error should be sent
1618 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301619 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1620 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1621 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
Harsh Bansal431eede2017-04-28 21:22:30 +05301622 data_buffer_p,
1623 buffer_size);
Varun B34da7a42017-02-13 16:16:53 +05301624 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301625 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301626 DEBUG_MSG_VV("Bytes written = %d", ret);
Harsh Bansal28718c52017-04-20 13:47:12 +05301627 }
1628 else if (event_id == AUDIO_EOS_MAIN_DD_DDP_EVENT
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301629 || event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT
Varun B34da7a42017-02-13 16:16:53 +05301630 || event_id == AUDIO_EOS_MAIN_AAC_EVENT
1631 || event_id == AUDIO_EOS_MAIN_AC4_EVENT
1632 || event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301633 struct stream_out *out = qaf_mod->stream_in[QAF_IN_MAIN];
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301634 struct stream_out *out_main2 = qaf_mod->stream_in[QAF_IN_MAIN_2];
Harsh Bansal28718c52017-04-20 13:47:12 +05301635 struct stream_out *out_assoc = qaf_mod->stream_in[QAF_IN_ASSOC];
1636 bool *main_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301637 bool *main2_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN_2];
Harsh Bansal28718c52017-04-20 13:47:12 +05301638 bool *assoc_drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
Varun B34da7a42017-02-13 16:16:53 +05301639
1640 /**
1641 * TODO:: Only DD/DDP Associate Eos is handled, need to add support
1642 * for other formats.
1643 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301644 if (event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT
1645 && (out_assoc != NULL)
1646 && (*assoc_drain_received)) {
1647
Varun B34da7a42017-02-13 16:16:53 +05301648 lock_output_stream(out_assoc);
Harsh Bansal28718c52017-04-20 13:47:12 +05301649 out_assoc->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_assoc->client_cookie);
1650 *assoc_drain_received = false;
1651 unlock_output_stream(out_assoc);
1652 DEBUG_MSG("sent associated DRAIN_READY");
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301653 } else if (event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT
1654 && (out_main2 != NULL)
1655 && (*main2_drain_received)) {
1656
1657 lock_output_stream(out_main2);
1658 out_main2->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_main2->client_cookie);
1659 *main2_drain_received = false;
1660 unlock_output_stream(out_main2);
1661 DEBUG_MSG("sent main2 DRAIN_READY");
Harsh Bansal28718c52017-04-20 13:47:12 +05301662 } else if ((out != NULL) && (*main_drain_received)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301663 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001664 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
Harsh Bansal28718c52017-04-20 13:47:12 +05301665 *main_drain_received = false;
1666 unlock_output_stream(out);
1667 DEBUG_MSG("sent main DRAIN_READY");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301668 }
1669 }
Harsh Bansal431eede2017-04-28 21:22:30 +05301670 else if (event_id == AUDIO_MAIN_EOS_EVENT || event_id == AUDIO_ASSOC_EOS_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301671 struct stream_out *out = NULL;
1672 bool *drain_received = NULL;
1673
1674 if (event_id == AUDIO_MAIN_EOS_EVENT) {
1675 out = qaf_mod->stream_in[QAF_IN_MAIN];
1676 drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
1677 } else {
1678 out = qaf_mod->stream_in[QAF_IN_ASSOC];
1679 drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
1680 }
1681
1682 if ((out != NULL) && (*drain_received)) {
1683 lock_output_stream(out);
1684 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
1685 *drain_received = false;
1686 unlock_output_stream(out);
1687 DEBUG_MSG("sent DRAIN_READY");
1688 }
1689 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301690
1691 pthread_mutex_unlock(&p_qaf->lock);
1692 return;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301693}
1694
Harsh Bansal28718c52017-04-20 13:47:12 +05301695/* Close the mm module session. */
1696static int qaf_session_close(struct qaf_module* qaf_mod)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301697{
Harsh Bansal28718c52017-04-20 13:47:12 +05301698 int j;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301699
Harsh Bansal28718c52017-04-20 13:47:12 +05301700 //Check if all streams are closed or not.
1701 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
1702 if (qaf_mod->stream_in[j] != NULL) {
1703 break;
1704 }
1705 }
1706 if (j != MAX_QAF_MODULE_IN) {
1707 return 0; //Some stream is already active, Can not close session.
1708 }
1709
1710 if (qaf_mod->session_handle != NULL && qaf_mod->qaf_audio_session_close) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301711 qaf_mod->qaf_audio_session_close(qaf_mod->session_handle);
1712 qaf_mod->session_handle = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301713 qaf_mod->is_vol_set = false;
1714 memset(qaf_mod->drain_received, 0, sizeof(qaf_mod->drain_received));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301715 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301716
1717 for (j = 0; j < MAX_QAF_MODULE_OUT; j++) {
1718 if (qaf_mod->stream_out[j]) {
1719 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1720 (struct audio_stream_out *)(qaf_mod->stream_out[j]));
1721 qaf_mod->stream_out[j] = NULL;
1722 }
Harsh Bansal431eede2017-04-28 21:22:30 +05301723 memset(&qaf_mod->out_stream_fmt[j], 0, sizeof(audio_qaf_media_format_t));
1724 qaf_mod->is_media_fmt_changed[j] = false;
Harsh Bansal28718c52017-04-20 13:47:12 +05301725 }
Harsh Bansal431eede2017-04-28 21:22:30 +05301726 qaf_mod->new_out_format_index = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05301727
1728 DEBUG_MSG("Session Closed.");
1729
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301730 return 0;
1731}
1732
Harsh Bansal28718c52017-04-20 13:47:12 +05301733/* Close the stream of QAF module. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301734static int qaf_stream_close(struct stream_out *out)
1735{
Harsh Bansal28718c52017-04-20 13:47:12 +05301736 int ret = -EINVAL;
1737 struct qaf_module *qaf_mod = NULL;
1738 int index = -1;
1739 DEBUG_MSG("Flag [0x%x], Stream handle [%p]", out->flags, out->qaf_stream_handle);
1740
1741 qaf_mod = get_qaf_module_for_input_stream(out);
1742 index = get_input_stream_index(out);
1743
1744 if (!qaf_mod || !qaf_mod->qaf_audio_stream_close || index < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301745 return -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301746 }
1747
1748 qaf_mod->stream_in[index] = NULL;
1749 memset(&qaf_mod->adsp_hdlr_config[index], 0, sizeof(struct qaf_adsp_hdlr_config_state));
1750
1751 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301752 if (out->qaf_stream_handle) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301753 ret = qaf_mod->qaf_audio_stream_close(out->qaf_stream_handle);
1754 out->qaf_stream_handle = NULL;
1755 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301756 unlock_output_stream(out);
1757
1758 //If all streams are closed then close the session.
1759 qaf_session_close(qaf_mod);
1760
1761 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301762 return ret;
1763}
1764
Harsh Bansal28718c52017-04-20 13:47:12 +05301765/* Open a MM module session with QAF. */
1766static int audio_extn_qaf_session_open(mm_module_type mod_type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301767{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301768 ALOGV("%s %d", __func__, __LINE__);
Harsh Bansal28718c52017-04-20 13:47:12 +05301769 unsigned char* license_data = NULL;
1770 device_license_config_t lic_config = {0};
1771 int ret = -ENOSYS, size = 0;
1772 char value[PROPERTY_VALUE_MAX] = {0};
1773 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301774
Harsh Bansal28718c52017-04-20 13:47:12 +05301775 if (mod_type >= MAX_MM_MODULE_TYPE || !(p_qaf->qaf_mod[mod_type].qaf_audio_session_open))
1776 return -ENOTSUP; //Not supported by QAF module.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301777
Harsh Bansal28718c52017-04-20 13:47:12 +05301778 pthread_mutex_lock(&p_qaf->lock);
1779
1780 qaf_mod = &(p_qaf->qaf_mod[mod_type]);
1781
1782 //If session is already opened then return.
1783 if (qaf_mod->session_handle) {
1784 DEBUG_MSG("Session is already opened.");
1785 pthread_mutex_unlock(&p_qaf->lock);
1786 return 0;
1787 }
1788
1789 if (mod_type == MS12) {
1790 //Getting the license
1791 license_data = platform_get_license((struct audio_hw_device *)(p_qaf->adev->platform),
1792 &size);
1793 if (!license_data) {
1794 ERROR_MSG("License data is not present.");
1795 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301796 return -EINVAL;
1797 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301798
1799 lic_config.p_license = (unsigned char*)calloc(1, size);
1800 if (lic_config.p_license == NULL) {
1801 ERROR_MSG("Out of Memory");
1802 ret = -ENOMEM;
1803 goto exit;
1804 }
1805
1806 lic_config.l_size = size;
1807 memcpy(lic_config.p_license, license_data, size);
1808
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001809 if (property_get("vendor.audio.qaf.manufacturer", value, "") && atoi(value)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301810 lic_config.manufacturer_id = (unsigned long)atoi(value);
1811 } else {
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001812 ERROR_MSG("vendor.audio.qaf.manufacturer id is not set");
Harsh Bansal28718c52017-04-20 13:47:12 +05301813 ret = -EINVAL;
1814 goto exit;
1815 }
1816 }
1817
1818 ret = qaf_mod->qaf_audio_session_open(&qaf_mod->session_handle,
1819 (void *)(qaf_mod),
1820 (void *)&lic_config);
1821 if (ret < 0) {
1822 ERROR_MSG("Error in session open %d", ret);
1823 goto exit;
1824 }
1825
1826 if (qaf_mod->session_handle == NULL) {
1827 ERROR_MSG("Session handle is NULL.");
1828 ret = -ENOMEM;
1829 goto exit;
1830 }
1831
1832 if (qaf_mod->qaf_register_event_callback)
1833 qaf_mod->qaf_register_event_callback(qaf_mod->session_handle,
1834 qaf_mod,
1835 &notify_event_callback,
Harsh Bansal431eede2017-04-28 21:22:30 +05301836 AUDIO_DATA_EVENT_V2);
Harsh Bansal28718c52017-04-20 13:47:12 +05301837
1838 set_hdmi_configuration_to_module();
1839
1840exit:
1841 if (license_data != NULL) {
1842 free(license_data);
1843 license_data = NULL;
1844 }
1845 if (lic_config.p_license != NULL) {
1846 free(lic_config.p_license);
1847 lic_config.p_license = NULL;
1848 }
1849
1850 pthread_mutex_unlock(&p_qaf->lock);
1851 return ret;
1852}
1853
1854/* opens a stream in QAF module. */
1855static int qaf_stream_open(struct stream_out *out,
1856 struct audio_config *config,
1857 audio_output_flags_t flags,
1858 audio_devices_t devices)
1859{
1860 int status = -EINVAL;
1861 mm_module_type mmtype = get_mm_module_for_format(config->format);
1862 struct qaf_module* qaf_mod = NULL;
1863 DEBUG_MSG("Flags 0x%x, Device 0x%x", flags, devices);
1864
1865 if (mmtype >= MAX_MM_MODULE_TYPE
1866 || p_qaf->qaf_mod[mmtype].qaf_audio_session_open == NULL
1867 || p_qaf->qaf_mod[mmtype].qaf_audio_stream_open == NULL) {
1868 ERROR_MSG("Unsupported Stream");
1869 return -ENOTSUP;
1870 }
1871
1872 //Open the module session, if not opened already.
1873 status = audio_extn_qaf_session_open(mmtype);
1874 qaf_mod = &(p_qaf->qaf_mod[mmtype]);
1875
1876 if ((status != 0) || (qaf_mod->session_handle == NULL)) {
1877 ERROR_MSG("Failed to open session.");
1878 return status;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301879 }
1880
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301881 audio_stream_config_t input_config;
1882 input_config.sample_rate = config->sample_rate;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301883 input_config.channels = popcount(config->channel_mask);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301884 input_config.format = config->format;
1885
Harsh Bansal28718c52017-04-20 13:47:12 +05301886 if (input_config.format != AUDIO_FORMAT_PCM_16_BIT) {
1887 input_config.format &= AUDIO_FORMAT_MAIN_MASK;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301888 }
1889
Harsh Bansal28718c52017-04-20 13:47:12 +05301890 DEBUG_MSG("stream_open sample_rate(%d) channels(%d) devices(%#x) flags(%#x) format(%#x)",
1891 input_config.sample_rate, input_config.channels, devices, flags, input_config.format);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301892
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301893 if (input_config.format == AUDIO_FORMAT_PCM_16_BIT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301894 //If PCM stream is already opened then fail this stream open.
1895 if (qaf_mod->stream_in[QAF_IN_PCM]) {
1896 ERROR_MSG("PCM input is already active.");
1897 return -ENOTSUP;
1898 }
1899
1900 //TODO: Flag can be system tone or external associated PCM.
1901 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle,
1902 &out->qaf_stream_handle,
1903 input_config,
1904 devices,
1905 AUDIO_STREAM_SYSTEM_TONE);
1906 qaf_mod->stream_in[QAF_IN_PCM] = out;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301907 } else if ((flags & AUDIO_OUTPUT_FLAG_MAIN) && (flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
1908 if (is_main_active(qaf_mod) || is_dual_main_active(qaf_mod)) {
1909 ERROR_MSG("Dual Main or Main already active. So, Cannot open main and associated stream");
1910 return -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301911 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301912 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN);
1913 if (status == 0) {
1914 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);
1915 qaf_mod->stream_in[QAF_IN_MAIN] = out;
1916 } else {
1917 ERROR_MSG("Stream Open FAILED !!!");
1918 }
1919 }
Naresh Tanniru908d9a02017-05-17 14:12:48 +05301920 } 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 +05301921 /* Assume Main if no flag is set */
1922 if (is_dual_main_active(qaf_mod)) {
1923 ERROR_MSG("Dual Main already active. So, Cannot open main stream");
1924 return -EINVAL;
1925 } else if (is_main_active(qaf_mod) && qaf_mod->stream_in[QAF_IN_ASSOC]) {
1926 ERROR_MSG("Main and Associated already active. So, Cannot open main stream");
1927 return -EINVAL;
1928 } else if (is_main_active(qaf_mod) && (mmtype != MS12)) {
1929 ERROR_MSG("Main already active and Not an MS12 format. So, Cannot open another main stream");
1930 return -EINVAL;
1931 } else {
1932 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN);
1933 if (status == 0) {
1934 DEBUG_MSG("Open stream for Input with only Main flag(%x) stream_handle(%p)", flags, out->qaf_stream_handle);
1935 if(qaf_mod->stream_in[QAF_IN_MAIN]) {
1936 qaf_mod->stream_in[QAF_IN_MAIN_2] = out;
1937 } else {
1938 qaf_mod->stream_in[QAF_IN_MAIN] = out;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301939 }
1940 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301941 ERROR_MSG("Stream Open FAILED !!!");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301942 }
1943 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301944 } else if ((flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
1945 if (is_dual_main_active(qaf_mod)) {
1946 ERROR_MSG("Dual Main already active. So, Cannot open associated stream");
1947 return -EINVAL;
1948 } else if (!is_main_active(qaf_mod)) {
1949 ERROR_MSG("Main not active. So, Cannot open associated stream");
1950 return -EINVAL;
1951 } else if (qaf_mod->stream_in[QAF_IN_ASSOC]) {
1952 ERROR_MSG("Associated already active. So, Cannot open associated stream");
1953 return -EINVAL;
1954 }
1955 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_ASSOCIATED);
1956 if (status == 0) {
1957 DEBUG_MSG("Open stream for Input with only Associated flag(%x) stream handle(%p)", flags, out->qaf_stream_handle);
1958 qaf_mod->stream_in[QAF_IN_ASSOC] = out;
1959 } else {
1960 ERROR_MSG("Stream Open FAILED !!!");
1961 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301962 }
1963
Harsh Bansal28718c52017-04-20 13:47:12 +05301964 if (status != 0) {
1965 //If no stream is active then close the session.
1966 qaf_session_close(qaf_mod);
1967 return status;
1968 }
1969
1970 //If Device is HDMI, QAF passthrough is enabled and there is no previous QAF passthrough input stream.
1971 if ((!p_qaf->passthrough_in)
1972 && (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1973 && audio_extn_qaf_passthrough_enabled(out)) {
1974 //Assign the QAF passthrough input stream.
1975 p_qaf->passthrough_in = out;
1976
1977 //If HDMI is connected and format is supported by HDMI then create QAF passthrough output stream.
1978 if (p_qaf->hdmi_connect
1979 && platform_is_edid_supported_format(p_qaf->adev->platform, out->format)) {
1980 status = create_qaf_passthrough_stream();
1981 if (status < 0) {
1982 qaf_stream_close(out);
1983 ERROR_MSG("QAF passthrough stream creation failed with error %d", status);
1984 return status;
1985 }
1986 }
1987 /*Else: since QAF passthrough input stream is already initialized,
1988 * when hdmi is connected
1989 * then qaf passthrough output stream will be created.
1990 */
1991 }
1992
1993 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301994 return status;
1995}
1996
Harsh Bansal28718c52017-04-20 13:47:12 +05301997/* Resume a QAF stream. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301998static int qaf_out_resume(struct audio_stream_out* stream)
1999{
2000 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05302001 int status = 0;
2002 DEBUG_MSG("Output Stream %p", out);
2003
2004
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302005 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05302006
2007 //If QAF passthrough is active then block the resume on module input streams.
2008 if (p_qaf->passthrough_out) {
2009 //If resume is received for the QAF passthrough stream then call the primary HAL api.
2010 pthread_mutex_lock(&p_qaf->lock);
2011 if (p_qaf->passthrough_in == out) {
2012 status = p_qaf->passthrough_out->stream.resume(
2013 (struct audio_stream_out*)p_qaf->passthrough_out);
2014 if (!status) out->offload_state = OFFLOAD_STATE_PLAYING;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302015 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302016 pthread_mutex_unlock(&p_qaf->lock);
2017 } else {
2018 //Flush the module input stream.
2019 status = qaf_stream_start(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302020 }
2021
Harsh Bansal28718c52017-04-20 13:47:12 +05302022 unlock_output_stream(out);
2023
2024 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302025 return status;
2026}
2027
Harsh Bansal28718c52017-04-20 13:47:12 +05302028/* Offload thread for QAF output streams. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302029static void *qaf_offload_thread_loop(void *context)
2030{
Harsh Bansal28718c52017-04-20 13:47:12 +05302031 struct stream_out *out = (struct stream_out *)context;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302032 struct listnode *item;
2033 int ret = 0;
2034 struct str_parms *parms = NULL;
2035 int value = 0;
2036 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05302037 struct qaf_module *qaf_mod = NULL;
2038
2039 qaf_mod = get_qaf_module_for_input_stream(out);
2040
2041 if (!qaf_mod) return NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302042
2043 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
2044 set_sched_policy(0, SP_FOREGROUND);
2045 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
2046
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302047 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05302048
2049 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302050 for (;;) {
2051 struct offload_cmd *cmd = NULL;
2052 stream_callback_event_t event;
2053 bool send_callback = false;
2054
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302055 DEBUG_MSG_VV("List Empty %d (1:TRUE, 0:FALSE)", list_empty(&out->qaf_offload_cmd_list));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302056 if (list_empty(&out->qaf_offload_cmd_list)) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302057 DEBUG_MSG_VV("SLEEPING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302058 pthread_cond_wait(&out->qaf_offload_cond, &out->lock);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302059 DEBUG_MSG_VV("RUNNING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302060 continue;
2061 }
2062
2063 item = list_head(&out->qaf_offload_cmd_list);
2064 cmd = node_to_item(item, struct offload_cmd, node);
2065 list_remove(item);
2066
2067 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
2068 free(cmd);
2069 break;
2070 }
2071
Harsh Bansal28718c52017-04-20 13:47:12 +05302072 unlock_output_stream(out);
2073
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302074 send_callback = false;
Harsh Bansal28718c52017-04-20 13:47:12 +05302075 switch (cmd->cmd) {
2076 case OFFLOAD_CMD_WAIT_FOR_BUFFER: {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302077 DEBUG_MSG_VV("wait for buffer availability");
Harsh Bansal28718c52017-04-20 13:47:12 +05302078
2079 while (1) {
2080 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle,
2081 "buf_available");
2082 if (kvpairs) {
2083 parms = str_parms_create_str(kvpairs);
2084 ret = str_parms_get_int(parms, "buf_available", &value);
2085 if (ret >= 0) {
2086 if (value >= (int)out->compr_config.fragment_size) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302087 DEBUG_MSG_VV("buffer available");
Harsh Bansal28718c52017-04-20 13:47:12 +05302088 str_parms_destroy(parms);
2089 parms = NULL;
2090 break;
2091 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302092 DEBUG_MSG_VV("sleep");
Harsh Bansal28718c52017-04-20 13:47:12 +05302093 str_parms_destroy(parms);
2094 parms = NULL;
2095 usleep(10000);
2096 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302097 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302098 free(kvpairs);
2099 kvpairs = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302100 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302101 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302102 send_callback = true;
2103 event = STREAM_CBK_EVENT_WRITE_READY;
2104 break;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302105 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302106 default:
2107 DEBUG_MSG("unknown command received: %d", cmd->cmd);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302108 break;
2109 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302110
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302111 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05302112
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002113 if (send_callback && out->client_callback) {
2114 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302115 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302116
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302117 free(cmd);
2118 }
2119
2120 while (!list_empty(&out->qaf_offload_cmd_list)) {
2121 item = list_head(&out->qaf_offload_cmd_list);
2122 list_remove(item);
Harsh Bansal28718c52017-04-20 13:47:12 +05302123 free (node_to_item( item, struct offload_cmd, node));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302124 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302125 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302126
2127 return NULL;
2128}
2129
Harsh Bansal28718c52017-04-20 13:47:12 +05302130/* Create the offload callback thread for QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302131static int qaf_create_offload_callback_thread(struct stream_out *out)
2132{
Harsh Bansal28718c52017-04-20 13:47:12 +05302133 DEBUG_MSG("Output Stream %p", out);
2134 lock_output_stream(out);
2135 pthread_cond_init(&out->qaf_offload_cond, (const pthread_condattr_t *)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302136 list_init(&out->qaf_offload_cmd_list);
Harsh Bansal28718c52017-04-20 13:47:12 +05302137 pthread_create(&out->qaf_offload_thread,
2138 (const pthread_attr_t *)NULL,
2139 qaf_offload_thread_loop,
2140 out);
2141 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302142 return 0;
2143}
2144
Harsh Bansal28718c52017-04-20 13:47:12 +05302145/* Destroy the offload callback thread of QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302146static int qaf_destroy_offload_callback_thread(struct stream_out *out)
2147{
Harsh Bansal28718c52017-04-20 13:47:12 +05302148 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302149 qaf_send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302150
Harsh Bansal28718c52017-04-20 13:47:12 +05302151 pthread_join(out->qaf_offload_thread, (void **)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302152 pthread_cond_destroy(&out->qaf_offload_cond);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302153 return 0;
2154}
2155
Harsh Bansal28718c52017-04-20 13:47:12 +05302156/* Sets the stream set parameters (device routing information). */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302157static int qaf_out_set_parameters(struct audio_stream *stream, const char *kvpairs)
2158{
2159 struct str_parms *parms, *new_parms;
2160 char value[32];
2161 char *new_kv_pairs;
2162 int val = 0;
2163 struct stream_out *out = (struct stream_out *)stream;
2164 int ret = 0;
2165 int err = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05302166 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302167
Harsh Bansal28718c52017-04-20 13:47:12 +05302168 DEBUG_MSG("usecase(%d: %s) kvpairs: %s", out->usecase, use_case_table[out->usecase], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302169
Harsh Bansal28718c52017-04-20 13:47:12 +05302170 parms = str_parms_create_str(kvpairs);
2171 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2172 if (err < 0)
2173 return err;
2174 val = atoi(value);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302175
Harsh Bansal28718c52017-04-20 13:47:12 +05302176 qaf_mod = get_qaf_module_for_input_stream(out);
2177 if (!qaf_mod) return (-EINVAL);
2178
2179 //TODO: HDMI is connected but user doesn't want HDMI output, close both HDMI outputs.
2180
2181 /* Setting new device information to the mm module input streams.
2182 * This is needed if QAF module output streams are not created yet.
2183 */
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302184 out->devices = val;
Harsh Bansal28718c52017-04-20 13:47:12 +05302185
2186 if (val == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
2187 //If device is BT then open the BT stream if not already opened.
2188 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) == NULL
2189 && audio_extn_bt_hal_get_device(qaf_mod->bt_hdl) != NULL) {
2190 ret = audio_extn_bt_hal_open_output_stream(qaf_mod->bt_hdl,
2191 QAF_OUTPUT_SAMPLING_RATE,
2192 AUDIO_CHANNEL_OUT_STEREO,
2193 CODEC_BACKEND_DEFAULT_BIT_WIDTH);
2194 if (ret != 0) {
2195 ERROR_MSG("BT Output stream open failure!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302196 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302197 }
2198 } else if (val != 0) {
2199 //If device is not BT then close the BT stream if already opened.
2200 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
2201 audio_extn_bt_hal_close_output_stream(qaf_mod->bt_hdl);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302202 }
2203 }
2204
Harsh Bansal28718c52017-04-20 13:47:12 +05302205 if (p_qaf->passthrough_in == out) { //Device routing is received for QAF passthrough stream.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302206
Harsh Bansal28718c52017-04-20 13:47:12 +05302207 if (!(val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) { //HDMI route is disabled.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302208
Harsh Bansal28718c52017-04-20 13:47:12 +05302209 //If QAF pasthrough output is enabled. Close it.
2210 close_qaf_passthrough_stream();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302211
Harsh Bansal28718c52017-04-20 13:47:12 +05302212 //Send the routing information to mm module pcm output.
2213 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2214 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2215 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302216 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302217 //else: device info is updated in the input streams.
2218 } else { //HDMI route is enabled.
2219
2220 //create the QAf passthrough stream, if not created already.
2221 ret = create_qaf_passthrough_stream();
2222
2223 if (p_qaf->passthrough_out != NULL) { //If QAF passthrough out is enabled then send routing information.
2224 ret = p_qaf->passthrough_out->stream.common.set_parameters(
2225 (struct audio_stream *)p_qaf->passthrough_out, kvpairs);
2226 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302227 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302228 } else {
2229 //Send the routing information to mm module pcm output.
2230 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2231 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2232 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
2233 }
2234 //else: device info is updated in the input streams.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302235 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302236 str_parms_destroy(parms);
2237
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002238 return ret;
2239}
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302240
Harsh Bansal28718c52017-04-20 13:47:12 +05302241/* Checks if a stream is QAF stream or not. */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002242bool audio_extn_is_qaf_stream(struct stream_out *out)
2243{
Harsh Bansal28718c52017-04-20 13:47:12 +05302244 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002245
Harsh Bansal28718c52017-04-20 13:47:12 +05302246 if (qaf_mod) {
2247 return true;
2248 }
2249 return false;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002250}
2251
2252/* API to send playback stream specific config parameters */
2253int audio_extn_qaf_out_set_param_data(struct stream_out *out,
Harsh Bansal28718c52017-04-20 13:47:12 +05302254 audio_extn_param_id param_id,
2255 audio_extn_param_payload *payload)
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002256{
2257 int ret = -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05302258 int index;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002259 struct stream_out *new_out = NULL;
2260 struct audio_adsp_event *adsp_event;
Harsh Bansal28718c52017-04-20 13:47:12 +05302261 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002262
Harsh Bansal28718c52017-04-20 13:47:12 +05302263 if (!out || !qaf_mod || !payload) {
2264 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002265 return ret;
2266 }
2267
2268 /* In qaf output render session may not be opened at this time.
Harsh Bansal28718c52017-04-20 13:47:12 +05302269 to handle it store adsp_hdlr param info so that it can be
2270 applied later after opening render session from ms12 callback
2271 */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002272 if (param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302273 index = get_input_stream_index(out);
2274 if (index < 0) {
2275 ERROR_MSG("Invalid stream");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002276 return ret;
2277 }
2278 adsp_event = (struct audio_adsp_event *)payload;
2279
Harsh Bansal28718c52017-04-20 13:47:12 +05302280 if (payload->adsp_event_params.payload_length <= AUDIO_MAX_ADSP_STREAM_CMD_PAYLOAD_LEN) {
2281 pthread_mutex_lock(&p_qaf->lock);
2282 memcpy(qaf_mod->adsp_hdlr_config[index].event_payload,
2283 adsp_event->payload,
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002284 adsp_event->payload_length);
Harsh Bansal28718c52017-04-20 13:47:12 +05302285 qaf_mod->adsp_hdlr_config[index].event_params.payload =
2286 qaf_mod->adsp_hdlr_config[index].event_payload;
2287 qaf_mod->adsp_hdlr_config[index].event_params.payload_length =
2288 adsp_event->payload_length;
2289 qaf_mod->adsp_hdlr_config[index].adsp_hdlr_config_valid = true;
2290 pthread_mutex_unlock(&p_qaf->lock);
2291 } else {
2292 ERROR_MSG("Invalid adsp event length %d", adsp_event->payload_length);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002293 return ret;
2294 }
2295 ret = 0;
2296 }
2297
Harsh Bansal28718c52017-04-20 13:47:12 +05302298 /* apply param for all active out sessions */
2299 for (index = 0; index < MAX_QAF_MODULE_OUT; index++) {
2300 new_out = qaf_mod->stream_out[index];
2301 if (!new_out) continue;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002302
Harsh Bansal28718c52017-04-20 13:47:12 +05302303 /*ADSP event is not supported for passthrough*/
2304 if ((param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD)
2305 && !(new_out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) continue;
2306 if (new_out->standby)
2307 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
2308 lock_output_stream(new_out);
2309 ret = audio_extn_out_set_param_data(new_out, param_id, payload);
2310 if (ret)
2311 ERROR_MSG("audio_extn_out_set_param_data error %d", ret);
2312 unlock_output_stream(new_out);
2313 }
2314 return ret;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002315}
2316
2317int audio_extn_qaf_out_get_param_data(struct stream_out *out,
2318 audio_extn_param_id param_id,
2319 audio_extn_param_payload *payload)
2320{
Harsh Bansal28718c52017-04-20 13:47:12 +05302321 int ret = -EINVAL, i;
2322 struct stream_out *new_out;
2323 struct audio_usecase *uc_info;
2324 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002325
Harsh Bansal28718c52017-04-20 13:47:12 +05302326 if (!out || !qaf_mod || !payload) {
2327 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002328 return ret;
2329 }
2330
Harsh Bansal28718c52017-04-20 13:47:12 +05302331 if (!p_qaf->hdmi_connect) {
2332 ERROR_MSG("hdmi not connected");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002333 return ret;
2334 }
2335
2336 /* get session which is routed to hdmi*/
Harsh Bansal28718c52017-04-20 13:47:12 +05302337 if (p_qaf->passthrough_out)
2338 new_out = p_qaf->passthrough_out;
2339 else {
2340 for (i = 0; i < MAX_QAF_MODULE_OUT; i++) {
2341 if (qaf_mod->stream_out[i]) {
2342 new_out = qaf_mod->stream_out[i];
2343 break;
2344 }
2345 }
2346 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002347
2348 if (!new_out) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302349 ERROR_MSG("No stream active.");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002350 return ret;
2351 }
2352
2353 if (new_out->standby)
Harsh Bansal28718c52017-04-20 13:47:12 +05302354 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002355
2356 lock_output_stream(new_out);
2357 ret = audio_extn_out_get_param_data(new_out, param_id, payload);
Harsh Bansal28718c52017-04-20 13:47:12 +05302358 if (ret)
2359 ERROR_MSG("audio_extn_out_get_param_data error %d", ret);
2360 unlock_output_stream(new_out);
2361
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302362 return ret;
2363}
2364
Harsh Bansal28718c52017-04-20 13:47:12 +05302365/* To open a stream with QAF. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302366int audio_extn_qaf_open_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302367 audio_io_handle_t handle,
2368 audio_devices_t devices,
2369 audio_output_flags_t flags,
2370 struct audio_config *config,
2371 struct audio_stream_out **stream_out,
2372 const char *address)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302373{
2374 int ret = 0;
2375 struct stream_out *out;
2376
2377 ret = adev_open_output_stream(dev, handle, devices, flags, config, stream_out, address);
2378 if (*stream_out == NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302379 ERROR_MSG("Stream open failed %d", ret);
Deepak Agarwal1e42b852017-02-11 17:57:04 +05302380 return ret;
2381 }
2382
Harsh Bansal28718c52017-04-20 13:47:12 +05302383 out = (struct stream_out *)*stream_out;
2384
2385 ret = qaf_stream_open(out, config, flags, devices);
2386 if (ret == -ENOTSUP) {
2387 //Stream not supported by QAF, Bypass QAF.
2388 return 0;
2389 } else if (ret < 0) {
2390 ERROR_MSG("Error opening QAF stream err[%d]!", ret);
2391 adev_close_output_stream(dev, *stream_out);
2392 return ret;
2393 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302394
2395 /* Override function pointers based on qaf definitions */
2396 out->stream.set_volume = qaf_out_set_volume;
2397 out->stream.pause = qaf_out_pause;
2398 out->stream.resume = qaf_out_resume;
2399 out->stream.drain = qaf_out_drain;
2400 out->stream.flush = qaf_out_flush;
2401
2402 out->stream.common.standby = qaf_out_standby;
2403 out->stream.common.set_parameters = qaf_out_set_parameters;
2404 out->stream.get_latency = qaf_out_get_latency;
2405 out->stream.write = qaf_out_write;
2406 out->stream.get_presentation_position = qaf_out_get_presentation_position;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302407 out->platform_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302408
Harsh Bansal28718c52017-04-20 13:47:12 +05302409 /*TODO: Need to handle this for DTS*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302410 if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) {
2411 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
Varun B34da7a42017-02-13 16:16:53 +05302412 out->config.period_size = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302413 out->config.period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT;
Varun B34da7a42017-02-13 16:16:53 +05302414 out->config.start_threshold = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
2415 out->config.avail_min = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302416 } else if(out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
2417 out->compr_config.fragment_size = qaf_get_pcm_offload_input_buffer_size(&(config->offload_info));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302418 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302419
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302420 *stream_out = &out->stream;
2421 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2422 qaf_create_offload_callback_thread(out);
2423 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302424
2425 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302426 return 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302427}
2428
Harsh Bansal28718c52017-04-20 13:47:12 +05302429/* Close a QAF stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302430void audio_extn_qaf_close_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302431 struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302432{
2433 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05302434 struct qaf_module* qaf_mod = get_qaf_module_for_input_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302435
Harsh Bansal28718c52017-04-20 13:47:12 +05302436 if (!qaf_mod) return;
2437
2438 DEBUG_MSG("stream_handle(%p) format = %x", out, out->format);
2439
2440 //If close is received for QAF passthrough stream then close the QAF passthrough output.
2441 if (p_qaf->passthrough_in == out) {
2442 if (p_qaf->passthrough_out) {
2443 ALOGD("%s %d closing stream handle %p", __func__, __LINE__, p_qaf->passthrough_out);
2444 pthread_mutex_lock(&p_qaf->lock);
2445 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2446 (struct audio_stream_out *)(p_qaf->passthrough_out));
2447 pthread_mutex_unlock(&p_qaf->lock);
2448 p_qaf->passthrough_out = NULL;
2449 }
2450
2451 p_qaf->passthrough_in = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302452 }
2453
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302454 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2455 qaf_destroy_offload_callback_thread(out);
2456 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002457
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302458 qaf_stream_close(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302459
2460 adev_close_output_stream(dev, stream);
Harsh Bansal28718c52017-04-20 13:47:12 +05302461
2462 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302463}
2464
Harsh Bansal28718c52017-04-20 13:47:12 +05302465/* Check if QAF is supported or not. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302466bool audio_extn_qaf_is_enabled()
2467{
2468 bool prop_enabled = false;
2469 char value[PROPERTY_VALUE_MAX] = {0};
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002470 property_get("vendor.audio.qaf.enabled", value, NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302471 prop_enabled = atoi(value) || !strncmp("true", value, 4);
2472 return (prop_enabled);
2473}
2474
Harsh Bansal28718c52017-04-20 13:47:12 +05302475/* Query HDMI EDID and sets module output accordingly.*/
2476void set_hdmi_configuration_to_module()
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302477{
Harsh Bansal28718c52017-04-20 13:47:12 +05302478 int channels = 0;
2479 char *format_params;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302480 struct str_parms *qaf_params;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302481 char prop_value[PROPERTY_VALUE_MAX];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302482 bool passth_support = false;
2483
Harsh Bansal28718c52017-04-20 13:47:12 +05302484 DEBUG_MSG("Entry");
2485
2486 if (!p_qaf) {
2487 return;
2488 }
2489
2490 if (!p_qaf->hdmi_connect) {
2491 DEBUG_MSG("HDMI is not connected.");
2492 return;
2493 }
2494
2495 p_qaf->hdmi_sink_channels = 0;
2496
2497 //QAF re-encoding and DSP offload passthrough is supported.
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002498 if (property_get_bool("vendor.audio.offload.passthrough", false)
2499 && property_get_bool("vendor.audio.qaf.reencode", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302500
2501 //If MS12 session is active.
2502 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2503
2504 bool do_setparam = false;
2505 qaf_params = str_parms_create();
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002506 property_get("vendor.audio.qaf.hdmi.out", prop_value, NULL);
Harsh Bansal28718c52017-04-20 13:47:12 +05302507
2508 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_E_AC3)
2509 && (strncmp(prop_value, "ddp", 3) == 0)) {
2510 do_setparam = true;
2511 if (qaf_params) {
2512 str_parms_add_str(qaf_params,
2513 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2514 AUDIO_QAF_PARAMETER_VALUE_REENCODE_EAC3);
2515 }
2516 } else if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_AC3)) {
2517 do_setparam = true;
2518 if (qaf_params) {
2519 str_parms_add_str(qaf_params,
2520 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2521 AUDIO_QAF_PARAMETER_VALUE_REENCODE_AC3);
2522 }
2523 }
2524
2525 if (do_setparam) {
2526 if (p_qaf->qaf_msmd_enabled) {
2527 str_parms_add_str(qaf_params,
2528 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2529 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2530 } else {
2531 str_parms_add_str(qaf_params,
2532 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2533 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2534 }
2535 format_params = str_parms_to_str(qaf_params);
2536
2537 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2538 format_params);
2539
2540 passth_support = true;
2541 }
2542 str_parms_destroy(qaf_params);
2543 }
2544
2545 //DTS_M8 session is active.
2546 if (p_qaf->qaf_mod[DTS_M8].session_handle
2547 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2548
2549 bool do_setparam = false;
2550 qaf_params = str_parms_create();
Harsh Bansal28718c52017-04-20 13:47:12 +05302551 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_DTS)) {
2552 do_setparam = true;
2553 if (qaf_params) {
2554 str_parms_add_str(qaf_params,
2555 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2556 AUDIO_QAF_PARAMETER_VALUE_REENCODE_DTS);
2557 }
2558 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302559
2560 if (do_setparam) {
2561 if (p_qaf->qaf_msmd_enabled) {
2562 str_parms_add_str(qaf_params,
2563 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2564 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2565 } else {
2566 str_parms_add_str(qaf_params,
2567 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2568 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2569 }
2570 format_params = str_parms_to_str(qaf_params);
2571
2572 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2573 format_params);
2574
2575 passth_support = true;
2576 }
2577 str_parms_destroy(qaf_params);
2578 }
2579 }
2580
2581 //Compressed passthrough is not enabled.
2582 if (!passth_support) {
2583
2584 channels = platform_edid_get_max_channels(p_qaf->adev->platform);
2585
2586 qaf_params = str_parms_create();
Harsh Bansal431eede2017-04-28 21:22:30 +05302587
2588 str_parms_add_str(qaf_params,
2589 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2590 AUDIO_QAF_PARAMETER_VALUE_PCM);
Harsh Bansal28718c52017-04-20 13:47:12 +05302591 switch (channels) {
2592 case 8:
2593 DEBUG_MSG("Switching Qaf output to 7.1 channels");
2594 str_parms_add_str(qaf_params,
2595 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2596 AUDIO_QAF_PARAMETER_VALUE_8_CHANNELS);
2597 if (p_qaf->qaf_msmd_enabled) {
2598 str_parms_add_str(qaf_params,
2599 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2600 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2601 } else {
2602 str_parms_add_str(qaf_params,
2603 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2604 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2605 }
2606 p_qaf->hdmi_sink_channels = channels;
2607 break;
2608 case 6:
2609 DEBUG_MSG("Switching Qaf output to 5.1 channels");
2610 str_parms_add_str(qaf_params,
2611 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2612 AUDIO_QAF_PARAMETER_VALUE_6_CHANNELS);
2613 if (p_qaf->qaf_msmd_enabled) {
2614 str_parms_add_str(qaf_params,
2615 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2616 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2617 } else {
2618 str_parms_add_str(qaf_params,
2619 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2620 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2621 }
2622 p_qaf->hdmi_sink_channels = channels;
2623 break;
2624 default:
2625 DEBUG_MSG("Switching Qaf output to default channels");
2626 str_parms_add_str(qaf_params,
2627 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2628 AUDIO_QAF_PARAMETER_VALUE_DEFAULT_CHANNELS);
2629 if (p_qaf->qaf_msmd_enabled) {
2630 str_parms_add_str(qaf_params,
2631 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2632 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2633 } else {
2634 str_parms_add_str(qaf_params,
2635 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2636 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2637 }
2638 p_qaf->hdmi_sink_channels = 2;
2639 break;
2640 }
2641
2642 format_params = str_parms_to_str(qaf_params);
2643
2644 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2645 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2646 format_params);
2647 }
2648 if (p_qaf->qaf_mod[DTS_M8].session_handle
2649 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2650 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2651 format_params);
2652 }
2653
2654 str_parms_destroy(qaf_params);
2655 }
2656 DEBUG_MSG("Exit");
2657}
2658
2659/* QAF set parameter function. For Device connect and disconnect. */
2660int audio_extn_qaf_set_parameters(struct audio_device *adev, struct str_parms *parms)
2661{
2662 int status = 0, val = 0, k;
2663 char *format_params, *kv_parirs;
2664 struct str_parms *qaf_params;
2665 char value[32];
2666
2667 DEBUG_MSG("Entry");
2668
2669 if (!p_qaf) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302670 return -EINVAL;
2671 }
2672
2673 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
Harsh Bansal28718c52017-04-20 13:47:12 +05302674
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302675 if (status >= 0) {
2676 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302677 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is connected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302678
Harsh Bansal28718c52017-04-20 13:47:12 +05302679 p_qaf->hdmi_connect = 1;
2680 p_qaf->hdmi_sink_channels = 0;
2681
2682 if (p_qaf->passthrough_in) { //If QAF passthrough is already initialized.
2683 lock_output_stream(p_qaf->passthrough_in);
2684 if (platform_is_edid_supported_format(adev->platform,
2685 p_qaf->passthrough_in->format)) {
2686 //If passthrough format is supported by HDMI then create the QAF passthrough output if not created already.
2687 create_qaf_passthrough_stream();
2688 //Ignoring the returned error, If error then QAF passthrough is disabled.
2689 } else {
2690 //If passthrough format is not supported by HDMI then close the QAF passthrough output if already created.
2691 close_qaf_passthrough_stream();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302692 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302693 unlock_output_stream(p_qaf->passthrough_in);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302694 }
2695
Harsh Bansal28718c52017-04-20 13:47:12 +05302696 set_hdmi_configuration_to_module();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302697
Bharath Gopal01310bb2016-12-05 15:39:32 +05302698 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302699 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2700 if (!p_qaf->qaf_mod[k].bt_hdl) {
2701 DEBUG_MSG("Opening a2dp output...");
2702 status = audio_extn_bt_hal_load(&p_qaf->qaf_mod[k].bt_hdl);
2703 if (status != 0) {
2704 ERROR_MSG("Error opening BT module");
2705 return status;
2706 }
2707 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05302708 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302709 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302710 //TODO else if: Need to consider other devices.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302711 }
2712
2713 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
2714 if (status >= 0) {
2715 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302716 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is disconnected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302717
Harsh Bansal28718c52017-04-20 13:47:12 +05302718 qaf_params = str_parms_create();
2719 str_parms_add_str(qaf_params,
2720 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2721 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2722 str_parms_add_str(qaf_params,
2723 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2724 AUDIO_QAF_PARAMETER_VALUE_PCM);
2725 p_qaf->hdmi_sink_channels = 0;
2726
2727 p_qaf->passthrough_enabled = 0;
2728 p_qaf->mch_pcm_hdmi_enabled = 0;
2729 p_qaf->hdmi_connect = 0;
2730
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302731 format_params = str_parms_to_str(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302732
2733 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2734 if (p_qaf->qaf_mod[k].session_handle
2735 && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2736 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2737 p_qaf->qaf_mod[k].session_handle, format_params);
2738 }
2739 }
2740 close_all_hdmi_output();
2741
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302742 str_parms_destroy(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302743 close_qaf_passthrough_stream();
Bharath Gopal01310bb2016-12-05 15:39:32 +05302744 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302745 DEBUG_MSG("Closing a2dp output...");
2746 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2747 if (p_qaf->qaf_mod[k].bt_hdl) {
2748 audio_extn_bt_hal_unload(p_qaf->qaf_mod[k].bt_hdl);
2749 p_qaf->qaf_mod[k].bt_hdl = NULL;
2750 }
2751 }
2752 }
2753 //TODO else if: Need to consider other devices.
2754 }
2755
2756 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2757 kv_parirs = str_parms_to_str(parms);
2758 if (p_qaf->qaf_mod[k].session_handle && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2759 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2760 p_qaf->qaf_mod[k].session_handle, kv_parirs);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302761 }
2762 }
2763
Harsh Bansal28718c52017-04-20 13:47:12 +05302764 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302765 return status;
2766}
2767
Harsh Bansal28718c52017-04-20 13:47:12 +05302768/* Create the QAF. */
2769int audio_extn_qaf_init(struct audio_device *adev)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302770{
Harsh Bansal28718c52017-04-20 13:47:12 +05302771 DEBUG_MSG("Entry");
2772
2773 p_qaf = calloc(1, sizeof(struct qaf));
2774 if (p_qaf == NULL) {
2775 ERROR_MSG("Out of memory");
2776 return -ENOMEM;
2777 }
2778
2779 p_qaf->adev = adev;
2780
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002781 if (property_get_bool("vendor.audio.qaf.msmd", false)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302782 p_qaf->qaf_msmd_enabled = 1;
2783 }
2784 pthread_mutex_init(&p_qaf->lock, (const pthread_mutexattr_t *) NULL);
2785
2786 int i = 0;
2787
2788 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2789 char value[PROPERTY_VALUE_MAX] = {0};
2790 char lib_name[PROPERTY_VALUE_MAX] = {0};
2791 struct qaf_module *qaf_mod = &(p_qaf->qaf_mod[i]);
2792
2793 if (i == MS12) {
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002794 property_get("vendor.audio.qaf.library", value, NULL);
Harsh Bansal28718c52017-04-20 13:47:12 +05302795 } else if (i == DTS_M8) {
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07002796 property_get("vendor.audio.qaf.m8.library", value, NULL);
Harsh Bansal28718c52017-04-20 13:47:12 +05302797 } else {
2798 continue;
2799 }
2800
2801 snprintf(lib_name, PROPERTY_VALUE_MAX, "%s", value);
2802
2803 qaf_mod->qaf_lib = dlopen(lib_name, RTLD_NOW);
2804 if (qaf_mod->qaf_lib == NULL) {
2805 ERROR_MSG("DLOPEN failed for %s", lib_name);
2806 continue;
2807 }
2808
2809 DEBUG_MSG("DLOPEN successful for %s", lib_name);
2810 qaf_mod->qaf_audio_session_open =
2811 (int (*)(audio_session_handle_t* session_handle, void *p_data, void* license_data))dlsym(qaf_mod->qaf_lib,
2812 "audio_session_open");
2813 qaf_mod->qaf_audio_session_close =
2814 (int (*)(audio_session_handle_t session_handle))dlsym(qaf_mod->qaf_lib,
2815 "audio_session_close");
2816 qaf_mod->qaf_audio_stream_open =
2817 (int (*)(audio_session_handle_t session_handle, audio_stream_handle_t* stream_handle,
2818 audio_stream_config_t input_config, audio_devices_t devices, stream_type_t flags))dlsym(qaf_mod->qaf_lib,
2819 "audio_stream_open");
2820 qaf_mod->qaf_audio_stream_close =
2821 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2822 "audio_stream_close");
2823 qaf_mod->qaf_audio_stream_set_param =
2824 (int (*)(audio_stream_handle_t stream_handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2825 "audio_stream_set_param");
2826 qaf_mod->qaf_audio_session_set_param =
2827 (int (*)(audio_session_handle_t handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2828 "audio_session_set_param");
2829 qaf_mod->qaf_audio_stream_get_param =
2830 (char* (*)(audio_stream_handle_t stream_handle, const char* key))dlsym(qaf_mod->qaf_lib,
2831 "audio_stream_get_param");
2832 qaf_mod->qaf_audio_session_get_param =
2833 (char* (*)(audio_session_handle_t handle, const char* key))dlsym(qaf_mod->qaf_lib,
2834 "audio_session_get_param");
2835 qaf_mod->qaf_audio_stream_start =
2836 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2837 "audio_stream_start");
2838 qaf_mod->qaf_audio_stream_stop =
2839 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2840 "audio_stream_stop");
2841 qaf_mod->qaf_audio_stream_pause =
2842 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2843 "audio_stream_pause");
2844 qaf_mod->qaf_audio_stream_flush =
2845 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2846 "audio_stream_flush");
2847 qaf_mod->qaf_audio_stream_write =
2848 (int (*)(audio_stream_handle_t stream_handle, const void* buf, int size))dlsym(qaf_mod->qaf_lib,
2849 "audio_stream_write");
2850 qaf_mod->qaf_register_event_callback =
2851 (void (*)(audio_session_handle_t session_handle, void *priv_data, notify_event_callback_t event_callback,
2852 audio_event_id_t event_id))dlsym(qaf_mod->qaf_lib,
2853 "register_event_callback");
2854 }
2855
2856 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302857 return 0;
2858}
2859
Harsh Bansal28718c52017-04-20 13:47:12 +05302860/* Tear down the qaf extension. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302861void audio_extn_qaf_deinit()
2862{
Harsh Bansal28718c52017-04-20 13:47:12 +05302863 int i;
2864 DEBUG_MSG("Entry");
2865
2866 if (p_qaf != NULL) {
2867 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2868 qaf_session_close(&p_qaf->qaf_mod[i]);
2869
2870 if (p_qaf->qaf_mod[i].qaf_lib != NULL) {
2871 dlclose(p_qaf->qaf_mod[i].qaf_lib);
2872 p_qaf->qaf_mod[i].qaf_lib = NULL;
2873 }
2874 }
2875 if (p_qaf->passthrough_out) {
2876 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2877 (struct audio_stream_out *)(p_qaf->passthrough_out));
2878 p_qaf->passthrough_out = NULL;
2879 }
2880
2881 pthread_mutex_destroy(&p_qaf->lock);
2882 free(p_qaf);
2883 p_qaf = NULL;
2884 }
2885 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302886}