blob: 21cf2da9c4d90a2cb41eb0014ad277989ce8a9e9 [file] [log] [blame]
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301/*
Varun B34da7a42017-02-13 16:16:53 +05302 * Copyright (c) 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
Harsh Bansal28718c52017-04-20 13:47:12 +0530120//TODO: Need to remove this.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530121#define QAF_OUTPUT_SAMPLING_RATE 48000
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530122
123#ifdef QAF_DUMP_ENABLED
124FILE *fp_output_writer_hdmi = NULL;
125#endif
126
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800127struct qaf_adsp_hdlr_config_state {
128 struct audio_adsp_event event_params;
129 /* For holding client audio_adsp_event payload */
130 uint8_t event_payload[AUDIO_MAX_ADSP_STREAM_CMD_PAYLOAD_LEN];
131 bool adsp_hdlr_config_valid;
132};
133
Harsh Bansal28718c52017-04-20 13:47:12 +0530134//Types of MM module, currently supported by QAF.
135typedef enum {
136 MS12,
137 DTS_M8,
138 MAX_MM_MODULE_TYPE,
139 INVALID_MM_MODULE
140} mm_module_type;
141
142typedef enum {
143 QAF_OUT_TRANSCODE_PASSTHROUGH = 0, /* Transcode passthrough via MM module*/
144 QAF_OUT_OFFLOAD_MCH, /* Multi-channel PCM offload*/
145 QAF_OUT_OFFLOAD, /* PCM offload */
146
147 MAX_QAF_MODULE_OUT
148} mm_module_output_type;
149
150typedef enum {
151 QAF_IN_MAIN = 0, /* Single PID Main/Primary or Dual-PID stream */
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530152 QAF_IN_ASSOC, /* Associated/Secondary stream */
153 QAF_IN_PCM, /* PCM stream. */
154 QAF_IN_MAIN_2, /* Single PID Main2 stream */
Harsh Bansal28718c52017-04-20 13:47:12 +0530155 MAX_QAF_MODULE_IN
156} mm_module_input_type;
157
158struct qaf_module {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530159 audio_session_handle_t session_handle;
160 void *qaf_lib;
Harsh Bansal28718c52017-04-20 13:47:12 +0530161 int (*qaf_audio_session_open)(audio_session_handle_t* session_handle,
162 void *p_data,
163 void* license_data);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530164 int (*qaf_audio_session_close)(audio_session_handle_t session_handle);
Harsh Bansal28718c52017-04-20 13:47:12 +0530165 int (*qaf_audio_stream_open)(audio_session_handle_t session_handle,
166 audio_stream_handle_t* stream_handle,
167 audio_stream_config_t input_config,
168 audio_devices_t devices,
169 stream_type_t flags);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530170 int (*qaf_audio_stream_close)(audio_stream_handle_t stream_handle);
171 int (*qaf_audio_stream_set_param)(audio_stream_handle_t stream_handle, const char* kv_pairs);
172 int (*qaf_audio_session_set_param)(audio_session_handle_t handle, const char* kv_pairs);
173 char* (*qaf_audio_stream_get_param)(audio_stream_handle_t stream_handle, const char* key);
174 char* (*qaf_audio_session_get_param)(audio_session_handle_t handle, const char* key);
175 int (*qaf_audio_stream_start)(audio_stream_handle_t handle);
176 int (*qaf_audio_stream_stop)(audio_stream_handle_t stream_handle);
177 int (*qaf_audio_stream_pause)(audio_stream_handle_t stream_handle);
178 int (*qaf_audio_stream_flush)(audio_stream_handle_t stream_handle);
179 int (*qaf_audio_stream_write)(audio_stream_handle_t stream_handle, const void* buf, int size);
Harsh Bansal28718c52017-04-20 13:47:12 +0530180 void (*qaf_register_event_callback)(audio_session_handle_t session_handle,
181 void *priv_data,
182 notify_event_callback_t event_callback,
183 audio_event_id_t event_id);
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800184
Harsh Bansal28718c52017-04-20 13:47:12 +0530185 /*Input stream of MM module */
186 struct stream_out *stream_in[MAX_QAF_MODULE_IN];
187 /*Output Stream from MM module */
188 struct stream_out *stream_out[MAX_QAF_MODULE_OUT];
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800189
Harsh Bansal28718c52017-04-20 13:47:12 +0530190 struct qaf_adsp_hdlr_config_state adsp_hdlr_config[MAX_QAF_MODULE_IN];
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800191
Harsh Bansal28718c52017-04-20 13:47:12 +0530192 //BT session handle.
Bharath Gopal01310bb2016-12-05 15:39:32 +0530193 void *bt_hdl;
Harsh Bansal28718c52017-04-20 13:47:12 +0530194
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530195 float vol_left;
196 float vol_right;
Harsh Bansal28718c52017-04-20 13:47:12 +0530197 bool is_vol_set;
198 bool drain_received[MAX_QAF_MODULE_IN];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530199};
200
Harsh Bansal28718c52017-04-20 13:47:12 +0530201struct qaf {
202 struct audio_device *adev;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530203
Harsh Bansal28718c52017-04-20 13:47:12 +0530204 pthread_mutex_t lock;
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800205
Harsh Bansal28718c52017-04-20 13:47:12 +0530206 bool hdmi_connect;
207 int hdmi_sink_channels;
208
209 //Flag to indicate if QAF transcode output stream is enabled from any mm module.
210 bool passthrough_enabled;
211 //Flag to indicate if QAF mch pcm output stream is enabled from any mm module.
212 bool mch_pcm_hdmi_enabled;
213
214 //Flag to indicate if msmd is supported.
215 bool qaf_msmd_enabled;
216
217 //Handle of QAF input stream, which is routed as QAF passthrough.
218 struct stream_out *passthrough_in;
219 //Handle of QAF passthrough stream.
220 struct stream_out *passthrough_out;
221
222 struct qaf_module qaf_mod[MAX_MM_MODULE_TYPE];
223};
224
225static int qaf_out_pause(struct audio_stream_out* stream);
226static int qaf_out_flush(struct audio_stream_out* stream);
227static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type);
228
229//Global handle of QAF. Access to this should be protected by mutex lock.
230static struct qaf *p_qaf = NULL;
231
232/* Gets the pointer to qaf module for the qaf input stream. */
233static struct qaf_module* get_qaf_module_for_input_stream(struct stream_out *out)
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530234{
Harsh Bansal28718c52017-04-20 13:47:12 +0530235 struct qaf_module *qaf_mod = NULL;
236 int i, j;
237 if (!p_qaf) return NULL;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530238
Harsh Bansal28718c52017-04-20 13:47:12 +0530239 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
240 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
241 if (p_qaf->qaf_mod[i].stream_in[j] == out) {
242 qaf_mod = &(p_qaf->qaf_mod[i]);
243 break;
244 }
245 }
246 }
247
248 return qaf_mod;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530249}
250
Harsh Bansal28718c52017-04-20 13:47:12 +0530251/* Finds the mm module input stream index for the QAF input stream. */
252static int get_input_stream_index(struct stream_out *out)
253{
254 int index = -1, j;
255 struct qaf_module* qaf_mod = NULL;
256
257 qaf_mod = get_qaf_module_for_input_stream(out);
258 if (!qaf_mod) return index;
259
260 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
261 if (qaf_mod->stream_in[j] == out) {
262 index = j;
263 break;
264 }
265 }
266
267 return index;
268}
269
270/* Finds the right mm module for the QAF input stream format. */
271static mm_module_type get_mm_module_for_format(audio_format_t format)
272{
273 int j;
274
275 DEBUG_MSG("Format 0x%x", format);
276
277 if (format == AUDIO_FORMAT_PCM_16_BIT) {
278 //If dts is not supported then alway support pcm with MS12
279 if (!property_get_bool("audio.qaf.dts_m8", false)) { //TODO: Need to add this property for DTS.
280 return MS12;
281 }
282
283 //If QAF passthrough is active then send the PCM stream to primary HAL.
284 if (!p_qaf->passthrough_out) {
285 /* Iff any stream is active in MS12 module then route PCM stream to it. */
286 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
287 if (p_qaf->qaf_mod[MS12].stream_in[j]) {
288 return MS12;
289 }
290 }
291 }
292 return INVALID_MM_MODULE;
293 }
294
295 switch (format & AUDIO_FORMAT_MAIN_MASK) {
296 case AUDIO_FORMAT_AC3:
297 case AUDIO_FORMAT_E_AC3:
298 case AUDIO_FORMAT_AAC:
299 case AUDIO_FORMAT_AAC_ADTS:
300 case AUDIO_FORMAT_AC4:
301 return MS12;
302 case AUDIO_FORMAT_DTS:
303 case AUDIO_FORMAT_DTS_HD:
304 return DTS_M8;
305 default:
306 return INVALID_MM_MODULE;
307 }
308}
309
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530310static bool is_main_active(struct qaf_module* qaf_mod)
311{
312 return (qaf_mod->stream_in[QAF_IN_MAIN] || qaf_mod->stream_in[QAF_IN_MAIN_2]);
313}
314
315static bool is_dual_main_active(struct qaf_module* qaf_mod)
316{
317 return (qaf_mod->stream_in[QAF_IN_MAIN] && qaf_mod->stream_in[QAF_IN_MAIN_2]);
318}
319
Harsh Bansal28718c52017-04-20 13:47:12 +0530320/* Gets the pcm output buffer size(in samples) for the mm module. */
321static uint32_t get_pcm_output_buffer_size_samples(struct qaf_module *qaf_mod)
322{
323 uint32_t pcm_output_buffer_size = 0;
324
325 if (qaf_mod == &p_qaf->qaf_mod[MS12]) {
326 pcm_output_buffer_size = MS12_PCM_OUT_FRAGMENT_SIZE;
327 } else if (qaf_mod == &p_qaf->qaf_mod[DTS_M8]) {
328 pcm_output_buffer_size = DTS_PCM_OUT_FRAGMENT_SIZE;
329 }
330
331 return pcm_output_buffer_size;
332}
333
334/* Acquire Mutex lock on output stream */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530335static void lock_output_stream(struct stream_out *out)
336{
337 pthread_mutex_lock(&out->pre_lock);
338 pthread_mutex_lock(&out->lock);
339 pthread_mutex_unlock(&out->pre_lock);
340}
341
Harsh Bansal28718c52017-04-20 13:47:12 +0530342/* Release Mutex lock on output stream */
343static void unlock_output_stream(struct stream_out *out)
344{
345 pthread_mutex_unlock(&out->lock);
346}
347
348/* Checks if stream can be routed as QAF passthrough or not. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530349static bool audio_extn_qaf_passthrough_enabled(struct stream_out *out)
350{
Harsh Bansal28718c52017-04-20 13:47:12 +0530351 DEBUG_MSG("Format 0x%x", out->format);
352 bool is_enabled = false;
353
354 if (!p_qaf) return false;
355
356 if ((!property_get_bool("audio.qaf.reencode", false))
357 && property_get_bool("audio.qaf.passthrough", false)) {
358
359 if ((out->format == AUDIO_FORMAT_PCM_16_BIT) && (popcount(out->channel_mask) > 2)) {
360 is_enabled = true;
361 } else if (property_get_bool("audio.offload.passthrough", false)) {
362 switch (out->format) {
363 case AUDIO_FORMAT_AC3:
364 case AUDIO_FORMAT_E_AC3:
365 case AUDIO_FORMAT_DTS:
Ben Romberger1aaaf862017-04-06 17:49:46 -0700366 case AUDIO_FORMAT_DTS_HD:
367 case AUDIO_FORMAT_DOLBY_TRUEHD: {
Harsh Bansal28718c52017-04-20 13:47:12 +0530368 is_enabled = true;
369 break;
370 }
371 default:
372 is_enabled = false;
373 break;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530374 }
375 }
376 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530377
378 return is_enabled;
379}
380
381/*Closes all pcm hdmi output from QAF. */
382static void close_all_pcm_hdmi_output()
383{
384 int i;
385 //Closing all the PCM HDMI output stream from QAF.
386 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
387 if (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]) {
388 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
389 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]));
390 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH] = NULL;
391 }
392
393 if ((p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD])
394 && (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
395 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
396 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]));
397 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD] = NULL;
398 }
399 }
400
401 p_qaf->mch_pcm_hdmi_enabled = 0;
402}
403
404static void close_all_hdmi_output()
405{
406 int k;
407 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
408 if (p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
409 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
410 (struct audio_stream_out *)(p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]));
411 p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] = NULL;
412 }
413 }
414 p_qaf->passthrough_enabled = 0;
415
416 close_all_pcm_hdmi_output();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530417}
418
419static int qaf_out_callback(stream_callback_event_t event, void *param __unused, void *cookie)
420{
421 struct stream_out *out = (struct stream_out *)cookie;
422
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800423 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530424 return 0;
425}
426
Harsh Bansal28718c52017-04-20 13:47:12 +0530427/* Creates the QAF passthrough output stream. */
428static int create_qaf_passthrough_stream()
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530429{
Harsh Bansal28718c52017-04-20 13:47:12 +0530430 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530431
Harsh Bansal28718c52017-04-20 13:47:12 +0530432 int ret = 0, k;
433 struct stream_out *out = p_qaf->passthrough_in;
434
435 if (!out) return -EINVAL;
436
437 pthread_mutex_lock(&p_qaf->lock);
438 lock_output_stream(out);
439
440 //Creating QAF passthrough output stream.
441 if (NULL == p_qaf->passthrough_out) {
442 audio_output_flags_t flags;
443 struct audio_config config;
444 audio_devices_t devices;
445
446 config.sample_rate = config.offload_info.sample_rate = out->sample_rate;
447 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
448 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
449 config.offload_info.format = out->format;
450 config.offload_info.bit_width = out->bit_width;
451 config.format = out->format;
452 config.offload_info.channel_mask = config.channel_mask = out->channel_mask;
453
454 //Device is copied from the QAF passthrough input stream.
455 devices = out->devices;
456 flags = out->flags;
457
458 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
459 QAF_DEFAULT_PASSTHROUGH_HANDLE,
460 devices,
461 flags,
462 &config,
463 (struct audio_stream_out **)&(p_qaf->passthrough_out),
464 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530465 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530466 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
467 unlock_output_stream(out);
468 return ret;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530469 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530470 p_qaf->passthrough_in = out;
471 p_qaf->passthrough_out->stream.set_callback((struct audio_stream_out *)p_qaf->passthrough_out,
472 (stream_callback_t) qaf_out_callback, out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530473 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530474
475 unlock_output_stream(out);
476
477 //Since QAF-Passthrough is created, close other HDMI outputs.
478 close_all_hdmi_output();
479
480 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530481 return ret;
482}
483
Harsh Bansal28718c52017-04-20 13:47:12 +0530484/* Closes the QAF passthrough output stream. */
485static void close_qaf_passthrough_stream()
486{
487 if (p_qaf->passthrough_out != NULL) { //QAF pasthroug is enabled. Close it.
488 pthread_mutex_lock(&p_qaf->lock);
489 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
490 (struct audio_stream_out *)(p_qaf->passthrough_out));
491 p_qaf->passthrough_out = NULL;
492 pthread_mutex_unlock(&p_qaf->lock);
493
494 if (p_qaf->passthrough_in->qaf_stream_handle) {
495 qaf_out_pause((struct audio_stream_out*)p_qaf->passthrough_in);
496 qaf_out_flush((struct audio_stream_out*)p_qaf->passthrough_in);
497 qaf_out_drain((struct audio_stream_out*)p_qaf->passthrough_in,
498 (audio_drain_type_t)STREAM_CBK_EVENT_DRAIN_READY);
499 }
500 }
501}
502
503/* Sends a command to output stream offload thread. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530504static int qaf_send_offload_cmd_l(struct stream_out* out, int command)
505{
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530506 DEBUG_MSG_VV("command is %d", command);
Harsh Bansal28718c52017-04-20 13:47:12 +0530507
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530508 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
509
510 if (!cmd) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530511 ERROR_MSG("failed to allocate mem for command 0x%x", command);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530512 return -ENOMEM;
513 }
514
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530515 cmd->cmd = command;
Harsh Bansal28718c52017-04-20 13:47:12 +0530516
517 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530518 list_add_tail(&out->qaf_offload_cmd_list, &cmd->node);
519 pthread_cond_signal(&out->qaf_offload_cond);
Harsh Bansal28718c52017-04-20 13:47:12 +0530520 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530521 return 0;
522}
523
Harsh Bansal28718c52017-04-20 13:47:12 +0530524/* Stops a QAF module stream.*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530525static int audio_extn_qaf_stream_stop(struct stream_out *out)
526{
Harsh Bansal28718c52017-04-20 13:47:12 +0530527 int ret = 0;
528 DEBUG_MSG("Output Stream 0x%x", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530529
Harsh Bansal28718c52017-04-20 13:47:12 +0530530 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
531 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_stop)) {
532 return ret;
533 }
534
535 if (out->qaf_stream_handle) {
536 ret = qaf_mod->qaf_audio_stream_stop(out->qaf_stream_handle);
537 }
538
539 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530540}
541
Harsh Bansal28718c52017-04-20 13:47:12 +0530542/* Puts a QAF module stream in standby. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530543static int qaf_out_standby(struct audio_stream *stream)
544{
545 struct stream_out *out = (struct stream_out *)stream;
546 int status = 0;
547
548 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
549 stream, out->usecase, use_case_table[out->usecase]);
550
551 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530552
553 //If QAF passthrough is active then block standby on all the input streams of QAF mm modules.
554 if (p_qaf->passthrough_out) {
555 //If standby is received on QAF passthrough stream then forward it to primary HAL.
556 if (p_qaf->passthrough_in == out) {
557 status = p_qaf->passthrough_out->stream.common.standby(
558 (struct audio_stream *)p_qaf->passthrough_out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530559 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530560 } else {
561 //If QAF passthrough stream is not active then stop the QAF module stream.
562 status = audio_extn_qaf_stream_stop(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530563 }
564
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530565 if (!out->standby) {
566 out->standby = true;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530567 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530568
569 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530570 return status;
571}
572
Harsh Bansal28718c52017-04-20 13:47:12 +0530573/* Sets the volume to PCM output stream. */
574static int qaf_out_set_volume(struct audio_stream_out *stream, float left, float right)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530575{
576 int ret = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530577 struct stream_out *out = (struct stream_out *)stream;
578 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530579
Harsh Bansal28718c52017-04-20 13:47:12 +0530580 DEBUG_MSG("Left %f, Right %f", left, right);
581
582 qaf_mod = get_qaf_module_for_input_stream(out);
583 if (!qaf_mod) {
584 return -EINVAL;
585 }
586
587 pthread_mutex_lock(&p_qaf->lock);
588 qaf_mod->vol_left = left;
589 qaf_mod->vol_right = right;
590 qaf_mod->is_vol_set = true;
591 pthread_mutex_unlock(&p_qaf->lock);
592
593 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL) {
594 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
595 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], left, right);
596 }
597
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530598 return ret;
599}
600
Harsh Bansal28718c52017-04-20 13:47:12 +0530601/* Starts a QAF module stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530602static int qaf_stream_start(struct stream_out *out)
603{
Harsh Bansal28718c52017-04-20 13:47:12 +0530604 int ret = -EINVAL;
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("Output Stream = %p", out);
608
609 qaf_mod = get_qaf_module_for_input_stream(out);
610 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_start)) {
611 return -EINVAL;
612 }
613
614 if (out->qaf_stream_handle) {
615 ret = qaf_mod->qaf_audio_stream_start(out->qaf_stream_handle);
616 }
617
618 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530619}
620
621static int qaf_start_output_stream(struct stream_out *out)
622{
623 int ret = 0;
624 struct audio_device *adev = out->dev;
625 int snd_card_status = get_snd_card_state(adev);
626
627 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
628 ret = -EINVAL;
629 usleep(50000);
630 return ret;
631 }
632
633 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
634 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
635 out->devices);
636
637 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
638 ALOGE("%s: sound card is not active/SSR returning error", __func__);
639 ret = -EIO;
640 usleep(50000);
641 return ret;
642 }
643
644 return qaf_stream_start(out);
645}
646
Harsh Bansal28718c52017-04-20 13:47:12 +0530647/* Sends input buffer to the QAF MM module. */
648static int qaf_module_write_input_buffer(struct stream_out *out, const void *buffer, int bytes)
649{
650 int ret = -EINVAL;
651 struct qaf_module *qaf_mod = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +0530652
653 qaf_mod = get_qaf_module_for_input_stream(out);
654 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_write)) {
655 return ret;
656 }
657
658 if (out->qaf_stream_handle) {
659 ret = qaf_mod->qaf_audio_stream_write(out->qaf_stream_handle, buffer, bytes);
660 }
661 return ret;
662}
663
664/* Writes buffer to QAF input stream. */
665static ssize_t qaf_out_write(struct audio_stream_out *stream, const void *buffer, size_t bytes)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530666{
667 struct stream_out *out = (struct stream_out *)stream;
668 struct audio_device *adev = out->dev;
669 ssize_t ret = 0;
670
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530671 DEBUG_MSG_VV("bytes = %d, usecase[%d] and flags[%x] for handle[%p]",
Harsh Bansal28718c52017-04-20 13:47:12 +0530672 (int)bytes, out->usecase, out->flags, out);
673
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530674 lock_output_stream(out);
675
Harsh Bansal28718c52017-04-20 13:47:12 +0530676 // If QAF passthrough is active then block writing data to QAF mm module.
677 if (p_qaf->passthrough_out) {
678 //If write is received for the QAF passthrough stream then send the buffer to primary HAL.
679 if (p_qaf->passthrough_in == out) {
680 ret = p_qaf->passthrough_out->stream.write(
681 (struct audio_stream_out *)(p_qaf->passthrough_out),
682 buffer,
683 bytes);
684 if (ret > 0) out->standby = false;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530685 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530686 unlock_output_stream(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530687 return ret;
Harsh Bansal28718c52017-04-20 13:47:12 +0530688 } else if (out->standby) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530689 pthread_mutex_lock(&adev->lock);
690 ret = qaf_start_output_stream(out);
691 pthread_mutex_unlock(&adev->lock);
Harsh Bansal28718c52017-04-20 13:47:12 +0530692 if (ret == 0) {
693 out->standby = false;
694 } else {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530695 goto exit;
696 }
697 }
698
Harsh Bansal28718c52017-04-20 13:47:12 +0530699 if ((adev->is_channel_status_set == false) && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530700 audio_utils_set_hdmi_channel_status(out, (char *)buffer, bytes);
701 adev->is_channel_status_set = true;
702 }
703
Harsh Bansal28718c52017-04-20 13:47:12 +0530704 ret = qaf_module_write_input_buffer(out, buffer, bytes);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530705 DEBUG_MSG_VV("ret [%d]", (int)ret);
Harsh Bansal28718c52017-04-20 13:47:12 +0530706
707 if (ret >= 0) {
708 bytes = ret;
709 out->written += bytes / ((popcount(out->channel_mask) * sizeof(short)));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530710 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530711
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530712
713exit:
Harsh Bansal28718c52017-04-20 13:47:12 +0530714 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530715
716 if (ret < 0) {
717 if (ret == -EAGAIN) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530718 DEBUG_MSG_VV("No space available in mm module, post msg to cb thread");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530719 ret = qaf_send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530720 bytes = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530721 } else if (ret == -ENOMEM || ret == -EPERM) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530722 if (out->pcm)
Harsh Bansal28718c52017-04-20 13:47:12 +0530723 ERROR_MSG("error %d, %s", (int)ret, pcm_get_error(out->pcm));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530724 qaf_out_standby(&out->stream.common);
Harsh Bansal28718c52017-04-20 13:47:12 +0530725 usleep(bytes * 1000000
726 / audio_stream_out_frame_size(stream)
727 / out->stream.common.get_sample_rate(&out->stream.common));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530728 }
729 }
730 return bytes;
731}
732
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530733/* Gets PCM offload buffer size for a given config. */
734static uint32_t qaf_get_pcm_offload_buffer_size(audio_offload_info_t* info,
735 uint32_t samples_per_frame)
Varun B34da7a42017-02-13 16:16:53 +0530736{
737 uint32_t fragment_size = 0;
Varun B34da7a42017-02-13 16:16:53 +0530738
Harsh Bansal28718c52017-04-20 13:47:12 +0530739 fragment_size = (samples_per_frame * (info->bit_width >> 3) * popcount(info->channel_mask));
740
741 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530742 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530743 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530744 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530745
Varun B34da7a42017-02-13 16:16:53 +0530746 // To have same PCM samples for all channels, the buffer size requires to
747 // be multiple of (number of channels * bytes per sample)
748 // For writes to succeed, the buffer must be written at address which is multiple of 32
Harsh Bansal28718c52017-04-20 13:47:12 +0530749 fragment_size = ALIGN(fragment_size,
750 ((info->bit_width >> 3) * popcount(info->channel_mask) * 32));
Varun B34da7a42017-02-13 16:16:53 +0530751
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530752 ALOGI("Qaf PCM offload Fragment size is %d bytes", fragment_size);
Varun B34da7a42017-02-13 16:16:53 +0530753
754 return fragment_size;
755}
756
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530757static uint32_t qaf_get_pcm_offload_input_buffer_size(info)
758{
759 return qaf_get_pcm_offload_buffer_size(info, MS12_PCM_IN_FRAGMENT_SIZE);
760}
761
762static uint32_t qaf_get_pcm_offload_output_buffer_size(struct qaf_module *qaf_mod,
763 audio_offload_info_t* info)
764{
765 return qaf_get_pcm_offload_buffer_size(info, get_pcm_output_buffer_size_samples(qaf_mod));
766}
767
Harsh Bansal28718c52017-04-20 13:47:12 +0530768/* Gets buffer latency in samples. */
769static int get_buffer_latency(struct stream_out *out, uint32_t buffer_size, uint32_t *latency)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530770{
Harsh Bansal28718c52017-04-20 13:47:12 +0530771 unsigned long int samples_in_one_encoded_frame;
772 unsigned long int size_of_one_encoded_frame;
773
774 switch (out->format) {
775 case AUDIO_FORMAT_AC3:
776 samples_in_one_encoded_frame = DD_FRAME_SIZE;
777 size_of_one_encoded_frame = DD_ENCODER_OUTPUT_SIZE;
778 break;
779 case AUDIO_FORMAT_E_AC3:
780 samples_in_one_encoded_frame = DDP_FRAME_SIZE;
781 size_of_one_encoded_frame = DDP_ENCODER_OUTPUT_SIZE;
782 break;
783 case AUDIO_FORMAT_DTS:
784 samples_in_one_encoded_frame = DTS_FRAME_SIZE;
785 size_of_one_encoded_frame = DTS_ENCODER_OUTPUT_SIZE;
786 break;
787 case AUDIO_FORMAT_DTS_HD:
788 samples_in_one_encoded_frame = DTSHD_FRAME_SIZE;
789 size_of_one_encoded_frame = DTSHD_ENCODER_OUTPUT_SIZE;
790 break;
791 case AUDIO_FORMAT_PCM_16_BIT:
792 samples_in_one_encoded_frame = 1;
793 size_of_one_encoded_frame = ((out->bit_width) >> 3) * popcount(out->channel_mask);
794 break;
795 default:
796 *latency = 0;
797 return (-EINVAL);
798 }
799
800 *latency = ((buffer_size * samples_in_one_encoded_frame) / size_of_one_encoded_frame);
801 return 0;
802}
803
804/* Returns the number of frames rendered to outside observer. */
805static int qaf_get_rendered_frames(struct stream_out *out, uint64_t *frames)
806{
807 int ret = 0, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530808 struct str_parms *parms;
809 int value = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530810 int module_latency = 0;
811 uint32_t kernel_latency = 0;
812 uint32_t dsp_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530813 int signed_frames = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530814 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +0530815 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530816
Harsh Bansal28718c52017-04-20 13:47:12 +0530817 DEBUG_MSG("Output Format %d", out->format);
818
819 qaf_mod = get_qaf_module_for_input_stream(out);
820 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_get_param)) {
821 return -EINVAL;
822 }
823
824 //Get MM module latency.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530825 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "get_latency");
826 if (kvpairs) {
827 parms = str_parms_create_str(kvpairs);
Harsh Bansal28718c52017-04-20 13:47:12 +0530828 ret = str_parms_get_int(parms, "get_latency", &module_latency);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530829 if (ret >= 0) {
830 str_parms_destroy(parms);
831 parms = NULL;
832 }
833 free(kvpairs);
834 kvpairs = NULL;
835 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530836
837 //Get kernel Latency
838 for (i = MAX_QAF_MODULE_OUT - 1; i >= 0; i--) {
839 if (qaf_mod->stream_out[i] == NULL) {
840 continue;
841 } else {
842 unsigned int num_fragments = qaf_mod->stream_out[i]->compr_config.fragments;
843 uint32_t fragment_size = qaf_mod->stream_out[i]->compr_config.fragment_size;
844 uint32_t kernel_buffer_size = num_fragments * fragment_size;
845 get_buffer_latency(qaf_mod->stream_out[i], kernel_buffer_size, &kernel_latency);
846 break;
847 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530848 }
849
Harsh Bansal28718c52017-04-20 13:47:12 +0530850 //Get DSP latency
851 if ((qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL)
852 || (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH] != NULL)) {
853 unsigned int sample_rate = 0;
854
855 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
856 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
857 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
858 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
859
860 audio_usecase_t platform_latency =
861 platform_render_latency(qaf_mod->stream_out[QAF_OUT_OFFLOAD]->usecase);
862 dsp_latency = (platform_latency * sample_rate) / 1000000LL;
863 } else if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] != NULL) {
864 unsigned int sample_rate = 0;
865
866 sample_rate = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->sample_rate; //TODO: How this sample rate can be used?
867 dsp_latency = (COMPRESS_OFFLOAD_PLAYBACK_LATENCY * sample_rate) / 1000;
868 }
869
870 // MM Module Latency + Kernel Latency + DSP Latency
871 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
872 out->platform_latency = module_latency + audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl);
873 } else {
874 out->platform_latency = (uint32_t)module_latency + kernel_latency + dsp_latency;
875 }
876
877 if (out->format & AUDIO_FORMAT_PCM_16_BIT) {
878 *frames = 0;
879 signed_frames = out->written - out->platform_latency;
880 // It would be unusual for this value to be negative, but check just in case ...
881 if (signed_frames >= 0) {
882 *frames = signed_frames;
883 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530884 } else if (qaf_mod->qaf_audio_stream_get_param) {
885 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "position");
886 if (kvpairs) {
887 parms = str_parms_create_str(kvpairs);
888 ret = str_parms_get_int(parms, "position", &value);
889 if (ret >= 0) {
890 *frames = value;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530891 signed_frames = value - out->platform_latency;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530892 // It would be unusual for this value to be negative, but check just in case ...
893 if (signed_frames >= 0) {
894 *frames = signed_frames;
895 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530896 }
897 str_parms_destroy(parms);
898 }
899 } else {
900 ret = -EINVAL;
901 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530902
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530903 return ret;
904}
905
906static int qaf_out_get_presentation_position(const struct audio_stream_out *stream,
Harsh Bansal28718c52017-04-20 13:47:12 +0530907 uint64_t *frames,
908 struct timespec *timestamp)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530909{
910 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530911 int ret = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530912
Harsh Bansal28718c52017-04-20 13:47:12 +0530913 DEBUG_MSG("Output Stream %p", stream);
914
915 //If QAF passthorugh output stream is active.
916 if (p_qaf->passthrough_out) {
917 if (p_qaf->passthrough_in == out) {
918 //If api is called for QAF passthorugh stream then call the primary HAL api to get the position.
919 pthread_mutex_lock(&p_qaf->lock);
920 ret = p_qaf->passthrough_out->stream.get_presentation_position(
921 (struct audio_stream_out *)p_qaf->passthrough_out,
922 frames,
923 timestamp);
924 pthread_mutex_unlock(&p_qaf->lock);
925 } else {
926 //If api is called for other stream then return zero frames.
927 *frames = 0;
928 clock_gettime(CLOCK_MONOTONIC, timestamp);
929 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530930 return ret;
931 }
932
Harsh Bansal28718c52017-04-20 13:47:12 +0530933 ret = qaf_get_rendered_frames(out, frames);
934 clock_gettime(CLOCK_MONOTONIC, timestamp);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530935
936 return ret;
937}
938
Harsh Bansal28718c52017-04-20 13:47:12 +0530939/* Pause the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530940static int qaf_stream_pause(struct stream_out *out)
941{
Harsh Bansal28718c52017-04-20 13:47:12 +0530942 struct qaf_module *qaf_mod = NULL;
943 int ret = -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530944
Harsh Bansal28718c52017-04-20 13:47:12 +0530945 qaf_mod = get_qaf_module_for_input_stream(out);
946 if (!qaf_mod || !qaf_mod->qaf_audio_stream_pause) {
947 return -EINVAL;
948 }
949
950 if (out->qaf_stream_handle)
951 ret = qaf_mod->qaf_audio_stream_pause(out->qaf_stream_handle);
952
953 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530954}
955
Harsh Bansal28718c52017-04-20 13:47:12 +0530956/* Pause a QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530957static int qaf_out_pause(struct audio_stream_out* stream)
958{
959 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530960 int status = 0;
961 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530962
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530963 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530964
965 //If QAF passthrough is enabled then block the pause on module stream.
966 if (p_qaf->passthrough_out) {
967 pthread_mutex_lock(&p_qaf->lock);
968 //If pause is received for QAF passthorugh stream then call the primary HAL api.
969 if (p_qaf->passthrough_in == out) {
970 status = p_qaf->passthrough_out->stream.pause(
971 (struct audio_stream_out *)p_qaf->passthrough_out);
972 out->offload_state = OFFLOAD_STATE_PAUSED;
973 }
974 pthread_mutex_unlock(&p_qaf->lock);
975 } else {
976 //Pause the module input stream.
977 status = qaf_stream_pause(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530978 }
979
Harsh Bansal28718c52017-04-20 13:47:12 +0530980 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530981 return status;
982}
983
Harsh Bansal28718c52017-04-20 13:47:12 +0530984/* Drains a qaf input stream. */
985static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530986{
987 struct stream_out *out = (struct stream_out *)stream;
988 int status = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530989 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530990
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530991 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530992
993 //If QAF passthrough is enabled then block the drain on module stream.
994 if (p_qaf->passthrough_out) {
995 pthread_mutex_lock(&p_qaf->lock);
996 //If drain is received for QAF passthorugh stream then call the primary HAL api.
997 if (p_qaf->passthrough_in == out) {
998 status = p_qaf->passthrough_out->stream.drain(
999 (struct audio_stream_out *)p_qaf->passthrough_out, type);
1000 }
1001 pthread_mutex_unlock(&p_qaf->lock);
1002 } else {
1003 //Drain the module input stream.
1004 /* Stream stop will trigger EOS and on EOS_EVENT received
1005 from callback DRAIN_READY command is sent */
1006 status = audio_extn_qaf_stream_stop(out);
1007
1008 if (status == 0) {
1009 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
1010 int index = get_input_stream_index(out);
1011 if (qaf_mod && index >= 0) qaf_mod->drain_received[index] = true;
1012 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301013 }
1014
Harsh Bansal28718c52017-04-20 13:47:12 +05301015 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301016 return status;
1017}
1018
Harsh Bansal28718c52017-04-20 13:47:12 +05301019/* Flush the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301020static int audio_extn_qaf_stream_flush(struct stream_out *out)
1021{
Harsh Bansal28718c52017-04-20 13:47:12 +05301022 DEBUG_MSG("Output Stream %p", out);
1023 int ret = -EINVAL;
1024 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301025
Harsh Bansal28718c52017-04-20 13:47:12 +05301026 qaf_mod = get_qaf_module_for_input_stream(out);
1027 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_flush)) {
1028 return -EINVAL;
1029 }
1030
1031 if (out->qaf_stream_handle)
1032 ret = qaf_mod->qaf_audio_stream_flush(out->qaf_stream_handle);
1033
1034 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301035}
1036
Harsh Bansal28718c52017-04-20 13:47:12 +05301037/* Flush the QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301038static int qaf_out_flush(struct audio_stream_out* stream)
1039{
1040 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05301041 int status = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301042
Harsh Bansal28718c52017-04-20 13:47:12 +05301043 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301044 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301045
1046 //If QAF passthrough is active then block the flush on module input streams.
1047 if (p_qaf->passthrough_out) {
1048 pthread_mutex_lock(&p_qaf->lock);
1049 //If flush is received for the QAF passthrough stream then call the primary HAL api.
1050 if (p_qaf->passthrough_in == out) {
1051 status = p_qaf->passthrough_out->stream.flush(
1052 (struct audio_stream_out *)p_qaf->passthrough_out);
1053 out->offload_state = OFFLOAD_STATE_IDLE;
1054 }
1055 pthread_mutex_unlock(&p_qaf->lock);
1056 } else {
1057 //Flush the module input stream.
1058 status = audio_extn_qaf_stream_flush(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301059 }
1060
Harsh Bansal28718c52017-04-20 13:47:12 +05301061 unlock_output_stream(out);
1062 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301063 return status;
1064}
1065
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301066static uint32_t qaf_out_get_latency(const struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301067{
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301068 struct stream_out *out = (struct stream_out *)stream;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301069 uint32_t latency = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05301070 struct qaf_module *qaf_mod = NULL;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301071 DEBUG_MSG_VV("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301072
Harsh Bansal28718c52017-04-20 13:47:12 +05301073 qaf_mod = get_qaf_module_for_input_stream(out);
1074 if (!qaf_mod) {
1075 return 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301076 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301077
Harsh Bansal28718c52017-04-20 13:47:12 +05301078 //If QAF passthrough is active then block the get latency on module input streams.
1079 if (p_qaf->passthrough_out) {
1080 pthread_mutex_lock(&p_qaf->lock);
1081 //If get latency is called for the QAF passthrough stream then call the primary HAL api.
1082 if (p_qaf->passthrough_in == out) {
1083 latency = p_qaf->passthrough_out->stream.get_latency(
1084 (struct audio_stream_out *)p_qaf->passthrough_out);
1085 }
1086 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301087 } else {
Bharath Gopal01310bb2016-12-05 15:39:32 +05301088 if (is_offload_usecase(out->usecase)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301089 latency = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301090 } else {
Harsh Bansal28718c52017-04-20 13:47:12 +05301091 uint32_t sample_rate = 0;
1092 latency = QAF_MODULE_PCM_INPUT_BUFFER_LATENCY; //Input latency
1093
1094 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
1095 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
1096 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
1097 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
1098
1099 if (sample_rate) {
1100 latency += (get_pcm_output_buffer_size_samples(qaf_mod) * 1000) / out->sample_rate;
1101 }
1102 }
1103
1104 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
1105 if (is_offload_usecase(out->usecase)) {
1106 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1107 QAF_COMPRESS_OFFLOAD_PROCESSING_LATENCY;
1108 } else {
1109 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1110 QAF_PCM_OFFLOAD_PROCESSING_LATENCY;
1111 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05301112 }
1113 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301114
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301115 DEBUG_MSG_VV("Latency %d", latency);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301116 return latency;
1117}
1118
Harsh Bansal28718c52017-04-20 13:47:12 +05301119static bool check_and_get_compressed_device_format(int device, int *format)
1120{
1121 switch (device) {
1122 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DD):
1123 *format = AUDIO_FORMAT_AC3;
1124 return true;
1125 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DDP):
1126 *format = AUDIO_FORMAT_E_AC3;
1127 return true;
1128 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_FORMAT_DTS):
1129 *format = AUDIO_FORMAT_DTS;
1130 return true;
1131 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_FORMAT_DTS_HD):
1132 *format = AUDIO_FORMAT_DTS_HD;
1133 return true;
1134 default:
1135 return false;
1136 }
1137}
1138
1139/* Call back function for mm module. */
1140static void notify_event_callback(audio_session_handle_t session_handle /*__unused*/,
1141 void *prv_data,
1142 void *buf,
1143 audio_event_id_t event_id,
1144 int size,
1145 int device) //TODO: add media format as well.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301146{
1147
Harsh Bansal28718c52017-04-20 13:47:12 +05301148 /*
1149 For SPKR:
1150 1. Open pcm device if device_id passed to it SPKR and write the data to
1151 pcm device
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301152
Harsh Bansal28718c52017-04-20 13:47:12 +05301153 For HDMI
1154 1.Open compress device for HDMI(PCM or AC3) based on current hdmi o/p format and write
1155 data to the HDMI device.
1156 */
1157 int ret, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301158 audio_output_flags_t flags;
Harsh Bansal28718c52017-04-20 13:47:12 +05301159 struct qaf_module* qaf_mod = (struct qaf_module*)prv_data;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301160 struct audio_stream_out *bt_stream = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301161 int format;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301162
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301163 DEBUG_MSG_VV("Device 0x%X, Event = 0x%X, Bytes to write %d", device, event_id, size);
Harsh Bansal28718c52017-04-20 13:47:12 +05301164 pthread_mutex_lock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301165
1166 if (event_id == AUDIO_DATA_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301167 if (p_qaf->passthrough_out != NULL) {
1168 //If QAF passthrough is active then all the module output will be dropped.
1169 pthread_mutex_unlock(&p_qaf->lock);
1170 DEBUG_MSG("QAF-PSTH is active, DROPPING DATA!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301171 return;
Harsh Bansal28718c52017-04-20 13:47:12 +05301172 }
1173
1174 if (check_and_get_compressed_device_format(device, &format)) {
1175 /*
1176 * CASE 1: Transcoded output of mm module.
1177 * If HDMI is not connected then drop the data.
1178 * Only one HDMI output can be supported from all the mm modules of QAF.
1179 * Multi-Channel PCM HDMI output streams will be closed from all the mm modules.
1180 * If transcoded output of other module is already enabled then this data will be dropped.
1181 */
1182
1183 if (!p_qaf->hdmi_connect) {
1184 DEBUG_MSG("HDMI not connected, DROPPING DATA!");
1185 pthread_mutex_unlock(&p_qaf->lock);
1186 return;
1187 }
1188
1189 //Closing all the PCM HDMI output stream from QAF.
1190 close_all_pcm_hdmi_output();
1191
1192 if (!p_qaf->passthrough_enabled
1193 && !(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH])) {
1194
1195 struct audio_config config;
1196 audio_devices_t devices;
1197
1198 //TODO: need to update the actual sample rate.
1199 config.sample_rate = config.offload_info.sample_rate =
1200 QAF_OUTPUT_SAMPLING_RATE;
1201 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1202 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
1203 config.format = config.offload_info.format = format;
1204 //TODO: need to update the actual bit width.
1205 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
1206 //TODO: What is the relevance of num of channels.
1207 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1208
1209 flags = (AUDIO_OUTPUT_FLAG_NON_BLOCKING
1210 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD
1211 | AUDIO_OUTPUT_FLAG_DIRECT);
1212 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1213
1214 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1215 QAF_DEFAULT_COMPR_PASSTHROUGH_HANDLE,
1216 devices,
1217 flags,
1218 &config,
1219 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]),
1220 NULL);
1221 if (ret < 0) {
1222 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1223 pthread_mutex_unlock(&p_qaf->lock);
1224 return;
1225 }
1226
1227 if (format & AUDIO_COMPRESSED_OUT_DDP) {
1228 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragment_size =
1229 COMPRESS_PASSTHROUGH_DDP_FRAGMENT_SIZE;
1230 }
1231 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragments =
1232 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1233
1234 p_qaf->passthrough_enabled = true;
1235 }
1236
1237 if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
1238 ret = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->stream.write(
1239 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH],
1240 buf,
1241 size);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301242 }
1243 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301244 else if ((device & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1245 && (p_qaf->hdmi_connect)
1246 && (p_qaf->hdmi_sink_channels > 2)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301247
Harsh Bansal28718c52017-04-20 13:47:12 +05301248 /* CASE 2: Multi-Channel PCM output to HDMI.
1249 * If any other HDMI output is already enabled then this has to be dropped.
1250 */
1251 bool create_mch_out_stream = false;
1252
1253 if (p_qaf->passthrough_enabled) {
1254 //Closing all the multi-Channel PCM HDMI output stream from QAF.
1255 close_all_pcm_hdmi_output();
1256
1257 //If passthrough is active then pcm hdmi output has to be dropped.
1258 pthread_mutex_unlock(&p_qaf->lock);
1259 DEBUG_MSG("Compressed passthrough enabled, DROPPING DATA!");
1260 return;
1261 }
1262
1263 if (!p_qaf->mch_pcm_hdmi_enabled && !(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301264 struct audio_config config;
1265 audio_devices_t devices;
1266
Harsh Bansal28718c52017-04-20 13:47:12 +05301267 //TODO: need to update the actual sample rate.
Varun B34da7a42017-02-13 16:16:53 +05301268 config.sample_rate = config.offload_info.sample_rate =
Harsh Bansal28718c52017-04-20 13:47:12 +05301269 QAF_OUTPUT_SAMPLING_RATE;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301270 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1271 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
Harsh Bansal28718c52017-04-20 13:47:12 +05301272 config.offload_info.format = config.format = AUDIO_FORMAT_PCM_16_BIT;
1273 //TODO: need to update the actual bit width.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301274 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301275
Harsh Bansal28718c52017-04-20 13:47:12 +05301276 if (p_qaf->hdmi_sink_channels == 8) {
Varun B34da7a42017-02-13 16:16:53 +05301277 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301278 AUDIO_CHANNEL_OUT_7POINT1;
1279 } else if (p_qaf->hdmi_sink_channels == 6) {
Varun B34da7a42017-02-13 16:16:53 +05301280 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301281 AUDIO_CHANNEL_OUT_5POINT1;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301282 } else {
Varun B34da7a42017-02-13 16:16:53 +05301283 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301284 AUDIO_CHANNEL_OUT_STEREO;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301285 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301286
Harsh Bansal28718c52017-04-20 13:47:12 +05301287 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1288 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
1289
1290 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1291 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1292 devices,
1293 flags,
1294 &config,
1295 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]),
1296 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301297 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301298 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1299 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301300 return;
1301 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001302
Harsh Bansal28718c52017-04-20 13:47:12 +05301303 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragments =
1304 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1305 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragment_size =
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301306 qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info);
Harsh Bansal28718c52017-04-20 13:47:12 +05301307
1308 p_qaf->mch_pcm_hdmi_enabled = true;
1309
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301310 if ((qaf_mod->stream_in[QAF_IN_MAIN]
1311 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) ||
1312 (qaf_mod->stream_in[QAF_IN_MAIN_2]
1313 && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301314
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301315 if (qaf_mod->stream_in[QAF_IN_MAIN]) {
1316 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
Harsh Bansal28718c52017-04-20 13:47:12 +05301317 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1318 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1319 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301320 }
1321 if (qaf_mod->stream_in[QAF_IN_MAIN_2]) {
1322 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1323 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1324 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback,
1325 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie);
1326 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301327 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1328 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1329
1330 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1331 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1332 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1333 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1334 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001335
1336 int index = -1;
1337 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301338 index = (int) QAF_IN_MAIN;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301339 else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid)
1340 index = (int) QAF_IN_MAIN_2;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001341 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301342 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001343
Harsh Bansal28718c52017-04-20 13:47:12 +05301344 if (index >= 0) {
1345 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->standby)
1346 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1347 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], NULL, 0);
1348
1349 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001350 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301351 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1352 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1353 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1354 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001355
1356 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301357 }
1358
Harsh Bansal28718c52017-04-20 13:47:12 +05301359 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) {
1360 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1361 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1362 buf,
1363 size);
Varun B34da7a42017-02-13 16:16:53 +05301364 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301365 }
1366 else {
1367 /* CASE 3: PCM output.
1368 */
Bharath Gopal01310bb2016-12-05 15:39:32 +05301369 bt_stream = audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl);
1370 if (bt_stream != NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301371 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1372 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1373 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD]));
1374 qaf_mod->stream_out[QAF_OUT_OFFLOAD] = NULL;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301375 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301376
Harsh Bansal28718c52017-04-20 13:47:12 +05301377 audio_extn_bt_hal_out_write(p_qaf->bt_hdl, buf, size);
1378 } else if (NULL == qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301379 struct audio_config config;
1380 audio_devices_t devices;
1381
Harsh Bansal28718c52017-04-20 13:47:12 +05301382 //TODO: need to update the actual sample rate.
Varun B34da7a42017-02-13 16:16:53 +05301383 config.sample_rate = config.offload_info.sample_rate =
Harsh Bansal28718c52017-04-20 13:47:12 +05301384 QAF_OUTPUT_SAMPLING_RATE;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301385 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1386 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
Harsh Bansal28718c52017-04-20 13:47:12 +05301387 config.offload_info.format = config.format = AUDIO_FORMAT_PCM_16_BIT;
1388 //TODO: need to update the actual bit width.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301389 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Harsh Bansal28718c52017-04-20 13:47:12 +05301390 //TODO: need to update the actual num channels.
1391 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301392
Harsh Bansal28718c52017-04-20 13:47:12 +05301393 if (qaf_mod->stream_in[QAF_IN_MAIN])
1394 devices = qaf_mod->stream_in[QAF_IN_MAIN]->devices;
1395 else
1396 devices = qaf_mod->stream_in[QAF_IN_PCM]->devices;
1397
1398 //If multi channel pcm or passthrough is already enabled then remove the hdmi flag from device.
1399 if (p_qaf->mch_pcm_hdmi_enabled || p_qaf->passthrough_enabled) {
1400 if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1401 devices ^= AUDIO_DEVICE_OUT_AUX_DIGITAL;
1402 }
1403 if (devices == 0) {
1404 devices = device;
1405 }
1406
1407 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001408
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301409 /* TODO:: Need to Propagate errors to framework */
Harsh Bansal28718c52017-04-20 13:47:12 +05301410 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1411 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1412 devices,
1413 flags,
1414 &config,
1415 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD]),
1416 NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301417 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301418 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1419 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301420 return;
1421 }
Deepak Agarwal1e42b852017-02-11 17:57:04 +05301422
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301423 if ((qaf_mod->stream_in[QAF_IN_MAIN]
1424 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) ||
1425 (qaf_mod->stream_in[QAF_IN_MAIN_2]
1426 && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001427
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301428 if (qaf_mod->stream_in[QAF_IN_MAIN]) {
1429 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
Harsh Bansal28718c52017-04-20 13:47:12 +05301430 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1431 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1432 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301433 }
1434 if (qaf_mod->stream_in[QAF_IN_MAIN_2]) {
1435 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1436 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1437 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback,
1438 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie);
1439 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301440 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1441 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1442
1443 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1444 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1445 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1446 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1447 }
1448
1449 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragments =
1450 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1451 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragment_size =
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301452 qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info);
Harsh Bansal28718c52017-04-20 13:47:12 +05301453
1454 if (qaf_mod->is_vol_set) {
1455 DEBUG_MSG("Setting Volume Left[%f], Right[%f]", qaf_mod->vol_left, qaf_mod->vol_right);
1456 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
1457 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1458 qaf_mod->vol_left,
1459 qaf_mod->vol_right);
1460 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001461
1462 int index = -1;
1463 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301464 index = (int) QAF_IN_MAIN;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301465 else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid)
1466 index = (int) QAF_IN_MAIN_2;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001467 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301468 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001469 if (index >= 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301470 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]->standby) {
1471 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1472 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], NULL, 0);
1473 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001474
Harsh Bansal28718c52017-04-20 13:47:12 +05301475 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001476 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301477 qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1478 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1479 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1480 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301481 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301482 }
1483
1484 /*
1485 * TODO:: Since this is mixed data,
1486 * need to identify to which stream the error should be sent
1487 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301488 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1489 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1490 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1491 buf,
1492 size);
Varun B34da7a42017-02-13 16:16:53 +05301493 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301494 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301495 DEBUG_MSG_VV("Bytes written = %d", ret);
Harsh Bansal28718c52017-04-20 13:47:12 +05301496 }
1497 else if (event_id == AUDIO_EOS_MAIN_DD_DDP_EVENT
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301498 || event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT
Varun B34da7a42017-02-13 16:16:53 +05301499 || event_id == AUDIO_EOS_MAIN_AAC_EVENT
1500 || event_id == AUDIO_EOS_MAIN_AC4_EVENT
1501 || event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301502 struct stream_out *out = qaf_mod->stream_in[QAF_IN_MAIN];
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301503 struct stream_out *out_main2 = qaf_mod->stream_in[QAF_IN_MAIN_2];
Harsh Bansal28718c52017-04-20 13:47:12 +05301504 struct stream_out *out_assoc = qaf_mod->stream_in[QAF_IN_ASSOC];
1505 bool *main_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301506 bool *main2_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN_2];
Harsh Bansal28718c52017-04-20 13:47:12 +05301507 bool *assoc_drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
Varun B34da7a42017-02-13 16:16:53 +05301508
1509 /**
1510 * TODO:: Only DD/DDP Associate Eos is handled, need to add support
1511 * for other formats.
1512 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301513 if (event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT
1514 && (out_assoc != NULL)
1515 && (*assoc_drain_received)) {
1516
Varun B34da7a42017-02-13 16:16:53 +05301517 lock_output_stream(out_assoc);
Harsh Bansal28718c52017-04-20 13:47:12 +05301518 out_assoc->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_assoc->client_cookie);
1519 *assoc_drain_received = false;
1520 unlock_output_stream(out_assoc);
1521 DEBUG_MSG("sent associated DRAIN_READY");
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301522 } else if (event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT
1523 && (out_main2 != NULL)
1524 && (*main2_drain_received)) {
1525
1526 lock_output_stream(out_main2);
1527 out_main2->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_main2->client_cookie);
1528 *main2_drain_received = false;
1529 unlock_output_stream(out_main2);
1530 DEBUG_MSG("sent main2 DRAIN_READY");
Harsh Bansal28718c52017-04-20 13:47:12 +05301531 } else if ((out != NULL) && (*main_drain_received)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301532 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001533 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
Harsh Bansal28718c52017-04-20 13:47:12 +05301534 *main_drain_received = false;
1535 unlock_output_stream(out);
1536 DEBUG_MSG("sent main DRAIN_READY");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301537 }
1538 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301539#if 0
1540 else if (event_id == AUDIO_MAIN_EOS_EVENT || event_id == AUDIO_ASSOC_EOS_EVENT) { //TODO: For DTS
1541 struct stream_out *out = NULL;
1542 bool *drain_received = NULL;
1543
1544 if (event_id == AUDIO_MAIN_EOS_EVENT) {
1545 out = qaf_mod->stream_in[QAF_IN_MAIN];
1546 drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
1547 } else {
1548 out = qaf_mod->stream_in[QAF_IN_ASSOC];
1549 drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
1550 }
1551
1552 if ((out != NULL) && (*drain_received)) {
1553 lock_output_stream(out);
1554 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
1555 *drain_received = false;
1556 unlock_output_stream(out);
1557 DEBUG_MSG("sent DRAIN_READY");
1558 }
1559 }
1560#endif
1561
1562 pthread_mutex_unlock(&p_qaf->lock);
1563 return;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301564}
1565
Harsh Bansal28718c52017-04-20 13:47:12 +05301566/* Close the mm module session. */
1567static int qaf_session_close(struct qaf_module* qaf_mod)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301568{
Harsh Bansal28718c52017-04-20 13:47:12 +05301569 int j;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301570
Harsh Bansal28718c52017-04-20 13:47:12 +05301571 //Check if all streams are closed or not.
1572 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
1573 if (qaf_mod->stream_in[j] != NULL) {
1574 break;
1575 }
1576 }
1577 if (j != MAX_QAF_MODULE_IN) {
1578 return 0; //Some stream is already active, Can not close session.
1579 }
1580
1581 if (qaf_mod->session_handle != NULL && qaf_mod->qaf_audio_session_close) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301582 qaf_mod->qaf_audio_session_close(qaf_mod->session_handle);
1583 qaf_mod->session_handle = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301584 qaf_mod->is_vol_set = false;
1585 memset(qaf_mod->drain_received, 0, sizeof(qaf_mod->drain_received));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301586 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301587
1588 for (j = 0; j < MAX_QAF_MODULE_OUT; j++) {
1589 if (qaf_mod->stream_out[j]) {
1590 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1591 (struct audio_stream_out *)(qaf_mod->stream_out[j]));
1592 qaf_mod->stream_out[j] = NULL;
1593 }
1594 }
1595
1596 DEBUG_MSG("Session Closed.");
1597
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301598 return 0;
1599}
1600
Harsh Bansal28718c52017-04-20 13:47:12 +05301601/* Close the stream of QAF module. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301602static int qaf_stream_close(struct stream_out *out)
1603{
Harsh Bansal28718c52017-04-20 13:47:12 +05301604 int ret = -EINVAL;
1605 struct qaf_module *qaf_mod = NULL;
1606 int index = -1;
1607 DEBUG_MSG("Flag [0x%x], Stream handle [%p]", out->flags, out->qaf_stream_handle);
1608
1609 qaf_mod = get_qaf_module_for_input_stream(out);
1610 index = get_input_stream_index(out);
1611
1612 if (!qaf_mod || !qaf_mod->qaf_audio_stream_close || index < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301613 return -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301614 }
1615
1616 qaf_mod->stream_in[index] = NULL;
1617 memset(&qaf_mod->adsp_hdlr_config[index], 0, sizeof(struct qaf_adsp_hdlr_config_state));
1618
1619 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301620 if (out->qaf_stream_handle) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301621 ret = qaf_mod->qaf_audio_stream_close(out->qaf_stream_handle);
1622 out->qaf_stream_handle = NULL;
1623 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301624 unlock_output_stream(out);
1625
1626 //If all streams are closed then close the session.
1627 qaf_session_close(qaf_mod);
1628
1629 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301630 return ret;
1631}
1632
Harsh Bansal28718c52017-04-20 13:47:12 +05301633/* Open a MM module session with QAF. */
1634static int audio_extn_qaf_session_open(mm_module_type mod_type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301635{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301636 ALOGV("%s %d", __func__, __LINE__);
Harsh Bansal28718c52017-04-20 13:47:12 +05301637 unsigned char* license_data = NULL;
1638 device_license_config_t lic_config = {0};
1639 int ret = -ENOSYS, size = 0;
1640 char value[PROPERTY_VALUE_MAX] = {0};
1641 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301642
Harsh Bansal28718c52017-04-20 13:47:12 +05301643 if (mod_type >= MAX_MM_MODULE_TYPE || !(p_qaf->qaf_mod[mod_type].qaf_audio_session_open))
1644 return -ENOTSUP; //Not supported by QAF module.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301645
Harsh Bansal28718c52017-04-20 13:47:12 +05301646 pthread_mutex_lock(&p_qaf->lock);
1647
1648 qaf_mod = &(p_qaf->qaf_mod[mod_type]);
1649
1650 //If session is already opened then return.
1651 if (qaf_mod->session_handle) {
1652 DEBUG_MSG("Session is already opened.");
1653 pthread_mutex_unlock(&p_qaf->lock);
1654 return 0;
1655 }
1656
1657 if (mod_type == MS12) {
1658 //Getting the license
1659 license_data = platform_get_license((struct audio_hw_device *)(p_qaf->adev->platform),
1660 &size);
1661 if (!license_data) {
1662 ERROR_MSG("License data is not present.");
1663 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301664 return -EINVAL;
1665 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301666
1667 lic_config.p_license = (unsigned char*)calloc(1, size);
1668 if (lic_config.p_license == NULL) {
1669 ERROR_MSG("Out of Memory");
1670 ret = -ENOMEM;
1671 goto exit;
1672 }
1673
1674 lic_config.l_size = size;
1675 memcpy(lic_config.p_license, license_data, size);
1676
1677 if (property_get("audio.qaf.manufacturer", value, "") && atoi(value)) {
1678 lic_config.manufacturer_id = (unsigned long)atoi(value);
1679 } else {
1680 ERROR_MSG("audio.qaf.manufacturer id is not set");
1681 ret = -EINVAL;
1682 goto exit;
1683 }
1684 }
1685
1686 ret = qaf_mod->qaf_audio_session_open(&qaf_mod->session_handle,
1687 (void *)(qaf_mod),
1688 (void *)&lic_config);
1689 if (ret < 0) {
1690 ERROR_MSG("Error in session open %d", ret);
1691 goto exit;
1692 }
1693
1694 if (qaf_mod->session_handle == NULL) {
1695 ERROR_MSG("Session handle is NULL.");
1696 ret = -ENOMEM;
1697 goto exit;
1698 }
1699
1700 if (qaf_mod->qaf_register_event_callback)
1701 qaf_mod->qaf_register_event_callback(qaf_mod->session_handle,
1702 qaf_mod,
1703 &notify_event_callback,
1704 AUDIO_DATA_EVENT);
1705
1706 set_hdmi_configuration_to_module();
1707
1708exit:
1709 if (license_data != NULL) {
1710 free(license_data);
1711 license_data = NULL;
1712 }
1713 if (lic_config.p_license != NULL) {
1714 free(lic_config.p_license);
1715 lic_config.p_license = NULL;
1716 }
1717
1718 pthread_mutex_unlock(&p_qaf->lock);
1719 return ret;
1720}
1721
1722/* opens a stream in QAF module. */
1723static int qaf_stream_open(struct stream_out *out,
1724 struct audio_config *config,
1725 audio_output_flags_t flags,
1726 audio_devices_t devices)
1727{
1728 int status = -EINVAL;
1729 mm_module_type mmtype = get_mm_module_for_format(config->format);
1730 struct qaf_module* qaf_mod = NULL;
1731 DEBUG_MSG("Flags 0x%x, Device 0x%x", flags, devices);
1732
1733 if (mmtype >= MAX_MM_MODULE_TYPE
1734 || p_qaf->qaf_mod[mmtype].qaf_audio_session_open == NULL
1735 || p_qaf->qaf_mod[mmtype].qaf_audio_stream_open == NULL) {
1736 ERROR_MSG("Unsupported Stream");
1737 return -ENOTSUP;
1738 }
1739
1740 //Open the module session, if not opened already.
1741 status = audio_extn_qaf_session_open(mmtype);
1742 qaf_mod = &(p_qaf->qaf_mod[mmtype]);
1743
1744 if ((status != 0) || (qaf_mod->session_handle == NULL)) {
1745 ERROR_MSG("Failed to open session.");
1746 return status;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301747 }
1748
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301749 audio_stream_config_t input_config;
1750 input_config.sample_rate = config->sample_rate;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301751 input_config.channels = popcount(config->channel_mask);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301752 input_config.format = config->format;
1753
Harsh Bansal28718c52017-04-20 13:47:12 +05301754 if (input_config.format != AUDIO_FORMAT_PCM_16_BIT) {
1755 input_config.format &= AUDIO_FORMAT_MAIN_MASK;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301756 }
1757
Harsh Bansal28718c52017-04-20 13:47:12 +05301758 DEBUG_MSG("stream_open sample_rate(%d) channels(%d) devices(%#x) flags(%#x) format(%#x)",
1759 input_config.sample_rate, input_config.channels, devices, flags, input_config.format);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301760
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301761 if (input_config.format == AUDIO_FORMAT_PCM_16_BIT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301762 //If PCM stream is already opened then fail this stream open.
1763 if (qaf_mod->stream_in[QAF_IN_PCM]) {
1764 ERROR_MSG("PCM input is already active.");
1765 return -ENOTSUP;
1766 }
1767
1768 //TODO: Flag can be system tone or external associated PCM.
1769 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle,
1770 &out->qaf_stream_handle,
1771 input_config,
1772 devices,
1773 AUDIO_STREAM_SYSTEM_TONE);
1774 qaf_mod->stream_in[QAF_IN_PCM] = out;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301775 } else if ((flags & AUDIO_OUTPUT_FLAG_MAIN) && (flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
1776 if (is_main_active(qaf_mod) || is_dual_main_active(qaf_mod)) {
1777 ERROR_MSG("Dual Main or Main already active. So, Cannot open main and associated stream");
1778 return -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301779 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301780 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN);
1781 if (status == 0) {
1782 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);
1783 qaf_mod->stream_in[QAF_IN_MAIN] = out;
1784 } else {
1785 ERROR_MSG("Stream Open FAILED !!!");
1786 }
1787 }
Naresh Tanniru908d9a02017-05-17 14:12:48 +05301788 } 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 +05301789 /* Assume Main if no flag is set */
1790 if (is_dual_main_active(qaf_mod)) {
1791 ERROR_MSG("Dual Main already active. So, Cannot open main stream");
1792 return -EINVAL;
1793 } else if (is_main_active(qaf_mod) && qaf_mod->stream_in[QAF_IN_ASSOC]) {
1794 ERROR_MSG("Main and Associated already active. So, Cannot open main stream");
1795 return -EINVAL;
1796 } else if (is_main_active(qaf_mod) && (mmtype != MS12)) {
1797 ERROR_MSG("Main already active and Not an MS12 format. So, Cannot open another main stream");
1798 return -EINVAL;
1799 } else {
1800 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN);
1801 if (status == 0) {
1802 DEBUG_MSG("Open stream for Input with only Main flag(%x) stream_handle(%p)", flags, out->qaf_stream_handle);
1803 if(qaf_mod->stream_in[QAF_IN_MAIN]) {
1804 qaf_mod->stream_in[QAF_IN_MAIN_2] = out;
1805 } else {
1806 qaf_mod->stream_in[QAF_IN_MAIN] = out;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301807 }
1808 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301809 ERROR_MSG("Stream Open FAILED !!!");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301810 }
1811 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301812 } else if ((flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
1813 if (is_dual_main_active(qaf_mod)) {
1814 ERROR_MSG("Dual Main already active. So, Cannot open associated stream");
1815 return -EINVAL;
1816 } else if (!is_main_active(qaf_mod)) {
1817 ERROR_MSG("Main not active. So, Cannot open associated stream");
1818 return -EINVAL;
1819 } else if (qaf_mod->stream_in[QAF_IN_ASSOC]) {
1820 ERROR_MSG("Associated already active. So, Cannot open associated stream");
1821 return -EINVAL;
1822 }
1823 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_ASSOCIATED);
1824 if (status == 0) {
1825 DEBUG_MSG("Open stream for Input with only Associated flag(%x) stream handle(%p)", flags, out->qaf_stream_handle);
1826 qaf_mod->stream_in[QAF_IN_ASSOC] = out;
1827 } else {
1828 ERROR_MSG("Stream Open FAILED !!!");
1829 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301830 }
1831
Harsh Bansal28718c52017-04-20 13:47:12 +05301832 if (status != 0) {
1833 //If no stream is active then close the session.
1834 qaf_session_close(qaf_mod);
1835 return status;
1836 }
1837
1838 //If Device is HDMI, QAF passthrough is enabled and there is no previous QAF passthrough input stream.
1839 if ((!p_qaf->passthrough_in)
1840 && (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1841 && audio_extn_qaf_passthrough_enabled(out)) {
1842 //Assign the QAF passthrough input stream.
1843 p_qaf->passthrough_in = out;
1844
1845 //If HDMI is connected and format is supported by HDMI then create QAF passthrough output stream.
1846 if (p_qaf->hdmi_connect
1847 && platform_is_edid_supported_format(p_qaf->adev->platform, out->format)) {
1848 status = create_qaf_passthrough_stream();
1849 if (status < 0) {
1850 qaf_stream_close(out);
1851 ERROR_MSG("QAF passthrough stream creation failed with error %d", status);
1852 return status;
1853 }
1854 }
1855 /*Else: since QAF passthrough input stream is already initialized,
1856 * when hdmi is connected
1857 * then qaf passthrough output stream will be created.
1858 */
1859 }
1860
1861 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301862 return status;
1863}
1864
Harsh Bansal28718c52017-04-20 13:47:12 +05301865/* Resume a QAF stream. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301866static int qaf_out_resume(struct audio_stream_out* stream)
1867{
1868 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05301869 int status = 0;
1870 DEBUG_MSG("Output Stream %p", out);
1871
1872
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301873 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301874
1875 //If QAF passthrough is active then block the resume on module input streams.
1876 if (p_qaf->passthrough_out) {
1877 //If resume is received for the QAF passthrough stream then call the primary HAL api.
1878 pthread_mutex_lock(&p_qaf->lock);
1879 if (p_qaf->passthrough_in == out) {
1880 status = p_qaf->passthrough_out->stream.resume(
1881 (struct audio_stream_out*)p_qaf->passthrough_out);
1882 if (!status) out->offload_state = OFFLOAD_STATE_PLAYING;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301883 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301884 pthread_mutex_unlock(&p_qaf->lock);
1885 } else {
1886 //Flush the module input stream.
1887 status = qaf_stream_start(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301888 }
1889
Harsh Bansal28718c52017-04-20 13:47:12 +05301890 unlock_output_stream(out);
1891
1892 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301893 return status;
1894}
1895
Harsh Bansal28718c52017-04-20 13:47:12 +05301896/* Offload thread for QAF output streams. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301897static void *qaf_offload_thread_loop(void *context)
1898{
Harsh Bansal28718c52017-04-20 13:47:12 +05301899 struct stream_out *out = (struct stream_out *)context;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301900 struct listnode *item;
1901 int ret = 0;
1902 struct str_parms *parms = NULL;
1903 int value = 0;
1904 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301905 struct qaf_module *qaf_mod = NULL;
1906
1907 qaf_mod = get_qaf_module_for_input_stream(out);
1908
1909 if (!qaf_mod) return NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301910
1911 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1912 set_sched_policy(0, SP_FOREGROUND);
1913 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1914
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301915 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301916
1917 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301918 for (;;) {
1919 struct offload_cmd *cmd = NULL;
1920 stream_callback_event_t event;
1921 bool send_callback = false;
1922
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301923 DEBUG_MSG_VV("List Empty %d (1:TRUE, 0:FALSE)", list_empty(&out->qaf_offload_cmd_list));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301924 if (list_empty(&out->qaf_offload_cmd_list)) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301925 DEBUG_MSG_VV("SLEEPING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301926 pthread_cond_wait(&out->qaf_offload_cond, &out->lock);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301927 DEBUG_MSG_VV("RUNNING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301928 continue;
1929 }
1930
1931 item = list_head(&out->qaf_offload_cmd_list);
1932 cmd = node_to_item(item, struct offload_cmd, node);
1933 list_remove(item);
1934
1935 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1936 free(cmd);
1937 break;
1938 }
1939
Harsh Bansal28718c52017-04-20 13:47:12 +05301940 unlock_output_stream(out);
1941
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301942 send_callback = false;
Harsh Bansal28718c52017-04-20 13:47:12 +05301943 switch (cmd->cmd) {
1944 case OFFLOAD_CMD_WAIT_FOR_BUFFER: {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301945 DEBUG_MSG_VV("wait for buffer availability");
Harsh Bansal28718c52017-04-20 13:47:12 +05301946
1947 while (1) {
1948 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle,
1949 "buf_available");
1950 if (kvpairs) {
1951 parms = str_parms_create_str(kvpairs);
1952 ret = str_parms_get_int(parms, "buf_available", &value);
1953 if (ret >= 0) {
1954 if (value >= (int)out->compr_config.fragment_size) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301955 DEBUG_MSG_VV("buffer available");
Harsh Bansal28718c52017-04-20 13:47:12 +05301956 str_parms_destroy(parms);
1957 parms = NULL;
1958 break;
1959 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301960 DEBUG_MSG_VV("sleep");
Harsh Bansal28718c52017-04-20 13:47:12 +05301961 str_parms_destroy(parms);
1962 parms = NULL;
1963 usleep(10000);
1964 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301965 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301966 free(kvpairs);
1967 kvpairs = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301968 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301969 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301970 send_callback = true;
1971 event = STREAM_CBK_EVENT_WRITE_READY;
1972 break;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301973 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301974 default:
1975 DEBUG_MSG("unknown command received: %d", cmd->cmd);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301976 break;
1977 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301978
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301979 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301980
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001981 if (send_callback && out->client_callback) {
1982 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301983 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301984
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301985 free(cmd);
1986 }
1987
1988 while (!list_empty(&out->qaf_offload_cmd_list)) {
1989 item = list_head(&out->qaf_offload_cmd_list);
1990 list_remove(item);
Harsh Bansal28718c52017-04-20 13:47:12 +05301991 free (node_to_item( item, struct offload_cmd, node));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301992 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301993 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301994
1995 return NULL;
1996}
1997
Harsh Bansal28718c52017-04-20 13:47:12 +05301998/* Create the offload callback thread for QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301999static int qaf_create_offload_callback_thread(struct stream_out *out)
2000{
Harsh Bansal28718c52017-04-20 13:47:12 +05302001 DEBUG_MSG("Output Stream %p", out);
2002 lock_output_stream(out);
2003 pthread_cond_init(&out->qaf_offload_cond, (const pthread_condattr_t *)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302004 list_init(&out->qaf_offload_cmd_list);
Harsh Bansal28718c52017-04-20 13:47:12 +05302005 pthread_create(&out->qaf_offload_thread,
2006 (const pthread_attr_t *)NULL,
2007 qaf_offload_thread_loop,
2008 out);
2009 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302010 return 0;
2011}
2012
Harsh Bansal28718c52017-04-20 13:47:12 +05302013/* Destroy the offload callback thread of QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302014static int qaf_destroy_offload_callback_thread(struct stream_out *out)
2015{
Harsh Bansal28718c52017-04-20 13:47:12 +05302016 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302017 qaf_send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302018
Harsh Bansal28718c52017-04-20 13:47:12 +05302019 pthread_join(out->qaf_offload_thread, (void **)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302020 pthread_cond_destroy(&out->qaf_offload_cond);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302021 return 0;
2022}
2023
Harsh Bansal28718c52017-04-20 13:47:12 +05302024/* Sets the stream set parameters (device routing information). */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302025static int qaf_out_set_parameters(struct audio_stream *stream, const char *kvpairs)
2026{
2027 struct str_parms *parms, *new_parms;
2028 char value[32];
2029 char *new_kv_pairs;
2030 int val = 0;
2031 struct stream_out *out = (struct stream_out *)stream;
2032 int ret = 0;
2033 int err = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05302034 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302035
Harsh Bansal28718c52017-04-20 13:47:12 +05302036 DEBUG_MSG("usecase(%d: %s) kvpairs: %s", out->usecase, use_case_table[out->usecase], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302037
Harsh Bansal28718c52017-04-20 13:47:12 +05302038 parms = str_parms_create_str(kvpairs);
2039 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2040 if (err < 0)
2041 return err;
2042 val = atoi(value);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302043
Harsh Bansal28718c52017-04-20 13:47:12 +05302044 qaf_mod = get_qaf_module_for_input_stream(out);
2045 if (!qaf_mod) return (-EINVAL);
2046
2047 //TODO: HDMI is connected but user doesn't want HDMI output, close both HDMI outputs.
2048
2049 /* Setting new device information to the mm module input streams.
2050 * This is needed if QAF module output streams are not created yet.
2051 */
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302052 out->devices = val;
Harsh Bansal28718c52017-04-20 13:47:12 +05302053
2054 if (val == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
2055 //If device is BT then open the BT stream if not already opened.
2056 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) == NULL
2057 && audio_extn_bt_hal_get_device(qaf_mod->bt_hdl) != NULL) {
2058 ret = audio_extn_bt_hal_open_output_stream(qaf_mod->bt_hdl,
2059 QAF_OUTPUT_SAMPLING_RATE,
2060 AUDIO_CHANNEL_OUT_STEREO,
2061 CODEC_BACKEND_DEFAULT_BIT_WIDTH);
2062 if (ret != 0) {
2063 ERROR_MSG("BT Output stream open failure!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302064 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302065 }
2066 } else if (val != 0) {
2067 //If device is not BT then close the BT stream if already opened.
2068 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
2069 audio_extn_bt_hal_close_output_stream(qaf_mod->bt_hdl);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302070 }
2071 }
2072
Harsh Bansal28718c52017-04-20 13:47:12 +05302073 if (p_qaf->passthrough_in == out) { //Device routing is received for QAF passthrough stream.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302074
Harsh Bansal28718c52017-04-20 13:47:12 +05302075 if (!(val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) { //HDMI route is disabled.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302076
Harsh Bansal28718c52017-04-20 13:47:12 +05302077 //If QAF pasthrough output is enabled. Close it.
2078 close_qaf_passthrough_stream();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302079
Harsh Bansal28718c52017-04-20 13:47:12 +05302080 //Send the routing information to mm module pcm output.
2081 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2082 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2083 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302084 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302085 //else: device info is updated in the input streams.
2086 } else { //HDMI route is enabled.
2087
2088 //create the QAf passthrough stream, if not created already.
2089 ret = create_qaf_passthrough_stream();
2090
2091 if (p_qaf->passthrough_out != NULL) { //If QAF passthrough out is enabled then send routing information.
2092 ret = p_qaf->passthrough_out->stream.common.set_parameters(
2093 (struct audio_stream *)p_qaf->passthrough_out, kvpairs);
2094 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302095 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302096 } else {
2097 //Send the routing information to mm module pcm output.
2098 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2099 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2100 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
2101 }
2102 //else: device info is updated in the input streams.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302103 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302104 str_parms_destroy(parms);
2105
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002106 return ret;
2107}
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302108
Harsh Bansal28718c52017-04-20 13:47:12 +05302109/* Checks if a stream is QAF stream or not. */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002110bool audio_extn_is_qaf_stream(struct stream_out *out)
2111{
Harsh Bansal28718c52017-04-20 13:47:12 +05302112 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002113
Harsh Bansal28718c52017-04-20 13:47:12 +05302114 if (qaf_mod) {
2115 return true;
2116 }
2117 return false;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002118}
2119
2120/* API to send playback stream specific config parameters */
2121int audio_extn_qaf_out_set_param_data(struct stream_out *out,
Harsh Bansal28718c52017-04-20 13:47:12 +05302122 audio_extn_param_id param_id,
2123 audio_extn_param_payload *payload)
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002124{
2125 int ret = -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05302126 int index;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002127 struct stream_out *new_out = NULL;
2128 struct audio_adsp_event *adsp_event;
Harsh Bansal28718c52017-04-20 13:47:12 +05302129 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002130
Harsh Bansal28718c52017-04-20 13:47:12 +05302131 if (!out || !qaf_mod || !payload) {
2132 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002133 return ret;
2134 }
2135
2136 /* In qaf output render session may not be opened at this time.
Harsh Bansal28718c52017-04-20 13:47:12 +05302137 to handle it store adsp_hdlr param info so that it can be
2138 applied later after opening render session from ms12 callback
2139 */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002140 if (param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302141 index = get_input_stream_index(out);
2142 if (index < 0) {
2143 ERROR_MSG("Invalid stream");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002144 return ret;
2145 }
2146 adsp_event = (struct audio_adsp_event *)payload;
2147
Harsh Bansal28718c52017-04-20 13:47:12 +05302148 if (payload->adsp_event_params.payload_length <= AUDIO_MAX_ADSP_STREAM_CMD_PAYLOAD_LEN) {
2149 pthread_mutex_lock(&p_qaf->lock);
2150 memcpy(qaf_mod->adsp_hdlr_config[index].event_payload,
2151 adsp_event->payload,
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002152 adsp_event->payload_length);
Harsh Bansal28718c52017-04-20 13:47:12 +05302153 qaf_mod->adsp_hdlr_config[index].event_params.payload =
2154 qaf_mod->adsp_hdlr_config[index].event_payload;
2155 qaf_mod->adsp_hdlr_config[index].event_params.payload_length =
2156 adsp_event->payload_length;
2157 qaf_mod->adsp_hdlr_config[index].adsp_hdlr_config_valid = true;
2158 pthread_mutex_unlock(&p_qaf->lock);
2159 } else {
2160 ERROR_MSG("Invalid adsp event length %d", adsp_event->payload_length);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002161 return ret;
2162 }
2163 ret = 0;
2164 }
2165
Harsh Bansal28718c52017-04-20 13:47:12 +05302166 /* apply param for all active out sessions */
2167 for (index = 0; index < MAX_QAF_MODULE_OUT; index++) {
2168 new_out = qaf_mod->stream_out[index];
2169 if (!new_out) continue;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002170
Harsh Bansal28718c52017-04-20 13:47:12 +05302171 /*ADSP event is not supported for passthrough*/
2172 if ((param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD)
2173 && !(new_out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) continue;
2174 if (new_out->standby)
2175 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
2176 lock_output_stream(new_out);
2177 ret = audio_extn_out_set_param_data(new_out, param_id, payload);
2178 if (ret)
2179 ERROR_MSG("audio_extn_out_set_param_data error %d", ret);
2180 unlock_output_stream(new_out);
2181 }
2182 return ret;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002183}
2184
2185int audio_extn_qaf_out_get_param_data(struct stream_out *out,
2186 audio_extn_param_id param_id,
2187 audio_extn_param_payload *payload)
2188{
Harsh Bansal28718c52017-04-20 13:47:12 +05302189 int ret = -EINVAL, i;
2190 struct stream_out *new_out;
2191 struct audio_usecase *uc_info;
2192 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002193
Harsh Bansal28718c52017-04-20 13:47:12 +05302194 if (!out || !qaf_mod || !payload) {
2195 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002196 return ret;
2197 }
2198
Harsh Bansal28718c52017-04-20 13:47:12 +05302199 if (!p_qaf->hdmi_connect) {
2200 ERROR_MSG("hdmi not connected");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002201 return ret;
2202 }
2203
2204 /* get session which is routed to hdmi*/
Harsh Bansal28718c52017-04-20 13:47:12 +05302205 if (p_qaf->passthrough_out)
2206 new_out = p_qaf->passthrough_out;
2207 else {
2208 for (i = 0; i < MAX_QAF_MODULE_OUT; i++) {
2209 if (qaf_mod->stream_out[i]) {
2210 new_out = qaf_mod->stream_out[i];
2211 break;
2212 }
2213 }
2214 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002215
2216 if (!new_out) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302217 ERROR_MSG("No stream active.");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002218 return ret;
2219 }
2220
2221 if (new_out->standby)
Harsh Bansal28718c52017-04-20 13:47:12 +05302222 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002223
2224 lock_output_stream(new_out);
2225 ret = audio_extn_out_get_param_data(new_out, param_id, payload);
Harsh Bansal28718c52017-04-20 13:47:12 +05302226 if (ret)
2227 ERROR_MSG("audio_extn_out_get_param_data error %d", ret);
2228 unlock_output_stream(new_out);
2229
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302230 return ret;
2231}
2232
Harsh Bansal28718c52017-04-20 13:47:12 +05302233/* To open a stream with QAF. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302234int audio_extn_qaf_open_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302235 audio_io_handle_t handle,
2236 audio_devices_t devices,
2237 audio_output_flags_t flags,
2238 struct audio_config *config,
2239 struct audio_stream_out **stream_out,
2240 const char *address)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302241{
2242 int ret = 0;
2243 struct stream_out *out;
2244
2245 ret = adev_open_output_stream(dev, handle, devices, flags, config, stream_out, address);
2246 if (*stream_out == NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302247 ERROR_MSG("Stream open failed %d", ret);
Deepak Agarwal1e42b852017-02-11 17:57:04 +05302248 return ret;
2249 }
2250
Harsh Bansal28718c52017-04-20 13:47:12 +05302251 out = (struct stream_out *)*stream_out;
2252
2253 ret = qaf_stream_open(out, config, flags, devices);
2254 if (ret == -ENOTSUP) {
2255 //Stream not supported by QAF, Bypass QAF.
2256 return 0;
2257 } else if (ret < 0) {
2258 ERROR_MSG("Error opening QAF stream err[%d]!", ret);
2259 adev_close_output_stream(dev, *stream_out);
2260 return ret;
2261 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302262
2263 /* Override function pointers based on qaf definitions */
2264 out->stream.set_volume = qaf_out_set_volume;
2265 out->stream.pause = qaf_out_pause;
2266 out->stream.resume = qaf_out_resume;
2267 out->stream.drain = qaf_out_drain;
2268 out->stream.flush = qaf_out_flush;
2269
2270 out->stream.common.standby = qaf_out_standby;
2271 out->stream.common.set_parameters = qaf_out_set_parameters;
2272 out->stream.get_latency = qaf_out_get_latency;
2273 out->stream.write = qaf_out_write;
2274 out->stream.get_presentation_position = qaf_out_get_presentation_position;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302275 out->platform_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302276
Harsh Bansal28718c52017-04-20 13:47:12 +05302277 /*TODO: Need to handle this for DTS*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302278 if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) {
2279 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
Varun B34da7a42017-02-13 16:16:53 +05302280 out->config.period_size = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302281 out->config.period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT;
Varun B34da7a42017-02-13 16:16:53 +05302282 out->config.start_threshold = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
2283 out->config.avail_min = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302284 } else if(out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
2285 out->compr_config.fragment_size = qaf_get_pcm_offload_input_buffer_size(&(config->offload_info));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302286 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302287
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302288 *stream_out = &out->stream;
2289 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2290 qaf_create_offload_callback_thread(out);
2291 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302292
2293 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302294 return 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302295}
2296
Harsh Bansal28718c52017-04-20 13:47:12 +05302297/* Close a QAF stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302298void audio_extn_qaf_close_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302299 struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302300{
2301 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05302302 struct qaf_module* qaf_mod = get_qaf_module_for_input_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302303
Harsh Bansal28718c52017-04-20 13:47:12 +05302304 if (!qaf_mod) return;
2305
2306 DEBUG_MSG("stream_handle(%p) format = %x", out, out->format);
2307
2308 //If close is received for QAF passthrough stream then close the QAF passthrough output.
2309 if (p_qaf->passthrough_in == out) {
2310 if (p_qaf->passthrough_out) {
2311 ALOGD("%s %d closing stream handle %p", __func__, __LINE__, p_qaf->passthrough_out);
2312 pthread_mutex_lock(&p_qaf->lock);
2313 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2314 (struct audio_stream_out *)(p_qaf->passthrough_out));
2315 pthread_mutex_unlock(&p_qaf->lock);
2316 p_qaf->passthrough_out = NULL;
2317 }
2318
2319 p_qaf->passthrough_in = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302320 }
2321
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302322 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2323 qaf_destroy_offload_callback_thread(out);
2324 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002325
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302326 qaf_stream_close(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302327
2328 adev_close_output_stream(dev, stream);
Harsh Bansal28718c52017-04-20 13:47:12 +05302329
2330 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302331}
2332
Harsh Bansal28718c52017-04-20 13:47:12 +05302333/* Check if QAF is supported or not. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302334bool audio_extn_qaf_is_enabled()
2335{
2336 bool prop_enabled = false;
2337 char value[PROPERTY_VALUE_MAX] = {0};
2338 property_get("audio.qaf.enabled", value, NULL);
2339 prop_enabled = atoi(value) || !strncmp("true", value, 4);
2340 return (prop_enabled);
2341}
2342
Harsh Bansal28718c52017-04-20 13:47:12 +05302343/* Query HDMI EDID and sets module output accordingly.*/
2344void set_hdmi_configuration_to_module()
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302345{
Harsh Bansal28718c52017-04-20 13:47:12 +05302346 int channels = 0;
2347 char *format_params;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302348 struct str_parms *qaf_params;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302349 char prop_value[PROPERTY_VALUE_MAX];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302350 bool passth_support = false;
2351
Harsh Bansal28718c52017-04-20 13:47:12 +05302352 DEBUG_MSG("Entry");
2353
2354 if (!p_qaf) {
2355 return;
2356 }
2357
2358 if (!p_qaf->hdmi_connect) {
2359 DEBUG_MSG("HDMI is not connected.");
2360 return;
2361 }
2362
2363 p_qaf->hdmi_sink_channels = 0;
2364
2365 //QAF re-encoding and DSP offload passthrough is supported.
2366 if (property_get_bool("audio.offload.passthrough", false)
2367 && property_get_bool("audio.qaf.reencode", false)) {
2368
2369 //If MS12 session is active.
2370 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2371
2372 bool do_setparam = false;
2373 qaf_params = str_parms_create();
2374 property_get("audio.qaf.hdmi.out", prop_value, NULL);
2375
2376 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_E_AC3)
2377 && (strncmp(prop_value, "ddp", 3) == 0)) {
2378 do_setparam = true;
2379 if (qaf_params) {
2380 str_parms_add_str(qaf_params,
2381 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2382 AUDIO_QAF_PARAMETER_VALUE_REENCODE_EAC3);
2383 }
2384 } else if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_AC3)) {
2385 do_setparam = true;
2386 if (qaf_params) {
2387 str_parms_add_str(qaf_params,
2388 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2389 AUDIO_QAF_PARAMETER_VALUE_REENCODE_AC3);
2390 }
2391 }
2392
2393 if (do_setparam) {
2394 if (p_qaf->qaf_msmd_enabled) {
2395 str_parms_add_str(qaf_params,
2396 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2397 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2398 } else {
2399 str_parms_add_str(qaf_params,
2400 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2401 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2402 }
2403 format_params = str_parms_to_str(qaf_params);
2404
2405 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2406 format_params);
2407
2408 passth_support = true;
2409 }
2410 str_parms_destroy(qaf_params);
2411 }
2412
2413 //DTS_M8 session is active.
2414 if (p_qaf->qaf_mod[DTS_M8].session_handle
2415 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2416
2417 bool do_setparam = false;
2418 qaf_params = str_parms_create();
2419#if 0 //TODO: Need to enable with DTS_M8 wrapper.
2420 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_DTS)) {
2421 do_setparam = true;
2422 if (qaf_params) {
2423 str_parms_add_str(qaf_params,
2424 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2425 AUDIO_QAF_PARAMETER_VALUE_REENCODE_DTS);
2426 }
2427 }
2428#endif
2429
2430 if (do_setparam) {
2431 if (p_qaf->qaf_msmd_enabled) {
2432 str_parms_add_str(qaf_params,
2433 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2434 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2435 } else {
2436 str_parms_add_str(qaf_params,
2437 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2438 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2439 }
2440 format_params = str_parms_to_str(qaf_params);
2441
2442 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2443 format_params);
2444
2445 passth_support = true;
2446 }
2447 str_parms_destroy(qaf_params);
2448 }
2449 }
2450
2451 //Compressed passthrough is not enabled.
2452 if (!passth_support) {
2453
2454 channels = platform_edid_get_max_channels(p_qaf->adev->platform);
2455
2456 qaf_params = str_parms_create();
2457 switch (channels) {
2458 case 8:
2459 DEBUG_MSG("Switching Qaf output to 7.1 channels");
2460 str_parms_add_str(qaf_params,
2461 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2462 AUDIO_QAF_PARAMETER_VALUE_8_CHANNELS);
2463 if (p_qaf->qaf_msmd_enabled) {
2464 str_parms_add_str(qaf_params,
2465 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2466 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2467 } else {
2468 str_parms_add_str(qaf_params,
2469 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2470 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2471 }
2472 p_qaf->hdmi_sink_channels = channels;
2473 break;
2474 case 6:
2475 DEBUG_MSG("Switching Qaf output to 5.1 channels");
2476 str_parms_add_str(qaf_params,
2477 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2478 AUDIO_QAF_PARAMETER_VALUE_6_CHANNELS);
2479 if (p_qaf->qaf_msmd_enabled) {
2480 str_parms_add_str(qaf_params,
2481 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2482 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2483 } else {
2484 str_parms_add_str(qaf_params,
2485 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2486 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2487 }
2488 p_qaf->hdmi_sink_channels = channels;
2489 break;
2490 default:
2491 DEBUG_MSG("Switching Qaf output to default channels");
2492 str_parms_add_str(qaf_params,
2493 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2494 AUDIO_QAF_PARAMETER_VALUE_DEFAULT_CHANNELS);
2495 if (p_qaf->qaf_msmd_enabled) {
2496 str_parms_add_str(qaf_params,
2497 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2498 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2499 } else {
2500 str_parms_add_str(qaf_params,
2501 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2502 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2503 }
2504 p_qaf->hdmi_sink_channels = 2;
2505 break;
2506 }
2507
2508 format_params = str_parms_to_str(qaf_params);
2509
2510 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2511 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2512 format_params);
2513 }
2514 if (p_qaf->qaf_mod[DTS_M8].session_handle
2515 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2516 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2517 format_params);
2518 }
2519
2520 str_parms_destroy(qaf_params);
2521 }
2522 DEBUG_MSG("Exit");
2523}
2524
2525/* QAF set parameter function. For Device connect and disconnect. */
2526int audio_extn_qaf_set_parameters(struct audio_device *adev, struct str_parms *parms)
2527{
2528 int status = 0, val = 0, k;
2529 char *format_params, *kv_parirs;
2530 struct str_parms *qaf_params;
2531 char value[32];
2532
2533 DEBUG_MSG("Entry");
2534
2535 if (!p_qaf) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302536 return -EINVAL;
2537 }
2538
2539 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
Harsh Bansal28718c52017-04-20 13:47:12 +05302540
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302541 if (status >= 0) {
2542 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302543 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is connected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302544
Harsh Bansal28718c52017-04-20 13:47:12 +05302545 p_qaf->hdmi_connect = 1;
2546 p_qaf->hdmi_sink_channels = 0;
2547
2548 if (p_qaf->passthrough_in) { //If QAF passthrough is already initialized.
2549 lock_output_stream(p_qaf->passthrough_in);
2550 if (platform_is_edid_supported_format(adev->platform,
2551 p_qaf->passthrough_in->format)) {
2552 //If passthrough format is supported by HDMI then create the QAF passthrough output if not created already.
2553 create_qaf_passthrough_stream();
2554 //Ignoring the returned error, If error then QAF passthrough is disabled.
2555 } else {
2556 //If passthrough format is not supported by HDMI then close the QAF passthrough output if already created.
2557 close_qaf_passthrough_stream();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302558 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302559 unlock_output_stream(p_qaf->passthrough_in);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302560 }
2561
Harsh Bansal28718c52017-04-20 13:47:12 +05302562 set_hdmi_configuration_to_module();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302563
Bharath Gopal01310bb2016-12-05 15:39:32 +05302564 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302565 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2566 if (!p_qaf->qaf_mod[k].bt_hdl) {
2567 DEBUG_MSG("Opening a2dp output...");
2568 status = audio_extn_bt_hal_load(&p_qaf->qaf_mod[k].bt_hdl);
2569 if (status != 0) {
2570 ERROR_MSG("Error opening BT module");
2571 return status;
2572 }
2573 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05302574 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302575 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302576 //TODO else if: Need to consider other devices.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302577 }
2578
2579 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
2580 if (status >= 0) {
2581 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302582 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is disconnected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302583
Harsh Bansal28718c52017-04-20 13:47:12 +05302584 qaf_params = str_parms_create();
2585 str_parms_add_str(qaf_params,
2586 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2587 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2588 str_parms_add_str(qaf_params,
2589 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2590 AUDIO_QAF_PARAMETER_VALUE_PCM);
2591 p_qaf->hdmi_sink_channels = 0;
2592
2593 p_qaf->passthrough_enabled = 0;
2594 p_qaf->mch_pcm_hdmi_enabled = 0;
2595 p_qaf->hdmi_connect = 0;
2596
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302597 format_params = str_parms_to_str(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302598
2599 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2600 if (p_qaf->qaf_mod[k].session_handle
2601 && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2602 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2603 p_qaf->qaf_mod[k].session_handle, format_params);
2604 }
2605 }
2606 close_all_hdmi_output();
2607
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302608 str_parms_destroy(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302609 close_qaf_passthrough_stream();
Bharath Gopal01310bb2016-12-05 15:39:32 +05302610 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302611 DEBUG_MSG("Closing a2dp output...");
2612 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2613 if (p_qaf->qaf_mod[k].bt_hdl) {
2614 audio_extn_bt_hal_unload(p_qaf->qaf_mod[k].bt_hdl);
2615 p_qaf->qaf_mod[k].bt_hdl = NULL;
2616 }
2617 }
2618 }
2619 //TODO else if: Need to consider other devices.
2620 }
2621
2622 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2623 kv_parirs = str_parms_to_str(parms);
2624 if (p_qaf->qaf_mod[k].session_handle && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2625 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2626 p_qaf->qaf_mod[k].session_handle, kv_parirs);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302627 }
2628 }
2629
Harsh Bansal28718c52017-04-20 13:47:12 +05302630 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302631 return status;
2632}
2633
Harsh Bansal28718c52017-04-20 13:47:12 +05302634/* Create the QAF. */
2635int audio_extn_qaf_init(struct audio_device *adev)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302636{
Harsh Bansal28718c52017-04-20 13:47:12 +05302637 DEBUG_MSG("Entry");
2638
2639 p_qaf = calloc(1, sizeof(struct qaf));
2640 if (p_qaf == NULL) {
2641 ERROR_MSG("Out of memory");
2642 return -ENOMEM;
2643 }
2644
2645 p_qaf->adev = adev;
2646
2647 if (property_get_bool("audio.qaf.msmd", false)) {
2648 p_qaf->qaf_msmd_enabled = 1;
2649 }
2650 pthread_mutex_init(&p_qaf->lock, (const pthread_mutexattr_t *) NULL);
2651
2652 int i = 0;
2653
2654 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2655 char value[PROPERTY_VALUE_MAX] = {0};
2656 char lib_name[PROPERTY_VALUE_MAX] = {0};
2657 struct qaf_module *qaf_mod = &(p_qaf->qaf_mod[i]);
2658
2659 if (i == MS12) {
2660 property_get("audio.qaf.library", value, NULL);
2661 } else if (i == DTS_M8) {
2662 property_get("audio.qaf.m8.library", value, NULL);
2663 } else {
2664 continue;
2665 }
2666
2667 snprintf(lib_name, PROPERTY_VALUE_MAX, "%s", value);
2668
2669 qaf_mod->qaf_lib = dlopen(lib_name, RTLD_NOW);
2670 if (qaf_mod->qaf_lib == NULL) {
2671 ERROR_MSG("DLOPEN failed for %s", lib_name);
2672 continue;
2673 }
2674
2675 DEBUG_MSG("DLOPEN successful for %s", lib_name);
2676 qaf_mod->qaf_audio_session_open =
2677 (int (*)(audio_session_handle_t* session_handle, void *p_data, void* license_data))dlsym(qaf_mod->qaf_lib,
2678 "audio_session_open");
2679 qaf_mod->qaf_audio_session_close =
2680 (int (*)(audio_session_handle_t session_handle))dlsym(qaf_mod->qaf_lib,
2681 "audio_session_close");
2682 qaf_mod->qaf_audio_stream_open =
2683 (int (*)(audio_session_handle_t session_handle, audio_stream_handle_t* stream_handle,
2684 audio_stream_config_t input_config, audio_devices_t devices, stream_type_t flags))dlsym(qaf_mod->qaf_lib,
2685 "audio_stream_open");
2686 qaf_mod->qaf_audio_stream_close =
2687 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2688 "audio_stream_close");
2689 qaf_mod->qaf_audio_stream_set_param =
2690 (int (*)(audio_stream_handle_t stream_handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2691 "audio_stream_set_param");
2692 qaf_mod->qaf_audio_session_set_param =
2693 (int (*)(audio_session_handle_t handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2694 "audio_session_set_param");
2695 qaf_mod->qaf_audio_stream_get_param =
2696 (char* (*)(audio_stream_handle_t stream_handle, const char* key))dlsym(qaf_mod->qaf_lib,
2697 "audio_stream_get_param");
2698 qaf_mod->qaf_audio_session_get_param =
2699 (char* (*)(audio_session_handle_t handle, const char* key))dlsym(qaf_mod->qaf_lib,
2700 "audio_session_get_param");
2701 qaf_mod->qaf_audio_stream_start =
2702 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2703 "audio_stream_start");
2704 qaf_mod->qaf_audio_stream_stop =
2705 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2706 "audio_stream_stop");
2707 qaf_mod->qaf_audio_stream_pause =
2708 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2709 "audio_stream_pause");
2710 qaf_mod->qaf_audio_stream_flush =
2711 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2712 "audio_stream_flush");
2713 qaf_mod->qaf_audio_stream_write =
2714 (int (*)(audio_stream_handle_t stream_handle, const void* buf, int size))dlsym(qaf_mod->qaf_lib,
2715 "audio_stream_write");
2716 qaf_mod->qaf_register_event_callback =
2717 (void (*)(audio_session_handle_t session_handle, void *priv_data, notify_event_callback_t event_callback,
2718 audio_event_id_t event_id))dlsym(qaf_mod->qaf_lib,
2719 "register_event_callback");
2720 }
2721
2722 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302723 return 0;
2724}
2725
Harsh Bansal28718c52017-04-20 13:47:12 +05302726/* Tear down the qaf extension. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302727void audio_extn_qaf_deinit()
2728{
Harsh Bansal28718c52017-04-20 13:47:12 +05302729 int i;
2730 DEBUG_MSG("Entry");
2731
2732 if (p_qaf != NULL) {
2733 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2734 qaf_session_close(&p_qaf->qaf_mod[i]);
2735
2736 if (p_qaf->qaf_mod[i].qaf_lib != NULL) {
2737 dlclose(p_qaf->qaf_mod[i].qaf_lib);
2738 p_qaf->qaf_mod[i].qaf_lib = NULL;
2739 }
2740 }
2741 if (p_qaf->passthrough_out) {
2742 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2743 (struct audio_stream_out *)(p_qaf->passthrough_out));
2744 p_qaf->passthrough_out = NULL;
2745 }
2746
2747 pthread_mutex_destroy(&p_qaf->lock);
2748 free(p_qaf);
2749 p_qaf = NULL;
2750 }
2751 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302752}