blob: ed669346fe563a3be7e7b55089ddbd79430a9c53 [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
Harsh Bansal28718c52017-04-20 13:47:12 +053039#define DEBUG_MSG(arg,...) ALOGV("%s: %d: " arg, __func__, __LINE__, ##__VA_ARGS__)
40#define ERROR_MSG(arg,...) ALOGE("%s: %d: " arg, __func__, __LINE__, ##__VA_ARGS__)
41
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053042#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 2
Harsh Bansal28718c52017-04-20 13:47:12 +053043#define COMPRESS_PASSTHROUGH_DDP_FRAGMENT_SIZE 4608
44
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053045#define QAF_DEFAULT_COMPR_AUDIO_HANDLE 1001
46#define QAF_DEFAULT_COMPR_PASSTHROUGH_HANDLE 1002
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053047#define QAF_DEFAULT_PASSTHROUGH_HANDLE 1003
Ben Rombergerd771a7c2017-02-22 18:05:17 -080048
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053049#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 300
Varun B34da7a42017-02-13 16:16:53 +053050
Varun B34da7a42017-02-13 16:16:53 +053051#define MIN_PCM_OFFLOAD_FRAGMENT_SIZE 512
52#define MAX_PCM_OFFLOAD_FRAGMENT_SIZE (240 * 1024)
53
54#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
55#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
56
57/* Pcm input node buffer size is 6144 bytes, i.e, 32msec for 48000 samplerate */
Harsh Bansal28718c52017-04-20 13:47:12 +053058#define QAF_MODULE_PCM_INPUT_BUFFER_LATENCY 32
Varun B34da7a42017-02-13 16:16:53 +053059
Harsh Bansal28718c52017-04-20 13:47:12 +053060#define MS12_PCM_OUT_FRAGMENT_SIZE 1536 //samples
Varun B34da7a42017-02-13 16:16:53 +053061
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053062#define DD_FRAME_SIZE 1536
Harsh Bansal28718c52017-04-20 13:47:12 +053063#define DDP_FRAME_SIZE DD_FRAME_SIZE
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053064/*
Harsh Bansal28718c52017-04-20 13:47:12 +053065 * DD encoder output size for one frame.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053066 */
67#define DD_ENCODER_OUTPUT_SIZE 2560
68/*
Harsh Bansal28718c52017-04-20 13:47:12 +053069 * DDP encoder output size for one frame.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053070 */
71#define DDP_ENCODER_OUTPUT_SIZE 4608
72
Harsh Bansal28718c52017-04-20 13:47:12 +053073/*********TODO Need to get correct values.*************************/
74
75#define DTS_PCM_OUT_FRAGMENT_SIZE 1024 //samples
76
77#define DTS_FRAME_SIZE 1536
78#define DTSHD_FRAME_SIZE DTS_FRAME_SIZE
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053079/*
Harsh Bansal28718c52017-04-20 13:47:12 +053080 * DTS encoder output size for one frame.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053081 */
Harsh Bansal28718c52017-04-20 13:47:12 +053082#define DTS_ENCODER_OUTPUT_SIZE 2560
83/*
84 * DTSHD encoder output size for one frame.
85 */
86#define DTSHD_ENCODER_OUTPUT_SIZE 4608
87/******************************************************************/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053088
Bharath Gopal01310bb2016-12-05 15:39:32 +053089/*
90 * QAF Latency to process buffers since out_write from primary HAL
91 */
92#define QAF_COMPRESS_OFFLOAD_PROCESSING_LATENCY 18
93#define QAF_PCM_OFFLOAD_PROCESSING_LATENCY 48
94
Harsh Bansal28718c52017-04-20 13:47:12 +053095//TODO: Need to handle for DTS
Varun B34da7a42017-02-13 16:16:53 +053096#define QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1536
97
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053098#include <stdlib.h>
99#include <pthread.h>
100#include <errno.h>
101#include <dlfcn.h>
102#include <sys/resource.h>
103#include <sys/prctl.h>
104#include <cutils/properties.h>
105#include <cutils/str_parms.h>
106#include <cutils/log.h>
107#include <cutils/atomic.h>
108#include "audio_utils/primitives.h"
109#include "audio_hw.h"
110#include "platform_api.h"
111#include <platform.h>
112#include <system/thread_defs.h>
113#include <cutils/sched_policy.h>
114#include "audio_extn.h"
115#include <qti_audio.h>
116#include "sound/compress_params.h"
117
Harsh Bansal28718c52017-04-20 13:47:12 +0530118//TODO: Need to remove this.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530119#define QAF_OUTPUT_SAMPLING_RATE 48000
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530120
121#ifdef QAF_DUMP_ENABLED
122FILE *fp_output_writer_hdmi = NULL;
123#endif
124
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800125struct qaf_adsp_hdlr_config_state {
126 struct audio_adsp_event event_params;
127 /* For holding client audio_adsp_event payload */
128 uint8_t event_payload[AUDIO_MAX_ADSP_STREAM_CMD_PAYLOAD_LEN];
129 bool adsp_hdlr_config_valid;
130};
131
Harsh Bansal28718c52017-04-20 13:47:12 +0530132//Types of MM module, currently supported by QAF.
133typedef enum {
134 MS12,
135 DTS_M8,
136 MAX_MM_MODULE_TYPE,
137 INVALID_MM_MODULE
138} mm_module_type;
139
140typedef enum {
141 QAF_OUT_TRANSCODE_PASSTHROUGH = 0, /* Transcode passthrough via MM module*/
142 QAF_OUT_OFFLOAD_MCH, /* Multi-channel PCM offload*/
143 QAF_OUT_OFFLOAD, /* PCM offload */
144
145 MAX_QAF_MODULE_OUT
146} mm_module_output_type;
147
148typedef enum {
149 QAF_IN_MAIN = 0, /* Single PID Main/Primary or Dual-PID stream */
150 QAF_IN_ASSOC, /* Associated/Secondary stream */
151 QAF_IN_PCM, /* PCM stream. */
152
153 MAX_QAF_MODULE_IN
154} mm_module_input_type;
155
156struct qaf_module {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530157 audio_session_handle_t session_handle;
158 void *qaf_lib;
Harsh Bansal28718c52017-04-20 13:47:12 +0530159 int (*qaf_audio_session_open)(audio_session_handle_t* session_handle,
160 void *p_data,
161 void* license_data);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530162 int (*qaf_audio_session_close)(audio_session_handle_t session_handle);
Harsh Bansal28718c52017-04-20 13:47:12 +0530163 int (*qaf_audio_stream_open)(audio_session_handle_t session_handle,
164 audio_stream_handle_t* stream_handle,
165 audio_stream_config_t input_config,
166 audio_devices_t devices,
167 stream_type_t flags);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530168 int (*qaf_audio_stream_close)(audio_stream_handle_t stream_handle);
169 int (*qaf_audio_stream_set_param)(audio_stream_handle_t stream_handle, const char* kv_pairs);
170 int (*qaf_audio_session_set_param)(audio_session_handle_t handle, const char* kv_pairs);
171 char* (*qaf_audio_stream_get_param)(audio_stream_handle_t stream_handle, const char* key);
172 char* (*qaf_audio_session_get_param)(audio_session_handle_t handle, const char* key);
173 int (*qaf_audio_stream_start)(audio_stream_handle_t handle);
174 int (*qaf_audio_stream_stop)(audio_stream_handle_t stream_handle);
175 int (*qaf_audio_stream_pause)(audio_stream_handle_t stream_handle);
176 int (*qaf_audio_stream_flush)(audio_stream_handle_t stream_handle);
177 int (*qaf_audio_stream_write)(audio_stream_handle_t stream_handle, const void* buf, int size);
Harsh Bansal28718c52017-04-20 13:47:12 +0530178 void (*qaf_register_event_callback)(audio_session_handle_t session_handle,
179 void *priv_data,
180 notify_event_callback_t event_callback,
181 audio_event_id_t event_id);
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800182
Harsh Bansal28718c52017-04-20 13:47:12 +0530183 /*Input stream of MM module */
184 struct stream_out *stream_in[MAX_QAF_MODULE_IN];
185 /*Output Stream from MM module */
186 struct stream_out *stream_out[MAX_QAF_MODULE_OUT];
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800187
Harsh Bansal28718c52017-04-20 13:47:12 +0530188 struct qaf_adsp_hdlr_config_state adsp_hdlr_config[MAX_QAF_MODULE_IN];
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800189
Harsh Bansal28718c52017-04-20 13:47:12 +0530190 //BT session handle.
Bharath Gopal01310bb2016-12-05 15:39:32 +0530191 void *bt_hdl;
Harsh Bansal28718c52017-04-20 13:47:12 +0530192
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530193 float vol_left;
194 float vol_right;
Harsh Bansal28718c52017-04-20 13:47:12 +0530195 bool is_vol_set;
196 bool drain_received[MAX_QAF_MODULE_IN];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530197};
198
Harsh Bansal28718c52017-04-20 13:47:12 +0530199struct qaf {
200 struct audio_device *adev;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530201
Harsh Bansal28718c52017-04-20 13:47:12 +0530202 pthread_mutex_t lock;
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800203
Harsh Bansal28718c52017-04-20 13:47:12 +0530204 bool hdmi_connect;
205 int hdmi_sink_channels;
206
207 //Flag to indicate if QAF transcode output stream is enabled from any mm module.
208 bool passthrough_enabled;
209 //Flag to indicate if QAF mch pcm output stream is enabled from any mm module.
210 bool mch_pcm_hdmi_enabled;
211
212 //Flag to indicate if msmd is supported.
213 bool qaf_msmd_enabled;
214
215 //Handle of QAF input stream, which is routed as QAF passthrough.
216 struct stream_out *passthrough_in;
217 //Handle of QAF passthrough stream.
218 struct stream_out *passthrough_out;
219
220 struct qaf_module qaf_mod[MAX_MM_MODULE_TYPE];
221};
222
223static int qaf_out_pause(struct audio_stream_out* stream);
224static int qaf_out_flush(struct audio_stream_out* stream);
225static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type);
226
227//Global handle of QAF. Access to this should be protected by mutex lock.
228static struct qaf *p_qaf = NULL;
229
230/* Gets the pointer to qaf module for the qaf input stream. */
231static struct qaf_module* get_qaf_module_for_input_stream(struct stream_out *out)
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530232{
Harsh Bansal28718c52017-04-20 13:47:12 +0530233 struct qaf_module *qaf_mod = NULL;
234 int i, j;
235 if (!p_qaf) return NULL;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530236
Harsh Bansal28718c52017-04-20 13:47:12 +0530237 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
238 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
239 if (p_qaf->qaf_mod[i].stream_in[j] == out) {
240 qaf_mod = &(p_qaf->qaf_mod[i]);
241 break;
242 }
243 }
244 }
245
246 return qaf_mod;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530247}
248
Harsh Bansal28718c52017-04-20 13:47:12 +0530249/* Finds the mm module input stream index for the QAF input stream. */
250static int get_input_stream_index(struct stream_out *out)
251{
252 int index = -1, j;
253 struct qaf_module* qaf_mod = NULL;
254
255 qaf_mod = get_qaf_module_for_input_stream(out);
256 if (!qaf_mod) return index;
257
258 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
259 if (qaf_mod->stream_in[j] == out) {
260 index = j;
261 break;
262 }
263 }
264
265 return index;
266}
267
268/* Finds the right mm module for the QAF input stream format. */
269static mm_module_type get_mm_module_for_format(audio_format_t format)
270{
271 int j;
272
273 DEBUG_MSG("Format 0x%x", format);
274
275 if (format == AUDIO_FORMAT_PCM_16_BIT) {
276 //If dts is not supported then alway support pcm with MS12
277 if (!property_get_bool("audio.qaf.dts_m8", false)) { //TODO: Need to add this property for DTS.
278 return MS12;
279 }
280
281 //If QAF passthrough is active then send the PCM stream to primary HAL.
282 if (!p_qaf->passthrough_out) {
283 /* Iff any stream is active in MS12 module then route PCM stream to it. */
284 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
285 if (p_qaf->qaf_mod[MS12].stream_in[j]) {
286 return MS12;
287 }
288 }
289 }
290 return INVALID_MM_MODULE;
291 }
292
293 switch (format & AUDIO_FORMAT_MAIN_MASK) {
294 case AUDIO_FORMAT_AC3:
295 case AUDIO_FORMAT_E_AC3:
296 case AUDIO_FORMAT_AAC:
297 case AUDIO_FORMAT_AAC_ADTS:
298 case AUDIO_FORMAT_AC4:
299 return MS12;
300 case AUDIO_FORMAT_DTS:
301 case AUDIO_FORMAT_DTS_HD:
302 return DTS_M8;
303 default:
304 return INVALID_MM_MODULE;
305 }
306}
307
308/* Gets the pcm output buffer size(in samples) for the mm module. */
309static uint32_t get_pcm_output_buffer_size_samples(struct qaf_module *qaf_mod)
310{
311 uint32_t pcm_output_buffer_size = 0;
312
313 if (qaf_mod == &p_qaf->qaf_mod[MS12]) {
314 pcm_output_buffer_size = MS12_PCM_OUT_FRAGMENT_SIZE;
315 } else if (qaf_mod == &p_qaf->qaf_mod[DTS_M8]) {
316 pcm_output_buffer_size = DTS_PCM_OUT_FRAGMENT_SIZE;
317 }
318
319 return pcm_output_buffer_size;
320}
321
322/* Acquire Mutex lock on output stream */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530323static void lock_output_stream(struct stream_out *out)
324{
325 pthread_mutex_lock(&out->pre_lock);
326 pthread_mutex_lock(&out->lock);
327 pthread_mutex_unlock(&out->pre_lock);
328}
329
Harsh Bansal28718c52017-04-20 13:47:12 +0530330/* Release Mutex lock on output stream */
331static void unlock_output_stream(struct stream_out *out)
332{
333 pthread_mutex_unlock(&out->lock);
334}
335
336/* Checks if stream can be routed as QAF passthrough or not. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530337static bool audio_extn_qaf_passthrough_enabled(struct stream_out *out)
338{
Harsh Bansal28718c52017-04-20 13:47:12 +0530339 DEBUG_MSG("Format 0x%x", out->format);
340 bool is_enabled = false;
341
342 if (!p_qaf) return false;
343
344 if ((!property_get_bool("audio.qaf.reencode", false))
345 && property_get_bool("audio.qaf.passthrough", false)) {
346
347 if ((out->format == AUDIO_FORMAT_PCM_16_BIT) && (popcount(out->channel_mask) > 2)) {
348 is_enabled = true;
349 } else if (property_get_bool("audio.offload.passthrough", false)) {
350 switch (out->format) {
351 case AUDIO_FORMAT_AC3:
352 case AUDIO_FORMAT_E_AC3:
353 case AUDIO_FORMAT_DTS:
354 case AUDIO_FORMAT_DTS_HD: {
355 is_enabled = true;
356 break;
357 }
358 default:
359 is_enabled = false;
360 break;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530361 }
362 }
363 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530364
365 return is_enabled;
366}
367
368/*Closes all pcm hdmi output from QAF. */
369static void close_all_pcm_hdmi_output()
370{
371 int i;
372 //Closing all the PCM HDMI output stream from QAF.
373 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
374 if (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]) {
375 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
376 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]));
377 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH] = NULL;
378 }
379
380 if ((p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD])
381 && (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
382 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
383 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]));
384 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD] = NULL;
385 }
386 }
387
388 p_qaf->mch_pcm_hdmi_enabled = 0;
389}
390
391static void close_all_hdmi_output()
392{
393 int k;
394 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
395 if (p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
396 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
397 (struct audio_stream_out *)(p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]));
398 p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] = NULL;
399 }
400 }
401 p_qaf->passthrough_enabled = 0;
402
403 close_all_pcm_hdmi_output();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530404}
405
406static int qaf_out_callback(stream_callback_event_t event, void *param __unused, void *cookie)
407{
408 struct stream_out *out = (struct stream_out *)cookie;
409
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800410 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530411 return 0;
412}
413
Harsh Bansal28718c52017-04-20 13:47:12 +0530414/* Creates the QAF passthrough output stream. */
415static int create_qaf_passthrough_stream()
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530416{
Harsh Bansal28718c52017-04-20 13:47:12 +0530417 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530418
Harsh Bansal28718c52017-04-20 13:47:12 +0530419 int ret = 0, k;
420 struct stream_out *out = p_qaf->passthrough_in;
421
422 if (!out) return -EINVAL;
423
424 pthread_mutex_lock(&p_qaf->lock);
425 lock_output_stream(out);
426
427 //Creating QAF passthrough output stream.
428 if (NULL == p_qaf->passthrough_out) {
429 audio_output_flags_t flags;
430 struct audio_config config;
431 audio_devices_t devices;
432
433 config.sample_rate = config.offload_info.sample_rate = out->sample_rate;
434 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
435 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
436 config.offload_info.format = out->format;
437 config.offload_info.bit_width = out->bit_width;
438 config.format = out->format;
439 config.offload_info.channel_mask = config.channel_mask = out->channel_mask;
440
441 //Device is copied from the QAF passthrough input stream.
442 devices = out->devices;
443 flags = out->flags;
444
445 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
446 QAF_DEFAULT_PASSTHROUGH_HANDLE,
447 devices,
448 flags,
449 &config,
450 (struct audio_stream_out **)&(p_qaf->passthrough_out),
451 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530452 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530453 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
454 unlock_output_stream(out);
455 return ret;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530456 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530457 p_qaf->passthrough_in = out;
458 p_qaf->passthrough_out->stream.set_callback((struct audio_stream_out *)p_qaf->passthrough_out,
459 (stream_callback_t) qaf_out_callback, out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530460 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530461
462 unlock_output_stream(out);
463
464 //Since QAF-Passthrough is created, close other HDMI outputs.
465 close_all_hdmi_output();
466
467 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530468 return ret;
469}
470
Harsh Bansal28718c52017-04-20 13:47:12 +0530471/* Closes the QAF passthrough output stream. */
472static void close_qaf_passthrough_stream()
473{
474 if (p_qaf->passthrough_out != NULL) { //QAF pasthroug is enabled. Close it.
475 pthread_mutex_lock(&p_qaf->lock);
476 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
477 (struct audio_stream_out *)(p_qaf->passthrough_out));
478 p_qaf->passthrough_out = NULL;
479 pthread_mutex_unlock(&p_qaf->lock);
480
481 if (p_qaf->passthrough_in->qaf_stream_handle) {
482 qaf_out_pause((struct audio_stream_out*)p_qaf->passthrough_in);
483 qaf_out_flush((struct audio_stream_out*)p_qaf->passthrough_in);
484 qaf_out_drain((struct audio_stream_out*)p_qaf->passthrough_in,
485 (audio_drain_type_t)STREAM_CBK_EVENT_DRAIN_READY);
486 }
487 }
488}
489
490/* Sends a command to output stream offload thread. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530491static int qaf_send_offload_cmd_l(struct stream_out* out, int command)
492{
Harsh Bansal28718c52017-04-20 13:47:12 +0530493 DEBUG_MSG("command is %d", command);
494
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530495 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
496
497 if (!cmd) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530498 ERROR_MSG("failed to allocate mem for command 0x%x", command);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530499 return -ENOMEM;
500 }
501
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530502 cmd->cmd = command;
Harsh Bansal28718c52017-04-20 13:47:12 +0530503
504 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530505 list_add_tail(&out->qaf_offload_cmd_list, &cmd->node);
506 pthread_cond_signal(&out->qaf_offload_cond);
Harsh Bansal28718c52017-04-20 13:47:12 +0530507 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530508 return 0;
509}
510
Harsh Bansal28718c52017-04-20 13:47:12 +0530511/* Stops a QAF module stream.*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530512static int audio_extn_qaf_stream_stop(struct stream_out *out)
513{
Harsh Bansal28718c52017-04-20 13:47:12 +0530514 int ret = 0;
515 DEBUG_MSG("Output Stream 0x%x", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530516
Harsh Bansal28718c52017-04-20 13:47:12 +0530517 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
518 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_stop)) {
519 return ret;
520 }
521
522 if (out->qaf_stream_handle) {
523 ret = qaf_mod->qaf_audio_stream_stop(out->qaf_stream_handle);
524 }
525
526 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530527}
528
Harsh Bansal28718c52017-04-20 13:47:12 +0530529/* Puts a QAF module stream in standby. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530530static int qaf_out_standby(struct audio_stream *stream)
531{
532 struct stream_out *out = (struct stream_out *)stream;
533 int status = 0;
534
535 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
536 stream, out->usecase, use_case_table[out->usecase]);
537
538 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530539
540 //If QAF passthrough is active then block standby on all the input streams of QAF mm modules.
541 if (p_qaf->passthrough_out) {
542 //If standby is received on QAF passthrough stream then forward it to primary HAL.
543 if (p_qaf->passthrough_in == out) {
544 status = p_qaf->passthrough_out->stream.common.standby(
545 (struct audio_stream *)p_qaf->passthrough_out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530546 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530547 } else {
548 //If QAF passthrough stream is not active then stop the QAF module stream.
549 status = audio_extn_qaf_stream_stop(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530550 }
551
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530552 if (!out->standby) {
553 out->standby = true;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530554 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530555
556 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530557 return status;
558}
559
Harsh Bansal28718c52017-04-20 13:47:12 +0530560/* Sets the volume to PCM output stream. */
561static int qaf_out_set_volume(struct audio_stream_out *stream, float left, float right)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530562{
563 int ret = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530564 struct stream_out *out = (struct stream_out *)stream;
565 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530566
Harsh Bansal28718c52017-04-20 13:47:12 +0530567 DEBUG_MSG("Left %f, Right %f", left, right);
568
569 qaf_mod = get_qaf_module_for_input_stream(out);
570 if (!qaf_mod) {
571 return -EINVAL;
572 }
573
574 pthread_mutex_lock(&p_qaf->lock);
575 qaf_mod->vol_left = left;
576 qaf_mod->vol_right = right;
577 qaf_mod->is_vol_set = true;
578 pthread_mutex_unlock(&p_qaf->lock);
579
580 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL) {
581 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
582 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], left, right);
583 }
584
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530585 return ret;
586}
587
Harsh Bansal28718c52017-04-20 13:47:12 +0530588/* Starts a QAF module stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530589static int qaf_stream_start(struct stream_out *out)
590{
Harsh Bansal28718c52017-04-20 13:47:12 +0530591 int ret = -EINVAL;
592 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530593
Harsh Bansal28718c52017-04-20 13:47:12 +0530594 DEBUG_MSG("Output Stream = %p", out);
595
596 qaf_mod = get_qaf_module_for_input_stream(out);
597 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_start)) {
598 return -EINVAL;
599 }
600
601 if (out->qaf_stream_handle) {
602 ret = qaf_mod->qaf_audio_stream_start(out->qaf_stream_handle);
603 }
604
605 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530606}
607
608static int qaf_start_output_stream(struct stream_out *out)
609{
610 int ret = 0;
611 struct audio_device *adev = out->dev;
612 int snd_card_status = get_snd_card_state(adev);
613
614 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
615 ret = -EINVAL;
616 usleep(50000);
617 return ret;
618 }
619
620 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
621 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
622 out->devices);
623
624 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
625 ALOGE("%s: sound card is not active/SSR returning error", __func__);
626 ret = -EIO;
627 usleep(50000);
628 return ret;
629 }
630
631 return qaf_stream_start(out);
632}
633
Harsh Bansal28718c52017-04-20 13:47:12 +0530634/* Sends input buffer to the QAF MM module. */
635static int qaf_module_write_input_buffer(struct stream_out *out, const void *buffer, int bytes)
636{
637 int ret = -EINVAL;
638 struct qaf_module *qaf_mod = NULL;
639 DEBUG_MSG("bytes = %d [%p]", bytes, out->qaf_stream_handle);
640
641 qaf_mod = get_qaf_module_for_input_stream(out);
642 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_write)) {
643 return ret;
644 }
645
646 if (out->qaf_stream_handle) {
647 ret = qaf_mod->qaf_audio_stream_write(out->qaf_stream_handle, buffer, bytes);
648 }
649 return ret;
650}
651
652/* Writes buffer to QAF input stream. */
653static ssize_t qaf_out_write(struct audio_stream_out *stream, const void *buffer, size_t bytes)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530654{
655 struct stream_out *out = (struct stream_out *)stream;
656 struct audio_device *adev = out->dev;
657 ssize_t ret = 0;
658
Harsh Bansal28718c52017-04-20 13:47:12 +0530659 DEBUG_MSG("bytes = %d, usecase[%d] and flags[%x] for handle[%p]",
660 (int)bytes, out->usecase, out->flags, out);
661
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530662 lock_output_stream(out);
663
Harsh Bansal28718c52017-04-20 13:47:12 +0530664 // If QAF passthrough is active then block writing data to QAF mm module.
665 if (p_qaf->passthrough_out) {
666 //If write is received for the QAF passthrough stream then send the buffer to primary HAL.
667 if (p_qaf->passthrough_in == out) {
668 ret = p_qaf->passthrough_out->stream.write(
669 (struct audio_stream_out *)(p_qaf->passthrough_out),
670 buffer,
671 bytes);
672 if (ret > 0) out->standby = false;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530673 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530674 unlock_output_stream(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530675 return ret;
Harsh Bansal28718c52017-04-20 13:47:12 +0530676 } else if (out->standby) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530677 pthread_mutex_lock(&adev->lock);
678 ret = qaf_start_output_stream(out);
679 pthread_mutex_unlock(&adev->lock);
Harsh Bansal28718c52017-04-20 13:47:12 +0530680 if (ret == 0) {
681 out->standby = false;
682 } else {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530683 goto exit;
684 }
685 }
686
Harsh Bansal28718c52017-04-20 13:47:12 +0530687 if ((adev->is_channel_status_set == false) && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530688 audio_utils_set_hdmi_channel_status(out, (char *)buffer, bytes);
689 adev->is_channel_status_set = true;
690 }
691
Harsh Bansal28718c52017-04-20 13:47:12 +0530692 ret = qaf_module_write_input_buffer(out, buffer, bytes);
693 DEBUG_MSG("ret [%d]", (int)ret);
694
695 if (ret >= 0) {
696 bytes = ret;
697 out->written += bytes / ((popcount(out->channel_mask) * sizeof(short)));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530698 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530699
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530700
701exit:
Harsh Bansal28718c52017-04-20 13:47:12 +0530702 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530703
704 if (ret < 0) {
705 if (ret == -EAGAIN) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530706 DEBUG_MSG("No space available in mm module, post msg to cb thread");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530707 ret = qaf_send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530708 bytes = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530709 } else if (ret == -ENOMEM || ret == -EPERM) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530710 if (out->pcm)
Harsh Bansal28718c52017-04-20 13:47:12 +0530711 ERROR_MSG("error %d, %s", (int)ret, pcm_get_error(out->pcm));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530712 qaf_out_standby(&out->stream.common);
Harsh Bansal28718c52017-04-20 13:47:12 +0530713 usleep(bytes * 1000000
714 / audio_stream_out_frame_size(stream)
715 / out->stream.common.get_sample_rate(&out->stream.common));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530716 }
717 }
718 return bytes;
719}
720
Harsh Bansal28718c52017-04-20 13:47:12 +0530721/* Gets PCM offload buffer size for QAF module output. */
722static uint32_t qaf_get_pcm_offload_buffer_size(struct qaf_module *qaf_mod,
723 audio_offload_info_t* info)
Varun B34da7a42017-02-13 16:16:53 +0530724{
Harsh Bansal28718c52017-04-20 13:47:12 +0530725 uint32_t samples_per_frame = get_pcm_output_buffer_size_samples(qaf_mod);
Varun B34da7a42017-02-13 16:16:53 +0530726 uint32_t fragment_size = 0;
Varun B34da7a42017-02-13 16:16:53 +0530727
Harsh Bansal28718c52017-04-20 13:47:12 +0530728 fragment_size = (samples_per_frame * (info->bit_width >> 3) * popcount(info->channel_mask));
729
730 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530731 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530732 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530733 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530734
Varun B34da7a42017-02-13 16:16:53 +0530735 // To have same PCM samples for all channels, the buffer size requires to
736 // be multiple of (number of channels * bytes per sample)
737 // For writes to succeed, the buffer must be written at address which is multiple of 32
Harsh Bansal28718c52017-04-20 13:47:12 +0530738 fragment_size = ALIGN(fragment_size,
739 ((info->bit_width >> 3) * popcount(info->channel_mask) * 32));
Varun B34da7a42017-02-13 16:16:53 +0530740
741 ALOGI("Qaf PCM offload Fragment size to %d bytes", fragment_size);
742
743 return fragment_size;
744}
745
Harsh Bansal28718c52017-04-20 13:47:12 +0530746/* Gets buffer latency in samples. */
747static int get_buffer_latency(struct stream_out *out, uint32_t buffer_size, uint32_t *latency)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530748{
Harsh Bansal28718c52017-04-20 13:47:12 +0530749 unsigned long int samples_in_one_encoded_frame;
750 unsigned long int size_of_one_encoded_frame;
751
752 switch (out->format) {
753 case AUDIO_FORMAT_AC3:
754 samples_in_one_encoded_frame = DD_FRAME_SIZE;
755 size_of_one_encoded_frame = DD_ENCODER_OUTPUT_SIZE;
756 break;
757 case AUDIO_FORMAT_E_AC3:
758 samples_in_one_encoded_frame = DDP_FRAME_SIZE;
759 size_of_one_encoded_frame = DDP_ENCODER_OUTPUT_SIZE;
760 break;
761 case AUDIO_FORMAT_DTS:
762 samples_in_one_encoded_frame = DTS_FRAME_SIZE;
763 size_of_one_encoded_frame = DTS_ENCODER_OUTPUT_SIZE;
764 break;
765 case AUDIO_FORMAT_DTS_HD:
766 samples_in_one_encoded_frame = DTSHD_FRAME_SIZE;
767 size_of_one_encoded_frame = DTSHD_ENCODER_OUTPUT_SIZE;
768 break;
769 case AUDIO_FORMAT_PCM_16_BIT:
770 samples_in_one_encoded_frame = 1;
771 size_of_one_encoded_frame = ((out->bit_width) >> 3) * popcount(out->channel_mask);
772 break;
773 default:
774 *latency = 0;
775 return (-EINVAL);
776 }
777
778 *latency = ((buffer_size * samples_in_one_encoded_frame) / size_of_one_encoded_frame);
779 return 0;
780}
781
782/* Returns the number of frames rendered to outside observer. */
783static int qaf_get_rendered_frames(struct stream_out *out, uint64_t *frames)
784{
785 int ret = 0, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530786 struct str_parms *parms;
787 int value = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530788 int module_latency = 0;
789 uint32_t kernel_latency = 0;
790 uint32_t dsp_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530791 int signed_frames = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530792 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +0530793 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530794
Harsh Bansal28718c52017-04-20 13:47:12 +0530795 DEBUG_MSG("Output Format %d", out->format);
796
797 qaf_mod = get_qaf_module_for_input_stream(out);
798 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_get_param)) {
799 return -EINVAL;
800 }
801
802 //Get MM module latency.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530803 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "get_latency");
804 if (kvpairs) {
805 parms = str_parms_create_str(kvpairs);
Harsh Bansal28718c52017-04-20 13:47:12 +0530806 ret = str_parms_get_int(parms, "get_latency", &module_latency);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530807 if (ret >= 0) {
808 str_parms_destroy(parms);
809 parms = NULL;
810 }
811 free(kvpairs);
812 kvpairs = NULL;
813 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530814
815 //Get kernel Latency
816 for (i = MAX_QAF_MODULE_OUT - 1; i >= 0; i--) {
817 if (qaf_mod->stream_out[i] == NULL) {
818 continue;
819 } else {
820 unsigned int num_fragments = qaf_mod->stream_out[i]->compr_config.fragments;
821 uint32_t fragment_size = qaf_mod->stream_out[i]->compr_config.fragment_size;
822 uint32_t kernel_buffer_size = num_fragments * fragment_size;
823 get_buffer_latency(qaf_mod->stream_out[i], kernel_buffer_size, &kernel_latency);
824 break;
825 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530826 }
827
Harsh Bansal28718c52017-04-20 13:47:12 +0530828 //Get DSP latency
829 if ((qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL)
830 || (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH] != NULL)) {
831 unsigned int sample_rate = 0;
832
833 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
834 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
835 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
836 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
837
838 audio_usecase_t platform_latency =
839 platform_render_latency(qaf_mod->stream_out[QAF_OUT_OFFLOAD]->usecase);
840 dsp_latency = (platform_latency * sample_rate) / 1000000LL;
841 } else if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] != NULL) {
842 unsigned int sample_rate = 0;
843
844 sample_rate = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->sample_rate; //TODO: How this sample rate can be used?
845 dsp_latency = (COMPRESS_OFFLOAD_PLAYBACK_LATENCY * sample_rate) / 1000;
846 }
847
848 // MM Module Latency + Kernel Latency + DSP Latency
849 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
850 out->platform_latency = module_latency + audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl);
851 } else {
852 out->platform_latency = (uint32_t)module_latency + kernel_latency + dsp_latency;
853 }
854
855 if (out->format & AUDIO_FORMAT_PCM_16_BIT) {
856 *frames = 0;
857 signed_frames = out->written - out->platform_latency;
858 // It would be unusual for this value to be negative, but check just in case ...
859 if (signed_frames >= 0) {
860 *frames = signed_frames;
861 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530862 } else if (qaf_mod->qaf_audio_stream_get_param) {
863 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "position");
864 if (kvpairs) {
865 parms = str_parms_create_str(kvpairs);
866 ret = str_parms_get_int(parms, "position", &value);
867 if (ret >= 0) {
868 *frames = value;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530869 signed_frames = value - out->platform_latency;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530870 // It would be unusual for this value to be negative, but check just in case ...
871 if (signed_frames >= 0) {
872 *frames = signed_frames;
873 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530874 }
875 str_parms_destroy(parms);
876 }
877 } else {
878 ret = -EINVAL;
879 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530880
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530881 return ret;
882}
883
884static int qaf_out_get_presentation_position(const struct audio_stream_out *stream,
Harsh Bansal28718c52017-04-20 13:47:12 +0530885 uint64_t *frames,
886 struct timespec *timestamp)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530887{
888 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530889 int ret = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530890
Harsh Bansal28718c52017-04-20 13:47:12 +0530891 DEBUG_MSG("Output Stream %p", stream);
892
893 //If QAF passthorugh output stream is active.
894 if (p_qaf->passthrough_out) {
895 if (p_qaf->passthrough_in == out) {
896 //If api is called for QAF passthorugh stream then call the primary HAL api to get the position.
897 pthread_mutex_lock(&p_qaf->lock);
898 ret = p_qaf->passthrough_out->stream.get_presentation_position(
899 (struct audio_stream_out *)p_qaf->passthrough_out,
900 frames,
901 timestamp);
902 pthread_mutex_unlock(&p_qaf->lock);
903 } else {
904 //If api is called for other stream then return zero frames.
905 *frames = 0;
906 clock_gettime(CLOCK_MONOTONIC, timestamp);
907 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530908 return ret;
909 }
910
Harsh Bansal28718c52017-04-20 13:47:12 +0530911 ret = qaf_get_rendered_frames(out, frames);
912 clock_gettime(CLOCK_MONOTONIC, timestamp);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530913
914 return ret;
915}
916
Harsh Bansal28718c52017-04-20 13:47:12 +0530917/* Pause the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530918static int qaf_stream_pause(struct stream_out *out)
919{
Harsh Bansal28718c52017-04-20 13:47:12 +0530920 struct qaf_module *qaf_mod = NULL;
921 int ret = -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530922
Harsh Bansal28718c52017-04-20 13:47:12 +0530923 qaf_mod = get_qaf_module_for_input_stream(out);
924 if (!qaf_mod || !qaf_mod->qaf_audio_stream_pause) {
925 return -EINVAL;
926 }
927
928 if (out->qaf_stream_handle)
929 ret = qaf_mod->qaf_audio_stream_pause(out->qaf_stream_handle);
930
931 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530932}
933
Harsh Bansal28718c52017-04-20 13:47:12 +0530934/* Pause a QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530935static int qaf_out_pause(struct audio_stream_out* stream)
936{
937 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530938 int status = 0;
939 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530940
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530941 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530942
943 //If QAF passthrough is enabled then block the pause on module stream.
944 if (p_qaf->passthrough_out) {
945 pthread_mutex_lock(&p_qaf->lock);
946 //If pause is received for QAF passthorugh stream then call the primary HAL api.
947 if (p_qaf->passthrough_in == out) {
948 status = p_qaf->passthrough_out->stream.pause(
949 (struct audio_stream_out *)p_qaf->passthrough_out);
950 out->offload_state = OFFLOAD_STATE_PAUSED;
951 }
952 pthread_mutex_unlock(&p_qaf->lock);
953 } else {
954 //Pause the module input stream.
955 status = qaf_stream_pause(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530956 }
957
Harsh Bansal28718c52017-04-20 13:47:12 +0530958 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530959 return status;
960}
961
Harsh Bansal28718c52017-04-20 13:47:12 +0530962/* Drains a qaf input stream. */
963static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530964{
965 struct stream_out *out = (struct stream_out *)stream;
966 int status = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530967 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530968
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530969 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530970
971 //If QAF passthrough is enabled then block the drain on module stream.
972 if (p_qaf->passthrough_out) {
973 pthread_mutex_lock(&p_qaf->lock);
974 //If drain is received for QAF passthorugh stream then call the primary HAL api.
975 if (p_qaf->passthrough_in == out) {
976 status = p_qaf->passthrough_out->stream.drain(
977 (struct audio_stream_out *)p_qaf->passthrough_out, type);
978 }
979 pthread_mutex_unlock(&p_qaf->lock);
980 } else {
981 //Drain the module input stream.
982 /* Stream stop will trigger EOS and on EOS_EVENT received
983 from callback DRAIN_READY command is sent */
984 status = audio_extn_qaf_stream_stop(out);
985
986 if (status == 0) {
987 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
988 int index = get_input_stream_index(out);
989 if (qaf_mod && index >= 0) qaf_mod->drain_received[index] = true;
990 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530991 }
992
Harsh Bansal28718c52017-04-20 13:47:12 +0530993 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530994 return status;
995}
996
Harsh Bansal28718c52017-04-20 13:47:12 +0530997/* Flush the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530998static int audio_extn_qaf_stream_flush(struct stream_out *out)
999{
Harsh Bansal28718c52017-04-20 13:47:12 +05301000 DEBUG_MSG("Output Stream %p", out);
1001 int ret = -EINVAL;
1002 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301003
Harsh Bansal28718c52017-04-20 13:47:12 +05301004 qaf_mod = get_qaf_module_for_input_stream(out);
1005 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_flush)) {
1006 return -EINVAL;
1007 }
1008
1009 if (out->qaf_stream_handle)
1010 ret = qaf_mod->qaf_audio_stream_flush(out->qaf_stream_handle);
1011
1012 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301013}
1014
Harsh Bansal28718c52017-04-20 13:47:12 +05301015/* Flush the QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301016static int qaf_out_flush(struct audio_stream_out* stream)
1017{
1018 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05301019 int status = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301020
Harsh Bansal28718c52017-04-20 13:47:12 +05301021 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301022 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301023
1024 //If QAF passthrough is active then block the flush on module input streams.
1025 if (p_qaf->passthrough_out) {
1026 pthread_mutex_lock(&p_qaf->lock);
1027 //If flush is received for the QAF passthrough stream then call the primary HAL api.
1028 if (p_qaf->passthrough_in == out) {
1029 status = p_qaf->passthrough_out->stream.flush(
1030 (struct audio_stream_out *)p_qaf->passthrough_out);
1031 out->offload_state = OFFLOAD_STATE_IDLE;
1032 }
1033 pthread_mutex_unlock(&p_qaf->lock);
1034 } else {
1035 //Flush the module input stream.
1036 status = audio_extn_qaf_stream_flush(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301037 }
1038
Harsh Bansal28718c52017-04-20 13:47:12 +05301039 unlock_output_stream(out);
1040 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301041 return status;
1042}
1043
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301044static uint32_t qaf_out_get_latency(const struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301045{
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301046 struct stream_out *out = (struct stream_out *)stream;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301047 uint32_t latency = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05301048 struct qaf_module *qaf_mod = NULL;
1049 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301050
Harsh Bansal28718c52017-04-20 13:47:12 +05301051 qaf_mod = get_qaf_module_for_input_stream(out);
1052 if (!qaf_mod) {
1053 return 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301054 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301055
Harsh Bansal28718c52017-04-20 13:47:12 +05301056 //If QAF passthrough is active then block the get latency on module input streams.
1057 if (p_qaf->passthrough_out) {
1058 pthread_mutex_lock(&p_qaf->lock);
1059 //If get latency is called for the QAF passthrough stream then call the primary HAL api.
1060 if (p_qaf->passthrough_in == out) {
1061 latency = p_qaf->passthrough_out->stream.get_latency(
1062 (struct audio_stream_out *)p_qaf->passthrough_out);
1063 }
1064 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301065 } else {
Bharath Gopal01310bb2016-12-05 15:39:32 +05301066 if (is_offload_usecase(out->usecase)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301067 latency = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301068 } else {
Harsh Bansal28718c52017-04-20 13:47:12 +05301069 uint32_t sample_rate = 0;
1070 latency = QAF_MODULE_PCM_INPUT_BUFFER_LATENCY; //Input latency
1071
1072 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
1073 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
1074 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
1075 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
1076
1077 if (sample_rate) {
1078 latency += (get_pcm_output_buffer_size_samples(qaf_mod) * 1000) / out->sample_rate;
1079 }
1080 }
1081
1082 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
1083 if (is_offload_usecase(out->usecase)) {
1084 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1085 QAF_COMPRESS_OFFLOAD_PROCESSING_LATENCY;
1086 } else {
1087 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1088 QAF_PCM_OFFLOAD_PROCESSING_LATENCY;
1089 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05301090 }
1091 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301092
1093 DEBUG_MSG("Latency %d", latency);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301094 return latency;
1095}
1096
Harsh Bansal28718c52017-04-20 13:47:12 +05301097static bool check_and_get_compressed_device_format(int device, int *format)
1098{
1099 switch (device) {
1100 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DD):
1101 *format = AUDIO_FORMAT_AC3;
1102 return true;
1103 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DDP):
1104 *format = AUDIO_FORMAT_E_AC3;
1105 return true;
1106 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_FORMAT_DTS):
1107 *format = AUDIO_FORMAT_DTS;
1108 return true;
1109 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_FORMAT_DTS_HD):
1110 *format = AUDIO_FORMAT_DTS_HD;
1111 return true;
1112 default:
1113 return false;
1114 }
1115}
1116
1117/* Call back function for mm module. */
1118static void notify_event_callback(audio_session_handle_t session_handle /*__unused*/,
1119 void *prv_data,
1120 void *buf,
1121 audio_event_id_t event_id,
1122 int size,
1123 int device) //TODO: add media format as well.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301124{
1125
Harsh Bansal28718c52017-04-20 13:47:12 +05301126 /*
1127 For SPKR:
1128 1. Open pcm device if device_id passed to it SPKR and write the data to
1129 pcm device
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301130
Harsh Bansal28718c52017-04-20 13:47:12 +05301131 For HDMI
1132 1.Open compress device for HDMI(PCM or AC3) based on current hdmi o/p format and write
1133 data to the HDMI device.
1134 */
1135 int ret, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301136 audio_output_flags_t flags;
Harsh Bansal28718c52017-04-20 13:47:12 +05301137 struct qaf_module* qaf_mod = (struct qaf_module*)prv_data;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301138 struct audio_stream_out *bt_stream = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301139 int format;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301140
Harsh Bansal28718c52017-04-20 13:47:12 +05301141 DEBUG_MSG("Device 0x%X, Event = 0x%X", device, event_id);
1142 pthread_mutex_lock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301143
1144 if (event_id == AUDIO_DATA_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301145 DEBUG_MSG("Device id 0x%X, bytes to write %d", device, size);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301146
Harsh Bansal28718c52017-04-20 13:47:12 +05301147 if (p_qaf->passthrough_out != NULL) {
1148 //If QAF passthrough is active then all the module output will be dropped.
1149 pthread_mutex_unlock(&p_qaf->lock);
1150 DEBUG_MSG("QAF-PSTH is active, DROPPING DATA!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301151 return;
Harsh Bansal28718c52017-04-20 13:47:12 +05301152 }
1153
1154 if (check_and_get_compressed_device_format(device, &format)) {
1155 /*
1156 * CASE 1: Transcoded output of mm module.
1157 * If HDMI is not connected then drop the data.
1158 * Only one HDMI output can be supported from all the mm modules of QAF.
1159 * Multi-Channel PCM HDMI output streams will be closed from all the mm modules.
1160 * If transcoded output of other module is already enabled then this data will be dropped.
1161 */
1162
1163 if (!p_qaf->hdmi_connect) {
1164 DEBUG_MSG("HDMI not connected, DROPPING DATA!");
1165 pthread_mutex_unlock(&p_qaf->lock);
1166 return;
1167 }
1168
1169 //Closing all the PCM HDMI output stream from QAF.
1170 close_all_pcm_hdmi_output();
1171
1172 if (!p_qaf->passthrough_enabled
1173 && !(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH])) {
1174
1175 struct audio_config config;
1176 audio_devices_t devices;
1177
1178 //TODO: need to update the actual sample rate.
1179 config.sample_rate = config.offload_info.sample_rate =
1180 QAF_OUTPUT_SAMPLING_RATE;
1181 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1182 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
1183 config.format = config.offload_info.format = format;
1184 //TODO: need to update the actual bit width.
1185 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
1186 //TODO: What is the relevance of num of channels.
1187 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1188
1189 flags = (AUDIO_OUTPUT_FLAG_NON_BLOCKING
1190 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD
1191 | AUDIO_OUTPUT_FLAG_DIRECT);
1192 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1193
1194 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1195 QAF_DEFAULT_COMPR_PASSTHROUGH_HANDLE,
1196 devices,
1197 flags,
1198 &config,
1199 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]),
1200 NULL);
1201 if (ret < 0) {
1202 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1203 pthread_mutex_unlock(&p_qaf->lock);
1204 return;
1205 }
1206
1207 if (format & AUDIO_COMPRESSED_OUT_DDP) {
1208 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragment_size =
1209 COMPRESS_PASSTHROUGH_DDP_FRAGMENT_SIZE;
1210 }
1211 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragments =
1212 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1213
1214 p_qaf->passthrough_enabled = true;
1215 }
1216
1217 if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
1218 ret = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->stream.write(
1219 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH],
1220 buf,
1221 size);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301222 }
1223 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301224 else if ((device & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1225 && (p_qaf->hdmi_connect)
1226 && (p_qaf->hdmi_sink_channels > 2)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301227
Harsh Bansal28718c52017-04-20 13:47:12 +05301228 /* CASE 2: Multi-Channel PCM output to HDMI.
1229 * If any other HDMI output is already enabled then this has to be dropped.
1230 */
1231 bool create_mch_out_stream = false;
1232
1233 if (p_qaf->passthrough_enabled) {
1234 //Closing all the multi-Channel PCM HDMI output stream from QAF.
1235 close_all_pcm_hdmi_output();
1236
1237 //If passthrough is active then pcm hdmi output has to be dropped.
1238 pthread_mutex_unlock(&p_qaf->lock);
1239 DEBUG_MSG("Compressed passthrough enabled, DROPPING DATA!");
1240 return;
1241 }
1242
1243 if (!p_qaf->mch_pcm_hdmi_enabled && !(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301244 struct audio_config config;
1245 audio_devices_t devices;
1246
Harsh Bansal28718c52017-04-20 13:47:12 +05301247 //TODO: need to update the actual sample rate.
Varun B34da7a42017-02-13 16:16:53 +05301248 config.sample_rate = config.offload_info.sample_rate =
Harsh Bansal28718c52017-04-20 13:47:12 +05301249 QAF_OUTPUT_SAMPLING_RATE;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301250 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1251 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
Harsh Bansal28718c52017-04-20 13:47:12 +05301252 config.offload_info.format = config.format = AUDIO_FORMAT_PCM_16_BIT;
1253 //TODO: need to update the actual bit width.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301254 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301255
Harsh Bansal28718c52017-04-20 13:47:12 +05301256 if (p_qaf->hdmi_sink_channels == 8) {
Varun B34da7a42017-02-13 16:16:53 +05301257 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301258 AUDIO_CHANNEL_OUT_7POINT1;
1259 } else if (p_qaf->hdmi_sink_channels == 6) {
Varun B34da7a42017-02-13 16:16:53 +05301260 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301261 AUDIO_CHANNEL_OUT_5POINT1;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301262 } else {
Varun B34da7a42017-02-13 16:16:53 +05301263 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301264 AUDIO_CHANNEL_OUT_STEREO;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301265 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301266
Harsh Bansal28718c52017-04-20 13:47:12 +05301267 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1268 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
1269
1270 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1271 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1272 devices,
1273 flags,
1274 &config,
1275 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]),
1276 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301277 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301278 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1279 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301280 return;
1281 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001282
Harsh Bansal28718c52017-04-20 13:47:12 +05301283 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragments =
1284 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1285 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragment_size =
1286 qaf_get_pcm_offload_buffer_size(qaf_mod, &config.offload_info);
1287
1288 p_qaf->mch_pcm_hdmi_enabled = true;
1289
1290 if (qaf_mod->stream_in[QAF_IN_MAIN]
1291 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) {
1292
1293 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1294 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1295 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1296 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
1297 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1298 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1299
1300 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1301 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1302 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1303 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1304 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001305
1306 int index = -1;
1307 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301308 index = (int) QAF_IN_MAIN;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001309 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301310 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001311
Harsh Bansal28718c52017-04-20 13:47:12 +05301312 if (index >= 0) {
1313 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->standby)
1314 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1315 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], NULL, 0);
1316
1317 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001318 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301319 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1320 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1321 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1322 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001323
1324 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301325 }
1326
Harsh Bansal28718c52017-04-20 13:47:12 +05301327 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) {
1328 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1329 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1330 buf,
1331 size);
Varun B34da7a42017-02-13 16:16:53 +05301332 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301333 }
1334 else {
1335 /* CASE 3: PCM output.
1336 */
Bharath Gopal01310bb2016-12-05 15:39:32 +05301337 bt_stream = audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl);
1338 if (bt_stream != NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301339 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1340 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1341 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD]));
1342 qaf_mod->stream_out[QAF_OUT_OFFLOAD] = NULL;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301343 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301344
Harsh Bansal28718c52017-04-20 13:47:12 +05301345 audio_extn_bt_hal_out_write(p_qaf->bt_hdl, buf, size);
1346 } else if (NULL == qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301347 struct audio_config config;
1348 audio_devices_t devices;
1349
Harsh Bansal28718c52017-04-20 13:47:12 +05301350 //TODO: need to update the actual sample rate.
Varun B34da7a42017-02-13 16:16:53 +05301351 config.sample_rate = config.offload_info.sample_rate =
Harsh Bansal28718c52017-04-20 13:47:12 +05301352 QAF_OUTPUT_SAMPLING_RATE;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301353 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1354 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
Harsh Bansal28718c52017-04-20 13:47:12 +05301355 config.offload_info.format = config.format = AUDIO_FORMAT_PCM_16_BIT;
1356 //TODO: need to update the actual bit width.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301357 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Harsh Bansal28718c52017-04-20 13:47:12 +05301358 //TODO: need to update the actual num channels.
1359 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301360
Harsh Bansal28718c52017-04-20 13:47:12 +05301361 if (qaf_mod->stream_in[QAF_IN_MAIN])
1362 devices = qaf_mod->stream_in[QAF_IN_MAIN]->devices;
1363 else
1364 devices = qaf_mod->stream_in[QAF_IN_PCM]->devices;
1365
1366 //If multi channel pcm or passthrough is already enabled then remove the hdmi flag from device.
1367 if (p_qaf->mch_pcm_hdmi_enabled || p_qaf->passthrough_enabled) {
1368 if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1369 devices ^= AUDIO_DEVICE_OUT_AUX_DIGITAL;
1370 }
1371 if (devices == 0) {
1372 devices = device;
1373 }
1374
1375 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001376
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301377 /* TODO:: Need to Propagate errors to framework */
Harsh Bansal28718c52017-04-20 13:47:12 +05301378 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1379 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1380 devices,
1381 flags,
1382 &config,
1383 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD]),
1384 NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301385 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301386 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1387 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301388 return;
1389 }
Deepak Agarwal1e42b852017-02-11 17:57:04 +05301390
Harsh Bansal28718c52017-04-20 13:47:12 +05301391 if (qaf_mod->stream_in[QAF_IN_MAIN]
1392 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001393
Harsh Bansal28718c52017-04-20 13:47:12 +05301394 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1395 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1396 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1397 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
1398 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1399 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1400
1401 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1402 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1403 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1404 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1405 }
1406
1407 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragments =
1408 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1409 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragment_size =
1410 qaf_get_pcm_offload_buffer_size(qaf_mod, &config.offload_info);
1411
1412 if (qaf_mod->is_vol_set) {
1413 DEBUG_MSG("Setting Volume Left[%f], Right[%f]", qaf_mod->vol_left, qaf_mod->vol_right);
1414 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
1415 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1416 qaf_mod->vol_left,
1417 qaf_mod->vol_right);
1418 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001419
1420 int index = -1;
1421 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301422 index = (int) QAF_IN_MAIN;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001423 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301424 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001425 if (index >= 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301426 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]->standby) {
1427 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1428 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], NULL, 0);
1429 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001430
Harsh Bansal28718c52017-04-20 13:47:12 +05301431 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001432 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301433 qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1434 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1435 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1436 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301437 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301438 }
1439
1440 /*
1441 * TODO:: Since this is mixed data,
1442 * need to identify to which stream the error should be sent
1443 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301444 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1445 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1446 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1447 buf,
1448 size);
Varun B34da7a42017-02-13 16:16:53 +05301449 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301450 }
1451
Harsh Bansal28718c52017-04-20 13:47:12 +05301452 DEBUG_MSG("Bytes written = %d", ret);
1453 }
1454 else if (event_id == AUDIO_EOS_MAIN_DD_DDP_EVENT
Varun B34da7a42017-02-13 16:16:53 +05301455 || event_id == AUDIO_EOS_MAIN_AAC_EVENT
1456 || event_id == AUDIO_EOS_MAIN_AC4_EVENT
1457 || event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301458 struct stream_out *out = qaf_mod->stream_in[QAF_IN_MAIN];
1459 struct stream_out *out_assoc = qaf_mod->stream_in[QAF_IN_ASSOC];
1460 bool *main_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
1461 bool *assoc_drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
Varun B34da7a42017-02-13 16:16:53 +05301462
1463 /**
1464 * TODO:: Only DD/DDP Associate Eos is handled, need to add support
1465 * for other formats.
1466 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301467 if (event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT
1468 && (out_assoc != NULL)
1469 && (*assoc_drain_received)) {
1470
Varun B34da7a42017-02-13 16:16:53 +05301471 lock_output_stream(out_assoc);
Harsh Bansal28718c52017-04-20 13:47:12 +05301472 out_assoc->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_assoc->client_cookie);
1473 *assoc_drain_received = false;
1474 unlock_output_stream(out_assoc);
1475 DEBUG_MSG("sent associated DRAIN_READY");
1476 } else if ((out != NULL) && (*main_drain_received)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301477 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001478 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
Harsh Bansal28718c52017-04-20 13:47:12 +05301479 *main_drain_received = false;
1480 unlock_output_stream(out);
1481 DEBUG_MSG("sent main DRAIN_READY");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301482 }
1483 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301484#if 0
1485 else if (event_id == AUDIO_MAIN_EOS_EVENT || event_id == AUDIO_ASSOC_EOS_EVENT) { //TODO: For DTS
1486 struct stream_out *out = NULL;
1487 bool *drain_received = NULL;
1488
1489 if (event_id == AUDIO_MAIN_EOS_EVENT) {
1490 out = qaf_mod->stream_in[QAF_IN_MAIN];
1491 drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
1492 } else {
1493 out = qaf_mod->stream_in[QAF_IN_ASSOC];
1494 drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
1495 }
1496
1497 if ((out != NULL) && (*drain_received)) {
1498 lock_output_stream(out);
1499 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
1500 *drain_received = false;
1501 unlock_output_stream(out);
1502 DEBUG_MSG("sent DRAIN_READY");
1503 }
1504 }
1505#endif
1506
1507 pthread_mutex_unlock(&p_qaf->lock);
1508 return;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301509}
1510
Harsh Bansal28718c52017-04-20 13:47:12 +05301511/* Close the mm module session. */
1512static int qaf_session_close(struct qaf_module* qaf_mod)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301513{
Harsh Bansal28718c52017-04-20 13:47:12 +05301514 int j;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301515
Harsh Bansal28718c52017-04-20 13:47:12 +05301516 //Check if all streams are closed or not.
1517 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
1518 if (qaf_mod->stream_in[j] != NULL) {
1519 break;
1520 }
1521 }
1522 if (j != MAX_QAF_MODULE_IN) {
1523 return 0; //Some stream is already active, Can not close session.
1524 }
1525
1526 if (qaf_mod->session_handle != NULL && qaf_mod->qaf_audio_session_close) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301527 qaf_mod->qaf_audio_session_close(qaf_mod->session_handle);
1528 qaf_mod->session_handle = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301529 qaf_mod->is_vol_set = false;
1530 memset(qaf_mod->drain_received, 0, sizeof(qaf_mod->drain_received));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301531 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301532
1533 for (j = 0; j < MAX_QAF_MODULE_OUT; j++) {
1534 if (qaf_mod->stream_out[j]) {
1535 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1536 (struct audio_stream_out *)(qaf_mod->stream_out[j]));
1537 qaf_mod->stream_out[j] = NULL;
1538 }
1539 }
1540
1541 DEBUG_MSG("Session Closed.");
1542
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301543 return 0;
1544}
1545
Harsh Bansal28718c52017-04-20 13:47:12 +05301546/* Close the stream of QAF module. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301547static int qaf_stream_close(struct stream_out *out)
1548{
Harsh Bansal28718c52017-04-20 13:47:12 +05301549 int ret = -EINVAL;
1550 struct qaf_module *qaf_mod = NULL;
1551 int index = -1;
1552 DEBUG_MSG("Flag [0x%x], Stream handle [%p]", out->flags, out->qaf_stream_handle);
1553
1554 qaf_mod = get_qaf_module_for_input_stream(out);
1555 index = get_input_stream_index(out);
1556
1557 if (!qaf_mod || !qaf_mod->qaf_audio_stream_close || index < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301558 return -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301559 }
1560
1561 qaf_mod->stream_in[index] = NULL;
1562 memset(&qaf_mod->adsp_hdlr_config[index], 0, sizeof(struct qaf_adsp_hdlr_config_state));
1563
1564 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301565 if (out->qaf_stream_handle) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301566 ret = qaf_mod->qaf_audio_stream_close(out->qaf_stream_handle);
1567 out->qaf_stream_handle = NULL;
1568 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301569 unlock_output_stream(out);
1570
1571 //If all streams are closed then close the session.
1572 qaf_session_close(qaf_mod);
1573
1574 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301575 return ret;
1576}
1577
Harsh Bansal28718c52017-04-20 13:47:12 +05301578/* Open a MM module session with QAF. */
1579static int audio_extn_qaf_session_open(mm_module_type mod_type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301580{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301581 ALOGV("%s %d", __func__, __LINE__);
Harsh Bansal28718c52017-04-20 13:47:12 +05301582 unsigned char* license_data = NULL;
1583 device_license_config_t lic_config = {0};
1584 int ret = -ENOSYS, size = 0;
1585 char value[PROPERTY_VALUE_MAX] = {0};
1586 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301587
Harsh Bansal28718c52017-04-20 13:47:12 +05301588 if (mod_type >= MAX_MM_MODULE_TYPE || !(p_qaf->qaf_mod[mod_type].qaf_audio_session_open))
1589 return -ENOTSUP; //Not supported by QAF module.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301590
Harsh Bansal28718c52017-04-20 13:47:12 +05301591 pthread_mutex_lock(&p_qaf->lock);
1592
1593 qaf_mod = &(p_qaf->qaf_mod[mod_type]);
1594
1595 //If session is already opened then return.
1596 if (qaf_mod->session_handle) {
1597 DEBUG_MSG("Session is already opened.");
1598 pthread_mutex_unlock(&p_qaf->lock);
1599 return 0;
1600 }
1601
1602 if (mod_type == MS12) {
1603 //Getting the license
1604 license_data = platform_get_license((struct audio_hw_device *)(p_qaf->adev->platform),
1605 &size);
1606 if (!license_data) {
1607 ERROR_MSG("License data is not present.");
1608 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301609 return -EINVAL;
1610 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301611
1612 lic_config.p_license = (unsigned char*)calloc(1, size);
1613 if (lic_config.p_license == NULL) {
1614 ERROR_MSG("Out of Memory");
1615 ret = -ENOMEM;
1616 goto exit;
1617 }
1618
1619 lic_config.l_size = size;
1620 memcpy(lic_config.p_license, license_data, size);
1621
1622 if (property_get("audio.qaf.manufacturer", value, "") && atoi(value)) {
1623 lic_config.manufacturer_id = (unsigned long)atoi(value);
1624 } else {
1625 ERROR_MSG("audio.qaf.manufacturer id is not set");
1626 ret = -EINVAL;
1627 goto exit;
1628 }
1629 }
1630
1631 ret = qaf_mod->qaf_audio_session_open(&qaf_mod->session_handle,
1632 (void *)(qaf_mod),
1633 (void *)&lic_config);
1634 if (ret < 0) {
1635 ERROR_MSG("Error in session open %d", ret);
1636 goto exit;
1637 }
1638
1639 if (qaf_mod->session_handle == NULL) {
1640 ERROR_MSG("Session handle is NULL.");
1641 ret = -ENOMEM;
1642 goto exit;
1643 }
1644
1645 if (qaf_mod->qaf_register_event_callback)
1646 qaf_mod->qaf_register_event_callback(qaf_mod->session_handle,
1647 qaf_mod,
1648 &notify_event_callback,
1649 AUDIO_DATA_EVENT);
1650
1651 set_hdmi_configuration_to_module();
1652
1653exit:
1654 if (license_data != NULL) {
1655 free(license_data);
1656 license_data = NULL;
1657 }
1658 if (lic_config.p_license != NULL) {
1659 free(lic_config.p_license);
1660 lic_config.p_license = NULL;
1661 }
1662
1663 pthread_mutex_unlock(&p_qaf->lock);
1664 return ret;
1665}
1666
1667/* opens a stream in QAF module. */
1668static int qaf_stream_open(struct stream_out *out,
1669 struct audio_config *config,
1670 audio_output_flags_t flags,
1671 audio_devices_t devices)
1672{
1673 int status = -EINVAL;
1674 mm_module_type mmtype = get_mm_module_for_format(config->format);
1675 struct qaf_module* qaf_mod = NULL;
1676 DEBUG_MSG("Flags 0x%x, Device 0x%x", flags, devices);
1677
1678 if (mmtype >= MAX_MM_MODULE_TYPE
1679 || p_qaf->qaf_mod[mmtype].qaf_audio_session_open == NULL
1680 || p_qaf->qaf_mod[mmtype].qaf_audio_stream_open == NULL) {
1681 ERROR_MSG("Unsupported Stream");
1682 return -ENOTSUP;
1683 }
1684
1685 //Open the module session, if not opened already.
1686 status = audio_extn_qaf_session_open(mmtype);
1687 qaf_mod = &(p_qaf->qaf_mod[mmtype]);
1688
1689 if ((status != 0) || (qaf_mod->session_handle == NULL)) {
1690 ERROR_MSG("Failed to open session.");
1691 return status;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301692 }
1693
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301694 audio_stream_config_t input_config;
1695 input_config.sample_rate = config->sample_rate;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301696 input_config.channels = popcount(config->channel_mask);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301697 input_config.format = config->format;
1698
Harsh Bansal28718c52017-04-20 13:47:12 +05301699 if (input_config.format != AUDIO_FORMAT_PCM_16_BIT) {
1700 input_config.format &= AUDIO_FORMAT_MAIN_MASK;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301701 }
1702
Harsh Bansal28718c52017-04-20 13:47:12 +05301703 DEBUG_MSG("stream_open sample_rate(%d) channels(%d) devices(%#x) flags(%#x) format(%#x)",
1704 input_config.sample_rate, input_config.channels, devices, flags, input_config.format);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301705
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301706 if (input_config.format == AUDIO_FORMAT_PCM_16_BIT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301707 //If PCM stream is already opened then fail this stream open.
1708 if (qaf_mod->stream_in[QAF_IN_PCM]) {
1709 ERROR_MSG("PCM input is already active.");
1710 return -ENOTSUP;
1711 }
1712
1713 //TODO: Flag can be system tone or external associated PCM.
1714 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle,
1715 &out->qaf_stream_handle,
1716 input_config,
1717 devices,
1718 AUDIO_STREAM_SYSTEM_TONE);
1719 qaf_mod->stream_in[QAF_IN_PCM] = out;
1720 } else {
1721 if (!qaf_mod->stream_in[QAF_IN_MAIN]) {
1722 if ((!(flags & AUDIO_OUTPUT_FLAG_MAIN)) && (flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
1723 ERROR_MSG("Error main input is not active.");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301724 return -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301725 }
1726
1727 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle,
1728 &out->qaf_stream_handle,
1729 input_config,
1730 devices,
1731 AUDIO_STREAM_MAIN);
1732 if (status == 0) {
1733 DEBUG_MSG("Open stream for Input with Main stream contents with flag [%x] and stream handle [%p]",
1734 flags, out->qaf_stream_handle);
1735 qaf_mod->stream_in[QAF_IN_MAIN] = out;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301736 }
1737 } else {
1738 if (flags & AUDIO_OUTPUT_FLAG_MAIN) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301739 ERROR_MSG("Error main input is already active");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301740 return -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301741 } else if ((flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)
1742 && (!qaf_mod->stream_in[QAF_IN_ASSOC])) {
1743 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle,
1744 &out->qaf_stream_handle,
1745 input_config,
1746 devices,
1747 AUDIO_STREAM_ASSOCIATED);
1748 if (status == 0) {
1749 DEBUG_MSG("Open stream for Input with only Associated flag [%x] stream handle [%p]",
1750 flags, out->qaf_stream_handle);
1751 qaf_mod->stream_in[QAF_IN_ASSOC] = out;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301752 }
1753 } else {
Harsh Bansal28718c52017-04-20 13:47:12 +05301754 ERROR_MSG("Invalid flag or associated is already active");
1755 status = -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301756 }
1757 }
1758 }
1759
Harsh Bansal28718c52017-04-20 13:47:12 +05301760 if (status != 0) {
1761 //If no stream is active then close the session.
1762 qaf_session_close(qaf_mod);
1763 return status;
1764 }
1765
1766 //If Device is HDMI, QAF passthrough is enabled and there is no previous QAF passthrough input stream.
1767 if ((!p_qaf->passthrough_in)
1768 && (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1769 && audio_extn_qaf_passthrough_enabled(out)) {
1770 //Assign the QAF passthrough input stream.
1771 p_qaf->passthrough_in = out;
1772
1773 //If HDMI is connected and format is supported by HDMI then create QAF passthrough output stream.
1774 if (p_qaf->hdmi_connect
1775 && platform_is_edid_supported_format(p_qaf->adev->platform, out->format)) {
1776 status = create_qaf_passthrough_stream();
1777 if (status < 0) {
1778 qaf_stream_close(out);
1779 ERROR_MSG("QAF passthrough stream creation failed with error %d", status);
1780 return status;
1781 }
1782 }
1783 /*Else: since QAF passthrough input stream is already initialized,
1784 * when hdmi is connected
1785 * then qaf passthrough output stream will be created.
1786 */
1787 }
1788
1789 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301790 return status;
1791}
1792
Harsh Bansal28718c52017-04-20 13:47:12 +05301793/* Resume a QAF stream. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301794static int qaf_out_resume(struct audio_stream_out* stream)
1795{
1796 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05301797 int status = 0;
1798 DEBUG_MSG("Output Stream %p", out);
1799
1800
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301801 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301802
1803 //If QAF passthrough is active then block the resume on module input streams.
1804 if (p_qaf->passthrough_out) {
1805 //If resume is received for the QAF passthrough stream then call the primary HAL api.
1806 pthread_mutex_lock(&p_qaf->lock);
1807 if (p_qaf->passthrough_in == out) {
1808 status = p_qaf->passthrough_out->stream.resume(
1809 (struct audio_stream_out*)p_qaf->passthrough_out);
1810 if (!status) out->offload_state = OFFLOAD_STATE_PLAYING;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301811 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301812 pthread_mutex_unlock(&p_qaf->lock);
1813 } else {
1814 //Flush the module input stream.
1815 status = qaf_stream_start(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301816 }
1817
Harsh Bansal28718c52017-04-20 13:47:12 +05301818 unlock_output_stream(out);
1819
1820 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301821 return status;
1822}
1823
Harsh Bansal28718c52017-04-20 13:47:12 +05301824/* Offload thread for QAF output streams. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301825static void *qaf_offload_thread_loop(void *context)
1826{
Harsh Bansal28718c52017-04-20 13:47:12 +05301827 struct stream_out *out = (struct stream_out *)context;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301828 struct listnode *item;
1829 int ret = 0;
1830 struct str_parms *parms = NULL;
1831 int value = 0;
1832 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301833 struct qaf_module *qaf_mod = NULL;
1834
1835 qaf_mod = get_qaf_module_for_input_stream(out);
1836
1837 if (!qaf_mod) return NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301838
1839 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1840 set_sched_policy(0, SP_FOREGROUND);
1841 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1842
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301843 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301844
1845 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301846 for (;;) {
1847 struct offload_cmd *cmd = NULL;
1848 stream_callback_event_t event;
1849 bool send_callback = false;
1850
Harsh Bansal28718c52017-04-20 13:47:12 +05301851 DEBUG_MSG("List Empty %d (1:TRUE, 0:FALSE)", list_empty(&out->qaf_offload_cmd_list));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301852 if (list_empty(&out->qaf_offload_cmd_list)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301853 DEBUG_MSG("SLEEPING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301854 pthread_cond_wait(&out->qaf_offload_cond, &out->lock);
Harsh Bansal28718c52017-04-20 13:47:12 +05301855 DEBUG_MSG("RUNNING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301856 continue;
1857 }
1858
1859 item = list_head(&out->qaf_offload_cmd_list);
1860 cmd = node_to_item(item, struct offload_cmd, node);
1861 list_remove(item);
1862
1863 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1864 free(cmd);
1865 break;
1866 }
1867
Harsh Bansal28718c52017-04-20 13:47:12 +05301868 unlock_output_stream(out);
1869
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301870 send_callback = false;
Harsh Bansal28718c52017-04-20 13:47:12 +05301871 switch (cmd->cmd) {
1872 case OFFLOAD_CMD_WAIT_FOR_BUFFER: {
1873 DEBUG_MSG("wait for buffer availability");
1874
1875 while (1) {
1876 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle,
1877 "buf_available");
1878 if (kvpairs) {
1879 parms = str_parms_create_str(kvpairs);
1880 ret = str_parms_get_int(parms, "buf_available", &value);
1881 if (ret >= 0) {
1882 if (value >= (int)out->compr_config.fragment_size) {
1883 DEBUG_MSG("buffer available");
1884 str_parms_destroy(parms);
1885 parms = NULL;
1886 break;
1887 } else {
1888 DEBUG_MSG("sleep");
1889 str_parms_destroy(parms);
1890 parms = NULL;
1891 usleep(10000);
1892 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301893 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301894 free(kvpairs);
1895 kvpairs = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301896 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301897 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301898 send_callback = true;
1899 event = STREAM_CBK_EVENT_WRITE_READY;
1900 break;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301901 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301902 default:
1903 DEBUG_MSG("unknown command received: %d", cmd->cmd);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301904 break;
1905 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301906
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301907 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301908
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001909 if (send_callback && out->client_callback) {
1910 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301911 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301912
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301913 free(cmd);
1914 }
1915
1916 while (!list_empty(&out->qaf_offload_cmd_list)) {
1917 item = list_head(&out->qaf_offload_cmd_list);
1918 list_remove(item);
Harsh Bansal28718c52017-04-20 13:47:12 +05301919 free (node_to_item( item, struct offload_cmd, node));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301920 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301921 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301922
1923 return NULL;
1924}
1925
Harsh Bansal28718c52017-04-20 13:47:12 +05301926/* Create the offload callback thread for QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301927static int qaf_create_offload_callback_thread(struct stream_out *out)
1928{
Harsh Bansal28718c52017-04-20 13:47:12 +05301929 DEBUG_MSG("Output Stream %p", out);
1930 lock_output_stream(out);
1931 pthread_cond_init(&out->qaf_offload_cond, (const pthread_condattr_t *)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301932 list_init(&out->qaf_offload_cmd_list);
Harsh Bansal28718c52017-04-20 13:47:12 +05301933 pthread_create(&out->qaf_offload_thread,
1934 (const pthread_attr_t *)NULL,
1935 qaf_offload_thread_loop,
1936 out);
1937 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301938 return 0;
1939}
1940
Harsh Bansal28718c52017-04-20 13:47:12 +05301941/* Destroy the offload callback thread of QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301942static int qaf_destroy_offload_callback_thread(struct stream_out *out)
1943{
Harsh Bansal28718c52017-04-20 13:47:12 +05301944 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301945 qaf_send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301946
Harsh Bansal28718c52017-04-20 13:47:12 +05301947 pthread_join(out->qaf_offload_thread, (void **)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301948 pthread_cond_destroy(&out->qaf_offload_cond);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301949 return 0;
1950}
1951
Harsh Bansal28718c52017-04-20 13:47:12 +05301952/* Sets the stream set parameters (device routing information). */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301953static int qaf_out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1954{
1955 struct str_parms *parms, *new_parms;
1956 char value[32];
1957 char *new_kv_pairs;
1958 int val = 0;
1959 struct stream_out *out = (struct stream_out *)stream;
1960 int ret = 0;
1961 int err = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05301962 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301963
Harsh Bansal28718c52017-04-20 13:47:12 +05301964 DEBUG_MSG("usecase(%d: %s) kvpairs: %s", out->usecase, use_case_table[out->usecase], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301965
Harsh Bansal28718c52017-04-20 13:47:12 +05301966 parms = str_parms_create_str(kvpairs);
1967 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1968 if (err < 0)
1969 return err;
1970 val = atoi(value);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301971
Harsh Bansal28718c52017-04-20 13:47:12 +05301972 qaf_mod = get_qaf_module_for_input_stream(out);
1973 if (!qaf_mod) return (-EINVAL);
1974
1975 //TODO: HDMI is connected but user doesn't want HDMI output, close both HDMI outputs.
1976
1977 /* Setting new device information to the mm module input streams.
1978 * This is needed if QAF module output streams are not created yet.
1979 */
1980 if (qaf_mod->stream_in[QAF_IN_MAIN] == out || qaf_mod->stream_in[QAF_IN_ASSOC] == out) {
1981 qaf_mod->stream_in[QAF_IN_MAIN]->devices = val;
1982 } else {
1983 out->devices = val;
1984 }
1985
1986 if (val == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
1987 //If device is BT then open the BT stream if not already opened.
1988 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) == NULL
1989 && audio_extn_bt_hal_get_device(qaf_mod->bt_hdl) != NULL) {
1990 ret = audio_extn_bt_hal_open_output_stream(qaf_mod->bt_hdl,
1991 QAF_OUTPUT_SAMPLING_RATE,
1992 AUDIO_CHANNEL_OUT_STEREO,
1993 CODEC_BACKEND_DEFAULT_BIT_WIDTH);
1994 if (ret != 0) {
1995 ERROR_MSG("BT Output stream open failure!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301996 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301997 }
1998 } else if (val != 0) {
1999 //If device is not BT then close the BT stream if already opened.
2000 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
2001 audio_extn_bt_hal_close_output_stream(qaf_mod->bt_hdl);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302002 }
2003 }
2004
Harsh Bansal28718c52017-04-20 13:47:12 +05302005 if (p_qaf->passthrough_in == out) { //Device routing is received for QAF passthrough stream.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302006
Harsh Bansal28718c52017-04-20 13:47:12 +05302007 if (!(val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) { //HDMI route is disabled.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302008
Harsh Bansal28718c52017-04-20 13:47:12 +05302009 //If QAF pasthrough output is enabled. Close it.
2010 close_qaf_passthrough_stream();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302011
Harsh Bansal28718c52017-04-20 13:47:12 +05302012 //Send the routing information to mm module pcm output.
2013 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2014 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2015 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302016 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302017 //else: device info is updated in the input streams.
2018 } else { //HDMI route is enabled.
2019
2020 //create the QAf passthrough stream, if not created already.
2021 ret = create_qaf_passthrough_stream();
2022
2023 if (p_qaf->passthrough_out != NULL) { //If QAF passthrough out is enabled then send routing information.
2024 ret = p_qaf->passthrough_out->stream.common.set_parameters(
2025 (struct audio_stream *)p_qaf->passthrough_out, kvpairs);
2026 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302027 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302028 } else {
2029 //Send the routing information to mm module pcm output.
2030 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2031 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2032 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
2033 }
2034 //else: device info is updated in the input streams.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302035 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302036 str_parms_destroy(parms);
2037
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002038 return ret;
2039}
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302040
Harsh Bansal28718c52017-04-20 13:47:12 +05302041/* Checks if a stream is QAF stream or not. */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002042bool audio_extn_is_qaf_stream(struct stream_out *out)
2043{
Harsh Bansal28718c52017-04-20 13:47:12 +05302044 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002045
Harsh Bansal28718c52017-04-20 13:47:12 +05302046 if (qaf_mod) {
2047 return true;
2048 }
2049 return false;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002050}
2051
2052/* API to send playback stream specific config parameters */
2053int audio_extn_qaf_out_set_param_data(struct stream_out *out,
Harsh Bansal28718c52017-04-20 13:47:12 +05302054 audio_extn_param_id param_id,
2055 audio_extn_param_payload *payload)
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002056{
2057 int ret = -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05302058 int index;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002059 struct stream_out *new_out = NULL;
2060 struct audio_adsp_event *adsp_event;
Harsh Bansal28718c52017-04-20 13:47:12 +05302061 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002062
Harsh Bansal28718c52017-04-20 13:47:12 +05302063 if (!out || !qaf_mod || !payload) {
2064 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002065 return ret;
2066 }
2067
2068 /* In qaf output render session may not be opened at this time.
Harsh Bansal28718c52017-04-20 13:47:12 +05302069 to handle it store adsp_hdlr param info so that it can be
2070 applied later after opening render session from ms12 callback
2071 */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002072 if (param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302073 index = get_input_stream_index(out);
2074 if (index < 0) {
2075 ERROR_MSG("Invalid stream");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002076 return ret;
2077 }
2078 adsp_event = (struct audio_adsp_event *)payload;
2079
Harsh Bansal28718c52017-04-20 13:47:12 +05302080 if (payload->adsp_event_params.payload_length <= AUDIO_MAX_ADSP_STREAM_CMD_PAYLOAD_LEN) {
2081 pthread_mutex_lock(&p_qaf->lock);
2082 memcpy(qaf_mod->adsp_hdlr_config[index].event_payload,
2083 adsp_event->payload,
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002084 adsp_event->payload_length);
Harsh Bansal28718c52017-04-20 13:47:12 +05302085 qaf_mod->adsp_hdlr_config[index].event_params.payload =
2086 qaf_mod->adsp_hdlr_config[index].event_payload;
2087 qaf_mod->adsp_hdlr_config[index].event_params.payload_length =
2088 adsp_event->payload_length;
2089 qaf_mod->adsp_hdlr_config[index].adsp_hdlr_config_valid = true;
2090 pthread_mutex_unlock(&p_qaf->lock);
2091 } else {
2092 ERROR_MSG("Invalid adsp event length %d", adsp_event->payload_length);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002093 return ret;
2094 }
2095 ret = 0;
2096 }
2097
Harsh Bansal28718c52017-04-20 13:47:12 +05302098 /* apply param for all active out sessions */
2099 for (index = 0; index < MAX_QAF_MODULE_OUT; index++) {
2100 new_out = qaf_mod->stream_out[index];
2101 if (!new_out) continue;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002102
Harsh Bansal28718c52017-04-20 13:47:12 +05302103 /*ADSP event is not supported for passthrough*/
2104 if ((param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD)
2105 && !(new_out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) continue;
2106 if (new_out->standby)
2107 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
2108 lock_output_stream(new_out);
2109 ret = audio_extn_out_set_param_data(new_out, param_id, payload);
2110 if (ret)
2111 ERROR_MSG("audio_extn_out_set_param_data error %d", ret);
2112 unlock_output_stream(new_out);
2113 }
2114 return ret;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002115}
2116
2117int audio_extn_qaf_out_get_param_data(struct stream_out *out,
2118 audio_extn_param_id param_id,
2119 audio_extn_param_payload *payload)
2120{
Harsh Bansal28718c52017-04-20 13:47:12 +05302121 int ret = -EINVAL, i;
2122 struct stream_out *new_out;
2123 struct audio_usecase *uc_info;
2124 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002125
Harsh Bansal28718c52017-04-20 13:47:12 +05302126 if (!out || !qaf_mod || !payload) {
2127 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002128 return ret;
2129 }
2130
Harsh Bansal28718c52017-04-20 13:47:12 +05302131 if (!p_qaf->hdmi_connect) {
2132 ERROR_MSG("hdmi not connected");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002133 return ret;
2134 }
2135
2136 /* get session which is routed to hdmi*/
Harsh Bansal28718c52017-04-20 13:47:12 +05302137 if (p_qaf->passthrough_out)
2138 new_out = p_qaf->passthrough_out;
2139 else {
2140 for (i = 0; i < MAX_QAF_MODULE_OUT; i++) {
2141 if (qaf_mod->stream_out[i]) {
2142 new_out = qaf_mod->stream_out[i];
2143 break;
2144 }
2145 }
2146 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002147
2148 if (!new_out) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302149 ERROR_MSG("No stream active.");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002150 return ret;
2151 }
2152
2153 if (new_out->standby)
Harsh Bansal28718c52017-04-20 13:47:12 +05302154 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002155
2156 lock_output_stream(new_out);
2157 ret = audio_extn_out_get_param_data(new_out, param_id, payload);
Harsh Bansal28718c52017-04-20 13:47:12 +05302158 if (ret)
2159 ERROR_MSG("audio_extn_out_get_param_data error %d", ret);
2160 unlock_output_stream(new_out);
2161
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302162 return ret;
2163}
2164
Harsh Bansal28718c52017-04-20 13:47:12 +05302165/* To open a stream with QAF. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302166int audio_extn_qaf_open_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302167 audio_io_handle_t handle,
2168 audio_devices_t devices,
2169 audio_output_flags_t flags,
2170 struct audio_config *config,
2171 struct audio_stream_out **stream_out,
2172 const char *address)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302173{
2174 int ret = 0;
2175 struct stream_out *out;
2176
2177 ret = adev_open_output_stream(dev, handle, devices, flags, config, stream_out, address);
2178 if (*stream_out == NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302179 ERROR_MSG("Stream open failed %d", ret);
Deepak Agarwal1e42b852017-02-11 17:57:04 +05302180 return ret;
2181 }
2182
Harsh Bansal28718c52017-04-20 13:47:12 +05302183 out = (struct stream_out *)*stream_out;
2184
2185 ret = qaf_stream_open(out, config, flags, devices);
2186 if (ret == -ENOTSUP) {
2187 //Stream not supported by QAF, Bypass QAF.
2188 return 0;
2189 } else if (ret < 0) {
2190 ERROR_MSG("Error opening QAF stream err[%d]!", ret);
2191 adev_close_output_stream(dev, *stream_out);
2192 return ret;
2193 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302194
2195 /* Override function pointers based on qaf definitions */
2196 out->stream.set_volume = qaf_out_set_volume;
2197 out->stream.pause = qaf_out_pause;
2198 out->stream.resume = qaf_out_resume;
2199 out->stream.drain = qaf_out_drain;
2200 out->stream.flush = qaf_out_flush;
2201
2202 out->stream.common.standby = qaf_out_standby;
2203 out->stream.common.set_parameters = qaf_out_set_parameters;
2204 out->stream.get_latency = qaf_out_get_latency;
2205 out->stream.write = qaf_out_write;
2206 out->stream.get_presentation_position = qaf_out_get_presentation_position;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302207 out->platform_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302208
Harsh Bansal28718c52017-04-20 13:47:12 +05302209 /*TODO: Need to handle this for DTS*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302210 if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) {
2211 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
Varun B34da7a42017-02-13 16:16:53 +05302212 out->config.period_size = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302213 out->config.period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT;
Varun B34da7a42017-02-13 16:16:53 +05302214 out->config.start_threshold = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
2215 out->config.avail_min = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302216 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302217 *stream_out = &out->stream;
2218 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2219 qaf_create_offload_callback_thread(out);
2220 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302221
2222 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302223 return 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302224}
2225
Harsh Bansal28718c52017-04-20 13:47:12 +05302226/* Close a QAF stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302227void audio_extn_qaf_close_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302228 struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302229{
2230 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05302231 struct qaf_module* qaf_mod = get_qaf_module_for_input_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302232
Harsh Bansal28718c52017-04-20 13:47:12 +05302233 if (!qaf_mod) return;
2234
2235 DEBUG_MSG("stream_handle(%p) format = %x", out, out->format);
2236
2237 //If close is received for QAF passthrough stream then close the QAF passthrough output.
2238 if (p_qaf->passthrough_in == out) {
2239 if (p_qaf->passthrough_out) {
2240 ALOGD("%s %d closing stream handle %p", __func__, __LINE__, p_qaf->passthrough_out);
2241 pthread_mutex_lock(&p_qaf->lock);
2242 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2243 (struct audio_stream_out *)(p_qaf->passthrough_out));
2244 pthread_mutex_unlock(&p_qaf->lock);
2245 p_qaf->passthrough_out = NULL;
2246 }
2247
2248 p_qaf->passthrough_in = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302249 }
2250
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302251 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2252 qaf_destroy_offload_callback_thread(out);
2253 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002254
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302255 qaf_stream_close(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302256
2257 adev_close_output_stream(dev, stream);
Harsh Bansal28718c52017-04-20 13:47:12 +05302258
2259 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302260}
2261
Harsh Bansal28718c52017-04-20 13:47:12 +05302262/* Check if QAF is supported or not. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302263bool audio_extn_qaf_is_enabled()
2264{
2265 bool prop_enabled = false;
2266 char value[PROPERTY_VALUE_MAX] = {0};
2267 property_get("audio.qaf.enabled", value, NULL);
2268 prop_enabled = atoi(value) || !strncmp("true", value, 4);
2269 return (prop_enabled);
2270}
2271
Harsh Bansal28718c52017-04-20 13:47:12 +05302272/* Query HDMI EDID and sets module output accordingly.*/
2273void set_hdmi_configuration_to_module()
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302274{
Harsh Bansal28718c52017-04-20 13:47:12 +05302275 int channels = 0;
2276 char *format_params;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302277 struct str_parms *qaf_params;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302278 char prop_value[PROPERTY_VALUE_MAX];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302279 bool passth_support = false;
2280
Harsh Bansal28718c52017-04-20 13:47:12 +05302281 DEBUG_MSG("Entry");
2282
2283 if (!p_qaf) {
2284 return;
2285 }
2286
2287 if (!p_qaf->hdmi_connect) {
2288 DEBUG_MSG("HDMI is not connected.");
2289 return;
2290 }
2291
2292 p_qaf->hdmi_sink_channels = 0;
2293
2294 //QAF re-encoding and DSP offload passthrough is supported.
2295 if (property_get_bool("audio.offload.passthrough", false)
2296 && property_get_bool("audio.qaf.reencode", false)) {
2297
2298 //If MS12 session is active.
2299 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2300
2301 bool do_setparam = false;
2302 qaf_params = str_parms_create();
2303 property_get("audio.qaf.hdmi.out", prop_value, NULL);
2304
2305 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_E_AC3)
2306 && (strncmp(prop_value, "ddp", 3) == 0)) {
2307 do_setparam = true;
2308 if (qaf_params) {
2309 str_parms_add_str(qaf_params,
2310 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2311 AUDIO_QAF_PARAMETER_VALUE_REENCODE_EAC3);
2312 }
2313 } else if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_AC3)) {
2314 do_setparam = true;
2315 if (qaf_params) {
2316 str_parms_add_str(qaf_params,
2317 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2318 AUDIO_QAF_PARAMETER_VALUE_REENCODE_AC3);
2319 }
2320 }
2321
2322 if (do_setparam) {
2323 if (p_qaf->qaf_msmd_enabled) {
2324 str_parms_add_str(qaf_params,
2325 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2326 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2327 } else {
2328 str_parms_add_str(qaf_params,
2329 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2330 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2331 }
2332 format_params = str_parms_to_str(qaf_params);
2333
2334 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2335 format_params);
2336
2337 passth_support = true;
2338 }
2339 str_parms_destroy(qaf_params);
2340 }
2341
2342 //DTS_M8 session is active.
2343 if (p_qaf->qaf_mod[DTS_M8].session_handle
2344 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2345
2346 bool do_setparam = false;
2347 qaf_params = str_parms_create();
2348#if 0 //TODO: Need to enable with DTS_M8 wrapper.
2349 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_DTS)) {
2350 do_setparam = true;
2351 if (qaf_params) {
2352 str_parms_add_str(qaf_params,
2353 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2354 AUDIO_QAF_PARAMETER_VALUE_REENCODE_DTS);
2355 }
2356 }
2357#endif
2358
2359 if (do_setparam) {
2360 if (p_qaf->qaf_msmd_enabled) {
2361 str_parms_add_str(qaf_params,
2362 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2363 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2364 } else {
2365 str_parms_add_str(qaf_params,
2366 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2367 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2368 }
2369 format_params = str_parms_to_str(qaf_params);
2370
2371 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2372 format_params);
2373
2374 passth_support = true;
2375 }
2376 str_parms_destroy(qaf_params);
2377 }
2378 }
2379
2380 //Compressed passthrough is not enabled.
2381 if (!passth_support) {
2382
2383 channels = platform_edid_get_max_channels(p_qaf->adev->platform);
2384
2385 qaf_params = str_parms_create();
2386 switch (channels) {
2387 case 8:
2388 DEBUG_MSG("Switching Qaf output to 7.1 channels");
2389 str_parms_add_str(qaf_params,
2390 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2391 AUDIO_QAF_PARAMETER_VALUE_8_CHANNELS);
2392 if (p_qaf->qaf_msmd_enabled) {
2393 str_parms_add_str(qaf_params,
2394 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2395 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2396 } else {
2397 str_parms_add_str(qaf_params,
2398 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2399 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2400 }
2401 p_qaf->hdmi_sink_channels = channels;
2402 break;
2403 case 6:
2404 DEBUG_MSG("Switching Qaf output to 5.1 channels");
2405 str_parms_add_str(qaf_params,
2406 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2407 AUDIO_QAF_PARAMETER_VALUE_6_CHANNELS);
2408 if (p_qaf->qaf_msmd_enabled) {
2409 str_parms_add_str(qaf_params,
2410 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2411 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2412 } else {
2413 str_parms_add_str(qaf_params,
2414 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2415 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2416 }
2417 p_qaf->hdmi_sink_channels = channels;
2418 break;
2419 default:
2420 DEBUG_MSG("Switching Qaf output to default channels");
2421 str_parms_add_str(qaf_params,
2422 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2423 AUDIO_QAF_PARAMETER_VALUE_DEFAULT_CHANNELS);
2424 if (p_qaf->qaf_msmd_enabled) {
2425 str_parms_add_str(qaf_params,
2426 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2427 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2428 } else {
2429 str_parms_add_str(qaf_params,
2430 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2431 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2432 }
2433 p_qaf->hdmi_sink_channels = 2;
2434 break;
2435 }
2436
2437 format_params = str_parms_to_str(qaf_params);
2438
2439 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2440 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2441 format_params);
2442 }
2443 if (p_qaf->qaf_mod[DTS_M8].session_handle
2444 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2445 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2446 format_params);
2447 }
2448
2449 str_parms_destroy(qaf_params);
2450 }
2451 DEBUG_MSG("Exit");
2452}
2453
2454/* QAF set parameter function. For Device connect and disconnect. */
2455int audio_extn_qaf_set_parameters(struct audio_device *adev, struct str_parms *parms)
2456{
2457 int status = 0, val = 0, k;
2458 char *format_params, *kv_parirs;
2459 struct str_parms *qaf_params;
2460 char value[32];
2461
2462 DEBUG_MSG("Entry");
2463
2464 if (!p_qaf) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302465 return -EINVAL;
2466 }
2467
2468 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
Harsh Bansal28718c52017-04-20 13:47:12 +05302469
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302470 if (status >= 0) {
2471 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302472 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is connected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302473
Harsh Bansal28718c52017-04-20 13:47:12 +05302474 p_qaf->hdmi_connect = 1;
2475 p_qaf->hdmi_sink_channels = 0;
2476
2477 if (p_qaf->passthrough_in) { //If QAF passthrough is already initialized.
2478 lock_output_stream(p_qaf->passthrough_in);
2479 if (platform_is_edid_supported_format(adev->platform,
2480 p_qaf->passthrough_in->format)) {
2481 //If passthrough format is supported by HDMI then create the QAF passthrough output if not created already.
2482 create_qaf_passthrough_stream();
2483 //Ignoring the returned error, If error then QAF passthrough is disabled.
2484 } else {
2485 //If passthrough format is not supported by HDMI then close the QAF passthrough output if already created.
2486 close_qaf_passthrough_stream();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302487 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302488 unlock_output_stream(p_qaf->passthrough_in);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302489 }
2490
Harsh Bansal28718c52017-04-20 13:47:12 +05302491 set_hdmi_configuration_to_module();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302492
Bharath Gopal01310bb2016-12-05 15:39:32 +05302493 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302494 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2495 if (!p_qaf->qaf_mod[k].bt_hdl) {
2496 DEBUG_MSG("Opening a2dp output...");
2497 status = audio_extn_bt_hal_load(&p_qaf->qaf_mod[k].bt_hdl);
2498 if (status != 0) {
2499 ERROR_MSG("Error opening BT module");
2500 return status;
2501 }
2502 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05302503 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302504 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302505 //TODO else if: Need to consider other devices.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302506 }
2507
2508 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
2509 if (status >= 0) {
2510 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302511 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is disconnected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302512
Harsh Bansal28718c52017-04-20 13:47:12 +05302513 qaf_params = str_parms_create();
2514 str_parms_add_str(qaf_params,
2515 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2516 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2517 str_parms_add_str(qaf_params,
2518 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2519 AUDIO_QAF_PARAMETER_VALUE_PCM);
2520 p_qaf->hdmi_sink_channels = 0;
2521
2522 p_qaf->passthrough_enabled = 0;
2523 p_qaf->mch_pcm_hdmi_enabled = 0;
2524 p_qaf->hdmi_connect = 0;
2525
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302526 format_params = str_parms_to_str(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302527
2528 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2529 if (p_qaf->qaf_mod[k].session_handle
2530 && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2531 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2532 p_qaf->qaf_mod[k].session_handle, format_params);
2533 }
2534 }
2535 close_all_hdmi_output();
2536
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302537 str_parms_destroy(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302538 close_qaf_passthrough_stream();
Bharath Gopal01310bb2016-12-05 15:39:32 +05302539 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302540 DEBUG_MSG("Closing a2dp output...");
2541 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2542 if (p_qaf->qaf_mod[k].bt_hdl) {
2543 audio_extn_bt_hal_unload(p_qaf->qaf_mod[k].bt_hdl);
2544 p_qaf->qaf_mod[k].bt_hdl = NULL;
2545 }
2546 }
2547 }
2548 //TODO else if: Need to consider other devices.
2549 }
2550
2551 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2552 kv_parirs = str_parms_to_str(parms);
2553 if (p_qaf->qaf_mod[k].session_handle && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2554 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2555 p_qaf->qaf_mod[k].session_handle, kv_parirs);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302556 }
2557 }
2558
Harsh Bansal28718c52017-04-20 13:47:12 +05302559 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302560 return status;
2561}
2562
Harsh Bansal28718c52017-04-20 13:47:12 +05302563/* Create the QAF. */
2564int audio_extn_qaf_init(struct audio_device *adev)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302565{
Harsh Bansal28718c52017-04-20 13:47:12 +05302566 DEBUG_MSG("Entry");
2567
2568 p_qaf = calloc(1, sizeof(struct qaf));
2569 if (p_qaf == NULL) {
2570 ERROR_MSG("Out of memory");
2571 return -ENOMEM;
2572 }
2573
2574 p_qaf->adev = adev;
2575
2576 if (property_get_bool("audio.qaf.msmd", false)) {
2577 p_qaf->qaf_msmd_enabled = 1;
2578 }
2579 pthread_mutex_init(&p_qaf->lock, (const pthread_mutexattr_t *) NULL);
2580
2581 int i = 0;
2582
2583 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2584 char value[PROPERTY_VALUE_MAX] = {0};
2585 char lib_name[PROPERTY_VALUE_MAX] = {0};
2586 struct qaf_module *qaf_mod = &(p_qaf->qaf_mod[i]);
2587
2588 if (i == MS12) {
2589 property_get("audio.qaf.library", value, NULL);
2590 } else if (i == DTS_M8) {
2591 property_get("audio.qaf.m8.library", value, NULL);
2592 } else {
2593 continue;
2594 }
2595
2596 snprintf(lib_name, PROPERTY_VALUE_MAX, "%s", value);
2597
2598 qaf_mod->qaf_lib = dlopen(lib_name, RTLD_NOW);
2599 if (qaf_mod->qaf_lib == NULL) {
2600 ERROR_MSG("DLOPEN failed for %s", lib_name);
2601 continue;
2602 }
2603
2604 DEBUG_MSG("DLOPEN successful for %s", lib_name);
2605 qaf_mod->qaf_audio_session_open =
2606 (int (*)(audio_session_handle_t* session_handle, void *p_data, void* license_data))dlsym(qaf_mod->qaf_lib,
2607 "audio_session_open");
2608 qaf_mod->qaf_audio_session_close =
2609 (int (*)(audio_session_handle_t session_handle))dlsym(qaf_mod->qaf_lib,
2610 "audio_session_close");
2611 qaf_mod->qaf_audio_stream_open =
2612 (int (*)(audio_session_handle_t session_handle, audio_stream_handle_t* stream_handle,
2613 audio_stream_config_t input_config, audio_devices_t devices, stream_type_t flags))dlsym(qaf_mod->qaf_lib,
2614 "audio_stream_open");
2615 qaf_mod->qaf_audio_stream_close =
2616 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2617 "audio_stream_close");
2618 qaf_mod->qaf_audio_stream_set_param =
2619 (int (*)(audio_stream_handle_t stream_handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2620 "audio_stream_set_param");
2621 qaf_mod->qaf_audio_session_set_param =
2622 (int (*)(audio_session_handle_t handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2623 "audio_session_set_param");
2624 qaf_mod->qaf_audio_stream_get_param =
2625 (char* (*)(audio_stream_handle_t stream_handle, const char* key))dlsym(qaf_mod->qaf_lib,
2626 "audio_stream_get_param");
2627 qaf_mod->qaf_audio_session_get_param =
2628 (char* (*)(audio_session_handle_t handle, const char* key))dlsym(qaf_mod->qaf_lib,
2629 "audio_session_get_param");
2630 qaf_mod->qaf_audio_stream_start =
2631 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2632 "audio_stream_start");
2633 qaf_mod->qaf_audio_stream_stop =
2634 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2635 "audio_stream_stop");
2636 qaf_mod->qaf_audio_stream_pause =
2637 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2638 "audio_stream_pause");
2639 qaf_mod->qaf_audio_stream_flush =
2640 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2641 "audio_stream_flush");
2642 qaf_mod->qaf_audio_stream_write =
2643 (int (*)(audio_stream_handle_t stream_handle, const void* buf, int size))dlsym(qaf_mod->qaf_lib,
2644 "audio_stream_write");
2645 qaf_mod->qaf_register_event_callback =
2646 (void (*)(audio_session_handle_t session_handle, void *priv_data, notify_event_callback_t event_callback,
2647 audio_event_id_t event_id))dlsym(qaf_mod->qaf_lib,
2648 "register_event_callback");
2649 }
2650
2651 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302652 return 0;
2653}
2654
Harsh Bansal28718c52017-04-20 13:47:12 +05302655/* Tear down the qaf extension. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302656void audio_extn_qaf_deinit()
2657{
Harsh Bansal28718c52017-04-20 13:47:12 +05302658 int i;
2659 DEBUG_MSG("Entry");
2660
2661 if (p_qaf != NULL) {
2662 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2663 qaf_session_close(&p_qaf->qaf_mod[i]);
2664
2665 if (p_qaf->qaf_mod[i].qaf_lib != NULL) {
2666 dlclose(p_qaf->qaf_mod[i].qaf_lib);
2667 p_qaf->qaf_mod[i].qaf_lib = NULL;
2668 }
2669 }
2670 if (p_qaf->passthrough_out) {
2671 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2672 (struct audio_stream_out *)(p_qaf->passthrough_out));
2673 p_qaf->passthrough_out = NULL;
2674 }
2675
2676 pthread_mutex_destroy(&p_qaf->lock);
2677 free(p_qaf);
2678 p_qaf = NULL;
2679 }
2680 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302681}