blob: d0a9a95507f10038dd545659bb7e29f60df07ab8 [file] [log] [blame]
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301/*
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +05302 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05303 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#define LOG_TAG "audio_hw_qaf"
31/*#define LOG_NDEBUG 0*/
32/*#define VERY_VERY_VERBOSE_LOGGING*/
33#ifdef VERY_VERY_VERBOSE_LOGGING
34#define ALOGVV ALOGV
35#else
36#define ALOGVV(a...) do { } while(0)
37#endif
38
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +053039#define DEBUG_MSG_VV(arg,...) ALOGVV("%s: %d: " arg, __func__, __LINE__, ##__VA_ARGS__)
Harsh Bansal28718c52017-04-20 13:47:12 +053040#define DEBUG_MSG(arg,...) ALOGV("%s: %d: " arg, __func__, __LINE__, ##__VA_ARGS__)
41#define ERROR_MSG(arg,...) ALOGE("%s: %d: " arg, __func__, __LINE__, ##__VA_ARGS__)
42
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053043#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 2
Harsh Bansal28718c52017-04-20 13:47:12 +053044#define COMPRESS_PASSTHROUGH_DDP_FRAGMENT_SIZE 4608
45
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053046#define QAF_DEFAULT_COMPR_AUDIO_HANDLE 1001
47#define QAF_DEFAULT_COMPR_PASSTHROUGH_HANDLE 1002
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053048#define QAF_DEFAULT_PASSTHROUGH_HANDLE 1003
Ben Rombergerd771a7c2017-02-22 18:05:17 -080049
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053050#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 300
Varun B34da7a42017-02-13 16:16:53 +053051
Varun B34da7a42017-02-13 16:16:53 +053052#define MIN_PCM_OFFLOAD_FRAGMENT_SIZE 512
53#define MAX_PCM_OFFLOAD_FRAGMENT_SIZE (240 * 1024)
54
55#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
56#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
57
58/* Pcm input node buffer size is 6144 bytes, i.e, 32msec for 48000 samplerate */
Harsh Bansal28718c52017-04-20 13:47:12 +053059#define QAF_MODULE_PCM_INPUT_BUFFER_LATENCY 32
Varun B34da7a42017-02-13 16:16:53 +053060
Harsh Bansal28718c52017-04-20 13:47:12 +053061#define MS12_PCM_OUT_FRAGMENT_SIZE 1536 //samples
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +053062#define MS12_PCM_IN_FRAGMENT_SIZE 1536 //samples
Varun B34da7a42017-02-13 16:16:53 +053063
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053064#define DD_FRAME_SIZE 1536
Harsh Bansal28718c52017-04-20 13:47:12 +053065#define DDP_FRAME_SIZE DD_FRAME_SIZE
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053066/*
Harsh Bansal28718c52017-04-20 13:47:12 +053067 * DD encoder output size for one frame.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053068 */
69#define DD_ENCODER_OUTPUT_SIZE 2560
70/*
Harsh Bansal28718c52017-04-20 13:47:12 +053071 * DDP encoder output size for one frame.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053072 */
73#define DDP_ENCODER_OUTPUT_SIZE 4608
74
Harsh Bansal28718c52017-04-20 13:47:12 +053075/*********TODO Need to get correct values.*************************/
76
77#define DTS_PCM_OUT_FRAGMENT_SIZE 1024 //samples
78
79#define DTS_FRAME_SIZE 1536
80#define DTSHD_FRAME_SIZE DTS_FRAME_SIZE
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053081/*
Harsh Bansal28718c52017-04-20 13:47:12 +053082 * DTS encoder output size for one frame.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +053083 */
Harsh Bansal28718c52017-04-20 13:47:12 +053084#define DTS_ENCODER_OUTPUT_SIZE 2560
85/*
86 * DTSHD encoder output size for one frame.
87 */
88#define DTSHD_ENCODER_OUTPUT_SIZE 4608
89/******************************************************************/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +053090
Bharath Gopal01310bb2016-12-05 15:39:32 +053091/*
92 * QAF Latency to process buffers since out_write from primary HAL
93 */
94#define QAF_COMPRESS_OFFLOAD_PROCESSING_LATENCY 18
95#define QAF_PCM_OFFLOAD_PROCESSING_LATENCY 48
96
Harsh Bansal28718c52017-04-20 13:47:12 +053097//TODO: Need to handle for DTS
Varun B34da7a42017-02-13 16:16:53 +053098#define QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1536
99
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530100#include <stdlib.h>
101#include <pthread.h>
102#include <errno.h>
103#include <dlfcn.h>
104#include <sys/resource.h>
105#include <sys/prctl.h>
106#include <cutils/properties.h>
107#include <cutils/str_parms.h>
108#include <cutils/log.h>
109#include <cutils/atomic.h>
110#include "audio_utils/primitives.h"
111#include "audio_hw.h"
112#include "platform_api.h"
113#include <platform.h>
114#include <system/thread_defs.h>
115#include <cutils/sched_policy.h>
116#include "audio_extn.h"
117#include <qti_audio.h>
118#include "sound/compress_params.h"
119
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +0530120#ifdef DYNAMIC_LOG_ENABLED
121#include <log_xml_parser.h>
122#define LOG_MASK HAL_MOD_FILE_QAF
123#include <log_utils.h>
124#endif
125
Harsh Bansal28718c52017-04-20 13:47:12 +0530126//TODO: Need to remove this.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530127#define QAF_OUTPUT_SAMPLING_RATE 48000
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530128
129#ifdef QAF_DUMP_ENABLED
130FILE *fp_output_writer_hdmi = NULL;
131#endif
132
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800133struct qaf_adsp_hdlr_config_state {
134 struct audio_adsp_event event_params;
135 /* For holding client audio_adsp_event payload */
136 uint8_t event_payload[AUDIO_MAX_ADSP_STREAM_CMD_PAYLOAD_LEN];
137 bool adsp_hdlr_config_valid;
138};
139
Harsh Bansal28718c52017-04-20 13:47:12 +0530140//Types of MM module, currently supported by QAF.
141typedef enum {
142 MS12,
143 DTS_M8,
144 MAX_MM_MODULE_TYPE,
145 INVALID_MM_MODULE
146} mm_module_type;
147
148typedef enum {
149 QAF_OUT_TRANSCODE_PASSTHROUGH = 0, /* Transcode passthrough via MM module*/
150 QAF_OUT_OFFLOAD_MCH, /* Multi-channel PCM offload*/
151 QAF_OUT_OFFLOAD, /* PCM offload */
152
153 MAX_QAF_MODULE_OUT
154} mm_module_output_type;
155
156typedef enum {
157 QAF_IN_MAIN = 0, /* Single PID Main/Primary or Dual-PID stream */
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530158 QAF_IN_ASSOC, /* Associated/Secondary stream */
159 QAF_IN_PCM, /* PCM stream. */
160 QAF_IN_MAIN_2, /* Single PID Main2 stream */
Harsh Bansal28718c52017-04-20 13:47:12 +0530161 MAX_QAF_MODULE_IN
162} mm_module_input_type;
163
164struct qaf_module {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530165 audio_session_handle_t session_handle;
166 void *qaf_lib;
Harsh Bansal28718c52017-04-20 13:47:12 +0530167 int (*qaf_audio_session_open)(audio_session_handle_t* session_handle,
168 void *p_data,
169 void* license_data);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530170 int (*qaf_audio_session_close)(audio_session_handle_t session_handle);
Harsh Bansal28718c52017-04-20 13:47:12 +0530171 int (*qaf_audio_stream_open)(audio_session_handle_t session_handle,
172 audio_stream_handle_t* stream_handle,
173 audio_stream_config_t input_config,
174 audio_devices_t devices,
175 stream_type_t flags);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530176 int (*qaf_audio_stream_close)(audio_stream_handle_t stream_handle);
177 int (*qaf_audio_stream_set_param)(audio_stream_handle_t stream_handle, const char* kv_pairs);
178 int (*qaf_audio_session_set_param)(audio_session_handle_t handle, const char* kv_pairs);
179 char* (*qaf_audio_stream_get_param)(audio_stream_handle_t stream_handle, const char* key);
180 char* (*qaf_audio_session_get_param)(audio_session_handle_t handle, const char* key);
181 int (*qaf_audio_stream_start)(audio_stream_handle_t handle);
182 int (*qaf_audio_stream_stop)(audio_stream_handle_t stream_handle);
183 int (*qaf_audio_stream_pause)(audio_stream_handle_t stream_handle);
184 int (*qaf_audio_stream_flush)(audio_stream_handle_t stream_handle);
185 int (*qaf_audio_stream_write)(audio_stream_handle_t stream_handle, const void* buf, int size);
Harsh Bansal28718c52017-04-20 13:47:12 +0530186 void (*qaf_register_event_callback)(audio_session_handle_t session_handle,
187 void *priv_data,
188 notify_event_callback_t event_callback,
189 audio_event_id_t event_id);
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800190
Harsh Bansal28718c52017-04-20 13:47:12 +0530191 /*Input stream of MM module */
192 struct stream_out *stream_in[MAX_QAF_MODULE_IN];
193 /*Output Stream from MM module */
194 struct stream_out *stream_out[MAX_QAF_MODULE_OUT];
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800195
Harsh Bansal28718c52017-04-20 13:47:12 +0530196 struct qaf_adsp_hdlr_config_state adsp_hdlr_config[MAX_QAF_MODULE_IN];
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800197
Harsh Bansal28718c52017-04-20 13:47:12 +0530198 //BT session handle.
Bharath Gopal01310bb2016-12-05 15:39:32 +0530199 void *bt_hdl;
Harsh Bansal28718c52017-04-20 13:47:12 +0530200
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530201 float vol_left;
202 float vol_right;
Harsh Bansal28718c52017-04-20 13:47:12 +0530203 bool is_vol_set;
204 bool drain_received[MAX_QAF_MODULE_IN];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530205};
206
Harsh Bansal28718c52017-04-20 13:47:12 +0530207struct qaf {
208 struct audio_device *adev;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530209
Harsh Bansal28718c52017-04-20 13:47:12 +0530210 pthread_mutex_t lock;
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800211
Harsh Bansal28718c52017-04-20 13:47:12 +0530212 bool hdmi_connect;
213 int hdmi_sink_channels;
214
215 //Flag to indicate if QAF transcode output stream is enabled from any mm module.
216 bool passthrough_enabled;
217 //Flag to indicate if QAF mch pcm output stream is enabled from any mm module.
218 bool mch_pcm_hdmi_enabled;
219
220 //Flag to indicate if msmd is supported.
221 bool qaf_msmd_enabled;
222
223 //Handle of QAF input stream, which is routed as QAF passthrough.
224 struct stream_out *passthrough_in;
225 //Handle of QAF passthrough stream.
226 struct stream_out *passthrough_out;
227
228 struct qaf_module qaf_mod[MAX_MM_MODULE_TYPE];
229};
230
231static int qaf_out_pause(struct audio_stream_out* stream);
232static int qaf_out_flush(struct audio_stream_out* stream);
233static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type);
234
235//Global handle of QAF. Access to this should be protected by mutex lock.
236static struct qaf *p_qaf = NULL;
237
238/* Gets the pointer to qaf module for the qaf input stream. */
239static struct qaf_module* get_qaf_module_for_input_stream(struct stream_out *out)
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530240{
Harsh Bansal28718c52017-04-20 13:47:12 +0530241 struct qaf_module *qaf_mod = NULL;
242 int i, j;
243 if (!p_qaf) return NULL;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530244
Harsh Bansal28718c52017-04-20 13:47:12 +0530245 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
246 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
247 if (p_qaf->qaf_mod[i].stream_in[j] == out) {
248 qaf_mod = &(p_qaf->qaf_mod[i]);
249 break;
250 }
251 }
252 }
253
254 return qaf_mod;
Deepak Agarwal1e42b852017-02-11 17:57:04 +0530255}
256
Harsh Bansal28718c52017-04-20 13:47:12 +0530257/* Finds the mm module input stream index for the QAF input stream. */
258static int get_input_stream_index(struct stream_out *out)
259{
260 int index = -1, j;
261 struct qaf_module* qaf_mod = NULL;
262
263 qaf_mod = get_qaf_module_for_input_stream(out);
264 if (!qaf_mod) return index;
265
266 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
267 if (qaf_mod->stream_in[j] == out) {
268 index = j;
269 break;
270 }
271 }
272
273 return index;
274}
275
276/* Finds the right mm module for the QAF input stream format. */
277static mm_module_type get_mm_module_for_format(audio_format_t format)
278{
279 int j;
280
281 DEBUG_MSG("Format 0x%x", format);
282
283 if (format == AUDIO_FORMAT_PCM_16_BIT) {
284 //If dts is not supported then alway support pcm with MS12
285 if (!property_get_bool("audio.qaf.dts_m8", false)) { //TODO: Need to add this property for DTS.
286 return MS12;
287 }
288
289 //If QAF passthrough is active then send the PCM stream to primary HAL.
290 if (!p_qaf->passthrough_out) {
291 /* Iff any stream is active in MS12 module then route PCM stream to it. */
292 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
293 if (p_qaf->qaf_mod[MS12].stream_in[j]) {
294 return MS12;
295 }
296 }
297 }
298 return INVALID_MM_MODULE;
299 }
300
301 switch (format & AUDIO_FORMAT_MAIN_MASK) {
302 case AUDIO_FORMAT_AC3:
303 case AUDIO_FORMAT_E_AC3:
304 case AUDIO_FORMAT_AAC:
305 case AUDIO_FORMAT_AAC_ADTS:
306 case AUDIO_FORMAT_AC4:
307 return MS12;
308 case AUDIO_FORMAT_DTS:
309 case AUDIO_FORMAT_DTS_HD:
310 return DTS_M8;
311 default:
312 return INVALID_MM_MODULE;
313 }
314}
315
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530316static bool is_main_active(struct qaf_module* qaf_mod)
317{
318 return (qaf_mod->stream_in[QAF_IN_MAIN] || qaf_mod->stream_in[QAF_IN_MAIN_2]);
319}
320
321static bool is_dual_main_active(struct qaf_module* qaf_mod)
322{
323 return (qaf_mod->stream_in[QAF_IN_MAIN] && qaf_mod->stream_in[QAF_IN_MAIN_2]);
324}
325
Harsh Bansal28718c52017-04-20 13:47:12 +0530326/* Gets the pcm output buffer size(in samples) for the mm module. */
327static uint32_t get_pcm_output_buffer_size_samples(struct qaf_module *qaf_mod)
328{
329 uint32_t pcm_output_buffer_size = 0;
330
331 if (qaf_mod == &p_qaf->qaf_mod[MS12]) {
332 pcm_output_buffer_size = MS12_PCM_OUT_FRAGMENT_SIZE;
333 } else if (qaf_mod == &p_qaf->qaf_mod[DTS_M8]) {
334 pcm_output_buffer_size = DTS_PCM_OUT_FRAGMENT_SIZE;
335 }
336
337 return pcm_output_buffer_size;
338}
339
340/* Acquire Mutex lock on output stream */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530341static void lock_output_stream(struct stream_out *out)
342{
343 pthread_mutex_lock(&out->pre_lock);
344 pthread_mutex_lock(&out->lock);
345 pthread_mutex_unlock(&out->pre_lock);
346}
347
Harsh Bansal28718c52017-04-20 13:47:12 +0530348/* Release Mutex lock on output stream */
349static void unlock_output_stream(struct stream_out *out)
350{
351 pthread_mutex_unlock(&out->lock);
352}
353
354/* Checks if stream can be routed as QAF passthrough or not. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530355static bool audio_extn_qaf_passthrough_enabled(struct stream_out *out)
356{
Harsh Bansal28718c52017-04-20 13:47:12 +0530357 DEBUG_MSG("Format 0x%x", out->format);
358 bool is_enabled = false;
359
360 if (!p_qaf) return false;
361
362 if ((!property_get_bool("audio.qaf.reencode", false))
363 && property_get_bool("audio.qaf.passthrough", false)) {
364
365 if ((out->format == AUDIO_FORMAT_PCM_16_BIT) && (popcount(out->channel_mask) > 2)) {
366 is_enabled = true;
367 } else if (property_get_bool("audio.offload.passthrough", false)) {
368 switch (out->format) {
369 case AUDIO_FORMAT_AC3:
370 case AUDIO_FORMAT_E_AC3:
371 case AUDIO_FORMAT_DTS:
Ben Romberger1aaaf862017-04-06 17:49:46 -0700372 case AUDIO_FORMAT_DTS_HD:
373 case AUDIO_FORMAT_DOLBY_TRUEHD: {
Harsh Bansal28718c52017-04-20 13:47:12 +0530374 is_enabled = true;
375 break;
376 }
377 default:
378 is_enabled = false;
379 break;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530380 }
381 }
382 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530383
384 return is_enabled;
385}
386
387/*Closes all pcm hdmi output from QAF. */
388static void close_all_pcm_hdmi_output()
389{
390 int i;
391 //Closing all the PCM HDMI output stream from QAF.
392 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
393 if (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]) {
394 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
395 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]));
396 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH] = NULL;
397 }
398
399 if ((p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD])
400 && (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
401 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
402 (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]));
403 p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD] = NULL;
404 }
405 }
406
407 p_qaf->mch_pcm_hdmi_enabled = 0;
408}
409
410static void close_all_hdmi_output()
411{
412 int k;
413 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
414 if (p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
415 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
416 (struct audio_stream_out *)(p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]));
417 p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] = NULL;
418 }
419 }
420 p_qaf->passthrough_enabled = 0;
421
422 close_all_pcm_hdmi_output();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530423}
424
425static int qaf_out_callback(stream_callback_event_t event, void *param __unused, void *cookie)
426{
427 struct stream_out *out = (struct stream_out *)cookie;
428
Ben Rombergerd771a7c2017-02-22 18:05:17 -0800429 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530430 return 0;
431}
432
Harsh Bansal28718c52017-04-20 13:47:12 +0530433/* Creates the QAF passthrough output stream. */
434static int create_qaf_passthrough_stream()
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530435{
Harsh Bansal28718c52017-04-20 13:47:12 +0530436 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530437
Harsh Bansal28718c52017-04-20 13:47:12 +0530438 int ret = 0, k;
439 struct stream_out *out = p_qaf->passthrough_in;
440
441 if (!out) return -EINVAL;
442
443 pthread_mutex_lock(&p_qaf->lock);
444 lock_output_stream(out);
445
446 //Creating QAF passthrough output stream.
447 if (NULL == p_qaf->passthrough_out) {
448 audio_output_flags_t flags;
449 struct audio_config config;
450 audio_devices_t devices;
451
452 config.sample_rate = config.offload_info.sample_rate = out->sample_rate;
453 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
454 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
455 config.offload_info.format = out->format;
456 config.offload_info.bit_width = out->bit_width;
457 config.format = out->format;
458 config.offload_info.channel_mask = config.channel_mask = out->channel_mask;
459
460 //Device is copied from the QAF passthrough input stream.
461 devices = out->devices;
462 flags = out->flags;
463
464 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
465 QAF_DEFAULT_PASSTHROUGH_HANDLE,
466 devices,
467 flags,
468 &config,
469 (struct audio_stream_out **)&(p_qaf->passthrough_out),
470 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530471 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530472 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
473 unlock_output_stream(out);
474 return ret;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530475 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530476 p_qaf->passthrough_in = out;
477 p_qaf->passthrough_out->stream.set_callback((struct audio_stream_out *)p_qaf->passthrough_out,
478 (stream_callback_t) qaf_out_callback, out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530479 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530480
481 unlock_output_stream(out);
482
483 //Since QAF-Passthrough is created, close other HDMI outputs.
484 close_all_hdmi_output();
485
486 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530487 return ret;
488}
489
Harsh Bansal28718c52017-04-20 13:47:12 +0530490/* Closes the QAF passthrough output stream. */
491static void close_qaf_passthrough_stream()
492{
493 if (p_qaf->passthrough_out != NULL) { //QAF pasthroug is enabled. Close it.
494 pthread_mutex_lock(&p_qaf->lock);
495 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
496 (struct audio_stream_out *)(p_qaf->passthrough_out));
497 p_qaf->passthrough_out = NULL;
498 pthread_mutex_unlock(&p_qaf->lock);
499
500 if (p_qaf->passthrough_in->qaf_stream_handle) {
501 qaf_out_pause((struct audio_stream_out*)p_qaf->passthrough_in);
502 qaf_out_flush((struct audio_stream_out*)p_qaf->passthrough_in);
503 qaf_out_drain((struct audio_stream_out*)p_qaf->passthrough_in,
504 (audio_drain_type_t)STREAM_CBK_EVENT_DRAIN_READY);
505 }
506 }
507}
508
509/* Sends a command to output stream offload thread. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530510static int qaf_send_offload_cmd_l(struct stream_out* out, int command)
511{
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530512 DEBUG_MSG_VV("command is %d", command);
Harsh Bansal28718c52017-04-20 13:47:12 +0530513
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530514 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
515
516 if (!cmd) {
Harsh Bansal28718c52017-04-20 13:47:12 +0530517 ERROR_MSG("failed to allocate mem for command 0x%x", command);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530518 return -ENOMEM;
519 }
520
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530521 cmd->cmd = command;
Harsh Bansal28718c52017-04-20 13:47:12 +0530522
523 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530524 list_add_tail(&out->qaf_offload_cmd_list, &cmd->node);
525 pthread_cond_signal(&out->qaf_offload_cond);
Harsh Bansal28718c52017-04-20 13:47:12 +0530526 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530527 return 0;
528}
529
Harsh Bansal28718c52017-04-20 13:47:12 +0530530/* Stops a QAF module stream.*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530531static int audio_extn_qaf_stream_stop(struct stream_out *out)
532{
Harsh Bansal28718c52017-04-20 13:47:12 +0530533 int ret = 0;
534 DEBUG_MSG("Output Stream 0x%x", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530535
Harsh Bansal28718c52017-04-20 13:47:12 +0530536 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
537 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_stop)) {
538 return ret;
539 }
540
541 if (out->qaf_stream_handle) {
542 ret = qaf_mod->qaf_audio_stream_stop(out->qaf_stream_handle);
543 }
544
545 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530546}
547
Harsh Bansal28718c52017-04-20 13:47:12 +0530548/* Puts a QAF module stream in standby. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530549static int qaf_out_standby(struct audio_stream *stream)
550{
551 struct stream_out *out = (struct stream_out *)stream;
552 int status = 0;
553
554 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
555 stream, out->usecase, use_case_table[out->usecase]);
556
557 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530558
559 //If QAF passthrough is active then block standby on all the input streams of QAF mm modules.
560 if (p_qaf->passthrough_out) {
561 //If standby is received on QAF passthrough stream then forward it to primary HAL.
562 if (p_qaf->passthrough_in == out) {
563 status = p_qaf->passthrough_out->stream.common.standby(
564 (struct audio_stream *)p_qaf->passthrough_out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530565 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530566 } else {
567 //If QAF passthrough stream is not active then stop the QAF module stream.
568 status = audio_extn_qaf_stream_stop(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530569 }
570
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530571 if (!out->standby) {
572 out->standby = true;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530573 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530574
575 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530576 return status;
577}
578
Harsh Bansal28718c52017-04-20 13:47:12 +0530579/* Sets the volume to PCM output stream. */
580static int qaf_out_set_volume(struct audio_stream_out *stream, float left, float right)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530581{
582 int ret = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530583 struct stream_out *out = (struct stream_out *)stream;
584 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530585
Harsh Bansal28718c52017-04-20 13:47:12 +0530586 DEBUG_MSG("Left %f, Right %f", left, right);
587
588 qaf_mod = get_qaf_module_for_input_stream(out);
589 if (!qaf_mod) {
590 return -EINVAL;
591 }
592
593 pthread_mutex_lock(&p_qaf->lock);
594 qaf_mod->vol_left = left;
595 qaf_mod->vol_right = right;
596 qaf_mod->is_vol_set = true;
597 pthread_mutex_unlock(&p_qaf->lock);
598
599 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL) {
600 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
601 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], left, right);
602 }
603
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530604 return ret;
605}
606
Harsh Bansal28718c52017-04-20 13:47:12 +0530607/* Starts a QAF module stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530608static int qaf_stream_start(struct stream_out *out)
609{
Harsh Bansal28718c52017-04-20 13:47:12 +0530610 int ret = -EINVAL;
611 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530612
Harsh Bansal28718c52017-04-20 13:47:12 +0530613 DEBUG_MSG("Output Stream = %p", out);
614
615 qaf_mod = get_qaf_module_for_input_stream(out);
616 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_start)) {
617 return -EINVAL;
618 }
619
620 if (out->qaf_stream_handle) {
621 ret = qaf_mod->qaf_audio_stream_start(out->qaf_stream_handle);
622 }
623
624 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530625}
626
627static int qaf_start_output_stream(struct stream_out *out)
628{
629 int ret = 0;
630 struct audio_device *adev = out->dev;
631 int snd_card_status = get_snd_card_state(adev);
632
633 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
634 ret = -EINVAL;
635 usleep(50000);
636 return ret;
637 }
638
639 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
640 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
641 out->devices);
642
643 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
644 ALOGE("%s: sound card is not active/SSR returning error", __func__);
645 ret = -EIO;
646 usleep(50000);
647 return ret;
648 }
649
650 return qaf_stream_start(out);
651}
652
Harsh Bansal28718c52017-04-20 13:47:12 +0530653/* Sends input buffer to the QAF MM module. */
654static int qaf_module_write_input_buffer(struct stream_out *out, const void *buffer, int bytes)
655{
656 int ret = -EINVAL;
657 struct qaf_module *qaf_mod = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +0530658
659 qaf_mod = get_qaf_module_for_input_stream(out);
660 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_write)) {
661 return ret;
662 }
663
664 if (out->qaf_stream_handle) {
665 ret = qaf_mod->qaf_audio_stream_write(out->qaf_stream_handle, buffer, bytes);
666 }
667 return ret;
668}
669
670/* Writes buffer to QAF input stream. */
671static ssize_t qaf_out_write(struct audio_stream_out *stream, const void *buffer, size_t bytes)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530672{
673 struct stream_out *out = (struct stream_out *)stream;
674 struct audio_device *adev = out->dev;
675 ssize_t ret = 0;
676
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530677 DEBUG_MSG_VV("bytes = %d, usecase[%d] and flags[%x] for handle[%p]",
Harsh Bansal28718c52017-04-20 13:47:12 +0530678 (int)bytes, out->usecase, out->flags, out);
679
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530680 lock_output_stream(out);
681
Harsh Bansal28718c52017-04-20 13:47:12 +0530682 // If QAF passthrough is active then block writing data to QAF mm module.
683 if (p_qaf->passthrough_out) {
684 //If write is received for the QAF passthrough stream then send the buffer to primary HAL.
685 if (p_qaf->passthrough_in == out) {
686 ret = p_qaf->passthrough_out->stream.write(
687 (struct audio_stream_out *)(p_qaf->passthrough_out),
688 buffer,
689 bytes);
690 if (ret > 0) out->standby = false;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530691 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530692 unlock_output_stream(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530693 return ret;
Harsh Bansal28718c52017-04-20 13:47:12 +0530694 } else if (out->standby) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530695 pthread_mutex_lock(&adev->lock);
696 ret = qaf_start_output_stream(out);
697 pthread_mutex_unlock(&adev->lock);
Harsh Bansal28718c52017-04-20 13:47:12 +0530698 if (ret == 0) {
699 out->standby = false;
700 } else {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530701 goto exit;
702 }
703 }
704
Harsh Bansal28718c52017-04-20 13:47:12 +0530705 if ((adev->is_channel_status_set == false) && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530706 audio_utils_set_hdmi_channel_status(out, (char *)buffer, bytes);
707 adev->is_channel_status_set = true;
708 }
709
Harsh Bansal28718c52017-04-20 13:47:12 +0530710 ret = qaf_module_write_input_buffer(out, buffer, bytes);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530711 DEBUG_MSG_VV("ret [%d]", (int)ret);
Harsh Bansal28718c52017-04-20 13:47:12 +0530712
713 if (ret >= 0) {
714 bytes = ret;
715 out->written += bytes / ((popcount(out->channel_mask) * sizeof(short)));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530716 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530717
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530718
719exit:
Harsh Bansal28718c52017-04-20 13:47:12 +0530720 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530721
722 if (ret < 0) {
723 if (ret == -EAGAIN) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530724 DEBUG_MSG_VV("No space available in mm module, post msg to cb thread");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530725 ret = qaf_send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530726 bytes = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530727 } else if (ret == -ENOMEM || ret == -EPERM) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530728 if (out->pcm)
Harsh Bansal28718c52017-04-20 13:47:12 +0530729 ERROR_MSG("error %d, %s", (int)ret, pcm_get_error(out->pcm));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530730 qaf_out_standby(&out->stream.common);
Harsh Bansal28718c52017-04-20 13:47:12 +0530731 usleep(bytes * 1000000
732 / audio_stream_out_frame_size(stream)
733 / out->stream.common.get_sample_rate(&out->stream.common));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530734 }
735 }
736 return bytes;
737}
738
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530739/* Gets PCM offload buffer size for a given config. */
740static uint32_t qaf_get_pcm_offload_buffer_size(audio_offload_info_t* info,
741 uint32_t samples_per_frame)
Varun B34da7a42017-02-13 16:16:53 +0530742{
743 uint32_t fragment_size = 0;
Varun B34da7a42017-02-13 16:16:53 +0530744
Harsh Bansal28718c52017-04-20 13:47:12 +0530745 fragment_size = (samples_per_frame * (info->bit_width >> 3) * popcount(info->channel_mask));
746
747 if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530748 fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530749 else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE)
Varun B34da7a42017-02-13 16:16:53 +0530750 fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE;
Harsh Bansal28718c52017-04-20 13:47:12 +0530751
Varun B34da7a42017-02-13 16:16:53 +0530752 // To have same PCM samples for all channels, the buffer size requires to
753 // be multiple of (number of channels * bytes per sample)
754 // For writes to succeed, the buffer must be written at address which is multiple of 32
Harsh Bansal28718c52017-04-20 13:47:12 +0530755 fragment_size = ALIGN(fragment_size,
756 ((info->bit_width >> 3) * popcount(info->channel_mask) * 32));
Varun B34da7a42017-02-13 16:16:53 +0530757
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530758 ALOGI("Qaf PCM offload Fragment size is %d bytes", fragment_size);
Varun B34da7a42017-02-13 16:16:53 +0530759
760 return fragment_size;
761}
762
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +0530763static uint32_t qaf_get_pcm_offload_input_buffer_size(info)
764{
765 return qaf_get_pcm_offload_buffer_size(info, MS12_PCM_IN_FRAGMENT_SIZE);
766}
767
768static uint32_t qaf_get_pcm_offload_output_buffer_size(struct qaf_module *qaf_mod,
769 audio_offload_info_t* info)
770{
771 return qaf_get_pcm_offload_buffer_size(info, get_pcm_output_buffer_size_samples(qaf_mod));
772}
773
Harsh Bansal28718c52017-04-20 13:47:12 +0530774/* Gets buffer latency in samples. */
775static int get_buffer_latency(struct stream_out *out, uint32_t buffer_size, uint32_t *latency)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530776{
Harsh Bansal28718c52017-04-20 13:47:12 +0530777 unsigned long int samples_in_one_encoded_frame;
778 unsigned long int size_of_one_encoded_frame;
779
780 switch (out->format) {
781 case AUDIO_FORMAT_AC3:
782 samples_in_one_encoded_frame = DD_FRAME_SIZE;
783 size_of_one_encoded_frame = DD_ENCODER_OUTPUT_SIZE;
784 break;
785 case AUDIO_FORMAT_E_AC3:
786 samples_in_one_encoded_frame = DDP_FRAME_SIZE;
787 size_of_one_encoded_frame = DDP_ENCODER_OUTPUT_SIZE;
788 break;
789 case AUDIO_FORMAT_DTS:
790 samples_in_one_encoded_frame = DTS_FRAME_SIZE;
791 size_of_one_encoded_frame = DTS_ENCODER_OUTPUT_SIZE;
792 break;
793 case AUDIO_FORMAT_DTS_HD:
794 samples_in_one_encoded_frame = DTSHD_FRAME_SIZE;
795 size_of_one_encoded_frame = DTSHD_ENCODER_OUTPUT_SIZE;
796 break;
797 case AUDIO_FORMAT_PCM_16_BIT:
798 samples_in_one_encoded_frame = 1;
799 size_of_one_encoded_frame = ((out->bit_width) >> 3) * popcount(out->channel_mask);
800 break;
801 default:
802 *latency = 0;
803 return (-EINVAL);
804 }
805
806 *latency = ((buffer_size * samples_in_one_encoded_frame) / size_of_one_encoded_frame);
807 return 0;
808}
809
810/* Returns the number of frames rendered to outside observer. */
811static int qaf_get_rendered_frames(struct stream_out *out, uint64_t *frames)
812{
813 int ret = 0, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530814 struct str_parms *parms;
815 int value = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530816 int module_latency = 0;
817 uint32_t kernel_latency = 0;
818 uint32_t dsp_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530819 int signed_frames = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530820 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +0530821 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530822
Harsh Bansal28718c52017-04-20 13:47:12 +0530823 DEBUG_MSG("Output Format %d", out->format);
824
825 qaf_mod = get_qaf_module_for_input_stream(out);
826 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_get_param)) {
827 return -EINVAL;
828 }
829
830 //Get MM module latency.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530831 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "get_latency");
832 if (kvpairs) {
833 parms = str_parms_create_str(kvpairs);
Harsh Bansal28718c52017-04-20 13:47:12 +0530834 ret = str_parms_get_int(parms, "get_latency", &module_latency);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530835 if (ret >= 0) {
836 str_parms_destroy(parms);
837 parms = NULL;
838 }
839 free(kvpairs);
840 kvpairs = NULL;
841 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530842
843 //Get kernel Latency
844 for (i = MAX_QAF_MODULE_OUT - 1; i >= 0; i--) {
845 if (qaf_mod->stream_out[i] == NULL) {
846 continue;
847 } else {
848 unsigned int num_fragments = qaf_mod->stream_out[i]->compr_config.fragments;
849 uint32_t fragment_size = qaf_mod->stream_out[i]->compr_config.fragment_size;
850 uint32_t kernel_buffer_size = num_fragments * fragment_size;
851 get_buffer_latency(qaf_mod->stream_out[i], kernel_buffer_size, &kernel_latency);
852 break;
853 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530854 }
855
Harsh Bansal28718c52017-04-20 13:47:12 +0530856 //Get DSP latency
857 if ((qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL)
858 || (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH] != NULL)) {
859 unsigned int sample_rate = 0;
860
861 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
862 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
863 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
864 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
865
866 audio_usecase_t platform_latency =
867 platform_render_latency(qaf_mod->stream_out[QAF_OUT_OFFLOAD]->usecase);
868 dsp_latency = (platform_latency * sample_rate) / 1000000LL;
869 } else if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] != NULL) {
870 unsigned int sample_rate = 0;
871
872 sample_rate = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->sample_rate; //TODO: How this sample rate can be used?
873 dsp_latency = (COMPRESS_OFFLOAD_PLAYBACK_LATENCY * sample_rate) / 1000;
874 }
875
876 // MM Module Latency + Kernel Latency + DSP Latency
877 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
878 out->platform_latency = module_latency + audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl);
879 } else {
880 out->platform_latency = (uint32_t)module_latency + kernel_latency + dsp_latency;
881 }
882
883 if (out->format & AUDIO_FORMAT_PCM_16_BIT) {
884 *frames = 0;
885 signed_frames = out->written - out->platform_latency;
886 // It would be unusual for this value to be negative, but check just in case ...
887 if (signed_frames >= 0) {
888 *frames = signed_frames;
889 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530890 } else if (qaf_mod->qaf_audio_stream_get_param) {
891 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "position");
892 if (kvpairs) {
893 parms = str_parms_create_str(kvpairs);
894 ret = str_parms_get_int(parms, "position", &value);
895 if (ret >= 0) {
896 *frames = value;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530897 signed_frames = value - out->platform_latency;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530898 // It would be unusual for this value to be negative, but check just in case ...
899 if (signed_frames >= 0) {
900 *frames = signed_frames;
901 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530902 }
903 str_parms_destroy(parms);
904 }
905 } else {
906 ret = -EINVAL;
907 }
Harsh Bansal28718c52017-04-20 13:47:12 +0530908
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530909 return ret;
910}
911
912static int qaf_out_get_presentation_position(const struct audio_stream_out *stream,
Harsh Bansal28718c52017-04-20 13:47:12 +0530913 uint64_t *frames,
914 struct timespec *timestamp)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530915{
916 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530917 int ret = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530918
Harsh Bansal28718c52017-04-20 13:47:12 +0530919 DEBUG_MSG("Output Stream %p", stream);
920
921 //If QAF passthorugh output stream is active.
922 if (p_qaf->passthrough_out) {
923 if (p_qaf->passthrough_in == out) {
924 //If api is called for QAF passthorugh stream then call the primary HAL api to get the position.
925 pthread_mutex_lock(&p_qaf->lock);
926 ret = p_qaf->passthrough_out->stream.get_presentation_position(
927 (struct audio_stream_out *)p_qaf->passthrough_out,
928 frames,
929 timestamp);
930 pthread_mutex_unlock(&p_qaf->lock);
931 } else {
932 //If api is called for other stream then return zero frames.
933 *frames = 0;
934 clock_gettime(CLOCK_MONOTONIC, timestamp);
935 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530936 return ret;
937 }
938
Harsh Bansal28718c52017-04-20 13:47:12 +0530939 ret = qaf_get_rendered_frames(out, frames);
940 clock_gettime(CLOCK_MONOTONIC, timestamp);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530941
942 return ret;
943}
944
Harsh Bansal28718c52017-04-20 13:47:12 +0530945/* Pause the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530946static int qaf_stream_pause(struct stream_out *out)
947{
Harsh Bansal28718c52017-04-20 13:47:12 +0530948 struct qaf_module *qaf_mod = NULL;
949 int ret = -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530950
Harsh Bansal28718c52017-04-20 13:47:12 +0530951 qaf_mod = get_qaf_module_for_input_stream(out);
952 if (!qaf_mod || !qaf_mod->qaf_audio_stream_pause) {
953 return -EINVAL;
954 }
955
956 if (out->qaf_stream_handle)
957 ret = qaf_mod->qaf_audio_stream_pause(out->qaf_stream_handle);
958
959 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530960}
961
Harsh Bansal28718c52017-04-20 13:47:12 +0530962/* Pause a QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530963static int qaf_out_pause(struct audio_stream_out* stream)
964{
965 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +0530966 int status = 0;
967 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 pause on module stream.
972 if (p_qaf->passthrough_out) {
973 pthread_mutex_lock(&p_qaf->lock);
974 //If pause 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.pause(
977 (struct audio_stream_out *)p_qaf->passthrough_out);
978 out->offload_state = OFFLOAD_STATE_PAUSED;
979 }
980 pthread_mutex_unlock(&p_qaf->lock);
981 } else {
982 //Pause the module input stream.
983 status = qaf_stream_pause(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530984 }
985
Harsh Bansal28718c52017-04-20 13:47:12 +0530986 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530987 return status;
988}
989
Harsh Bansal28718c52017-04-20 13:47:12 +0530990/* Drains a qaf input stream. */
991static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530992{
993 struct stream_out *out = (struct stream_out *)stream;
994 int status = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +0530995 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +0530996
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +0530997 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +0530998
999 //If QAF passthrough is enabled then block the drain on module stream.
1000 if (p_qaf->passthrough_out) {
1001 pthread_mutex_lock(&p_qaf->lock);
1002 //If drain is received for QAF passthorugh stream then call the primary HAL api.
1003 if (p_qaf->passthrough_in == out) {
1004 status = p_qaf->passthrough_out->stream.drain(
1005 (struct audio_stream_out *)p_qaf->passthrough_out, type);
1006 }
1007 pthread_mutex_unlock(&p_qaf->lock);
1008 } else {
1009 //Drain the module input stream.
1010 /* Stream stop will trigger EOS and on EOS_EVENT received
1011 from callback DRAIN_READY command is sent */
1012 status = audio_extn_qaf_stream_stop(out);
1013
1014 if (status == 0) {
1015 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
1016 int index = get_input_stream_index(out);
1017 if (qaf_mod && index >= 0) qaf_mod->drain_received[index] = true;
1018 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301019 }
1020
Harsh Bansal28718c52017-04-20 13:47:12 +05301021 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301022 return status;
1023}
1024
Harsh Bansal28718c52017-04-20 13:47:12 +05301025/* Flush the QAF module input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301026static int audio_extn_qaf_stream_flush(struct stream_out *out)
1027{
Harsh Bansal28718c52017-04-20 13:47:12 +05301028 DEBUG_MSG("Output Stream %p", out);
1029 int ret = -EINVAL;
1030 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301031
Harsh Bansal28718c52017-04-20 13:47:12 +05301032 qaf_mod = get_qaf_module_for_input_stream(out);
1033 if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_flush)) {
1034 return -EINVAL;
1035 }
1036
1037 if (out->qaf_stream_handle)
1038 ret = qaf_mod->qaf_audio_stream_flush(out->qaf_stream_handle);
1039
1040 return ret;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301041}
1042
Harsh Bansal28718c52017-04-20 13:47:12 +05301043/* Flush the QAF input stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301044static int qaf_out_flush(struct audio_stream_out* stream)
1045{
1046 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05301047 int status = 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301048
Harsh Bansal28718c52017-04-20 13:47:12 +05301049 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301050 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301051
1052 //If QAF passthrough is active then block the flush on module input streams.
1053 if (p_qaf->passthrough_out) {
1054 pthread_mutex_lock(&p_qaf->lock);
1055 //If flush is received for the QAF passthrough stream then call the primary HAL api.
1056 if (p_qaf->passthrough_in == out) {
1057 status = p_qaf->passthrough_out->stream.flush(
1058 (struct audio_stream_out *)p_qaf->passthrough_out);
1059 out->offload_state = OFFLOAD_STATE_IDLE;
1060 }
1061 pthread_mutex_unlock(&p_qaf->lock);
1062 } else {
1063 //Flush the module input stream.
1064 status = audio_extn_qaf_stream_flush(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301065 }
1066
Harsh Bansal28718c52017-04-20 13:47:12 +05301067 unlock_output_stream(out);
1068 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301069 return status;
1070}
1071
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301072static uint32_t qaf_out_get_latency(const struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301073{
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301074 struct stream_out *out = (struct stream_out *)stream;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301075 uint32_t latency = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05301076 struct qaf_module *qaf_mod = NULL;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301077 DEBUG_MSG_VV("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301078
Harsh Bansal28718c52017-04-20 13:47:12 +05301079 qaf_mod = get_qaf_module_for_input_stream(out);
1080 if (!qaf_mod) {
1081 return 0;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301082 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301083
Harsh Bansal28718c52017-04-20 13:47:12 +05301084 //If QAF passthrough is active then block the get latency on module input streams.
1085 if (p_qaf->passthrough_out) {
1086 pthread_mutex_lock(&p_qaf->lock);
1087 //If get latency is called for the QAF passthrough stream then call the primary HAL api.
1088 if (p_qaf->passthrough_in == out) {
1089 latency = p_qaf->passthrough_out->stream.get_latency(
1090 (struct audio_stream_out *)p_qaf->passthrough_out);
1091 }
1092 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301093 } else {
Bharath Gopal01310bb2016-12-05 15:39:32 +05301094 if (is_offload_usecase(out->usecase)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301095 latency = COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301096 } else {
Harsh Bansal28718c52017-04-20 13:47:12 +05301097 uint32_t sample_rate = 0;
1098 latency = QAF_MODULE_PCM_INPUT_BUFFER_LATENCY; //Input latency
1099
1100 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD])
1101 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate;
1102 else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])
1103 sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate;
1104
1105 if (sample_rate) {
1106 latency += (get_pcm_output_buffer_size_samples(qaf_mod) * 1000) / out->sample_rate;
1107 }
1108 }
1109
1110 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
1111 if (is_offload_usecase(out->usecase)) {
1112 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1113 QAF_COMPRESS_OFFLOAD_PROCESSING_LATENCY;
1114 } else {
1115 latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) +
1116 QAF_PCM_OFFLOAD_PROCESSING_LATENCY;
1117 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05301118 }
1119 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301120
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301121 DEBUG_MSG_VV("Latency %d", latency);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301122 return latency;
1123}
1124
Harsh Bansal28718c52017-04-20 13:47:12 +05301125static bool check_and_get_compressed_device_format(int device, int *format)
1126{
1127 switch (device) {
1128 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DD):
1129 *format = AUDIO_FORMAT_AC3;
1130 return true;
1131 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DDP):
1132 *format = AUDIO_FORMAT_E_AC3;
1133 return true;
1134 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_FORMAT_DTS):
1135 *format = AUDIO_FORMAT_DTS;
1136 return true;
1137 case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_FORMAT_DTS_HD):
1138 *format = AUDIO_FORMAT_DTS_HD;
1139 return true;
1140 default:
1141 return false;
1142 }
1143}
1144
1145/* Call back function for mm module. */
1146static void notify_event_callback(audio_session_handle_t session_handle /*__unused*/,
1147 void *prv_data,
1148 void *buf,
1149 audio_event_id_t event_id,
1150 int size,
1151 int device) //TODO: add media format as well.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301152{
1153
Harsh Bansal28718c52017-04-20 13:47:12 +05301154 /*
1155 For SPKR:
1156 1. Open pcm device if device_id passed to it SPKR and write the data to
1157 pcm device
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301158
Harsh Bansal28718c52017-04-20 13:47:12 +05301159 For HDMI
1160 1.Open compress device for HDMI(PCM or AC3) based on current hdmi o/p format and write
1161 data to the HDMI device.
1162 */
1163 int ret, i;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301164 audio_output_flags_t flags;
Harsh Bansal28718c52017-04-20 13:47:12 +05301165 struct qaf_module* qaf_mod = (struct qaf_module*)prv_data;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301166 struct audio_stream_out *bt_stream = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301167 int format;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301168
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301169 DEBUG_MSG_VV("Device 0x%X, Event = 0x%X, Bytes to write %d", device, event_id, size);
Harsh Bansal28718c52017-04-20 13:47:12 +05301170 pthread_mutex_lock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301171
1172 if (event_id == AUDIO_DATA_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301173 if (p_qaf->passthrough_out != NULL) {
1174 //If QAF passthrough is active then all the module output will be dropped.
1175 pthread_mutex_unlock(&p_qaf->lock);
1176 DEBUG_MSG("QAF-PSTH is active, DROPPING DATA!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301177 return;
Harsh Bansal28718c52017-04-20 13:47:12 +05301178 }
1179
1180 if (check_and_get_compressed_device_format(device, &format)) {
1181 /*
1182 * CASE 1: Transcoded output of mm module.
1183 * If HDMI is not connected then drop the data.
1184 * Only one HDMI output can be supported from all the mm modules of QAF.
1185 * Multi-Channel PCM HDMI output streams will be closed from all the mm modules.
1186 * If transcoded output of other module is already enabled then this data will be dropped.
1187 */
1188
1189 if (!p_qaf->hdmi_connect) {
1190 DEBUG_MSG("HDMI not connected, DROPPING DATA!");
1191 pthread_mutex_unlock(&p_qaf->lock);
1192 return;
1193 }
1194
1195 //Closing all the PCM HDMI output stream from QAF.
1196 close_all_pcm_hdmi_output();
1197
1198 if (!p_qaf->passthrough_enabled
1199 && !(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH])) {
1200
1201 struct audio_config config;
1202 audio_devices_t devices;
1203
1204 //TODO: need to update the actual sample rate.
1205 config.sample_rate = config.offload_info.sample_rate =
1206 QAF_OUTPUT_SAMPLING_RATE;
1207 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1208 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
1209 config.format = config.offload_info.format = format;
1210 //TODO: need to update the actual bit width.
1211 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
1212 //TODO: What is the relevance of num of channels.
1213 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1214
1215 flags = (AUDIO_OUTPUT_FLAG_NON_BLOCKING
1216 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD
1217 | AUDIO_OUTPUT_FLAG_DIRECT);
1218 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1219
1220 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1221 QAF_DEFAULT_COMPR_PASSTHROUGH_HANDLE,
1222 devices,
1223 flags,
1224 &config,
1225 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]),
1226 NULL);
1227 if (ret < 0) {
1228 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1229 pthread_mutex_unlock(&p_qaf->lock);
1230 return;
1231 }
1232
1233 if (format & AUDIO_COMPRESSED_OUT_DDP) {
1234 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragment_size =
1235 COMPRESS_PASSTHROUGH_DDP_FRAGMENT_SIZE;
1236 }
1237 qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragments =
1238 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1239
1240 p_qaf->passthrough_enabled = true;
1241 }
1242
1243 if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) {
1244 ret = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->stream.write(
1245 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH],
1246 buf,
1247 size);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301248 }
1249 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301250 else if ((device & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1251 && (p_qaf->hdmi_connect)
1252 && (p_qaf->hdmi_sink_channels > 2)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301253
Harsh Bansal28718c52017-04-20 13:47:12 +05301254 /* CASE 2: Multi-Channel PCM output to HDMI.
1255 * If any other HDMI output is already enabled then this has to be dropped.
1256 */
1257 bool create_mch_out_stream = false;
1258
1259 if (p_qaf->passthrough_enabled) {
1260 //Closing all the multi-Channel PCM HDMI output stream from QAF.
1261 close_all_pcm_hdmi_output();
1262
1263 //If passthrough is active then pcm hdmi output has to be dropped.
1264 pthread_mutex_unlock(&p_qaf->lock);
1265 DEBUG_MSG("Compressed passthrough enabled, DROPPING DATA!");
1266 return;
1267 }
1268
1269 if (!p_qaf->mch_pcm_hdmi_enabled && !(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301270 struct audio_config config;
1271 audio_devices_t devices;
1272
Harsh Bansal28718c52017-04-20 13:47:12 +05301273 //TODO: need to update the actual sample rate.
Varun B34da7a42017-02-13 16:16:53 +05301274 config.sample_rate = config.offload_info.sample_rate =
Harsh Bansal28718c52017-04-20 13:47:12 +05301275 QAF_OUTPUT_SAMPLING_RATE;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301276 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1277 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
Harsh Bansal28718c52017-04-20 13:47:12 +05301278 config.offload_info.format = config.format = AUDIO_FORMAT_PCM_16_BIT;
1279 //TODO: need to update the actual bit width.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301280 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301281
Harsh Bansal28718c52017-04-20 13:47:12 +05301282 if (p_qaf->hdmi_sink_channels == 8) {
Varun B34da7a42017-02-13 16:16:53 +05301283 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301284 AUDIO_CHANNEL_OUT_7POINT1;
1285 } else if (p_qaf->hdmi_sink_channels == 6) {
Varun B34da7a42017-02-13 16:16:53 +05301286 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301287 AUDIO_CHANNEL_OUT_5POINT1;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301288 } else {
Varun B34da7a42017-02-13 16:16:53 +05301289 config.offload_info.channel_mask = config.channel_mask =
Harsh Bansal28718c52017-04-20 13:47:12 +05301290 AUDIO_CHANNEL_OUT_STEREO;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301291 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301292
Harsh Bansal28718c52017-04-20 13:47:12 +05301293 devices = AUDIO_DEVICE_OUT_AUX_DIGITAL;
1294 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
1295
1296 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1297 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1298 devices,
1299 flags,
1300 &config,
1301 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]),
1302 NULL);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301303 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301304 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1305 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301306 return;
1307 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001308
Harsh Bansal28718c52017-04-20 13:47:12 +05301309 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragments =
1310 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1311 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragment_size =
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301312 qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info);
Harsh Bansal28718c52017-04-20 13:47:12 +05301313
1314 p_qaf->mch_pcm_hdmi_enabled = true;
1315
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301316 if ((qaf_mod->stream_in[QAF_IN_MAIN]
1317 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) ||
1318 (qaf_mod->stream_in[QAF_IN_MAIN_2]
1319 && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301320
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301321 if (qaf_mod->stream_in[QAF_IN_MAIN]) {
1322 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
Harsh Bansal28718c52017-04-20 13:47:12 +05301323 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1324 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1325 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301326 }
1327 if (qaf_mod->stream_in[QAF_IN_MAIN_2]) {
1328 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1329 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1330 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback,
1331 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie);
1332 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301333 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1334 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1335
1336 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback(
1337 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1338 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1339 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1340 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001341
1342 int index = -1;
1343 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301344 index = (int) QAF_IN_MAIN;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301345 else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid)
1346 index = (int) QAF_IN_MAIN_2;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001347 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301348 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001349
Harsh Bansal28718c52017-04-20 13:47:12 +05301350 if (index >= 0) {
1351 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->standby)
1352 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1353 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], NULL, 0);
1354
1355 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001356 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301357 qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1358 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1359 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1360 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001361
1362 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301363 }
1364
Harsh Bansal28718c52017-04-20 13:47:12 +05301365 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) {
1366 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write(
1367 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH],
1368 buf,
1369 size);
Varun B34da7a42017-02-13 16:16:53 +05301370 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301371 }
1372 else {
1373 /* CASE 3: PCM output.
1374 */
Bharath Gopal01310bb2016-12-05 15:39:32 +05301375 bt_stream = audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl);
1376 if (bt_stream != NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301377 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1378 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1379 (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD]));
1380 qaf_mod->stream_out[QAF_OUT_OFFLOAD] = NULL;
Bharath Gopal01310bb2016-12-05 15:39:32 +05301381 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301382
Harsh Bansal28718c52017-04-20 13:47:12 +05301383 audio_extn_bt_hal_out_write(p_qaf->bt_hdl, buf, size);
1384 } else if (NULL == qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301385 struct audio_config config;
1386 audio_devices_t devices;
1387
Harsh Bansal28718c52017-04-20 13:47:12 +05301388 //TODO: need to update the actual sample rate.
Varun B34da7a42017-02-13 16:16:53 +05301389 config.sample_rate = config.offload_info.sample_rate =
Harsh Bansal28718c52017-04-20 13:47:12 +05301390 QAF_OUTPUT_SAMPLING_RATE;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301391 config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
1392 config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
Harsh Bansal28718c52017-04-20 13:47:12 +05301393 config.offload_info.format = config.format = AUDIO_FORMAT_PCM_16_BIT;
1394 //TODO: need to update the actual bit width.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301395 config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Harsh Bansal28718c52017-04-20 13:47:12 +05301396 //TODO: need to update the actual num channels.
1397 config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301398
Harsh Bansal28718c52017-04-20 13:47:12 +05301399 if (qaf_mod->stream_in[QAF_IN_MAIN])
1400 devices = qaf_mod->stream_in[QAF_IN_MAIN]->devices;
1401 else
1402 devices = qaf_mod->stream_in[QAF_IN_PCM]->devices;
1403
1404 //If multi channel pcm or passthrough is already enabled then remove the hdmi flag from device.
1405 if (p_qaf->mch_pcm_hdmi_enabled || p_qaf->passthrough_enabled) {
1406 if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1407 devices ^= AUDIO_DEVICE_OUT_AUX_DIGITAL;
1408 }
1409 if (devices == 0) {
1410 devices = device;
1411 }
1412
1413 flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001414
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301415 /* TODO:: Need to Propagate errors to framework */
Harsh Bansal28718c52017-04-20 13:47:12 +05301416 ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev,
1417 QAF_DEFAULT_COMPR_AUDIO_HANDLE,
1418 devices,
1419 flags,
1420 &config,
1421 (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD]),
1422 NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301423 if (ret < 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301424 ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret);
1425 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301426 return;
1427 }
Deepak Agarwal1e42b852017-02-11 17:57:04 +05301428
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301429 if ((qaf_mod->stream_in[QAF_IN_MAIN]
1430 && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) ||
1431 (qaf_mod->stream_in[QAF_IN_MAIN_2]
1432 && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) {
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001433
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301434 if (qaf_mod->stream_in[QAF_IN_MAIN]) {
1435 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
Harsh Bansal28718c52017-04-20 13:47:12 +05301436 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1437 qaf_mod->stream_in[QAF_IN_MAIN]->client_callback,
1438 qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301439 }
1440 if (qaf_mod->stream_in[QAF_IN_MAIN_2]) {
1441 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1442 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1443 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback,
1444 qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie);
1445 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301446 } else if (qaf_mod->stream_in[QAF_IN_PCM]
1447 && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) {
1448
1449 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback(
1450 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1451 qaf_mod->stream_in[QAF_IN_PCM]->client_callback,
1452 qaf_mod->stream_in[QAF_IN_PCM]->client_cookie);
1453 }
1454
1455 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragments =
1456 COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1457 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragment_size =
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301458 qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info);
Harsh Bansal28718c52017-04-20 13:47:12 +05301459
1460 if (qaf_mod->is_vol_set) {
1461 DEBUG_MSG("Setting Volume Left[%f], Right[%f]", qaf_mod->vol_left, qaf_mod->vol_right);
1462 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume(
1463 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1464 qaf_mod->vol_left,
1465 qaf_mod->vol_right);
1466 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001467
1468 int index = -1;
1469 if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301470 index = (int) QAF_IN_MAIN;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301471 else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid)
1472 index = (int) QAF_IN_MAIN_2;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001473 else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid)
Harsh Bansal28718c52017-04-20 13:47:12 +05301474 index = (int) QAF_IN_PCM;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001475 if (index >= 0) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301476 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]->standby) {
1477 qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1478 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], NULL, 0);
1479 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001480
Harsh Bansal28718c52017-04-20 13:47:12 +05301481 lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001482 ret = audio_extn_out_set_param_data(
Harsh Bansal28718c52017-04-20 13:47:12 +05301483 qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1484 AUDIO_EXTN_PARAM_ADSP_STREAM_CMD,
1485 (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params);
1486 unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301487 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301488 }
1489
1490 /*
1491 * TODO:: Since this is mixed data,
1492 * need to identify to which stream the error should be sent
1493 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301494 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
1495 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write(
1496 (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD],
1497 buf,
1498 size);
Varun B34da7a42017-02-13 16:16:53 +05301499 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301500 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301501 DEBUG_MSG_VV("Bytes written = %d", ret);
Harsh Bansal28718c52017-04-20 13:47:12 +05301502 }
1503 else if (event_id == AUDIO_EOS_MAIN_DD_DDP_EVENT
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301504 || event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT
Varun B34da7a42017-02-13 16:16:53 +05301505 || event_id == AUDIO_EOS_MAIN_AAC_EVENT
1506 || event_id == AUDIO_EOS_MAIN_AC4_EVENT
1507 || event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301508 struct stream_out *out = qaf_mod->stream_in[QAF_IN_MAIN];
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301509 struct stream_out *out_main2 = qaf_mod->stream_in[QAF_IN_MAIN_2];
Harsh Bansal28718c52017-04-20 13:47:12 +05301510 struct stream_out *out_assoc = qaf_mod->stream_in[QAF_IN_ASSOC];
1511 bool *main_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301512 bool *main2_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN_2];
Harsh Bansal28718c52017-04-20 13:47:12 +05301513 bool *assoc_drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
Varun B34da7a42017-02-13 16:16:53 +05301514
1515 /**
1516 * TODO:: Only DD/DDP Associate Eos is handled, need to add support
1517 * for other formats.
1518 */
Harsh Bansal28718c52017-04-20 13:47:12 +05301519 if (event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT
1520 && (out_assoc != NULL)
1521 && (*assoc_drain_received)) {
1522
Varun B34da7a42017-02-13 16:16:53 +05301523 lock_output_stream(out_assoc);
Harsh Bansal28718c52017-04-20 13:47:12 +05301524 out_assoc->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_assoc->client_cookie);
1525 *assoc_drain_received = false;
1526 unlock_output_stream(out_assoc);
1527 DEBUG_MSG("sent associated DRAIN_READY");
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301528 } else if (event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT
1529 && (out_main2 != NULL)
1530 && (*main2_drain_received)) {
1531
1532 lock_output_stream(out_main2);
1533 out_main2->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_main2->client_cookie);
1534 *main2_drain_received = false;
1535 unlock_output_stream(out_main2);
1536 DEBUG_MSG("sent main2 DRAIN_READY");
Harsh Bansal28718c52017-04-20 13:47:12 +05301537 } else if ((out != NULL) && (*main_drain_received)) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301538 lock_output_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001539 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
Harsh Bansal28718c52017-04-20 13:47:12 +05301540 *main_drain_received = false;
1541 unlock_output_stream(out);
1542 DEBUG_MSG("sent main DRAIN_READY");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301543 }
1544 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301545#if 0
1546 else if (event_id == AUDIO_MAIN_EOS_EVENT || event_id == AUDIO_ASSOC_EOS_EVENT) { //TODO: For DTS
1547 struct stream_out *out = NULL;
1548 bool *drain_received = NULL;
1549
1550 if (event_id == AUDIO_MAIN_EOS_EVENT) {
1551 out = qaf_mod->stream_in[QAF_IN_MAIN];
1552 drain_received = &qaf_mod->drain_received[QAF_IN_MAIN];
1553 } else {
1554 out = qaf_mod->stream_in[QAF_IN_ASSOC];
1555 drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC];
1556 }
1557
1558 if ((out != NULL) && (*drain_received)) {
1559 lock_output_stream(out);
1560 out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie);
1561 *drain_received = false;
1562 unlock_output_stream(out);
1563 DEBUG_MSG("sent DRAIN_READY");
1564 }
1565 }
1566#endif
1567
1568 pthread_mutex_unlock(&p_qaf->lock);
1569 return;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301570}
1571
Harsh Bansal28718c52017-04-20 13:47:12 +05301572/* Close the mm module session. */
1573static int qaf_session_close(struct qaf_module* qaf_mod)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301574{
Harsh Bansal28718c52017-04-20 13:47:12 +05301575 int j;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301576
Harsh Bansal28718c52017-04-20 13:47:12 +05301577 //Check if all streams are closed or not.
1578 for (j = 0; j < MAX_QAF_MODULE_IN; j++) {
1579 if (qaf_mod->stream_in[j] != NULL) {
1580 break;
1581 }
1582 }
1583 if (j != MAX_QAF_MODULE_IN) {
1584 return 0; //Some stream is already active, Can not close session.
1585 }
1586
1587 if (qaf_mod->session_handle != NULL && qaf_mod->qaf_audio_session_close) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301588 qaf_mod->qaf_audio_session_close(qaf_mod->session_handle);
1589 qaf_mod->session_handle = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301590 qaf_mod->is_vol_set = false;
1591 memset(qaf_mod->drain_received, 0, sizeof(qaf_mod->drain_received));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301592 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301593
1594 for (j = 0; j < MAX_QAF_MODULE_OUT; j++) {
1595 if (qaf_mod->stream_out[j]) {
1596 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
1597 (struct audio_stream_out *)(qaf_mod->stream_out[j]));
1598 qaf_mod->stream_out[j] = NULL;
1599 }
1600 }
1601
1602 DEBUG_MSG("Session Closed.");
1603
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301604 return 0;
1605}
1606
Harsh Bansal28718c52017-04-20 13:47:12 +05301607/* Close the stream of QAF module. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301608static int qaf_stream_close(struct stream_out *out)
1609{
Harsh Bansal28718c52017-04-20 13:47:12 +05301610 int ret = -EINVAL;
1611 struct qaf_module *qaf_mod = NULL;
1612 int index = -1;
1613 DEBUG_MSG("Flag [0x%x], Stream handle [%p]", out->flags, out->qaf_stream_handle);
1614
1615 qaf_mod = get_qaf_module_for_input_stream(out);
1616 index = get_input_stream_index(out);
1617
1618 if (!qaf_mod || !qaf_mod->qaf_audio_stream_close || index < 0) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301619 return -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301620 }
1621
1622 qaf_mod->stream_in[index] = NULL;
1623 memset(&qaf_mod->adsp_hdlr_config[index], 0, sizeof(struct qaf_adsp_hdlr_config_state));
1624
1625 lock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301626 if (out->qaf_stream_handle) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301627 ret = qaf_mod->qaf_audio_stream_close(out->qaf_stream_handle);
1628 out->qaf_stream_handle = NULL;
1629 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301630 unlock_output_stream(out);
1631
1632 //If all streams are closed then close the session.
1633 qaf_session_close(qaf_mod);
1634
1635 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301636 return ret;
1637}
1638
Harsh Bansal28718c52017-04-20 13:47:12 +05301639/* Open a MM module session with QAF. */
1640static int audio_extn_qaf_session_open(mm_module_type mod_type)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301641{
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301642 ALOGV("%s %d", __func__, __LINE__);
Harsh Bansal28718c52017-04-20 13:47:12 +05301643 unsigned char* license_data = NULL;
1644 device_license_config_t lic_config = {0};
1645 int ret = -ENOSYS, size = 0;
1646 char value[PROPERTY_VALUE_MAX] = {0};
1647 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301648
Harsh Bansal28718c52017-04-20 13:47:12 +05301649 if (mod_type >= MAX_MM_MODULE_TYPE || !(p_qaf->qaf_mod[mod_type].qaf_audio_session_open))
1650 return -ENOTSUP; //Not supported by QAF module.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301651
Harsh Bansal28718c52017-04-20 13:47:12 +05301652 pthread_mutex_lock(&p_qaf->lock);
1653
1654 qaf_mod = &(p_qaf->qaf_mod[mod_type]);
1655
1656 //If session is already opened then return.
1657 if (qaf_mod->session_handle) {
1658 DEBUG_MSG("Session is already opened.");
1659 pthread_mutex_unlock(&p_qaf->lock);
1660 return 0;
1661 }
1662
1663 if (mod_type == MS12) {
1664 //Getting the license
1665 license_data = platform_get_license((struct audio_hw_device *)(p_qaf->adev->platform),
1666 &size);
1667 if (!license_data) {
1668 ERROR_MSG("License data is not present.");
1669 pthread_mutex_unlock(&p_qaf->lock);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301670 return -EINVAL;
1671 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301672
1673 lic_config.p_license = (unsigned char*)calloc(1, size);
1674 if (lic_config.p_license == NULL) {
1675 ERROR_MSG("Out of Memory");
1676 ret = -ENOMEM;
1677 goto exit;
1678 }
1679
1680 lic_config.l_size = size;
1681 memcpy(lic_config.p_license, license_data, size);
1682
1683 if (property_get("audio.qaf.manufacturer", value, "") && atoi(value)) {
1684 lic_config.manufacturer_id = (unsigned long)atoi(value);
1685 } else {
1686 ERROR_MSG("audio.qaf.manufacturer id is not set");
1687 ret = -EINVAL;
1688 goto exit;
1689 }
1690 }
1691
1692 ret = qaf_mod->qaf_audio_session_open(&qaf_mod->session_handle,
1693 (void *)(qaf_mod),
1694 (void *)&lic_config);
1695 if (ret < 0) {
1696 ERROR_MSG("Error in session open %d", ret);
1697 goto exit;
1698 }
1699
1700 if (qaf_mod->session_handle == NULL) {
1701 ERROR_MSG("Session handle is NULL.");
1702 ret = -ENOMEM;
1703 goto exit;
1704 }
1705
1706 if (qaf_mod->qaf_register_event_callback)
1707 qaf_mod->qaf_register_event_callback(qaf_mod->session_handle,
1708 qaf_mod,
1709 &notify_event_callback,
1710 AUDIO_DATA_EVENT);
1711
1712 set_hdmi_configuration_to_module();
1713
1714exit:
1715 if (license_data != NULL) {
1716 free(license_data);
1717 license_data = NULL;
1718 }
1719 if (lic_config.p_license != NULL) {
1720 free(lic_config.p_license);
1721 lic_config.p_license = NULL;
1722 }
1723
1724 pthread_mutex_unlock(&p_qaf->lock);
1725 return ret;
1726}
1727
1728/* opens a stream in QAF module. */
1729static int qaf_stream_open(struct stream_out *out,
1730 struct audio_config *config,
1731 audio_output_flags_t flags,
1732 audio_devices_t devices)
1733{
1734 int status = -EINVAL;
1735 mm_module_type mmtype = get_mm_module_for_format(config->format);
1736 struct qaf_module* qaf_mod = NULL;
1737 DEBUG_MSG("Flags 0x%x, Device 0x%x", flags, devices);
1738
1739 if (mmtype >= MAX_MM_MODULE_TYPE
1740 || p_qaf->qaf_mod[mmtype].qaf_audio_session_open == NULL
1741 || p_qaf->qaf_mod[mmtype].qaf_audio_stream_open == NULL) {
1742 ERROR_MSG("Unsupported Stream");
1743 return -ENOTSUP;
1744 }
1745
1746 //Open the module session, if not opened already.
1747 status = audio_extn_qaf_session_open(mmtype);
1748 qaf_mod = &(p_qaf->qaf_mod[mmtype]);
1749
1750 if ((status != 0) || (qaf_mod->session_handle == NULL)) {
1751 ERROR_MSG("Failed to open session.");
1752 return status;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301753 }
1754
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301755 audio_stream_config_t input_config;
1756 input_config.sample_rate = config->sample_rate;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301757 input_config.channels = popcount(config->channel_mask);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301758 input_config.format = config->format;
1759
Harsh Bansal28718c52017-04-20 13:47:12 +05301760 if (input_config.format != AUDIO_FORMAT_PCM_16_BIT) {
1761 input_config.format &= AUDIO_FORMAT_MAIN_MASK;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301762 }
1763
Harsh Bansal28718c52017-04-20 13:47:12 +05301764 DEBUG_MSG("stream_open sample_rate(%d) channels(%d) devices(%#x) flags(%#x) format(%#x)",
1765 input_config.sample_rate, input_config.channels, devices, flags, input_config.format);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301766
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301767 if (input_config.format == AUDIO_FORMAT_PCM_16_BIT) {
Harsh Bansal28718c52017-04-20 13:47:12 +05301768 //If PCM stream is already opened then fail this stream open.
1769 if (qaf_mod->stream_in[QAF_IN_PCM]) {
1770 ERROR_MSG("PCM input is already active.");
1771 return -ENOTSUP;
1772 }
1773
1774 //TODO: Flag can be system tone or external associated PCM.
1775 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle,
1776 &out->qaf_stream_handle,
1777 input_config,
1778 devices,
1779 AUDIO_STREAM_SYSTEM_TONE);
1780 qaf_mod->stream_in[QAF_IN_PCM] = out;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301781 } else if ((flags & AUDIO_OUTPUT_FLAG_MAIN) && (flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
1782 if (is_main_active(qaf_mod) || is_dual_main_active(qaf_mod)) {
1783 ERROR_MSG("Dual Main or Main already active. So, Cannot open main and associated stream");
1784 return -EINVAL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301785 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301786 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN);
1787 if (status == 0) {
1788 DEBUG_MSG("Open stream for Input with both Main and Associated stream contents with flag(%x) and stream_handle(%p)", flags, out->qaf_stream_handle);
1789 qaf_mod->stream_in[QAF_IN_MAIN] = out;
1790 } else {
1791 ERROR_MSG("Stream Open FAILED !!!");
1792 }
1793 }
Naresh Tanniru908d9a02017-05-17 14:12:48 +05301794 } else if ((flags & AUDIO_OUTPUT_FLAG_MAIN) || ((!(flags & AUDIO_OUTPUT_FLAG_MAIN)) && (!(flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)))) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301795 /* Assume Main if no flag is set */
1796 if (is_dual_main_active(qaf_mod)) {
1797 ERROR_MSG("Dual Main already active. So, Cannot open main stream");
1798 return -EINVAL;
1799 } else if (is_main_active(qaf_mod) && qaf_mod->stream_in[QAF_IN_ASSOC]) {
1800 ERROR_MSG("Main and Associated already active. So, Cannot open main stream");
1801 return -EINVAL;
1802 } else if (is_main_active(qaf_mod) && (mmtype != MS12)) {
1803 ERROR_MSG("Main already active and Not an MS12 format. So, Cannot open another main stream");
1804 return -EINVAL;
1805 } else {
1806 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN);
1807 if (status == 0) {
1808 DEBUG_MSG("Open stream for Input with only Main flag(%x) stream_handle(%p)", flags, out->qaf_stream_handle);
1809 if(qaf_mod->stream_in[QAF_IN_MAIN]) {
1810 qaf_mod->stream_in[QAF_IN_MAIN_2] = out;
1811 } else {
1812 qaf_mod->stream_in[QAF_IN_MAIN] = out;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301813 }
1814 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301815 ERROR_MSG("Stream Open FAILED !!!");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301816 }
1817 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301818 } else if ((flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
1819 if (is_dual_main_active(qaf_mod)) {
1820 ERROR_MSG("Dual Main already active. So, Cannot open associated stream");
1821 return -EINVAL;
1822 } else if (!is_main_active(qaf_mod)) {
1823 ERROR_MSG("Main not active. So, Cannot open associated stream");
1824 return -EINVAL;
1825 } else if (qaf_mod->stream_in[QAF_IN_ASSOC]) {
1826 ERROR_MSG("Associated already active. So, Cannot open associated stream");
1827 return -EINVAL;
1828 }
1829 status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_ASSOCIATED);
1830 if (status == 0) {
1831 DEBUG_MSG("Open stream for Input with only Associated flag(%x) stream handle(%p)", flags, out->qaf_stream_handle);
1832 qaf_mod->stream_in[QAF_IN_ASSOC] = out;
1833 } else {
1834 ERROR_MSG("Stream Open FAILED !!!");
1835 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301836 }
1837
Harsh Bansal28718c52017-04-20 13:47:12 +05301838 if (status != 0) {
1839 //If no stream is active then close the session.
1840 qaf_session_close(qaf_mod);
1841 return status;
1842 }
1843
1844 //If Device is HDMI, QAF passthrough is enabled and there is no previous QAF passthrough input stream.
1845 if ((!p_qaf->passthrough_in)
1846 && (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1847 && audio_extn_qaf_passthrough_enabled(out)) {
1848 //Assign the QAF passthrough input stream.
1849 p_qaf->passthrough_in = out;
1850
1851 //If HDMI is connected and format is supported by HDMI then create QAF passthrough output stream.
1852 if (p_qaf->hdmi_connect
1853 && platform_is_edid_supported_format(p_qaf->adev->platform, out->format)) {
1854 status = create_qaf_passthrough_stream();
1855 if (status < 0) {
1856 qaf_stream_close(out);
1857 ERROR_MSG("QAF passthrough stream creation failed with error %d", status);
1858 return status;
1859 }
1860 }
1861 /*Else: since QAF passthrough input stream is already initialized,
1862 * when hdmi is connected
1863 * then qaf passthrough output stream will be created.
1864 */
1865 }
1866
1867 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301868 return status;
1869}
1870
Harsh Bansal28718c52017-04-20 13:47:12 +05301871/* Resume a QAF stream. */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301872static int qaf_out_resume(struct audio_stream_out* stream)
1873{
1874 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05301875 int status = 0;
1876 DEBUG_MSG("Output Stream %p", out);
1877
1878
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301879 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301880
1881 //If QAF passthrough is active then block the resume on module input streams.
1882 if (p_qaf->passthrough_out) {
1883 //If resume is received for the QAF passthrough stream then call the primary HAL api.
1884 pthread_mutex_lock(&p_qaf->lock);
1885 if (p_qaf->passthrough_in == out) {
1886 status = p_qaf->passthrough_out->stream.resume(
1887 (struct audio_stream_out*)p_qaf->passthrough_out);
1888 if (!status) out->offload_state = OFFLOAD_STATE_PLAYING;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301889 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301890 pthread_mutex_unlock(&p_qaf->lock);
1891 } else {
1892 //Flush the module input stream.
1893 status = qaf_stream_start(out);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301894 }
1895
Harsh Bansal28718c52017-04-20 13:47:12 +05301896 unlock_output_stream(out);
1897
1898 DEBUG_MSG();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05301899 return status;
1900}
1901
Harsh Bansal28718c52017-04-20 13:47:12 +05301902/* Offload thread for QAF output streams. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301903static void *qaf_offload_thread_loop(void *context)
1904{
Harsh Bansal28718c52017-04-20 13:47:12 +05301905 struct stream_out *out = (struct stream_out *)context;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301906 struct listnode *item;
1907 int ret = 0;
1908 struct str_parms *parms = NULL;
1909 int value = 0;
1910 char* kvpairs = NULL;
Harsh Bansal28718c52017-04-20 13:47:12 +05301911 struct qaf_module *qaf_mod = NULL;
1912
1913 qaf_mod = get_qaf_module_for_input_stream(out);
1914
1915 if (!qaf_mod) return NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301916
1917 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1918 set_sched_policy(0, SP_FOREGROUND);
1919 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1920
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301921 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301922
1923 DEBUG_MSG();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301924 for (;;) {
1925 struct offload_cmd *cmd = NULL;
1926 stream_callback_event_t event;
1927 bool send_callback = false;
1928
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301929 DEBUG_MSG_VV("List Empty %d (1:TRUE, 0:FALSE)", list_empty(&out->qaf_offload_cmd_list));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301930 if (list_empty(&out->qaf_offload_cmd_list)) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301931 DEBUG_MSG_VV("SLEEPING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301932 pthread_cond_wait(&out->qaf_offload_cond, &out->lock);
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301933 DEBUG_MSG_VV("RUNNING");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301934 continue;
1935 }
1936
1937 item = list_head(&out->qaf_offload_cmd_list);
1938 cmd = node_to_item(item, struct offload_cmd, node);
1939 list_remove(item);
1940
1941 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1942 free(cmd);
1943 break;
1944 }
1945
Harsh Bansal28718c52017-04-20 13:47:12 +05301946 unlock_output_stream(out);
1947
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301948 send_callback = false;
Harsh Bansal28718c52017-04-20 13:47:12 +05301949 switch (cmd->cmd) {
1950 case OFFLOAD_CMD_WAIT_FOR_BUFFER: {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301951 DEBUG_MSG_VV("wait for buffer availability");
Harsh Bansal28718c52017-04-20 13:47:12 +05301952
1953 while (1) {
1954 kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle,
1955 "buf_available");
1956 if (kvpairs) {
1957 parms = str_parms_create_str(kvpairs);
1958 ret = str_parms_get_int(parms, "buf_available", &value);
1959 if (ret >= 0) {
1960 if (value >= (int)out->compr_config.fragment_size) {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301961 DEBUG_MSG_VV("buffer available");
Harsh Bansal28718c52017-04-20 13:47:12 +05301962 str_parms_destroy(parms);
1963 parms = NULL;
1964 break;
1965 } else {
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05301966 DEBUG_MSG_VV("sleep");
Harsh Bansal28718c52017-04-20 13:47:12 +05301967 str_parms_destroy(parms);
1968 parms = NULL;
1969 usleep(10000);
1970 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301971 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301972 free(kvpairs);
1973 kvpairs = NULL;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301974 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301975 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301976 send_callback = true;
1977 event = STREAM_CBK_EVENT_WRITE_READY;
1978 break;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301979 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301980 default:
1981 DEBUG_MSG("unknown command received: %d", cmd->cmd);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301982 break;
1983 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301984
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301985 lock_output_stream(out);
Harsh Bansal28718c52017-04-20 13:47:12 +05301986
Ben Rombergerd771a7c2017-02-22 18:05:17 -08001987 if (send_callback && out->client_callback) {
1988 out->client_callback(event, NULL, out->client_cookie);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301989 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301990
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301991 free(cmd);
1992 }
1993
1994 while (!list_empty(&out->qaf_offload_cmd_list)) {
1995 item = list_head(&out->qaf_offload_cmd_list);
1996 list_remove(item);
Harsh Bansal28718c52017-04-20 13:47:12 +05301997 free (node_to_item( item, struct offload_cmd, node));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05301998 }
Harsh Bansal28718c52017-04-20 13:47:12 +05301999 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302000
2001 return NULL;
2002}
2003
Harsh Bansal28718c52017-04-20 13:47:12 +05302004/* Create the offload callback thread for QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302005static int qaf_create_offload_callback_thread(struct stream_out *out)
2006{
Harsh Bansal28718c52017-04-20 13:47:12 +05302007 DEBUG_MSG("Output Stream %p", out);
2008 lock_output_stream(out);
2009 pthread_cond_init(&out->qaf_offload_cond, (const pthread_condattr_t *)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302010 list_init(&out->qaf_offload_cmd_list);
Harsh Bansal28718c52017-04-20 13:47:12 +05302011 pthread_create(&out->qaf_offload_thread,
2012 (const pthread_attr_t *)NULL,
2013 qaf_offload_thread_loop,
2014 out);
2015 unlock_output_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302016 return 0;
2017}
2018
Harsh Bansal28718c52017-04-20 13:47:12 +05302019/* Destroy the offload callback thread of QAF output stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302020static int qaf_destroy_offload_callback_thread(struct stream_out *out)
2021{
Harsh Bansal28718c52017-04-20 13:47:12 +05302022 DEBUG_MSG("Output Stream %p", out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302023 qaf_send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302024
Harsh Bansal28718c52017-04-20 13:47:12 +05302025 pthread_join(out->qaf_offload_thread, (void **)NULL);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302026 pthread_cond_destroy(&out->qaf_offload_cond);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302027 return 0;
2028}
2029
Harsh Bansal28718c52017-04-20 13:47:12 +05302030/* Sets the stream set parameters (device routing information). */
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302031static int qaf_out_set_parameters(struct audio_stream *stream, const char *kvpairs)
2032{
2033 struct str_parms *parms, *new_parms;
2034 char value[32];
2035 char *new_kv_pairs;
2036 int val = 0;
2037 struct stream_out *out = (struct stream_out *)stream;
2038 int ret = 0;
2039 int err = 0;
Harsh Bansal28718c52017-04-20 13:47:12 +05302040 struct qaf_module *qaf_mod = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302041
Harsh Bansal28718c52017-04-20 13:47:12 +05302042 DEBUG_MSG("usecase(%d: %s) kvpairs: %s", out->usecase, use_case_table[out->usecase], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302043
Harsh Bansal28718c52017-04-20 13:47:12 +05302044 parms = str_parms_create_str(kvpairs);
2045 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2046 if (err < 0)
2047 return err;
2048 val = atoi(value);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302049
Harsh Bansal28718c52017-04-20 13:47:12 +05302050 qaf_mod = get_qaf_module_for_input_stream(out);
2051 if (!qaf_mod) return (-EINVAL);
2052
2053 //TODO: HDMI is connected but user doesn't want HDMI output, close both HDMI outputs.
2054
2055 /* Setting new device information to the mm module input streams.
2056 * This is needed if QAF module output streams are not created yet.
2057 */
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302058 out->devices = val;
Harsh Bansal28718c52017-04-20 13:47:12 +05302059
2060 if (val == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
2061 //If device is BT then open the BT stream if not already opened.
2062 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) == NULL
2063 && audio_extn_bt_hal_get_device(qaf_mod->bt_hdl) != NULL) {
2064 ret = audio_extn_bt_hal_open_output_stream(qaf_mod->bt_hdl,
2065 QAF_OUTPUT_SAMPLING_RATE,
2066 AUDIO_CHANNEL_OUT_STEREO,
2067 CODEC_BACKEND_DEFAULT_BIT_WIDTH);
2068 if (ret != 0) {
2069 ERROR_MSG("BT Output stream open failure!");
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302070 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302071 }
2072 } else if (val != 0) {
2073 //If device is not BT then close the BT stream if already opened.
2074 if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) {
2075 audio_extn_bt_hal_close_output_stream(qaf_mod->bt_hdl);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302076 }
2077 }
2078
Harsh Bansal28718c52017-04-20 13:47:12 +05302079 if (p_qaf->passthrough_in == out) { //Device routing is received for QAF passthrough stream.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302080
Harsh Bansal28718c52017-04-20 13:47:12 +05302081 if (!(val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) { //HDMI route is disabled.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302082
Harsh Bansal28718c52017-04-20 13:47:12 +05302083 //If QAF pasthrough output is enabled. Close it.
2084 close_qaf_passthrough_stream();
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302085
Harsh Bansal28718c52017-04-20 13:47:12 +05302086 //Send the routing information to mm module pcm output.
2087 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2088 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2089 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302090 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302091 //else: device info is updated in the input streams.
2092 } else { //HDMI route is enabled.
2093
2094 //create the QAf passthrough stream, if not created already.
2095 ret = create_qaf_passthrough_stream();
2096
2097 if (p_qaf->passthrough_out != NULL) { //If QAF passthrough out is enabled then send routing information.
2098 ret = p_qaf->passthrough_out->stream.common.set_parameters(
2099 (struct audio_stream *)p_qaf->passthrough_out, kvpairs);
2100 }
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302101 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302102 } else {
2103 //Send the routing information to mm module pcm output.
2104 if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) {
2105 ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters(
2106 (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs);
2107 }
2108 //else: device info is updated in the input streams.
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302109 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302110 str_parms_destroy(parms);
2111
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002112 return ret;
2113}
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302114
Harsh Bansal28718c52017-04-20 13:47:12 +05302115/* Checks if a stream is QAF stream or not. */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002116bool audio_extn_is_qaf_stream(struct stream_out *out)
2117{
Harsh Bansal28718c52017-04-20 13:47:12 +05302118 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002119
Harsh Bansal28718c52017-04-20 13:47:12 +05302120 if (qaf_mod) {
2121 return true;
2122 }
2123 return false;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002124}
2125
2126/* API to send playback stream specific config parameters */
2127int audio_extn_qaf_out_set_param_data(struct stream_out *out,
Harsh Bansal28718c52017-04-20 13:47:12 +05302128 audio_extn_param_id param_id,
2129 audio_extn_param_payload *payload)
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002130{
2131 int ret = -EINVAL;
Harsh Bansal28718c52017-04-20 13:47:12 +05302132 int index;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002133 struct stream_out *new_out = NULL;
2134 struct audio_adsp_event *adsp_event;
Harsh Bansal28718c52017-04-20 13:47:12 +05302135 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002136
Harsh Bansal28718c52017-04-20 13:47:12 +05302137 if (!out || !qaf_mod || !payload) {
2138 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002139 return ret;
2140 }
2141
2142 /* In qaf output render session may not be opened at this time.
Harsh Bansal28718c52017-04-20 13:47:12 +05302143 to handle it store adsp_hdlr param info so that it can be
2144 applied later after opening render session from ms12 callback
2145 */
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002146 if (param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302147 index = get_input_stream_index(out);
2148 if (index < 0) {
2149 ERROR_MSG("Invalid stream");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002150 return ret;
2151 }
2152 adsp_event = (struct audio_adsp_event *)payload;
2153
Harsh Bansal28718c52017-04-20 13:47:12 +05302154 if (payload->adsp_event_params.payload_length <= AUDIO_MAX_ADSP_STREAM_CMD_PAYLOAD_LEN) {
2155 pthread_mutex_lock(&p_qaf->lock);
2156 memcpy(qaf_mod->adsp_hdlr_config[index].event_payload,
2157 adsp_event->payload,
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002158 adsp_event->payload_length);
Harsh Bansal28718c52017-04-20 13:47:12 +05302159 qaf_mod->adsp_hdlr_config[index].event_params.payload =
2160 qaf_mod->adsp_hdlr_config[index].event_payload;
2161 qaf_mod->adsp_hdlr_config[index].event_params.payload_length =
2162 adsp_event->payload_length;
2163 qaf_mod->adsp_hdlr_config[index].adsp_hdlr_config_valid = true;
2164 pthread_mutex_unlock(&p_qaf->lock);
2165 } else {
2166 ERROR_MSG("Invalid adsp event length %d", adsp_event->payload_length);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002167 return ret;
2168 }
2169 ret = 0;
2170 }
2171
Harsh Bansal28718c52017-04-20 13:47:12 +05302172 /* apply param for all active out sessions */
2173 for (index = 0; index < MAX_QAF_MODULE_OUT; index++) {
2174 new_out = qaf_mod->stream_out[index];
2175 if (!new_out) continue;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002176
Harsh Bansal28718c52017-04-20 13:47:12 +05302177 /*ADSP event is not supported for passthrough*/
2178 if ((param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD)
2179 && !(new_out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) continue;
2180 if (new_out->standby)
2181 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
2182 lock_output_stream(new_out);
2183 ret = audio_extn_out_set_param_data(new_out, param_id, payload);
2184 if (ret)
2185 ERROR_MSG("audio_extn_out_set_param_data error %d", ret);
2186 unlock_output_stream(new_out);
2187 }
2188 return ret;
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002189}
2190
2191int audio_extn_qaf_out_get_param_data(struct stream_out *out,
2192 audio_extn_param_id param_id,
2193 audio_extn_param_payload *payload)
2194{
Harsh Bansal28718c52017-04-20 13:47:12 +05302195 int ret = -EINVAL, i;
2196 struct stream_out *new_out;
2197 struct audio_usecase *uc_info;
2198 struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002199
Harsh Bansal28718c52017-04-20 13:47:12 +05302200 if (!out || !qaf_mod || !payload) {
2201 ERROR_MSG("Invalid Param");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002202 return ret;
2203 }
2204
Harsh Bansal28718c52017-04-20 13:47:12 +05302205 if (!p_qaf->hdmi_connect) {
2206 ERROR_MSG("hdmi not connected");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002207 return ret;
2208 }
2209
2210 /* get session which is routed to hdmi*/
Harsh Bansal28718c52017-04-20 13:47:12 +05302211 if (p_qaf->passthrough_out)
2212 new_out = p_qaf->passthrough_out;
2213 else {
2214 for (i = 0; i < MAX_QAF_MODULE_OUT; i++) {
2215 if (qaf_mod->stream_out[i]) {
2216 new_out = qaf_mod->stream_out[i];
2217 break;
2218 }
2219 }
2220 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002221
2222 if (!new_out) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302223 ERROR_MSG("No stream active.");
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002224 return ret;
2225 }
2226
2227 if (new_out->standby)
Harsh Bansal28718c52017-04-20 13:47:12 +05302228 new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0);
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002229
2230 lock_output_stream(new_out);
2231 ret = audio_extn_out_get_param_data(new_out, param_id, payload);
Harsh Bansal28718c52017-04-20 13:47:12 +05302232 if (ret)
2233 ERROR_MSG("audio_extn_out_get_param_data error %d", ret);
2234 unlock_output_stream(new_out);
2235
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302236 return ret;
2237}
2238
Harsh Bansal28718c52017-04-20 13:47:12 +05302239/* To open a stream with QAF. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302240int audio_extn_qaf_open_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302241 audio_io_handle_t handle,
2242 audio_devices_t devices,
2243 audio_output_flags_t flags,
2244 struct audio_config *config,
2245 struct audio_stream_out **stream_out,
2246 const char *address)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302247{
2248 int ret = 0;
2249 struct stream_out *out;
2250
2251 ret = adev_open_output_stream(dev, handle, devices, flags, config, stream_out, address);
2252 if (*stream_out == NULL) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302253 ERROR_MSG("Stream open failed %d", ret);
Deepak Agarwal1e42b852017-02-11 17:57:04 +05302254 return ret;
2255 }
2256
Harsh Bansal28718c52017-04-20 13:47:12 +05302257 out = (struct stream_out *)*stream_out;
2258
2259 ret = qaf_stream_open(out, config, flags, devices);
2260 if (ret == -ENOTSUP) {
2261 //Stream not supported by QAF, Bypass QAF.
2262 return 0;
2263 } else if (ret < 0) {
2264 ERROR_MSG("Error opening QAF stream err[%d]!", ret);
2265 adev_close_output_stream(dev, *stream_out);
2266 return ret;
2267 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302268
2269 /* Override function pointers based on qaf definitions */
2270 out->stream.set_volume = qaf_out_set_volume;
2271 out->stream.pause = qaf_out_pause;
2272 out->stream.resume = qaf_out_resume;
2273 out->stream.drain = qaf_out_drain;
2274 out->stream.flush = qaf_out_flush;
2275
2276 out->stream.common.standby = qaf_out_standby;
2277 out->stream.common.set_parameters = qaf_out_set_parameters;
2278 out->stream.get_latency = qaf_out_get_latency;
2279 out->stream.write = qaf_out_write;
2280 out->stream.get_presentation_position = qaf_out_get_presentation_position;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302281 out->platform_latency = 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302282
Harsh Bansal28718c52017-04-20 13:47:12 +05302283 /*TODO: Need to handle this for DTS*/
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302284 if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) {
2285 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
Varun B34da7a42017-02-13 16:16:53 +05302286 out->config.period_size = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302287 out->config.period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT;
Varun B34da7a42017-02-13 16:16:53 +05302288 out->config.start_threshold = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
2289 out->config.avail_min = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4;
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302290 } else if(out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) {
2291 out->compr_config.fragment_size = qaf_get_pcm_offload_input_buffer_size(&(config->offload_info));
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302292 }
Satish Babu Patakokilaa8c136d2017-04-21 12:48:19 +05302293
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302294 *stream_out = &out->stream;
2295 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2296 qaf_create_offload_callback_thread(out);
2297 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302298
2299 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302300 return 0;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302301}
2302
Harsh Bansal28718c52017-04-20 13:47:12 +05302303/* Close a QAF stream. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302304void audio_extn_qaf_close_output_stream(struct audio_hw_device *dev,
Harsh Bansal28718c52017-04-20 13:47:12 +05302305 struct audio_stream_out *stream)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302306{
2307 struct stream_out *out = (struct stream_out *)stream;
Harsh Bansal28718c52017-04-20 13:47:12 +05302308 struct qaf_module* qaf_mod = get_qaf_module_for_input_stream(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302309
Harsh Bansal28718c52017-04-20 13:47:12 +05302310 if (!qaf_mod) return;
2311
2312 DEBUG_MSG("stream_handle(%p) format = %x", out, out->format);
2313
2314 //If close is received for QAF passthrough stream then close the QAF passthrough output.
2315 if (p_qaf->passthrough_in == out) {
2316 if (p_qaf->passthrough_out) {
2317 ALOGD("%s %d closing stream handle %p", __func__, __LINE__, p_qaf->passthrough_out);
2318 pthread_mutex_lock(&p_qaf->lock);
2319 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2320 (struct audio_stream_out *)(p_qaf->passthrough_out));
2321 pthread_mutex_unlock(&p_qaf->lock);
2322 p_qaf->passthrough_out = NULL;
2323 }
2324
2325 p_qaf->passthrough_in = NULL;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302326 }
2327
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302328 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2329 qaf_destroy_offload_callback_thread(out);
2330 }
Ben Rombergerd771a7c2017-02-22 18:05:17 -08002331
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302332 qaf_stream_close(out);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302333
2334 adev_close_output_stream(dev, stream);
Harsh Bansal28718c52017-04-20 13:47:12 +05302335
2336 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302337}
2338
Harsh Bansal28718c52017-04-20 13:47:12 +05302339/* Check if QAF is supported or not. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302340bool audio_extn_qaf_is_enabled()
2341{
2342 bool prop_enabled = false;
2343 char value[PROPERTY_VALUE_MAX] = {0};
2344 property_get("audio.qaf.enabled", value, NULL);
2345 prop_enabled = atoi(value) || !strncmp("true", value, 4);
2346 return (prop_enabled);
2347}
2348
Harsh Bansal28718c52017-04-20 13:47:12 +05302349/* Query HDMI EDID and sets module output accordingly.*/
2350void set_hdmi_configuration_to_module()
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302351{
Harsh Bansal28718c52017-04-20 13:47:12 +05302352 int channels = 0;
2353 char *format_params;
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302354 struct str_parms *qaf_params;
Lakshman Chaluvarajub4ec8702016-11-04 19:21:12 +05302355 char prop_value[PROPERTY_VALUE_MAX];
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302356 bool passth_support = false;
2357
Harsh Bansal28718c52017-04-20 13:47:12 +05302358 DEBUG_MSG("Entry");
2359
2360 if (!p_qaf) {
2361 return;
2362 }
2363
2364 if (!p_qaf->hdmi_connect) {
2365 DEBUG_MSG("HDMI is not connected.");
2366 return;
2367 }
2368
2369 p_qaf->hdmi_sink_channels = 0;
2370
2371 //QAF re-encoding and DSP offload passthrough is supported.
2372 if (property_get_bool("audio.offload.passthrough", false)
2373 && property_get_bool("audio.qaf.reencode", false)) {
2374
2375 //If MS12 session is active.
2376 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2377
2378 bool do_setparam = false;
2379 qaf_params = str_parms_create();
2380 property_get("audio.qaf.hdmi.out", prop_value, NULL);
2381
2382 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_E_AC3)
2383 && (strncmp(prop_value, "ddp", 3) == 0)) {
2384 do_setparam = true;
2385 if (qaf_params) {
2386 str_parms_add_str(qaf_params,
2387 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2388 AUDIO_QAF_PARAMETER_VALUE_REENCODE_EAC3);
2389 }
2390 } else if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_AC3)) {
2391 do_setparam = true;
2392 if (qaf_params) {
2393 str_parms_add_str(qaf_params,
2394 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2395 AUDIO_QAF_PARAMETER_VALUE_REENCODE_AC3);
2396 }
2397 }
2398
2399 if (do_setparam) {
2400 if (p_qaf->qaf_msmd_enabled) {
2401 str_parms_add_str(qaf_params,
2402 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2403 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2404 } else {
2405 str_parms_add_str(qaf_params,
2406 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2407 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2408 }
2409 format_params = str_parms_to_str(qaf_params);
2410
2411 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2412 format_params);
2413
2414 passth_support = true;
2415 }
2416 str_parms_destroy(qaf_params);
2417 }
2418
2419 //DTS_M8 session is active.
2420 if (p_qaf->qaf_mod[DTS_M8].session_handle
2421 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2422
2423 bool do_setparam = false;
2424 qaf_params = str_parms_create();
2425#if 0 //TODO: Need to enable with DTS_M8 wrapper.
2426 if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_DTS)) {
2427 do_setparam = true;
2428 if (qaf_params) {
2429 str_parms_add_str(qaf_params,
2430 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2431 AUDIO_QAF_PARAMETER_VALUE_REENCODE_DTS);
2432 }
2433 }
2434#endif
2435
2436 if (do_setparam) {
2437 if (p_qaf->qaf_msmd_enabled) {
2438 str_parms_add_str(qaf_params,
2439 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2440 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement.
2441 } else {
2442 str_parms_add_str(qaf_params,
2443 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2444 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2445 }
2446 format_params = str_parms_to_str(qaf_params);
2447
2448 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2449 format_params);
2450
2451 passth_support = true;
2452 }
2453 str_parms_destroy(qaf_params);
2454 }
2455 }
2456
2457 //Compressed passthrough is not enabled.
2458 if (!passth_support) {
2459
2460 channels = platform_edid_get_max_channels(p_qaf->adev->platform);
2461
2462 qaf_params = str_parms_create();
2463 switch (channels) {
2464 case 8:
2465 DEBUG_MSG("Switching Qaf output to 7.1 channels");
2466 str_parms_add_str(qaf_params,
2467 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2468 AUDIO_QAF_PARAMETER_VALUE_8_CHANNELS);
2469 if (p_qaf->qaf_msmd_enabled) {
2470 str_parms_add_str(qaf_params,
2471 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2472 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2473 } else {
2474 str_parms_add_str(qaf_params,
2475 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2476 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2477 }
2478 p_qaf->hdmi_sink_channels = channels;
2479 break;
2480 case 6:
2481 DEBUG_MSG("Switching Qaf output to 5.1 channels");
2482 str_parms_add_str(qaf_params,
2483 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2484 AUDIO_QAF_PARAMETER_VALUE_6_CHANNELS);
2485 if (p_qaf->qaf_msmd_enabled) {
2486 str_parms_add_str(qaf_params,
2487 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2488 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2489 } else {
2490 str_parms_add_str(qaf_params,
2491 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2492 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI);
2493 }
2494 p_qaf->hdmi_sink_channels = channels;
2495 break;
2496 default:
2497 DEBUG_MSG("Switching Qaf output to default channels");
2498 str_parms_add_str(qaf_params,
2499 AUDIO_QAF_PARAMETER_KEY_CHANNELS,
2500 AUDIO_QAF_PARAMETER_VALUE_DEFAULT_CHANNELS);
2501 if (p_qaf->qaf_msmd_enabled) {
2502 str_parms_add_str(qaf_params,
2503 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2504 AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK);
2505 } else {
2506 str_parms_add_str(qaf_params,
2507 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2508 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2509 }
2510 p_qaf->hdmi_sink_channels = 2;
2511 break;
2512 }
2513
2514 format_params = str_parms_to_str(qaf_params);
2515
2516 if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) {
2517 p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle,
2518 format_params);
2519 }
2520 if (p_qaf->qaf_mod[DTS_M8].session_handle
2521 && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) {
2522 p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle,
2523 format_params);
2524 }
2525
2526 str_parms_destroy(qaf_params);
2527 }
2528 DEBUG_MSG("Exit");
2529}
2530
2531/* QAF set parameter function. For Device connect and disconnect. */
2532int audio_extn_qaf_set_parameters(struct audio_device *adev, struct str_parms *parms)
2533{
2534 int status = 0, val = 0, k;
2535 char *format_params, *kv_parirs;
2536 struct str_parms *qaf_params;
2537 char value[32];
2538
2539 DEBUG_MSG("Entry");
2540
2541 if (!p_qaf) {
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302542 return -EINVAL;
2543 }
2544
2545 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
Harsh Bansal28718c52017-04-20 13:47:12 +05302546
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302547 if (status >= 0) {
2548 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302549 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is connected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302550
Harsh Bansal28718c52017-04-20 13:47:12 +05302551 p_qaf->hdmi_connect = 1;
2552 p_qaf->hdmi_sink_channels = 0;
2553
2554 if (p_qaf->passthrough_in) { //If QAF passthrough is already initialized.
2555 lock_output_stream(p_qaf->passthrough_in);
2556 if (platform_is_edid_supported_format(adev->platform,
2557 p_qaf->passthrough_in->format)) {
2558 //If passthrough format is supported by HDMI then create the QAF passthrough output if not created already.
2559 create_qaf_passthrough_stream();
2560 //Ignoring the returned error, If error then QAF passthrough is disabled.
2561 } else {
2562 //If passthrough format is not supported by HDMI then close the QAF passthrough output if already created.
2563 close_qaf_passthrough_stream();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302564 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302565 unlock_output_stream(p_qaf->passthrough_in);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302566 }
2567
Harsh Bansal28718c52017-04-20 13:47:12 +05302568 set_hdmi_configuration_to_module();
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302569
Bharath Gopal01310bb2016-12-05 15:39:32 +05302570 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302571 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2572 if (!p_qaf->qaf_mod[k].bt_hdl) {
2573 DEBUG_MSG("Opening a2dp output...");
2574 status = audio_extn_bt_hal_load(&p_qaf->qaf_mod[k].bt_hdl);
2575 if (status != 0) {
2576 ERROR_MSG("Error opening BT module");
2577 return status;
2578 }
2579 }
Bharath Gopal01310bb2016-12-05 15:39:32 +05302580 }
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302581 }
Harsh Bansal28718c52017-04-20 13:47:12 +05302582 //TODO else if: Need to consider other devices.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302583 }
2584
2585 status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
2586 if (status >= 0) {
2587 val = atoi(value);
Harsh Bansal28718c52017-04-20 13:47:12 +05302588 if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is disconnected.
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302589
Harsh Bansal28718c52017-04-20 13:47:12 +05302590 qaf_params = str_parms_create();
2591 str_parms_add_str(qaf_params,
2592 AUDIO_QAF_PARAMETER_KEY_DEVICE,
2593 AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER);
2594 str_parms_add_str(qaf_params,
2595 AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT,
2596 AUDIO_QAF_PARAMETER_VALUE_PCM);
2597 p_qaf->hdmi_sink_channels = 0;
2598
2599 p_qaf->passthrough_enabled = 0;
2600 p_qaf->mch_pcm_hdmi_enabled = 0;
2601 p_qaf->hdmi_connect = 0;
2602
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302603 format_params = str_parms_to_str(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302604
2605 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2606 if (p_qaf->qaf_mod[k].session_handle
2607 && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2608 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2609 p_qaf->qaf_mod[k].session_handle, format_params);
2610 }
2611 }
2612 close_all_hdmi_output();
2613
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302614 str_parms_destroy(qaf_params);
Harsh Bansal28718c52017-04-20 13:47:12 +05302615 close_qaf_passthrough_stream();
Bharath Gopal01310bb2016-12-05 15:39:32 +05302616 } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
Harsh Bansal28718c52017-04-20 13:47:12 +05302617 DEBUG_MSG("Closing a2dp output...");
2618 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2619 if (p_qaf->qaf_mod[k].bt_hdl) {
2620 audio_extn_bt_hal_unload(p_qaf->qaf_mod[k].bt_hdl);
2621 p_qaf->qaf_mod[k].bt_hdl = NULL;
2622 }
2623 }
2624 }
2625 //TODO else if: Need to consider other devices.
2626 }
2627
2628 for (k = 0; k < MAX_MM_MODULE_TYPE; k++) {
2629 kv_parirs = str_parms_to_str(parms);
2630 if (p_qaf->qaf_mod[k].session_handle && p_qaf->qaf_mod[k].qaf_audio_session_set_param) {
2631 p_qaf->qaf_mod[k].qaf_audio_session_set_param(
2632 p_qaf->qaf_mod[k].session_handle, kv_parirs);
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302633 }
2634 }
2635
Harsh Bansal28718c52017-04-20 13:47:12 +05302636 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302637 return status;
2638}
2639
Harsh Bansal28718c52017-04-20 13:47:12 +05302640/* Create the QAF. */
2641int audio_extn_qaf_init(struct audio_device *adev)
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302642{
Harsh Bansal28718c52017-04-20 13:47:12 +05302643 DEBUG_MSG("Entry");
2644
2645 p_qaf = calloc(1, sizeof(struct qaf));
2646 if (p_qaf == NULL) {
2647 ERROR_MSG("Out of memory");
2648 return -ENOMEM;
2649 }
2650
2651 p_qaf->adev = adev;
2652
2653 if (property_get_bool("audio.qaf.msmd", false)) {
2654 p_qaf->qaf_msmd_enabled = 1;
2655 }
2656 pthread_mutex_init(&p_qaf->lock, (const pthread_mutexattr_t *) NULL);
2657
2658 int i = 0;
2659
2660 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2661 char value[PROPERTY_VALUE_MAX] = {0};
2662 char lib_name[PROPERTY_VALUE_MAX] = {0};
2663 struct qaf_module *qaf_mod = &(p_qaf->qaf_mod[i]);
2664
2665 if (i == MS12) {
2666 property_get("audio.qaf.library", value, NULL);
2667 } else if (i == DTS_M8) {
2668 property_get("audio.qaf.m8.library", value, NULL);
2669 } else {
2670 continue;
2671 }
2672
2673 snprintf(lib_name, PROPERTY_VALUE_MAX, "%s", value);
2674
2675 qaf_mod->qaf_lib = dlopen(lib_name, RTLD_NOW);
2676 if (qaf_mod->qaf_lib == NULL) {
2677 ERROR_MSG("DLOPEN failed for %s", lib_name);
2678 continue;
2679 }
2680
2681 DEBUG_MSG("DLOPEN successful for %s", lib_name);
2682 qaf_mod->qaf_audio_session_open =
2683 (int (*)(audio_session_handle_t* session_handle, void *p_data, void* license_data))dlsym(qaf_mod->qaf_lib,
2684 "audio_session_open");
2685 qaf_mod->qaf_audio_session_close =
2686 (int (*)(audio_session_handle_t session_handle))dlsym(qaf_mod->qaf_lib,
2687 "audio_session_close");
2688 qaf_mod->qaf_audio_stream_open =
2689 (int (*)(audio_session_handle_t session_handle, audio_stream_handle_t* stream_handle,
2690 audio_stream_config_t input_config, audio_devices_t devices, stream_type_t flags))dlsym(qaf_mod->qaf_lib,
2691 "audio_stream_open");
2692 qaf_mod->qaf_audio_stream_close =
2693 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2694 "audio_stream_close");
2695 qaf_mod->qaf_audio_stream_set_param =
2696 (int (*)(audio_stream_handle_t stream_handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2697 "audio_stream_set_param");
2698 qaf_mod->qaf_audio_session_set_param =
2699 (int (*)(audio_session_handle_t handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib,
2700 "audio_session_set_param");
2701 qaf_mod->qaf_audio_stream_get_param =
2702 (char* (*)(audio_stream_handle_t stream_handle, const char* key))dlsym(qaf_mod->qaf_lib,
2703 "audio_stream_get_param");
2704 qaf_mod->qaf_audio_session_get_param =
2705 (char* (*)(audio_session_handle_t handle, const char* key))dlsym(qaf_mod->qaf_lib,
2706 "audio_session_get_param");
2707 qaf_mod->qaf_audio_stream_start =
2708 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2709 "audio_stream_start");
2710 qaf_mod->qaf_audio_stream_stop =
2711 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2712 "audio_stream_stop");
2713 qaf_mod->qaf_audio_stream_pause =
2714 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2715 "audio_stream_pause");
2716 qaf_mod->qaf_audio_stream_flush =
2717 (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib,
2718 "audio_stream_flush");
2719 qaf_mod->qaf_audio_stream_write =
2720 (int (*)(audio_stream_handle_t stream_handle, const void* buf, int size))dlsym(qaf_mod->qaf_lib,
2721 "audio_stream_write");
2722 qaf_mod->qaf_register_event_callback =
2723 (void (*)(audio_session_handle_t session_handle, void *priv_data, notify_event_callback_t event_callback,
2724 audio_event_id_t event_id))dlsym(qaf_mod->qaf_lib,
2725 "register_event_callback");
2726 }
2727
2728 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302729 return 0;
2730}
2731
Harsh Bansal28718c52017-04-20 13:47:12 +05302732/* Tear down the qaf extension. */
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302733void audio_extn_qaf_deinit()
2734{
Harsh Bansal28718c52017-04-20 13:47:12 +05302735 int i;
2736 DEBUG_MSG("Entry");
2737
2738 if (p_qaf != NULL) {
2739 for (i = 0; i < MAX_MM_MODULE_TYPE; i++) {
2740 qaf_session_close(&p_qaf->qaf_mod[i]);
2741
2742 if (p_qaf->qaf_mod[i].qaf_lib != NULL) {
2743 dlclose(p_qaf->qaf_mod[i].qaf_lib);
2744 p_qaf->qaf_mod[i].qaf_lib = NULL;
2745 }
2746 }
2747 if (p_qaf->passthrough_out) {
2748 adev_close_output_stream((struct audio_hw_device *)p_qaf->adev,
2749 (struct audio_stream_out *)(p_qaf->passthrough_out));
2750 p_qaf->passthrough_out = NULL;
2751 }
2752
2753 pthread_mutex_destroy(&p_qaf->lock);
2754 free(p_qaf);
2755 p_qaf = NULL;
2756 }
2757 DEBUG_MSG("Exit");
Lakshman Chaluvaraju22ba9f12016-09-12 11:42:10 +05302758}