Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1 | /* |
Revathi Uddaraju | 1eac8b0 | 2017-05-18 17:13:33 +0530 | [diff] [blame] | 2 | * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 3 | * |
| 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 Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 39 | #define DEBUG_MSG_VV(arg,...) ALOGVV("%s: %d: " arg, __func__, __LINE__, ##__VA_ARGS__) |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 40 | #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 Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 43 | #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 2 |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 44 | #define COMPRESS_PASSTHROUGH_DDP_FRAGMENT_SIZE 4608 |
| 45 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 46 | #define QAF_DEFAULT_COMPR_AUDIO_HANDLE 1001 |
| 47 | #define QAF_DEFAULT_COMPR_PASSTHROUGH_HANDLE 1002 |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 48 | #define QAF_DEFAULT_PASSTHROUGH_HANDLE 1003 |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 49 | |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 50 | #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 300 |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 51 | |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 52 | #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 Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 59 | #define QAF_MODULE_PCM_INPUT_BUFFER_LATENCY 32 |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 60 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 61 | #define MS12_PCM_OUT_FRAGMENT_SIZE 1536 //samples |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 62 | #define MS12_PCM_IN_FRAGMENT_SIZE 1536 //samples |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 63 | |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 64 | #define DD_FRAME_SIZE 1536 |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 65 | #define DDP_FRAME_SIZE DD_FRAME_SIZE |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 66 | /* |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 67 | * DD encoder output size for one frame. |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 68 | */ |
| 69 | #define DD_ENCODER_OUTPUT_SIZE 2560 |
| 70 | /* |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 71 | * DDP encoder output size for one frame. |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 72 | */ |
| 73 | #define DDP_ENCODER_OUTPUT_SIZE 4608 |
| 74 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 75 | /*********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 Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 81 | /* |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 82 | * DTS encoder output size for one frame. |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 83 | */ |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 84 | #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 Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 90 | |
Bharath Gopal | 01310bb | 2016-12-05 15:39:32 +0530 | [diff] [blame] | 91 | /* |
| 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 Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 97 | //TODO: Need to handle for DTS |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 98 | #define QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE 1536 |
| 99 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 100 | #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 Uddaraju | 1eac8b0 | 2017-05-18 17:13:33 +0530 | [diff] [blame] | 120 | #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 Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 126 | //TODO: Need to remove this. |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 127 | #define QAF_OUTPUT_SAMPLING_RATE 48000 |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 128 | |
| 129 | #ifdef QAF_DUMP_ENABLED |
| 130 | FILE *fp_output_writer_hdmi = NULL; |
| 131 | #endif |
| 132 | |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 133 | struct 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 Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 140 | //Types of MM module, currently supported by QAF. |
| 141 | typedef enum { |
| 142 | MS12, |
| 143 | DTS_M8, |
| 144 | MAX_MM_MODULE_TYPE, |
| 145 | INVALID_MM_MODULE |
| 146 | } mm_module_type; |
| 147 | |
| 148 | typedef 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 | |
| 156 | typedef enum { |
| 157 | QAF_IN_MAIN = 0, /* Single PID Main/Primary or Dual-PID stream */ |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 158 | QAF_IN_ASSOC, /* Associated/Secondary stream */ |
| 159 | QAF_IN_PCM, /* PCM stream. */ |
| 160 | QAF_IN_MAIN_2, /* Single PID Main2 stream */ |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 161 | MAX_QAF_MODULE_IN |
| 162 | } mm_module_input_type; |
| 163 | |
| 164 | struct qaf_module { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 165 | audio_session_handle_t session_handle; |
| 166 | void *qaf_lib; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 167 | int (*qaf_audio_session_open)(audio_session_handle_t* session_handle, |
Sri Karri | 1fbfef5 | 2017-06-29 19:45:47 +0530 | [diff] [blame] | 168 | audio_session_type_t s_type, |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 169 | void *p_data, |
| 170 | void* license_data); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 171 | int (*qaf_audio_session_close)(audio_session_handle_t session_handle); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 172 | int (*qaf_audio_stream_open)(audio_session_handle_t session_handle, |
| 173 | audio_stream_handle_t* stream_handle, |
| 174 | audio_stream_config_t input_config, |
| 175 | audio_devices_t devices, |
| 176 | stream_type_t flags); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 177 | int (*qaf_audio_stream_close)(audio_stream_handle_t stream_handle); |
| 178 | int (*qaf_audio_stream_set_param)(audio_stream_handle_t stream_handle, const char* kv_pairs); |
| 179 | int (*qaf_audio_session_set_param)(audio_session_handle_t handle, const char* kv_pairs); |
| 180 | char* (*qaf_audio_stream_get_param)(audio_stream_handle_t stream_handle, const char* key); |
| 181 | char* (*qaf_audio_session_get_param)(audio_session_handle_t handle, const char* key); |
| 182 | int (*qaf_audio_stream_start)(audio_stream_handle_t handle); |
| 183 | int (*qaf_audio_stream_stop)(audio_stream_handle_t stream_handle); |
| 184 | int (*qaf_audio_stream_pause)(audio_stream_handle_t stream_handle); |
| 185 | int (*qaf_audio_stream_flush)(audio_stream_handle_t stream_handle); |
| 186 | int (*qaf_audio_stream_write)(audio_stream_handle_t stream_handle, const void* buf, int size); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 187 | void (*qaf_register_event_callback)(audio_session_handle_t session_handle, |
| 188 | void *priv_data, |
| 189 | notify_event_callback_t event_callback, |
| 190 | audio_event_id_t event_id); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 191 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 192 | /*Input stream of MM module */ |
| 193 | struct stream_out *stream_in[MAX_QAF_MODULE_IN]; |
| 194 | /*Output Stream from MM module */ |
| 195 | struct stream_out *stream_out[MAX_QAF_MODULE_OUT]; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 196 | |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 197 | /*Media format associated with each output id raised by mm module. */ |
| 198 | audio_qaf_media_format_t out_stream_fmt[MAX_QAF_MODULE_OUT]; |
| 199 | /*Flag is set if media format is changed for an mm module output. */ |
| 200 | bool is_media_fmt_changed[MAX_QAF_MODULE_OUT]; |
| 201 | /*Index to be updated in out_stream_fmt array for a new mm module output. */ |
| 202 | int new_out_format_index; |
| 203 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 204 | struct qaf_adsp_hdlr_config_state adsp_hdlr_config[MAX_QAF_MODULE_IN]; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 205 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 206 | //BT session handle. |
Bharath Gopal | 01310bb | 2016-12-05 15:39:32 +0530 | [diff] [blame] | 207 | void *bt_hdl; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 208 | |
Deepak Agarwal | 1e42b85 | 2017-02-11 17:57:04 +0530 | [diff] [blame] | 209 | float vol_left; |
| 210 | float vol_right; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 211 | bool is_vol_set; |
| 212 | bool drain_received[MAX_QAF_MODULE_IN]; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 213 | }; |
| 214 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 215 | struct qaf { |
| 216 | struct audio_device *adev; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 217 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 218 | pthread_mutex_t lock; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 219 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 220 | bool hdmi_connect; |
| 221 | int hdmi_sink_channels; |
| 222 | |
| 223 | //Flag to indicate if QAF transcode output stream is enabled from any mm module. |
| 224 | bool passthrough_enabled; |
| 225 | //Flag to indicate if QAF mch pcm output stream is enabled from any mm module. |
| 226 | bool mch_pcm_hdmi_enabled; |
| 227 | |
| 228 | //Flag to indicate if msmd is supported. |
| 229 | bool qaf_msmd_enabled; |
| 230 | |
| 231 | //Handle of QAF input stream, which is routed as QAF passthrough. |
| 232 | struct stream_out *passthrough_in; |
| 233 | //Handle of QAF passthrough stream. |
| 234 | struct stream_out *passthrough_out; |
| 235 | |
| 236 | struct qaf_module qaf_mod[MAX_MM_MODULE_TYPE]; |
| 237 | }; |
| 238 | |
| 239 | static int qaf_out_pause(struct audio_stream_out* stream); |
| 240 | static int qaf_out_flush(struct audio_stream_out* stream); |
| 241 | static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type); |
| 242 | |
| 243 | //Global handle of QAF. Access to this should be protected by mutex lock. |
| 244 | static struct qaf *p_qaf = NULL; |
| 245 | |
| 246 | /* Gets the pointer to qaf module for the qaf input stream. */ |
| 247 | static struct qaf_module* get_qaf_module_for_input_stream(struct stream_out *out) |
Deepak Agarwal | 1e42b85 | 2017-02-11 17:57:04 +0530 | [diff] [blame] | 248 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 249 | struct qaf_module *qaf_mod = NULL; |
| 250 | int i, j; |
| 251 | if (!p_qaf) return NULL; |
Deepak Agarwal | 1e42b85 | 2017-02-11 17:57:04 +0530 | [diff] [blame] | 252 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 253 | for (i = 0; i < MAX_MM_MODULE_TYPE; i++) { |
| 254 | for (j = 0; j < MAX_QAF_MODULE_IN; j++) { |
| 255 | if (p_qaf->qaf_mod[i].stream_in[j] == out) { |
| 256 | qaf_mod = &(p_qaf->qaf_mod[i]); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return qaf_mod; |
Deepak Agarwal | 1e42b85 | 2017-02-11 17:57:04 +0530 | [diff] [blame] | 263 | } |
| 264 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 265 | /* Finds the mm module input stream index for the QAF input stream. */ |
| 266 | static int get_input_stream_index(struct stream_out *out) |
| 267 | { |
| 268 | int index = -1, j; |
| 269 | struct qaf_module* qaf_mod = NULL; |
| 270 | |
| 271 | qaf_mod = get_qaf_module_for_input_stream(out); |
| 272 | if (!qaf_mod) return index; |
| 273 | |
| 274 | for (j = 0; j < MAX_QAF_MODULE_IN; j++) { |
| 275 | if (qaf_mod->stream_in[j] == out) { |
| 276 | index = j; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return index; |
| 282 | } |
| 283 | |
| 284 | /* Finds the right mm module for the QAF input stream format. */ |
| 285 | static mm_module_type get_mm_module_for_format(audio_format_t format) |
| 286 | { |
| 287 | int j; |
| 288 | |
| 289 | DEBUG_MSG("Format 0x%x", format); |
| 290 | |
| 291 | if (format == AUDIO_FORMAT_PCM_16_BIT) { |
| 292 | //If dts is not supported then alway support pcm with MS12 |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 293 | if (!property_get_bool("vendor.audio.qaf.dts_m8", false)) { //TODO: Need to add this property for DTS. |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 294 | return MS12; |
| 295 | } |
| 296 | |
| 297 | //If QAF passthrough is active then send the PCM stream to primary HAL. |
| 298 | if (!p_qaf->passthrough_out) { |
| 299 | /* Iff any stream is active in MS12 module then route PCM stream to it. */ |
| 300 | for (j = 0; j < MAX_QAF_MODULE_IN; j++) { |
| 301 | if (p_qaf->qaf_mod[MS12].stream_in[j]) { |
| 302 | return MS12; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | return INVALID_MM_MODULE; |
| 307 | } |
| 308 | |
| 309 | switch (format & AUDIO_FORMAT_MAIN_MASK) { |
| 310 | case AUDIO_FORMAT_AC3: |
| 311 | case AUDIO_FORMAT_E_AC3: |
| 312 | case AUDIO_FORMAT_AAC: |
| 313 | case AUDIO_FORMAT_AAC_ADTS: |
| 314 | case AUDIO_FORMAT_AC4: |
| 315 | return MS12; |
| 316 | case AUDIO_FORMAT_DTS: |
| 317 | case AUDIO_FORMAT_DTS_HD: |
| 318 | return DTS_M8; |
| 319 | default: |
| 320 | return INVALID_MM_MODULE; |
| 321 | } |
| 322 | } |
| 323 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 324 | static bool is_main_active(struct qaf_module* qaf_mod) |
| 325 | { |
| 326 | return (qaf_mod->stream_in[QAF_IN_MAIN] || qaf_mod->stream_in[QAF_IN_MAIN_2]); |
| 327 | } |
| 328 | |
| 329 | static bool is_dual_main_active(struct qaf_module* qaf_mod) |
| 330 | { |
| 331 | return (qaf_mod->stream_in[QAF_IN_MAIN] && qaf_mod->stream_in[QAF_IN_MAIN_2]); |
| 332 | } |
| 333 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 334 | /* Gets the pcm output buffer size(in samples) for the mm module. */ |
| 335 | static uint32_t get_pcm_output_buffer_size_samples(struct qaf_module *qaf_mod) |
| 336 | { |
| 337 | uint32_t pcm_output_buffer_size = 0; |
| 338 | |
| 339 | if (qaf_mod == &p_qaf->qaf_mod[MS12]) { |
| 340 | pcm_output_buffer_size = MS12_PCM_OUT_FRAGMENT_SIZE; |
| 341 | } else if (qaf_mod == &p_qaf->qaf_mod[DTS_M8]) { |
| 342 | pcm_output_buffer_size = DTS_PCM_OUT_FRAGMENT_SIZE; |
| 343 | } |
| 344 | |
| 345 | return pcm_output_buffer_size; |
| 346 | } |
| 347 | |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 348 | static int get_media_fmt_array_index_for_output_id( |
| 349 | struct qaf_module* qaf_mod, |
| 350 | int output_id) |
| 351 | { |
| 352 | int i; |
| 353 | for (i = 0; i < MAX_QAF_MODULE_OUT; i++) { |
| 354 | if (qaf_mod->out_stream_fmt[i].output_id == output_id) { |
| 355 | return i; |
| 356 | } |
| 357 | } |
| 358 | return -1; |
| 359 | } |
| 360 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 361 | /* Acquire Mutex lock on output stream */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 362 | static void lock_output_stream(struct stream_out *out) |
| 363 | { |
| 364 | pthread_mutex_lock(&out->pre_lock); |
| 365 | pthread_mutex_lock(&out->lock); |
| 366 | pthread_mutex_unlock(&out->pre_lock); |
| 367 | } |
| 368 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 369 | /* Release Mutex lock on output stream */ |
| 370 | static void unlock_output_stream(struct stream_out *out) |
| 371 | { |
| 372 | pthread_mutex_unlock(&out->lock); |
| 373 | } |
| 374 | |
| 375 | /* Checks if stream can be routed as QAF passthrough or not. */ |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 376 | static bool audio_extn_qaf_passthrough_enabled(struct stream_out *out) |
| 377 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 378 | DEBUG_MSG("Format 0x%x", out->format); |
| 379 | bool is_enabled = false; |
| 380 | |
| 381 | if (!p_qaf) return false; |
| 382 | |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 383 | if ((!property_get_bool("vendor.audio.qaf.reencode", false)) |
| 384 | && property_get_bool("vendor.audio.qaf.passthrough", false)) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 385 | |
| 386 | if ((out->format == AUDIO_FORMAT_PCM_16_BIT) && (popcount(out->channel_mask) > 2)) { |
| 387 | is_enabled = true; |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 388 | } else if (property_get_bool("vendor.audio.offload.passthrough", false)) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 389 | switch (out->format) { |
| 390 | case AUDIO_FORMAT_AC3: |
| 391 | case AUDIO_FORMAT_E_AC3: |
| 392 | case AUDIO_FORMAT_DTS: |
Ben Romberger | 1aaaf86 | 2017-04-06 17:49:46 -0700 | [diff] [blame] | 393 | case AUDIO_FORMAT_DTS_HD: |
Naresh Tanniru | 928f086 | 2017-04-07 16:44:23 -0700 | [diff] [blame] | 394 | case AUDIO_FORMAT_DOLBY_TRUEHD: |
| 395 | case AUDIO_FORMAT_IEC61937: { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 396 | is_enabled = true; |
| 397 | break; |
| 398 | } |
| 399 | default: |
| 400 | is_enabled = false; |
| 401 | break; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 405 | |
| 406 | return is_enabled; |
| 407 | } |
| 408 | |
| 409 | /*Closes all pcm hdmi output from QAF. */ |
| 410 | static void close_all_pcm_hdmi_output() |
| 411 | { |
| 412 | int i; |
| 413 | //Closing all the PCM HDMI output stream from QAF. |
| 414 | for (i = 0; i < MAX_MM_MODULE_TYPE; i++) { |
| 415 | if (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH]) { |
| 416 | adev_close_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 417 | (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH])); |
| 418 | p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD_MCH] = NULL; |
| 419 | } |
| 420 | |
| 421 | if ((p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]) |
| 422 | && (p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD]->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) { |
| 423 | adev_close_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 424 | (struct audio_stream_out *)(p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD])); |
| 425 | p_qaf->qaf_mod[i].stream_out[QAF_OUT_OFFLOAD] = NULL; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | p_qaf->mch_pcm_hdmi_enabled = 0; |
| 430 | } |
| 431 | |
| 432 | static void close_all_hdmi_output() |
| 433 | { |
| 434 | int k; |
| 435 | for (k = 0; k < MAX_MM_MODULE_TYPE; k++) { |
| 436 | if (p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) { |
| 437 | adev_close_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 438 | (struct audio_stream_out *)(p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH])); |
| 439 | p_qaf->qaf_mod[k].stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] = NULL; |
| 440 | } |
| 441 | } |
| 442 | p_qaf->passthrough_enabled = 0; |
| 443 | |
| 444 | close_all_pcm_hdmi_output(); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static int qaf_out_callback(stream_callback_event_t event, void *param __unused, void *cookie) |
| 448 | { |
| 449 | struct stream_out *out = (struct stream_out *)cookie; |
| 450 | |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 451 | out->client_callback(event, NULL, out->client_cookie); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 455 | /* Creates the QAF passthrough output stream. */ |
| 456 | static int create_qaf_passthrough_stream() |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 457 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 458 | DEBUG_MSG(); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 459 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 460 | int ret = 0, k; |
| 461 | struct stream_out *out = p_qaf->passthrough_in; |
| 462 | |
| 463 | if (!out) return -EINVAL; |
| 464 | |
| 465 | pthread_mutex_lock(&p_qaf->lock); |
| 466 | lock_output_stream(out); |
| 467 | |
| 468 | //Creating QAF passthrough output stream. |
| 469 | if (NULL == p_qaf->passthrough_out) { |
| 470 | audio_output_flags_t flags; |
| 471 | struct audio_config config; |
| 472 | audio_devices_t devices; |
| 473 | |
| 474 | config.sample_rate = config.offload_info.sample_rate = out->sample_rate; |
| 475 | config.offload_info.version = AUDIO_INFO_INITIALIZER.version; |
| 476 | config.offload_info.size = AUDIO_INFO_INITIALIZER.size; |
| 477 | config.offload_info.format = out->format; |
| 478 | config.offload_info.bit_width = out->bit_width; |
| 479 | config.format = out->format; |
| 480 | config.offload_info.channel_mask = config.channel_mask = out->channel_mask; |
| 481 | |
| 482 | //Device is copied from the QAF passthrough input stream. |
| 483 | devices = out->devices; |
| 484 | flags = out->flags; |
| 485 | |
| 486 | ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 487 | QAF_DEFAULT_PASSTHROUGH_HANDLE, |
| 488 | devices, |
| 489 | flags, |
| 490 | &config, |
| 491 | (struct audio_stream_out **)&(p_qaf->passthrough_out), |
| 492 | NULL); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 493 | if (ret < 0) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 494 | ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret); |
| 495 | unlock_output_stream(out); |
| 496 | return ret; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 497 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 498 | p_qaf->passthrough_in = out; |
| 499 | p_qaf->passthrough_out->stream.set_callback((struct audio_stream_out *)p_qaf->passthrough_out, |
| 500 | (stream_callback_t) qaf_out_callback, out); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 501 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 502 | |
| 503 | unlock_output_stream(out); |
| 504 | |
| 505 | //Since QAF-Passthrough is created, close other HDMI outputs. |
| 506 | close_all_hdmi_output(); |
| 507 | |
| 508 | pthread_mutex_unlock(&p_qaf->lock); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 509 | return ret; |
| 510 | } |
| 511 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 512 | /* Closes the QAF passthrough output stream. */ |
| 513 | static void close_qaf_passthrough_stream() |
| 514 | { |
| 515 | if (p_qaf->passthrough_out != NULL) { //QAF pasthroug is enabled. Close it. |
| 516 | pthread_mutex_lock(&p_qaf->lock); |
| 517 | adev_close_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 518 | (struct audio_stream_out *)(p_qaf->passthrough_out)); |
| 519 | p_qaf->passthrough_out = NULL; |
| 520 | pthread_mutex_unlock(&p_qaf->lock); |
| 521 | |
| 522 | if (p_qaf->passthrough_in->qaf_stream_handle) { |
| 523 | qaf_out_pause((struct audio_stream_out*)p_qaf->passthrough_in); |
| 524 | qaf_out_flush((struct audio_stream_out*)p_qaf->passthrough_in); |
| 525 | qaf_out_drain((struct audio_stream_out*)p_qaf->passthrough_in, |
| 526 | (audio_drain_type_t)STREAM_CBK_EVENT_DRAIN_READY); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /* Sends a command to output stream offload thread. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 532 | static int qaf_send_offload_cmd_l(struct stream_out* out, int command) |
| 533 | { |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 534 | DEBUG_MSG_VV("command is %d", command); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 535 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 536 | struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd)); |
| 537 | |
| 538 | if (!cmd) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 539 | ERROR_MSG("failed to allocate mem for command 0x%x", command); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 540 | return -ENOMEM; |
| 541 | } |
| 542 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 543 | cmd->cmd = command; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 544 | |
| 545 | lock_output_stream(out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 546 | list_add_tail(&out->qaf_offload_cmd_list, &cmd->node); |
| 547 | pthread_cond_signal(&out->qaf_offload_cond); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 548 | unlock_output_stream(out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 552 | /* Stops a QAF module stream.*/ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 553 | static int audio_extn_qaf_stream_stop(struct stream_out *out) |
| 554 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 555 | int ret = 0; |
| 556 | DEBUG_MSG("Output Stream 0x%x", out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 557 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 558 | struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out); |
| 559 | if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_stop)) { |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | if (out->qaf_stream_handle) { |
| 564 | ret = qaf_mod->qaf_audio_stream_stop(out->qaf_stream_handle); |
| 565 | } |
| 566 | |
| 567 | return ret; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 568 | } |
| 569 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 570 | /* Puts a QAF module stream in standby. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 571 | static int qaf_out_standby(struct audio_stream *stream) |
| 572 | { |
| 573 | struct stream_out *out = (struct stream_out *)stream; |
| 574 | int status = 0; |
| 575 | |
| 576 | ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__, |
| 577 | stream, out->usecase, use_case_table[out->usecase]); |
| 578 | |
| 579 | lock_output_stream(out); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 580 | |
| 581 | //If QAF passthrough is active then block standby on all the input streams of QAF mm modules. |
| 582 | if (p_qaf->passthrough_out) { |
| 583 | //If standby is received on QAF passthrough stream then forward it to primary HAL. |
| 584 | if (p_qaf->passthrough_in == out) { |
| 585 | status = p_qaf->passthrough_out->stream.common.standby( |
| 586 | (struct audio_stream *)p_qaf->passthrough_out); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 587 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 588 | } else { |
| 589 | //If QAF passthrough stream is not active then stop the QAF module stream. |
| 590 | status = audio_extn_qaf_stream_stop(out); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 591 | } |
| 592 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 593 | if (!out->standby) { |
| 594 | out->standby = true; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 595 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 596 | |
| 597 | unlock_output_stream(out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 598 | return status; |
| 599 | } |
| 600 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 601 | /* Sets the volume to PCM output stream. */ |
| 602 | static int qaf_out_set_volume(struct audio_stream_out *stream, float left, float right) |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 603 | { |
| 604 | int ret = 0; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 605 | struct stream_out *out = (struct stream_out *)stream; |
| 606 | struct qaf_module *qaf_mod = NULL; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 607 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 608 | DEBUG_MSG("Left %f, Right %f", left, right); |
| 609 | |
| 610 | qaf_mod = get_qaf_module_for_input_stream(out); |
| 611 | if (!qaf_mod) { |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | |
| 615 | pthread_mutex_lock(&p_qaf->lock); |
| 616 | qaf_mod->vol_left = left; |
| 617 | qaf_mod->vol_right = right; |
| 618 | qaf_mod->is_vol_set = true; |
| 619 | pthread_mutex_unlock(&p_qaf->lock); |
| 620 | |
| 621 | if (qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL) { |
| 622 | ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume( |
| 623 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], left, right); |
| 624 | } |
| 625 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 626 | return ret; |
| 627 | } |
| 628 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 629 | /* Starts a QAF module stream. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 630 | static int qaf_stream_start(struct stream_out *out) |
| 631 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 632 | int ret = -EINVAL; |
| 633 | struct qaf_module *qaf_mod = NULL; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 634 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 635 | DEBUG_MSG("Output Stream = %p", out); |
| 636 | |
| 637 | qaf_mod = get_qaf_module_for_input_stream(out); |
| 638 | if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_start)) { |
| 639 | return -EINVAL; |
| 640 | } |
| 641 | |
| 642 | if (out->qaf_stream_handle) { |
| 643 | ret = qaf_mod->qaf_audio_stream_start(out->qaf_stream_handle); |
| 644 | } |
| 645 | |
| 646 | return ret; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | static int qaf_start_output_stream(struct stream_out *out) |
| 650 | { |
| 651 | int ret = 0; |
| 652 | struct audio_device *adev = out->dev; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 653 | |
| 654 | if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) { |
| 655 | ret = -EINVAL; |
| 656 | usleep(50000); |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)", |
| 661 | __func__, &out->stream, out->usecase, use_case_table[out->usecase], |
| 662 | out->devices); |
| 663 | |
Dhananjay Kumar | e6293dd | 2017-05-25 17:25:30 +0530 | [diff] [blame] | 664 | if (CARD_STATUS_OFFLINE == out->card_status || |
| 665 | CARD_STATUS_OFFLINE == adev->card_status) { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 666 | ALOGE("%s: sound card is not active/SSR returning error", __func__); |
| 667 | ret = -EIO; |
| 668 | usleep(50000); |
| 669 | return ret; |
| 670 | } |
| 671 | |
| 672 | return qaf_stream_start(out); |
| 673 | } |
| 674 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 675 | /* Sends input buffer to the QAF MM module. */ |
| 676 | static int qaf_module_write_input_buffer(struct stream_out *out, const void *buffer, int bytes) |
| 677 | { |
| 678 | int ret = -EINVAL; |
| 679 | struct qaf_module *qaf_mod = NULL; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 680 | |
| 681 | qaf_mod = get_qaf_module_for_input_stream(out); |
| 682 | if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_write)) { |
| 683 | return ret; |
| 684 | } |
| 685 | |
| 686 | if (out->qaf_stream_handle) { |
| 687 | ret = qaf_mod->qaf_audio_stream_write(out->qaf_stream_handle, buffer, bytes); |
| 688 | } |
| 689 | return ret; |
| 690 | } |
| 691 | |
| 692 | /* Writes buffer to QAF input stream. */ |
| 693 | static ssize_t qaf_out_write(struct audio_stream_out *stream, const void *buffer, size_t bytes) |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 694 | { |
| 695 | struct stream_out *out = (struct stream_out *)stream; |
| 696 | struct audio_device *adev = out->dev; |
| 697 | ssize_t ret = 0; |
| 698 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 699 | DEBUG_MSG_VV("bytes = %d, usecase[%d] and flags[%x] for handle[%p]", |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 700 | (int)bytes, out->usecase, out->flags, out); |
| 701 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 702 | lock_output_stream(out); |
| 703 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 704 | // If QAF passthrough is active then block writing data to QAF mm module. |
| 705 | if (p_qaf->passthrough_out) { |
| 706 | //If write is received for the QAF passthrough stream then send the buffer to primary HAL. |
| 707 | if (p_qaf->passthrough_in == out) { |
| 708 | ret = p_qaf->passthrough_out->stream.write( |
| 709 | (struct audio_stream_out *)(p_qaf->passthrough_out), |
| 710 | buffer, |
| 711 | bytes); |
| 712 | if (ret > 0) out->standby = false; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 713 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 714 | unlock_output_stream(out); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 715 | return ret; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 716 | } else if (out->standby) { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 717 | pthread_mutex_lock(&adev->lock); |
| 718 | ret = qaf_start_output_stream(out); |
| 719 | pthread_mutex_unlock(&adev->lock); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 720 | if (ret == 0) { |
| 721 | out->standby = false; |
| 722 | } else { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 723 | goto exit; |
| 724 | } |
| 725 | } |
| 726 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 727 | if ((adev->is_channel_status_set == false) && (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 728 | audio_utils_set_hdmi_channel_status(out, (char *)buffer, bytes); |
| 729 | adev->is_channel_status_set = true; |
| 730 | } |
| 731 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 732 | ret = qaf_module_write_input_buffer(out, buffer, bytes); |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 733 | DEBUG_MSG_VV("ret [%d]", (int)ret); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 734 | |
| 735 | if (ret >= 0) { |
| 736 | bytes = ret; |
| 737 | out->written += bytes / ((popcount(out->channel_mask) * sizeof(short))); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 738 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 739 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 740 | |
| 741 | exit: |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 742 | unlock_output_stream(out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 743 | |
| 744 | if (ret < 0) { |
| 745 | if (ret == -EAGAIN) { |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 746 | DEBUG_MSG_VV("No space available in mm module, post msg to cb thread"); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 747 | ret = qaf_send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 748 | bytes = 0; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 749 | } else if (ret == -ENOMEM || ret == -EPERM) { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 750 | if (out->pcm) |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 751 | ERROR_MSG("error %d, %s", (int)ret, pcm_get_error(out->pcm)); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 752 | qaf_out_standby(&out->stream.common); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 753 | usleep(bytes * 1000000 |
| 754 | / audio_stream_out_frame_size(stream) |
| 755 | / out->stream.common.get_sample_rate(&out->stream.common)); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | return bytes; |
| 759 | } |
| 760 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 761 | /* Gets PCM offload buffer size for a given config. */ |
| 762 | static uint32_t qaf_get_pcm_offload_buffer_size(audio_offload_info_t* info, |
| 763 | uint32_t samples_per_frame) |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 764 | { |
| 765 | uint32_t fragment_size = 0; |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 766 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 767 | fragment_size = (samples_per_frame * (info->bit_width >> 3) * popcount(info->channel_mask)); |
| 768 | |
| 769 | if (fragment_size < MIN_PCM_OFFLOAD_FRAGMENT_SIZE) |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 770 | fragment_size = MIN_PCM_OFFLOAD_FRAGMENT_SIZE; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 771 | else if (fragment_size > MAX_PCM_OFFLOAD_FRAGMENT_SIZE) |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 772 | fragment_size = MAX_PCM_OFFLOAD_FRAGMENT_SIZE; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 773 | |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 774 | // To have same PCM samples for all channels, the buffer size requires to |
| 775 | // be multiple of (number of channels * bytes per sample) |
| 776 | // For writes to succeed, the buffer must be written at address which is multiple of 32 |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 777 | fragment_size = ALIGN(fragment_size, |
| 778 | ((info->bit_width >> 3) * popcount(info->channel_mask) * 32)); |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 779 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 780 | ALOGI("Qaf PCM offload Fragment size is %d bytes", fragment_size); |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 781 | |
| 782 | return fragment_size; |
| 783 | } |
| 784 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 785 | static uint32_t qaf_get_pcm_offload_input_buffer_size(info) |
| 786 | { |
| 787 | return qaf_get_pcm_offload_buffer_size(info, MS12_PCM_IN_FRAGMENT_SIZE); |
| 788 | } |
| 789 | |
| 790 | static uint32_t qaf_get_pcm_offload_output_buffer_size(struct qaf_module *qaf_mod, |
| 791 | audio_offload_info_t* info) |
| 792 | { |
| 793 | return qaf_get_pcm_offload_buffer_size(info, get_pcm_output_buffer_size_samples(qaf_mod)); |
| 794 | } |
| 795 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 796 | /* Gets buffer latency in samples. */ |
| 797 | static int get_buffer_latency(struct stream_out *out, uint32_t buffer_size, uint32_t *latency) |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 798 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 799 | unsigned long int samples_in_one_encoded_frame; |
| 800 | unsigned long int size_of_one_encoded_frame; |
| 801 | |
| 802 | switch (out->format) { |
| 803 | case AUDIO_FORMAT_AC3: |
| 804 | samples_in_one_encoded_frame = DD_FRAME_SIZE; |
| 805 | size_of_one_encoded_frame = DD_ENCODER_OUTPUT_SIZE; |
| 806 | break; |
| 807 | case AUDIO_FORMAT_E_AC3: |
| 808 | samples_in_one_encoded_frame = DDP_FRAME_SIZE; |
| 809 | size_of_one_encoded_frame = DDP_ENCODER_OUTPUT_SIZE; |
| 810 | break; |
| 811 | case AUDIO_FORMAT_DTS: |
| 812 | samples_in_one_encoded_frame = DTS_FRAME_SIZE; |
| 813 | size_of_one_encoded_frame = DTS_ENCODER_OUTPUT_SIZE; |
| 814 | break; |
| 815 | case AUDIO_FORMAT_DTS_HD: |
| 816 | samples_in_one_encoded_frame = DTSHD_FRAME_SIZE; |
| 817 | size_of_one_encoded_frame = DTSHD_ENCODER_OUTPUT_SIZE; |
| 818 | break; |
| 819 | case AUDIO_FORMAT_PCM_16_BIT: |
| 820 | samples_in_one_encoded_frame = 1; |
| 821 | size_of_one_encoded_frame = ((out->bit_width) >> 3) * popcount(out->channel_mask); |
| 822 | break; |
| 823 | default: |
| 824 | *latency = 0; |
| 825 | return (-EINVAL); |
| 826 | } |
| 827 | |
| 828 | *latency = ((buffer_size * samples_in_one_encoded_frame) / size_of_one_encoded_frame); |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | /* Returns the number of frames rendered to outside observer. */ |
| 833 | static int qaf_get_rendered_frames(struct stream_out *out, uint64_t *frames) |
| 834 | { |
| 835 | int ret = 0, i; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 836 | struct str_parms *parms; |
| 837 | int value = 0; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 838 | int module_latency = 0; |
| 839 | uint32_t kernel_latency = 0; |
| 840 | uint32_t dsp_latency = 0; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 841 | int signed_frames = 0; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 842 | char* kvpairs = NULL; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 843 | struct qaf_module *qaf_mod = NULL; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 844 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 845 | DEBUG_MSG("Output Format %d", out->format); |
| 846 | |
| 847 | qaf_mod = get_qaf_module_for_input_stream(out); |
| 848 | if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_get_param)) { |
| 849 | return -EINVAL; |
| 850 | } |
| 851 | |
| 852 | //Get MM module latency. |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 853 | kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "get_latency"); |
| 854 | if (kvpairs) { |
| 855 | parms = str_parms_create_str(kvpairs); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 856 | ret = str_parms_get_int(parms, "get_latency", &module_latency); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 857 | if (ret >= 0) { |
| 858 | str_parms_destroy(parms); |
| 859 | parms = NULL; |
| 860 | } |
| 861 | free(kvpairs); |
| 862 | kvpairs = NULL; |
| 863 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 864 | |
| 865 | //Get kernel Latency |
| 866 | for (i = MAX_QAF_MODULE_OUT - 1; i >= 0; i--) { |
| 867 | if (qaf_mod->stream_out[i] == NULL) { |
| 868 | continue; |
| 869 | } else { |
| 870 | unsigned int num_fragments = qaf_mod->stream_out[i]->compr_config.fragments; |
| 871 | uint32_t fragment_size = qaf_mod->stream_out[i]->compr_config.fragment_size; |
| 872 | uint32_t kernel_buffer_size = num_fragments * fragment_size; |
| 873 | get_buffer_latency(qaf_mod->stream_out[i], kernel_buffer_size, &kernel_latency); |
| 874 | break; |
| 875 | } |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 876 | } |
| 877 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 878 | //Get DSP latency |
| 879 | if ((qaf_mod->stream_out[QAF_OUT_OFFLOAD] != NULL) |
| 880 | || (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH] != NULL)) { |
| 881 | unsigned int sample_rate = 0; |
| 882 | |
| 883 | if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) |
| 884 | sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate; |
| 885 | else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) |
| 886 | sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate; |
| 887 | |
| 888 | audio_usecase_t platform_latency = |
| 889 | platform_render_latency(qaf_mod->stream_out[QAF_OUT_OFFLOAD]->usecase); |
| 890 | dsp_latency = (platform_latency * sample_rate) / 1000000LL; |
| 891 | } else if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] != NULL) { |
| 892 | unsigned int sample_rate = 0; |
| 893 | |
| 894 | sample_rate = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->sample_rate; //TODO: How this sample rate can be used? |
| 895 | dsp_latency = (COMPRESS_OFFLOAD_PLAYBACK_LATENCY * sample_rate) / 1000; |
| 896 | } |
| 897 | |
| 898 | // MM Module Latency + Kernel Latency + DSP Latency |
| 899 | if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) { |
| 900 | out->platform_latency = module_latency + audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl); |
| 901 | } else { |
| 902 | out->platform_latency = (uint32_t)module_latency + kernel_latency + dsp_latency; |
| 903 | } |
| 904 | |
| 905 | if (out->format & AUDIO_FORMAT_PCM_16_BIT) { |
| 906 | *frames = 0; |
| 907 | signed_frames = out->written - out->platform_latency; |
| 908 | // It would be unusual for this value to be negative, but check just in case ... |
| 909 | if (signed_frames >= 0) { |
| 910 | *frames = signed_frames; |
| 911 | } |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 912 | } else if (qaf_mod->qaf_audio_stream_get_param) { |
| 913 | kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, "position"); |
| 914 | if (kvpairs) { |
| 915 | parms = str_parms_create_str(kvpairs); |
| 916 | ret = str_parms_get_int(parms, "position", &value); |
| 917 | if (ret >= 0) { |
| 918 | *frames = value; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 919 | signed_frames = value - out->platform_latency; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 920 | // It would be unusual for this value to be negative, but check just in case ... |
| 921 | if (signed_frames >= 0) { |
| 922 | *frames = signed_frames; |
| 923 | } |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 924 | } |
| 925 | str_parms_destroy(parms); |
| 926 | } |
| 927 | } else { |
| 928 | ret = -EINVAL; |
| 929 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 930 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 931 | return ret; |
| 932 | } |
| 933 | |
| 934 | static int qaf_out_get_presentation_position(const struct audio_stream_out *stream, |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 935 | uint64_t *frames, |
| 936 | struct timespec *timestamp) |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 937 | { |
| 938 | struct stream_out *out = (struct stream_out *)stream; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 939 | int ret = 0; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 940 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 941 | DEBUG_MSG("Output Stream %p", stream); |
| 942 | |
| 943 | //If QAF passthorugh output stream is active. |
| 944 | if (p_qaf->passthrough_out) { |
| 945 | if (p_qaf->passthrough_in == out) { |
| 946 | //If api is called for QAF passthorugh stream then call the primary HAL api to get the position. |
| 947 | pthread_mutex_lock(&p_qaf->lock); |
| 948 | ret = p_qaf->passthrough_out->stream.get_presentation_position( |
| 949 | (struct audio_stream_out *)p_qaf->passthrough_out, |
| 950 | frames, |
| 951 | timestamp); |
| 952 | pthread_mutex_unlock(&p_qaf->lock); |
| 953 | } else { |
| 954 | //If api is called for other stream then return zero frames. |
| 955 | *frames = 0; |
| 956 | clock_gettime(CLOCK_MONOTONIC, timestamp); |
| 957 | } |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 958 | return ret; |
| 959 | } |
| 960 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 961 | ret = qaf_get_rendered_frames(out, frames); |
| 962 | clock_gettime(CLOCK_MONOTONIC, timestamp); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 963 | |
| 964 | return ret; |
| 965 | } |
| 966 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 967 | /* Pause the QAF module input stream. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 968 | static int qaf_stream_pause(struct stream_out *out) |
| 969 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 970 | struct qaf_module *qaf_mod = NULL; |
| 971 | int ret = -EINVAL; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 972 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 973 | qaf_mod = get_qaf_module_for_input_stream(out); |
| 974 | if (!qaf_mod || !qaf_mod->qaf_audio_stream_pause) { |
| 975 | return -EINVAL; |
| 976 | } |
| 977 | |
| 978 | if (out->qaf_stream_handle) |
| 979 | ret = qaf_mod->qaf_audio_stream_pause(out->qaf_stream_handle); |
| 980 | |
| 981 | return ret; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 982 | } |
| 983 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 984 | /* Pause a QAF input stream. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 985 | static int qaf_out_pause(struct audio_stream_out* stream) |
| 986 | { |
| 987 | struct stream_out *out = (struct stream_out *)stream; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 988 | int status = 0; |
| 989 | DEBUG_MSG("Output Stream %p", out); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 990 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 991 | lock_output_stream(out); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 992 | |
| 993 | //If QAF passthrough is enabled then block the pause on module stream. |
| 994 | if (p_qaf->passthrough_out) { |
| 995 | pthread_mutex_lock(&p_qaf->lock); |
| 996 | //If pause is received for QAF passthorugh stream then call the primary HAL api. |
| 997 | if (p_qaf->passthrough_in == out) { |
| 998 | status = p_qaf->passthrough_out->stream.pause( |
| 999 | (struct audio_stream_out *)p_qaf->passthrough_out); |
| 1000 | out->offload_state = OFFLOAD_STATE_PAUSED; |
| 1001 | } |
| 1002 | pthread_mutex_unlock(&p_qaf->lock); |
| 1003 | } else { |
| 1004 | //Pause the module input stream. |
| 1005 | status = qaf_stream_pause(out); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1006 | } |
| 1007 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1008 | unlock_output_stream(out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1009 | return status; |
| 1010 | } |
| 1011 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1012 | /* Drains a qaf input stream. */ |
| 1013 | static int qaf_out_drain(struct audio_stream_out* stream, audio_drain_type_t type) |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1014 | { |
| 1015 | struct stream_out *out = (struct stream_out *)stream; |
| 1016 | int status = 0; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1017 | DEBUG_MSG("Output Stream %p", out); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1018 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1019 | lock_output_stream(out); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1020 | |
| 1021 | //If QAF passthrough is enabled then block the drain on module stream. |
| 1022 | if (p_qaf->passthrough_out) { |
| 1023 | pthread_mutex_lock(&p_qaf->lock); |
| 1024 | //If drain is received for QAF passthorugh stream then call the primary HAL api. |
| 1025 | if (p_qaf->passthrough_in == out) { |
| 1026 | status = p_qaf->passthrough_out->stream.drain( |
| 1027 | (struct audio_stream_out *)p_qaf->passthrough_out, type); |
| 1028 | } |
| 1029 | pthread_mutex_unlock(&p_qaf->lock); |
| 1030 | } else { |
| 1031 | //Drain the module input stream. |
| 1032 | /* Stream stop will trigger EOS and on EOS_EVENT received |
| 1033 | from callback DRAIN_READY command is sent */ |
| 1034 | status = audio_extn_qaf_stream_stop(out); |
| 1035 | |
| 1036 | if (status == 0) { |
| 1037 | struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out); |
| 1038 | int index = get_input_stream_index(out); |
| 1039 | if (qaf_mod && index >= 0) qaf_mod->drain_received[index] = true; |
| 1040 | } |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1041 | } |
| 1042 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1043 | unlock_output_stream(out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1044 | return status; |
| 1045 | } |
| 1046 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1047 | /* Flush the QAF module input stream. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1048 | static int audio_extn_qaf_stream_flush(struct stream_out *out) |
| 1049 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1050 | DEBUG_MSG("Output Stream %p", out); |
| 1051 | int ret = -EINVAL; |
| 1052 | struct qaf_module *qaf_mod = NULL; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1053 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1054 | qaf_mod = get_qaf_module_for_input_stream(out); |
| 1055 | if ((!qaf_mod) || (!qaf_mod->qaf_audio_stream_flush)) { |
| 1056 | return -EINVAL; |
| 1057 | } |
| 1058 | |
| 1059 | if (out->qaf_stream_handle) |
| 1060 | ret = qaf_mod->qaf_audio_stream_flush(out->qaf_stream_handle); |
| 1061 | |
| 1062 | return ret; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1063 | } |
| 1064 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1065 | /* Flush the QAF input stream. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1066 | static int qaf_out_flush(struct audio_stream_out* stream) |
| 1067 | { |
| 1068 | struct stream_out *out = (struct stream_out *)stream; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1069 | int status = 0; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1070 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1071 | DEBUG_MSG("Output Stream %p", out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1072 | lock_output_stream(out); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1073 | |
| 1074 | //If QAF passthrough is active then block the flush on module input streams. |
| 1075 | if (p_qaf->passthrough_out) { |
| 1076 | pthread_mutex_lock(&p_qaf->lock); |
| 1077 | //If flush is received for the QAF passthrough stream then call the primary HAL api. |
| 1078 | if (p_qaf->passthrough_in == out) { |
| 1079 | status = p_qaf->passthrough_out->stream.flush( |
| 1080 | (struct audio_stream_out *)p_qaf->passthrough_out); |
| 1081 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 1082 | } |
| 1083 | pthread_mutex_unlock(&p_qaf->lock); |
| 1084 | } else { |
| 1085 | //Flush the module input stream. |
| 1086 | status = audio_extn_qaf_stream_flush(out); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1087 | } |
| 1088 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1089 | unlock_output_stream(out); |
| 1090 | DEBUG_MSG("Exit"); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1091 | return status; |
| 1092 | } |
| 1093 | |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1094 | static uint32_t qaf_out_get_latency(const struct audio_stream_out *stream) |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1095 | { |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1096 | struct stream_out *out = (struct stream_out *)stream; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1097 | uint32_t latency = 0; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1098 | struct qaf_module *qaf_mod = NULL; |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1099 | DEBUG_MSG_VV("Output Stream %p", out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1100 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1101 | qaf_mod = get_qaf_module_for_input_stream(out); |
| 1102 | if (!qaf_mod) { |
| 1103 | return 0; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1104 | } |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1105 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1106 | //If QAF passthrough is active then block the get latency on module input streams. |
| 1107 | if (p_qaf->passthrough_out) { |
| 1108 | pthread_mutex_lock(&p_qaf->lock); |
| 1109 | //If get latency is called for the QAF passthrough stream then call the primary HAL api. |
| 1110 | if (p_qaf->passthrough_in == out) { |
| 1111 | latency = p_qaf->passthrough_out->stream.get_latency( |
| 1112 | (struct audio_stream_out *)p_qaf->passthrough_out); |
| 1113 | } |
| 1114 | pthread_mutex_unlock(&p_qaf->lock); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1115 | } else { |
Bharath Gopal | 01310bb | 2016-12-05 15:39:32 +0530 | [diff] [blame] | 1116 | if (is_offload_usecase(out->usecase)) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1117 | latency = COMPRESS_OFFLOAD_PLAYBACK_LATENCY; |
Bharath Gopal | 01310bb | 2016-12-05 15:39:32 +0530 | [diff] [blame] | 1118 | } else { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1119 | uint32_t sample_rate = 0; |
| 1120 | latency = QAF_MODULE_PCM_INPUT_BUFFER_LATENCY; //Input latency |
| 1121 | |
| 1122 | if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) |
| 1123 | sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->sample_rate; |
| 1124 | else if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) |
| 1125 | sample_rate = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->sample_rate; |
| 1126 | |
| 1127 | if (sample_rate) { |
| 1128 | latency += (get_pcm_output_buffer_size_samples(qaf_mod) * 1000) / out->sample_rate; |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) { |
| 1133 | if (is_offload_usecase(out->usecase)) { |
| 1134 | latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) + |
| 1135 | QAF_COMPRESS_OFFLOAD_PROCESSING_LATENCY; |
| 1136 | } else { |
| 1137 | latency = audio_extn_bt_hal_get_latency(qaf_mod->bt_hdl) + |
| 1138 | QAF_PCM_OFFLOAD_PROCESSING_LATENCY; |
| 1139 | } |
Bharath Gopal | 01310bb | 2016-12-05 15:39:32 +0530 | [diff] [blame] | 1140 | } |
| 1141 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1142 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1143 | DEBUG_MSG_VV("Latency %d", latency); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1144 | return latency; |
| 1145 | } |
| 1146 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1147 | static bool check_and_get_compressed_device_format(int device, int *format) |
| 1148 | { |
| 1149 | switch (device) { |
| 1150 | case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DD): |
| 1151 | *format = AUDIO_FORMAT_AC3; |
| 1152 | return true; |
| 1153 | case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_COMPRESSED_OUT_DDP): |
| 1154 | *format = AUDIO_FORMAT_E_AC3; |
| 1155 | return true; |
| 1156 | case (AUDIO_DEVICE_OUT_AUX_DIGITAL | AUDIO_FORMAT_DTS): |
| 1157 | *format = AUDIO_FORMAT_DTS; |
| 1158 | return true; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1159 | default: |
| 1160 | return false; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | /* Call back function for mm module. */ |
| 1165 | static void notify_event_callback(audio_session_handle_t session_handle /*__unused*/, |
| 1166 | void *prv_data, |
| 1167 | void *buf, |
| 1168 | audio_event_id_t event_id, |
| 1169 | int size, |
| 1170 | int device) //TODO: add media format as well. |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1171 | { |
| 1172 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1173 | /* |
| 1174 | For SPKR: |
| 1175 | 1. Open pcm device if device_id passed to it SPKR and write the data to |
| 1176 | pcm device |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1177 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1178 | For HDMI |
| 1179 | 1.Open compress device for HDMI(PCM or AC3) based on current hdmi o/p format and write |
| 1180 | data to the HDMI device. |
| 1181 | */ |
| 1182 | int ret, i; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1183 | audio_output_flags_t flags; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1184 | struct qaf_module* qaf_mod = (struct qaf_module*)prv_data; |
Bharath Gopal | 01310bb | 2016-12-05 15:39:32 +0530 | [diff] [blame] | 1185 | struct audio_stream_out *bt_stream = NULL; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1186 | int format; |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1187 | int8_t *data_buffer_p = NULL; |
| 1188 | uint32_t buffer_size = 0; |
| 1189 | bool need_to_recreate_stream = false; |
| 1190 | struct audio_config config; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1191 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1192 | DEBUG_MSG_VV("Device 0x%X, Event = 0x%X, Bytes to write %d", device, event_id, size); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1193 | pthread_mutex_lock(&p_qaf->lock); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1194 | |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1195 | /* Default config initialization. */ |
| 1196 | config.sample_rate = config.offload_info.sample_rate = QAF_OUTPUT_SAMPLING_RATE; |
| 1197 | config.offload_info.version = AUDIO_INFO_INITIALIZER.version; |
| 1198 | config.offload_info.size = AUDIO_INFO_INITIALIZER.size; |
| 1199 | config.format = config.offload_info.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1200 | config.offload_info.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH; |
| 1201 | config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1202 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1203 | if (event_id == AUDIO_DATA_EVENT) { |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1204 | data_buffer_p = (int8_t*)buf; |
| 1205 | buffer_size = size; |
| 1206 | } else if (event_id == AUDIO_DATA_EVENT_V2) { |
| 1207 | audio_qaf_out_buffer_t *buf_payload = (audio_qaf_out_buffer_t*)buf; |
| 1208 | int index = -1; |
| 1209 | audio_qaf_media_format_t *p_cached_fmt = NULL; |
| 1210 | |
| 1211 | if (size < sizeof(audio_qaf_out_buffer_t)) { |
| 1212 | ERROR_MSG("AUDIO_DATA_EVENT_V2 payload size is not sufficient."); |
| 1213 | return; |
| 1214 | } |
| 1215 | |
| 1216 | data_buffer_p = (int8_t*)buf_payload->data + buf_payload->offset; |
| 1217 | buffer_size = buf_payload->size - buf_payload->offset; |
| 1218 | |
| 1219 | index = get_media_fmt_array_index_for_output_id(qaf_mod, buf_payload->output_id); |
| 1220 | |
| 1221 | if (index < 0) { |
| 1222 | /*If media format is not received then switch to default values.*/ |
| 1223 | event_id = AUDIO_DATA_EVENT; |
| 1224 | } else { |
| 1225 | p_cached_fmt = &qaf_mod->out_stream_fmt[index]; |
| 1226 | need_to_recreate_stream = qaf_mod->is_media_fmt_changed[index]; |
| 1227 | qaf_mod->is_media_fmt_changed[index] = false; |
| 1228 | |
| 1229 | config.sample_rate = config.offload_info.sample_rate = p_cached_fmt->sample_rate; |
| 1230 | config.offload_info.version = AUDIO_INFO_INITIALIZER.version; |
| 1231 | config.offload_info.size = AUDIO_INFO_INITIALIZER.size; |
| 1232 | config.format = config.offload_info.format = p_cached_fmt->format; |
| 1233 | config.offload_info.bit_width = p_cached_fmt->bit_width; |
| 1234 | |
| 1235 | if (p_cached_fmt->format == AUDIO_FORMAT_PCM) { |
| 1236 | if (p_cached_fmt->bit_width == 16) |
| 1237 | config.format = config.offload_info.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1238 | else if (p_cached_fmt->bit_width == 24) |
| 1239 | config.format = config.offload_info.format = AUDIO_FORMAT_PCM_24_BIT_PACKED; |
| 1240 | else |
| 1241 | config.format = config.offload_info.format = AUDIO_FORMAT_PCM_32_BIT; |
| 1242 | } |
| 1243 | |
| 1244 | device |= (p_cached_fmt->format & AUDIO_FORMAT_MAIN_MASK); |
| 1245 | |
| 1246 | switch (p_cached_fmt->channels) { |
| 1247 | case 1: |
| 1248 | config.offload_info.channel_mask = config.channel_mask = |
| 1249 | AUDIO_CHANNEL_OUT_MONO; |
| 1250 | break; |
| 1251 | case 2: |
| 1252 | config.offload_info.channel_mask = config.channel_mask = |
| 1253 | AUDIO_CHANNEL_OUT_STEREO; |
| 1254 | break; |
| 1255 | case 6: |
| 1256 | config.offload_info.channel_mask = config.channel_mask = |
| 1257 | AUDIO_CHANNEL_OUT_5POINT1; |
| 1258 | break; |
| 1259 | case 8: |
| 1260 | config.offload_info.channel_mask = config.channel_mask = |
| 1261 | AUDIO_CHANNEL_OUT_7POINT1; |
| 1262 | break; |
| 1263 | default: |
| 1264 | config.offload_info.channel_mask = config.channel_mask = |
| 1265 | AUDIO_CHANNEL_OUT_5POINT1; |
| 1266 | break; |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | if (event_id == AUDIO_OUTPUT_MEDIA_FORMAT_EVENT) { |
| 1272 | audio_qaf_media_format_t *p_fmt = (audio_qaf_media_format_t*)buf; |
| 1273 | audio_qaf_media_format_t *p_cached_fmt = NULL; |
| 1274 | int index = -1; |
| 1275 | |
| 1276 | if (size < sizeof(audio_qaf_media_format_t)) { |
| 1277 | ERROR_MSG("Size is not proper for the event AUDIO_OUTPUT_MEDIA_FORMAT_EVENT."); |
| 1278 | return ; |
| 1279 | } |
| 1280 | |
| 1281 | index = get_media_fmt_array_index_for_output_id(qaf_mod, p_fmt->output_id); |
| 1282 | |
| 1283 | if (index >= 0) { |
| 1284 | p_cached_fmt = &qaf_mod->out_stream_fmt[index]; |
| 1285 | } else if (index < 0 && qaf_mod->new_out_format_index < MAX_QAF_MODULE_OUT) { |
| 1286 | index = qaf_mod->new_out_format_index; |
| 1287 | p_cached_fmt = &qaf_mod->out_stream_fmt[index]; |
| 1288 | qaf_mod->new_out_format_index++; |
| 1289 | } |
| 1290 | |
| 1291 | if (p_cached_fmt == NULL) { |
| 1292 | ERROR_MSG("Maximum output from a QAF module is reached. Can not process new output."); |
| 1293 | return ; |
| 1294 | } |
| 1295 | |
| 1296 | if (memcmp(p_cached_fmt, p_fmt, sizeof(audio_qaf_media_format_t)) != 0) { |
| 1297 | memcpy(p_cached_fmt, p_fmt, sizeof(audio_qaf_media_format_t)); |
| 1298 | qaf_mod->is_media_fmt_changed[index] = true; |
| 1299 | } |
| 1300 | } else if (event_id == AUDIO_DATA_EVENT || event_id == AUDIO_DATA_EVENT_V2) { |
| 1301 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1302 | if (p_qaf->passthrough_out != NULL) { |
| 1303 | //If QAF passthrough is active then all the module output will be dropped. |
| 1304 | pthread_mutex_unlock(&p_qaf->lock); |
| 1305 | DEBUG_MSG("QAF-PSTH is active, DROPPING DATA!"); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1306 | return; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | if (check_and_get_compressed_device_format(device, &format)) { |
| 1310 | /* |
| 1311 | * CASE 1: Transcoded output of mm module. |
| 1312 | * If HDMI is not connected then drop the data. |
| 1313 | * Only one HDMI output can be supported from all the mm modules of QAF. |
| 1314 | * Multi-Channel PCM HDMI output streams will be closed from all the mm modules. |
| 1315 | * If transcoded output of other module is already enabled then this data will be dropped. |
| 1316 | */ |
| 1317 | |
| 1318 | if (!p_qaf->hdmi_connect) { |
| 1319 | DEBUG_MSG("HDMI not connected, DROPPING DATA!"); |
| 1320 | pthread_mutex_unlock(&p_qaf->lock); |
| 1321 | return; |
| 1322 | } |
| 1323 | |
| 1324 | //Closing all the PCM HDMI output stream from QAF. |
| 1325 | close_all_pcm_hdmi_output(); |
| 1326 | |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1327 | /* If Media format was changed for this stream then need to re-create the stream. */ |
| 1328 | if (need_to_recreate_stream && qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) { |
| 1329 | adev_close_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 1330 | (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH])); |
| 1331 | qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH] = NULL; |
| 1332 | p_qaf->passthrough_enabled = false; |
| 1333 | } |
| 1334 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1335 | if (!p_qaf->passthrough_enabled |
| 1336 | && !(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH])) { |
| 1337 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1338 | audio_devices_t devices; |
| 1339 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1340 | config.format = config.offload_info.format = format; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1341 | config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_5POINT1; |
| 1342 | |
| 1343 | flags = (AUDIO_OUTPUT_FLAG_NON_BLOCKING |
| 1344 | | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
| 1345 | | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1346 | devices = AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 1347 | |
| 1348 | ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 1349 | QAF_DEFAULT_COMPR_PASSTHROUGH_HANDLE, |
| 1350 | devices, |
| 1351 | flags, |
| 1352 | &config, |
| 1353 | (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]), |
| 1354 | NULL); |
| 1355 | if (ret < 0) { |
| 1356 | ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret); |
| 1357 | pthread_mutex_unlock(&p_qaf->lock); |
| 1358 | return; |
| 1359 | } |
| 1360 | |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1361 | if (format & AUDIO_FORMAT_E_AC3) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1362 | qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragment_size = |
| 1363 | COMPRESS_PASSTHROUGH_DDP_FRAGMENT_SIZE; |
| 1364 | } |
| 1365 | qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->compr_config.fragments = |
| 1366 | COMPRESS_OFFLOAD_NUM_FRAGMENTS; |
| 1367 | |
| 1368 | p_qaf->passthrough_enabled = true; |
| 1369 | } |
| 1370 | |
| 1371 | if (qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]) { |
| 1372 | ret = qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH]->stream.write( |
| 1373 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_TRANSCODE_PASSTHROUGH], |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1374 | data_buffer_p, |
| 1375 | buffer_size); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1376 | } |
| 1377 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1378 | else if ((device & AUDIO_DEVICE_OUT_AUX_DIGITAL) |
| 1379 | && (p_qaf->hdmi_connect) |
| 1380 | && (p_qaf->hdmi_sink_channels > 2)) { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1381 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1382 | /* CASE 2: Multi-Channel PCM output to HDMI. |
| 1383 | * If any other HDMI output is already enabled then this has to be dropped. |
| 1384 | */ |
| 1385 | bool create_mch_out_stream = false; |
| 1386 | |
| 1387 | if (p_qaf->passthrough_enabled) { |
| 1388 | //Closing all the multi-Channel PCM HDMI output stream from QAF. |
| 1389 | close_all_pcm_hdmi_output(); |
| 1390 | |
| 1391 | //If passthrough is active then pcm hdmi output has to be dropped. |
| 1392 | pthread_mutex_unlock(&p_qaf->lock); |
| 1393 | DEBUG_MSG("Compressed passthrough enabled, DROPPING DATA!"); |
| 1394 | return; |
| 1395 | } |
| 1396 | |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1397 | /* If Media format was changed for this stream then need to re-create the stream. */ |
| 1398 | if (need_to_recreate_stream && qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) { |
| 1399 | adev_close_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 1400 | (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])); |
| 1401 | qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH] = NULL; |
| 1402 | p_qaf->mch_pcm_hdmi_enabled = false; |
| 1403 | } |
| 1404 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1405 | if (!p_qaf->mch_pcm_hdmi_enabled && !(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH])) { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1406 | audio_devices_t devices; |
| 1407 | |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1408 | if (event_id == AUDIO_DATA_EVENT) { |
| 1409 | config.offload_info.format = config.format = AUDIO_FORMAT_PCM_16_BIT; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1410 | |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1411 | if (p_qaf->hdmi_sink_channels == 8) { |
| 1412 | config.offload_info.channel_mask = config.channel_mask = |
| 1413 | AUDIO_CHANNEL_OUT_7POINT1; |
| 1414 | } else if (p_qaf->hdmi_sink_channels == 6) { |
| 1415 | config.offload_info.channel_mask = config.channel_mask = |
| 1416 | AUDIO_CHANNEL_OUT_5POINT1; |
| 1417 | } else { |
| 1418 | config.offload_info.channel_mask = config.channel_mask = |
| 1419 | AUDIO_CHANNEL_OUT_STEREO; |
| 1420 | } |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1421 | } |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1422 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1423 | devices = AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 1424 | flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM); |
| 1425 | |
| 1426 | ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 1427 | QAF_DEFAULT_COMPR_AUDIO_HANDLE, |
| 1428 | devices, |
| 1429 | flags, |
| 1430 | &config, |
| 1431 | (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]), |
| 1432 | NULL); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1433 | if (ret < 0) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1434 | ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret); |
| 1435 | pthread_mutex_unlock(&p_qaf->lock); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1436 | return; |
| 1437 | } |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1438 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1439 | qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragments = |
| 1440 | COMPRESS_OFFLOAD_NUM_FRAGMENTS; |
| 1441 | qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->compr_config.fragment_size = |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1442 | qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1443 | |
| 1444 | p_qaf->mch_pcm_hdmi_enabled = true; |
| 1445 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1446 | if ((qaf_mod->stream_in[QAF_IN_MAIN] |
| 1447 | && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) || |
| 1448 | (qaf_mod->stream_in[QAF_IN_MAIN_2] |
| 1449 | && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1450 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1451 | if (qaf_mod->stream_in[QAF_IN_MAIN]) { |
| 1452 | qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback( |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1453 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], |
| 1454 | qaf_mod->stream_in[QAF_IN_MAIN]->client_callback, |
| 1455 | qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie); |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1456 | } |
| 1457 | if (qaf_mod->stream_in[QAF_IN_MAIN_2]) { |
| 1458 | qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback( |
| 1459 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], |
| 1460 | qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback, |
| 1461 | qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie); |
| 1462 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1463 | } else if (qaf_mod->stream_in[QAF_IN_PCM] |
| 1464 | && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) { |
| 1465 | |
| 1466 | qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.set_callback( |
| 1467 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], |
| 1468 | qaf_mod->stream_in[QAF_IN_PCM]->client_callback, |
| 1469 | qaf_mod->stream_in[QAF_IN_PCM]->client_cookie); |
| 1470 | } |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1471 | |
| 1472 | int index = -1; |
| 1473 | if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid) |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1474 | index = (int) QAF_IN_MAIN; |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1475 | else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid) |
| 1476 | index = (int) QAF_IN_MAIN_2; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1477 | else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid) |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1478 | index = (int) QAF_IN_PCM; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1479 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1480 | if (index >= 0) { |
| 1481 | if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->standby) |
| 1482 | qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write( |
| 1483 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], NULL, 0); |
| 1484 | |
| 1485 | lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1486 | ret = audio_extn_out_set_param_data( |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1487 | qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], |
| 1488 | AUDIO_EXTN_PARAM_ADSP_STREAM_CMD, |
| 1489 | (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params); |
| 1490 | unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1491 | |
| 1492 | } |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1493 | } |
| 1494 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1495 | if (qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]) { |
| 1496 | ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH]->stream.write( |
| 1497 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD_MCH], |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1498 | data_buffer_p, |
| 1499 | buffer_size); |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 1500 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1501 | } |
| 1502 | else { |
| 1503 | /* CASE 3: PCM output. |
| 1504 | */ |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1505 | |
| 1506 | /* If Media format was changed for this stream then need to re-create the stream. */ |
| 1507 | if (need_to_recreate_stream && qaf_mod->stream_out[QAF_OUT_OFFLOAD]) { |
| 1508 | adev_close_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 1509 | (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD])); |
| 1510 | qaf_mod->stream_out[QAF_OUT_OFFLOAD] = NULL; |
| 1511 | } |
| 1512 | |
Bharath Gopal | 01310bb | 2016-12-05 15:39:32 +0530 | [diff] [blame] | 1513 | bt_stream = audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl); |
| 1514 | if (bt_stream != NULL) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1515 | if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) { |
| 1516 | adev_close_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 1517 | (struct audio_stream_out *)(qaf_mod->stream_out[QAF_OUT_OFFLOAD])); |
| 1518 | qaf_mod->stream_out[QAF_OUT_OFFLOAD] = NULL; |
Bharath Gopal | 01310bb | 2016-12-05 15:39:32 +0530 | [diff] [blame] | 1519 | } |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1520 | |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1521 | audio_extn_bt_hal_out_write(p_qaf->bt_hdl, data_buffer_p, buffer_size); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1522 | } else if (NULL == qaf_mod->stream_out[QAF_OUT_OFFLOAD]) { |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1523 | audio_devices_t devices; |
| 1524 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1525 | if (qaf_mod->stream_in[QAF_IN_MAIN]) |
| 1526 | devices = qaf_mod->stream_in[QAF_IN_MAIN]->devices; |
| 1527 | else |
| 1528 | devices = qaf_mod->stream_in[QAF_IN_PCM]->devices; |
| 1529 | |
| 1530 | //If multi channel pcm or passthrough is already enabled then remove the hdmi flag from device. |
| 1531 | if (p_qaf->mch_pcm_hdmi_enabled || p_qaf->passthrough_enabled) { |
| 1532 | if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) |
| 1533 | devices ^= AUDIO_DEVICE_OUT_AUX_DIGITAL; |
| 1534 | } |
| 1535 | if (devices == 0) { |
| 1536 | devices = device; |
| 1537 | } |
| 1538 | |
| 1539 | flags = (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_DIRECT_PCM); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1540 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1541 | /* TODO:: Need to Propagate errors to framework */ |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1542 | ret = adev_open_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 1543 | QAF_DEFAULT_COMPR_AUDIO_HANDLE, |
| 1544 | devices, |
| 1545 | flags, |
| 1546 | &config, |
| 1547 | (struct audio_stream_out **)&(qaf_mod->stream_out[QAF_OUT_OFFLOAD]), |
| 1548 | NULL); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1549 | if (ret < 0) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1550 | ERROR_MSG("adev_open_output_stream failed with ret = %d!", ret); |
| 1551 | pthread_mutex_unlock(&p_qaf->lock); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1552 | return; |
| 1553 | } |
Deepak Agarwal | 1e42b85 | 2017-02-11 17:57:04 +0530 | [diff] [blame] | 1554 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1555 | if ((qaf_mod->stream_in[QAF_IN_MAIN] |
| 1556 | && qaf_mod->stream_in[QAF_IN_MAIN]->client_callback != NULL) || |
| 1557 | (qaf_mod->stream_in[QAF_IN_MAIN_2] |
| 1558 | && qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback != NULL)) { |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1559 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1560 | if (qaf_mod->stream_in[QAF_IN_MAIN]) { |
| 1561 | qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback( |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1562 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], |
| 1563 | qaf_mod->stream_in[QAF_IN_MAIN]->client_callback, |
| 1564 | qaf_mod->stream_in[QAF_IN_MAIN]->client_cookie); |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1565 | } |
| 1566 | if (qaf_mod->stream_in[QAF_IN_MAIN_2]) { |
| 1567 | qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback( |
| 1568 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], |
| 1569 | qaf_mod->stream_in[QAF_IN_MAIN_2]->client_callback, |
| 1570 | qaf_mod->stream_in[QAF_IN_MAIN_2]->client_cookie); |
| 1571 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1572 | } else if (qaf_mod->stream_in[QAF_IN_PCM] |
| 1573 | && qaf_mod->stream_in[QAF_IN_PCM]->client_callback != NULL) { |
| 1574 | |
| 1575 | qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_callback( |
| 1576 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], |
| 1577 | qaf_mod->stream_in[QAF_IN_PCM]->client_callback, |
| 1578 | qaf_mod->stream_in[QAF_IN_PCM]->client_cookie); |
| 1579 | } |
| 1580 | |
| 1581 | qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragments = |
| 1582 | COMPRESS_OFFLOAD_NUM_FRAGMENTS; |
| 1583 | qaf_mod->stream_out[QAF_OUT_OFFLOAD]->compr_config.fragment_size = |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1584 | qaf_get_pcm_offload_output_buffer_size(qaf_mod, &config.offload_info); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1585 | |
| 1586 | if (qaf_mod->is_vol_set) { |
| 1587 | DEBUG_MSG("Setting Volume Left[%f], Right[%f]", qaf_mod->vol_left, qaf_mod->vol_right); |
| 1588 | qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.set_volume( |
| 1589 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], |
| 1590 | qaf_mod->vol_left, |
| 1591 | qaf_mod->vol_right); |
| 1592 | } |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1593 | |
| 1594 | int index = -1; |
| 1595 | if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN].adsp_hdlr_config_valid) |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1596 | index = (int) QAF_IN_MAIN; |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1597 | else if (qaf_mod->adsp_hdlr_config[QAF_IN_MAIN_2].adsp_hdlr_config_valid) |
| 1598 | index = (int) QAF_IN_MAIN_2; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1599 | else if (qaf_mod->adsp_hdlr_config[QAF_IN_PCM].adsp_hdlr_config_valid) |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1600 | index = (int) QAF_IN_PCM; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1601 | if (index >= 0) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1602 | if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]->standby) { |
| 1603 | qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write( |
| 1604 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], NULL, 0); |
| 1605 | } |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1606 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1607 | lock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1608 | ret = audio_extn_out_set_param_data( |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1609 | qaf_mod->stream_out[QAF_OUT_OFFLOAD], |
| 1610 | AUDIO_EXTN_PARAM_ADSP_STREAM_CMD, |
| 1611 | (audio_extn_param_payload *)&qaf_mod->adsp_hdlr_config[index].event_params); |
| 1612 | unlock_output_stream(qaf_mod->stream_out[QAF_OUT_OFFLOAD]); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1613 | } |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | /* |
| 1617 | * TODO:: Since this is mixed data, |
| 1618 | * need to identify to which stream the error should be sent |
| 1619 | */ |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1620 | if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) { |
| 1621 | ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.write( |
| 1622 | (struct audio_stream_out *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1623 | data_buffer_p, |
| 1624 | buffer_size); |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 1625 | } |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1626 | } |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1627 | DEBUG_MSG_VV("Bytes written = %d", ret); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1628 | } |
| 1629 | else if (event_id == AUDIO_EOS_MAIN_DD_DDP_EVENT |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1630 | || event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 1631 | || event_id == AUDIO_EOS_MAIN_AAC_EVENT |
| 1632 | || event_id == AUDIO_EOS_MAIN_AC4_EVENT |
| 1633 | || event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1634 | struct stream_out *out = qaf_mod->stream_in[QAF_IN_MAIN]; |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1635 | struct stream_out *out_main2 = qaf_mod->stream_in[QAF_IN_MAIN_2]; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1636 | struct stream_out *out_assoc = qaf_mod->stream_in[QAF_IN_ASSOC]; |
| 1637 | bool *main_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN]; |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1638 | bool *main2_drain_received = &qaf_mod->drain_received[QAF_IN_MAIN_2]; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1639 | bool *assoc_drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC]; |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 1640 | |
| 1641 | /** |
| 1642 | * TODO:: Only DD/DDP Associate Eos is handled, need to add support |
| 1643 | * for other formats. |
| 1644 | */ |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1645 | if (event_id == AUDIO_EOS_ASSOC_DD_DDP_EVENT |
| 1646 | && (out_assoc != NULL) |
| 1647 | && (*assoc_drain_received)) { |
| 1648 | |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 1649 | lock_output_stream(out_assoc); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1650 | out_assoc->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_assoc->client_cookie); |
| 1651 | *assoc_drain_received = false; |
| 1652 | unlock_output_stream(out_assoc); |
| 1653 | DEBUG_MSG("sent associated DRAIN_READY"); |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1654 | } else if (event_id == AUDIO_EOS_MAIN_2_DD_DDP_EVENT |
| 1655 | && (out_main2 != NULL) |
| 1656 | && (*main2_drain_received)) { |
| 1657 | |
| 1658 | lock_output_stream(out_main2); |
| 1659 | out_main2->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out_main2->client_cookie); |
| 1660 | *main2_drain_received = false; |
| 1661 | unlock_output_stream(out_main2); |
| 1662 | DEBUG_MSG("sent main2 DRAIN_READY"); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1663 | } else if ((out != NULL) && (*main_drain_received)) { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1664 | lock_output_stream(out); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 1665 | out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1666 | *main_drain_received = false; |
| 1667 | unlock_output_stream(out); |
| 1668 | DEBUG_MSG("sent main DRAIN_READY"); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1669 | } |
| 1670 | } |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1671 | else if (event_id == AUDIO_MAIN_EOS_EVENT || event_id == AUDIO_ASSOC_EOS_EVENT) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1672 | struct stream_out *out = NULL; |
| 1673 | bool *drain_received = NULL; |
| 1674 | |
| 1675 | if (event_id == AUDIO_MAIN_EOS_EVENT) { |
| 1676 | out = qaf_mod->stream_in[QAF_IN_MAIN]; |
| 1677 | drain_received = &qaf_mod->drain_received[QAF_IN_MAIN]; |
| 1678 | } else { |
| 1679 | out = qaf_mod->stream_in[QAF_IN_ASSOC]; |
| 1680 | drain_received = &qaf_mod->drain_received[QAF_IN_ASSOC]; |
| 1681 | } |
| 1682 | |
| 1683 | if ((out != NULL) && (*drain_received)) { |
| 1684 | lock_output_stream(out); |
| 1685 | out->client_callback(STREAM_CBK_EVENT_DRAIN_READY, NULL, out->client_cookie); |
| 1686 | *drain_received = false; |
| 1687 | unlock_output_stream(out); |
| 1688 | DEBUG_MSG("sent DRAIN_READY"); |
| 1689 | } |
| 1690 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1691 | |
| 1692 | pthread_mutex_unlock(&p_qaf->lock); |
| 1693 | return; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1694 | } |
| 1695 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1696 | /* Close the mm module session. */ |
| 1697 | static int qaf_session_close(struct qaf_module* qaf_mod) |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1698 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1699 | int j; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1700 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1701 | //Check if all streams are closed or not. |
| 1702 | for (j = 0; j < MAX_QAF_MODULE_IN; j++) { |
| 1703 | if (qaf_mod->stream_in[j] != NULL) { |
| 1704 | break; |
| 1705 | } |
| 1706 | } |
| 1707 | if (j != MAX_QAF_MODULE_IN) { |
| 1708 | return 0; //Some stream is already active, Can not close session. |
| 1709 | } |
| 1710 | |
| 1711 | if (qaf_mod->session_handle != NULL && qaf_mod->qaf_audio_session_close) { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1712 | qaf_mod->qaf_audio_session_close(qaf_mod->session_handle); |
| 1713 | qaf_mod->session_handle = NULL; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1714 | qaf_mod->is_vol_set = false; |
| 1715 | memset(qaf_mod->drain_received, 0, sizeof(qaf_mod->drain_received)); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1716 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1717 | |
| 1718 | for (j = 0; j < MAX_QAF_MODULE_OUT; j++) { |
| 1719 | if (qaf_mod->stream_out[j]) { |
| 1720 | adev_close_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 1721 | (struct audio_stream_out *)(qaf_mod->stream_out[j])); |
| 1722 | qaf_mod->stream_out[j] = NULL; |
| 1723 | } |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1724 | memset(&qaf_mod->out_stream_fmt[j], 0, sizeof(audio_qaf_media_format_t)); |
| 1725 | qaf_mod->is_media_fmt_changed[j] = false; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1726 | } |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1727 | qaf_mod->new_out_format_index = 0; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1728 | |
| 1729 | DEBUG_MSG("Session Closed."); |
| 1730 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1731 | return 0; |
| 1732 | } |
| 1733 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1734 | /* Close the stream of QAF module. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1735 | static int qaf_stream_close(struct stream_out *out) |
| 1736 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1737 | int ret = -EINVAL; |
| 1738 | struct qaf_module *qaf_mod = NULL; |
| 1739 | int index = -1; |
| 1740 | DEBUG_MSG("Flag [0x%x], Stream handle [%p]", out->flags, out->qaf_stream_handle); |
| 1741 | |
| 1742 | qaf_mod = get_qaf_module_for_input_stream(out); |
| 1743 | index = get_input_stream_index(out); |
| 1744 | |
| 1745 | if (!qaf_mod || !qaf_mod->qaf_audio_stream_close || index < 0) { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1746 | return -EINVAL; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | qaf_mod->stream_in[index] = NULL; |
| 1750 | memset(&qaf_mod->adsp_hdlr_config[index], 0, sizeof(struct qaf_adsp_hdlr_config_state)); |
| 1751 | |
| 1752 | lock_output_stream(out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1753 | if (out->qaf_stream_handle) { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1754 | ret = qaf_mod->qaf_audio_stream_close(out->qaf_stream_handle); |
| 1755 | out->qaf_stream_handle = NULL; |
| 1756 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1757 | unlock_output_stream(out); |
| 1758 | |
| 1759 | //If all streams are closed then close the session. |
| 1760 | qaf_session_close(qaf_mod); |
| 1761 | |
| 1762 | DEBUG_MSG(); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1763 | return ret; |
| 1764 | } |
| 1765 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1766 | /* Open a MM module session with QAF. */ |
| 1767 | static int audio_extn_qaf_session_open(mm_module_type mod_type) |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1768 | { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1769 | ALOGV("%s %d", __func__, __LINE__); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1770 | unsigned char* license_data = NULL; |
| 1771 | device_license_config_t lic_config = {0}; |
| 1772 | int ret = -ENOSYS, size = 0; |
| 1773 | char value[PROPERTY_VALUE_MAX] = {0}; |
| 1774 | struct qaf_module *qaf_mod = NULL; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1775 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1776 | if (mod_type >= MAX_MM_MODULE_TYPE || !(p_qaf->qaf_mod[mod_type].qaf_audio_session_open)) |
| 1777 | return -ENOTSUP; //Not supported by QAF module. |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1778 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1779 | pthread_mutex_lock(&p_qaf->lock); |
| 1780 | |
| 1781 | qaf_mod = &(p_qaf->qaf_mod[mod_type]); |
| 1782 | |
| 1783 | //If session is already opened then return. |
| 1784 | if (qaf_mod->session_handle) { |
| 1785 | DEBUG_MSG("Session is already opened."); |
| 1786 | pthread_mutex_unlock(&p_qaf->lock); |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
| 1790 | if (mod_type == MS12) { |
| 1791 | //Getting the license |
| 1792 | license_data = platform_get_license((struct audio_hw_device *)(p_qaf->adev->platform), |
| 1793 | &size); |
| 1794 | if (!license_data) { |
| 1795 | ERROR_MSG("License data is not present."); |
| 1796 | pthread_mutex_unlock(&p_qaf->lock); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1797 | return -EINVAL; |
| 1798 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1799 | |
| 1800 | lic_config.p_license = (unsigned char*)calloc(1, size); |
| 1801 | if (lic_config.p_license == NULL) { |
| 1802 | ERROR_MSG("Out of Memory"); |
| 1803 | ret = -ENOMEM; |
| 1804 | goto exit; |
| 1805 | } |
| 1806 | |
| 1807 | lic_config.l_size = size; |
| 1808 | memcpy(lic_config.p_license, license_data, size); |
| 1809 | |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 1810 | if (property_get("vendor.audio.qaf.manufacturer", value, "") && atoi(value)) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1811 | lic_config.manufacturer_id = (unsigned long)atoi(value); |
| 1812 | } else { |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 1813 | ERROR_MSG("vendor.audio.qaf.manufacturer id is not set"); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1814 | ret = -EINVAL; |
| 1815 | goto exit; |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | ret = qaf_mod->qaf_audio_session_open(&qaf_mod->session_handle, |
Sri Karri | 1fbfef5 | 2017-06-29 19:45:47 +0530 | [diff] [blame] | 1820 | AUDIO_SESSION_BROADCAST, |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1821 | (void *)(qaf_mod), |
| 1822 | (void *)&lic_config); |
| 1823 | if (ret < 0) { |
| 1824 | ERROR_MSG("Error in session open %d", ret); |
| 1825 | goto exit; |
| 1826 | } |
| 1827 | |
| 1828 | if (qaf_mod->session_handle == NULL) { |
| 1829 | ERROR_MSG("Session handle is NULL."); |
| 1830 | ret = -ENOMEM; |
| 1831 | goto exit; |
| 1832 | } |
| 1833 | |
| 1834 | if (qaf_mod->qaf_register_event_callback) |
| 1835 | qaf_mod->qaf_register_event_callback(qaf_mod->session_handle, |
| 1836 | qaf_mod, |
| 1837 | ¬ify_event_callback, |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 1838 | AUDIO_DATA_EVENT_V2); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1839 | |
| 1840 | set_hdmi_configuration_to_module(); |
| 1841 | |
| 1842 | exit: |
| 1843 | if (license_data != NULL) { |
| 1844 | free(license_data); |
| 1845 | license_data = NULL; |
| 1846 | } |
| 1847 | if (lic_config.p_license != NULL) { |
| 1848 | free(lic_config.p_license); |
| 1849 | lic_config.p_license = NULL; |
| 1850 | } |
| 1851 | |
| 1852 | pthread_mutex_unlock(&p_qaf->lock); |
| 1853 | return ret; |
| 1854 | } |
| 1855 | |
| 1856 | /* opens a stream in QAF module. */ |
| 1857 | static int qaf_stream_open(struct stream_out *out, |
| 1858 | struct audio_config *config, |
| 1859 | audio_output_flags_t flags, |
| 1860 | audio_devices_t devices) |
| 1861 | { |
| 1862 | int status = -EINVAL; |
| 1863 | mm_module_type mmtype = get_mm_module_for_format(config->format); |
| 1864 | struct qaf_module* qaf_mod = NULL; |
| 1865 | DEBUG_MSG("Flags 0x%x, Device 0x%x", flags, devices); |
| 1866 | |
| 1867 | if (mmtype >= MAX_MM_MODULE_TYPE |
| 1868 | || p_qaf->qaf_mod[mmtype].qaf_audio_session_open == NULL |
| 1869 | || p_qaf->qaf_mod[mmtype].qaf_audio_stream_open == NULL) { |
| 1870 | ERROR_MSG("Unsupported Stream"); |
| 1871 | return -ENOTSUP; |
| 1872 | } |
| 1873 | |
| 1874 | //Open the module session, if not opened already. |
| 1875 | status = audio_extn_qaf_session_open(mmtype); |
| 1876 | qaf_mod = &(p_qaf->qaf_mod[mmtype]); |
| 1877 | |
| 1878 | if ((status != 0) || (qaf_mod->session_handle == NULL)) { |
| 1879 | ERROR_MSG("Failed to open session."); |
| 1880 | return status; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1881 | } |
| 1882 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1883 | audio_stream_config_t input_config; |
| 1884 | input_config.sample_rate = config->sample_rate; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 1885 | input_config.channels = popcount(config->channel_mask); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1886 | input_config.format = config->format; |
| 1887 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1888 | if (input_config.format != AUDIO_FORMAT_PCM_16_BIT) { |
| 1889 | input_config.format &= AUDIO_FORMAT_MAIN_MASK; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1890 | } |
| 1891 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1892 | DEBUG_MSG("stream_open sample_rate(%d) channels(%d) devices(%#x) flags(%#x) format(%#x)", |
| 1893 | input_config.sample_rate, input_config.channels, devices, flags, input_config.format); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1894 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1895 | if (input_config.format == AUDIO_FORMAT_PCM_16_BIT) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1896 | //If PCM stream is already opened then fail this stream open. |
| 1897 | if (qaf_mod->stream_in[QAF_IN_PCM]) { |
| 1898 | ERROR_MSG("PCM input is already active."); |
| 1899 | return -ENOTSUP; |
| 1900 | } |
| 1901 | |
| 1902 | //TODO: Flag can be system tone or external associated PCM. |
| 1903 | status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, |
| 1904 | &out->qaf_stream_handle, |
| 1905 | input_config, |
| 1906 | devices, |
| 1907 | AUDIO_STREAM_SYSTEM_TONE); |
| 1908 | qaf_mod->stream_in[QAF_IN_PCM] = out; |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1909 | } else if ((flags & AUDIO_OUTPUT_FLAG_MAIN) && (flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) { |
| 1910 | if (is_main_active(qaf_mod) || is_dual_main_active(qaf_mod)) { |
| 1911 | ERROR_MSG("Dual Main or Main already active. So, Cannot open main and associated stream"); |
| 1912 | return -EINVAL; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1913 | } else { |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1914 | status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN); |
| 1915 | if (status == 0) { |
| 1916 | 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); |
| 1917 | qaf_mod->stream_in[QAF_IN_MAIN] = out; |
| 1918 | } else { |
| 1919 | ERROR_MSG("Stream Open FAILED !!!"); |
| 1920 | } |
| 1921 | } |
Naresh Tanniru | 908d9a0 | 2017-05-17 14:12:48 +0530 | [diff] [blame] | 1922 | } else if ((flags & AUDIO_OUTPUT_FLAG_MAIN) || ((!(flags & AUDIO_OUTPUT_FLAG_MAIN)) && (!(flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)))) { |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1923 | /* Assume Main if no flag is set */ |
| 1924 | if (is_dual_main_active(qaf_mod)) { |
| 1925 | ERROR_MSG("Dual Main already active. So, Cannot open main stream"); |
| 1926 | return -EINVAL; |
| 1927 | } else if (is_main_active(qaf_mod) && qaf_mod->stream_in[QAF_IN_ASSOC]) { |
| 1928 | ERROR_MSG("Main and Associated already active. So, Cannot open main stream"); |
| 1929 | return -EINVAL; |
| 1930 | } else if (is_main_active(qaf_mod) && (mmtype != MS12)) { |
| 1931 | ERROR_MSG("Main already active and Not an MS12 format. So, Cannot open another main stream"); |
| 1932 | return -EINVAL; |
| 1933 | } else { |
| 1934 | status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_MAIN); |
| 1935 | if (status == 0) { |
| 1936 | DEBUG_MSG("Open stream for Input with only Main flag(%x) stream_handle(%p)", flags, out->qaf_stream_handle); |
| 1937 | if(qaf_mod->stream_in[QAF_IN_MAIN]) { |
| 1938 | qaf_mod->stream_in[QAF_IN_MAIN_2] = out; |
| 1939 | } else { |
| 1940 | qaf_mod->stream_in[QAF_IN_MAIN] = out; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1941 | } |
| 1942 | } else { |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1943 | ERROR_MSG("Stream Open FAILED !!!"); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1944 | } |
| 1945 | } |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 1946 | } else if ((flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) { |
| 1947 | if (is_dual_main_active(qaf_mod)) { |
| 1948 | ERROR_MSG("Dual Main already active. So, Cannot open associated stream"); |
| 1949 | return -EINVAL; |
| 1950 | } else if (!is_main_active(qaf_mod)) { |
| 1951 | ERROR_MSG("Main not active. So, Cannot open associated stream"); |
| 1952 | return -EINVAL; |
| 1953 | } else if (qaf_mod->stream_in[QAF_IN_ASSOC]) { |
| 1954 | ERROR_MSG("Associated already active. So, Cannot open associated stream"); |
| 1955 | return -EINVAL; |
| 1956 | } |
| 1957 | status = qaf_mod->qaf_audio_stream_open(qaf_mod->session_handle, &out->qaf_stream_handle, input_config, devices, /*flags*/AUDIO_STREAM_ASSOCIATED); |
| 1958 | if (status == 0) { |
| 1959 | DEBUG_MSG("Open stream for Input with only Associated flag(%x) stream handle(%p)", flags, out->qaf_stream_handle); |
| 1960 | qaf_mod->stream_in[QAF_IN_ASSOC] = out; |
| 1961 | } else { |
| 1962 | ERROR_MSG("Stream Open FAILED !!!"); |
| 1963 | } |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1964 | } |
| 1965 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1966 | if (status != 0) { |
| 1967 | //If no stream is active then close the session. |
| 1968 | qaf_session_close(qaf_mod); |
| 1969 | return status; |
| 1970 | } |
| 1971 | |
| 1972 | //If Device is HDMI, QAF passthrough is enabled and there is no previous QAF passthrough input stream. |
| 1973 | if ((!p_qaf->passthrough_in) |
| 1974 | && (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) |
| 1975 | && audio_extn_qaf_passthrough_enabled(out)) { |
| 1976 | //Assign the QAF passthrough input stream. |
| 1977 | p_qaf->passthrough_in = out; |
| 1978 | |
| 1979 | //If HDMI is connected and format is supported by HDMI then create QAF passthrough output stream. |
| 1980 | if (p_qaf->hdmi_connect |
| 1981 | && platform_is_edid_supported_format(p_qaf->adev->platform, out->format)) { |
| 1982 | status = create_qaf_passthrough_stream(); |
| 1983 | if (status < 0) { |
| 1984 | qaf_stream_close(out); |
| 1985 | ERROR_MSG("QAF passthrough stream creation failed with error %d", status); |
| 1986 | return status; |
| 1987 | } |
| 1988 | } |
| 1989 | /*Else: since QAF passthrough input stream is already initialized, |
| 1990 | * when hdmi is connected |
| 1991 | * then qaf passthrough output stream will be created. |
| 1992 | */ |
| 1993 | } |
| 1994 | |
| 1995 | DEBUG_MSG(); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 1996 | return status; |
| 1997 | } |
| 1998 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 1999 | /* Resume a QAF stream. */ |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2000 | static int qaf_out_resume(struct audio_stream_out* stream) |
| 2001 | { |
| 2002 | struct stream_out *out = (struct stream_out *)stream; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2003 | int status = 0; |
| 2004 | DEBUG_MSG("Output Stream %p", out); |
| 2005 | |
| 2006 | |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2007 | lock_output_stream(out); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2008 | |
| 2009 | //If QAF passthrough is active then block the resume on module input streams. |
| 2010 | if (p_qaf->passthrough_out) { |
| 2011 | //If resume is received for the QAF passthrough stream then call the primary HAL api. |
| 2012 | pthread_mutex_lock(&p_qaf->lock); |
| 2013 | if (p_qaf->passthrough_in == out) { |
| 2014 | status = p_qaf->passthrough_out->stream.resume( |
| 2015 | (struct audio_stream_out*)p_qaf->passthrough_out); |
| 2016 | if (!status) out->offload_state = OFFLOAD_STATE_PLAYING; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2017 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2018 | pthread_mutex_unlock(&p_qaf->lock); |
| 2019 | } else { |
| 2020 | //Flush the module input stream. |
| 2021 | status = qaf_stream_start(out); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2022 | } |
| 2023 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2024 | unlock_output_stream(out); |
| 2025 | |
| 2026 | DEBUG_MSG(); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2027 | return status; |
| 2028 | } |
| 2029 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2030 | /* Offload thread for QAF output streams. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2031 | static void *qaf_offload_thread_loop(void *context) |
| 2032 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2033 | struct stream_out *out = (struct stream_out *)context; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2034 | struct listnode *item; |
| 2035 | int ret = 0; |
| 2036 | struct str_parms *parms = NULL; |
| 2037 | int value = 0; |
| 2038 | char* kvpairs = NULL; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2039 | struct qaf_module *qaf_mod = NULL; |
| 2040 | |
| 2041 | qaf_mod = get_qaf_module_for_input_stream(out); |
| 2042 | |
| 2043 | if (!qaf_mod) return NULL; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2044 | |
| 2045 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); |
| 2046 | set_sched_policy(0, SP_FOREGROUND); |
| 2047 | prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0); |
| 2048 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2049 | lock_output_stream(out); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2050 | |
| 2051 | DEBUG_MSG(); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2052 | for (;;) { |
| 2053 | struct offload_cmd *cmd = NULL; |
| 2054 | stream_callback_event_t event; |
| 2055 | bool send_callback = false; |
| 2056 | |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 2057 | DEBUG_MSG_VV("List Empty %d (1:TRUE, 0:FALSE)", list_empty(&out->qaf_offload_cmd_list)); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2058 | if (list_empty(&out->qaf_offload_cmd_list)) { |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 2059 | DEBUG_MSG_VV("SLEEPING"); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2060 | pthread_cond_wait(&out->qaf_offload_cond, &out->lock); |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 2061 | DEBUG_MSG_VV("RUNNING"); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2062 | continue; |
| 2063 | } |
| 2064 | |
| 2065 | item = list_head(&out->qaf_offload_cmd_list); |
| 2066 | cmd = node_to_item(item, struct offload_cmd, node); |
| 2067 | list_remove(item); |
| 2068 | |
| 2069 | if (cmd->cmd == OFFLOAD_CMD_EXIT) { |
| 2070 | free(cmd); |
| 2071 | break; |
| 2072 | } |
| 2073 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2074 | unlock_output_stream(out); |
| 2075 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2076 | send_callback = false; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2077 | switch (cmd->cmd) { |
| 2078 | case OFFLOAD_CMD_WAIT_FOR_BUFFER: { |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 2079 | DEBUG_MSG_VV("wait for buffer availability"); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2080 | |
| 2081 | while (1) { |
| 2082 | kvpairs = qaf_mod->qaf_audio_stream_get_param(out->qaf_stream_handle, |
| 2083 | "buf_available"); |
| 2084 | if (kvpairs) { |
| 2085 | parms = str_parms_create_str(kvpairs); |
| 2086 | ret = str_parms_get_int(parms, "buf_available", &value); |
| 2087 | if (ret >= 0) { |
| 2088 | if (value >= (int)out->compr_config.fragment_size) { |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 2089 | DEBUG_MSG_VV("buffer available"); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2090 | str_parms_destroy(parms); |
| 2091 | parms = NULL; |
| 2092 | break; |
| 2093 | } else { |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 2094 | DEBUG_MSG_VV("sleep"); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2095 | str_parms_destroy(parms); |
| 2096 | parms = NULL; |
| 2097 | usleep(10000); |
| 2098 | } |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2099 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2100 | free(kvpairs); |
| 2101 | kvpairs = NULL; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2102 | } |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2103 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2104 | send_callback = true; |
| 2105 | event = STREAM_CBK_EVENT_WRITE_READY; |
| 2106 | break; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2107 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2108 | default: |
| 2109 | DEBUG_MSG("unknown command received: %d", cmd->cmd); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2110 | break; |
| 2111 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2112 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2113 | lock_output_stream(out); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2114 | |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2115 | if (send_callback && out->client_callback) { |
| 2116 | out->client_callback(event, NULL, out->client_cookie); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2117 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2118 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2119 | free(cmd); |
| 2120 | } |
| 2121 | |
| 2122 | while (!list_empty(&out->qaf_offload_cmd_list)) { |
| 2123 | item = list_head(&out->qaf_offload_cmd_list); |
| 2124 | list_remove(item); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2125 | free (node_to_item( item, struct offload_cmd, node)); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2126 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2127 | unlock_output_stream(out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2128 | |
| 2129 | return NULL; |
| 2130 | } |
| 2131 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2132 | /* Create the offload callback thread for QAF output stream. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2133 | static int qaf_create_offload_callback_thread(struct stream_out *out) |
| 2134 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2135 | DEBUG_MSG("Output Stream %p", out); |
| 2136 | lock_output_stream(out); |
| 2137 | pthread_cond_init(&out->qaf_offload_cond, (const pthread_condattr_t *)NULL); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2138 | list_init(&out->qaf_offload_cmd_list); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2139 | pthread_create(&out->qaf_offload_thread, |
| 2140 | (const pthread_attr_t *)NULL, |
| 2141 | qaf_offload_thread_loop, |
| 2142 | out); |
| 2143 | unlock_output_stream(out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2144 | return 0; |
| 2145 | } |
| 2146 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2147 | /* Destroy the offload callback thread of QAF output stream. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2148 | static int qaf_destroy_offload_callback_thread(struct stream_out *out) |
| 2149 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2150 | DEBUG_MSG("Output Stream %p", out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2151 | qaf_send_offload_cmd_l(out, OFFLOAD_CMD_EXIT); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2152 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2153 | pthread_join(out->qaf_offload_thread, (void **)NULL); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2154 | pthread_cond_destroy(&out->qaf_offload_cond); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2155 | return 0; |
| 2156 | } |
| 2157 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2158 | /* Sets the stream set parameters (device routing information). */ |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2159 | static int qaf_out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 2160 | { |
| 2161 | struct str_parms *parms, *new_parms; |
| 2162 | char value[32]; |
| 2163 | char *new_kv_pairs; |
| 2164 | int val = 0; |
| 2165 | struct stream_out *out = (struct stream_out *)stream; |
| 2166 | int ret = 0; |
| 2167 | int err = 0; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2168 | struct qaf_module *qaf_mod = NULL; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2169 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2170 | DEBUG_MSG("usecase(%d: %s) kvpairs: %s", out->usecase, use_case_table[out->usecase], kvpairs); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2171 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2172 | parms = str_parms_create_str(kvpairs); |
| 2173 | err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 2174 | if (err < 0) |
| 2175 | return err; |
| 2176 | val = atoi(value); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2177 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2178 | qaf_mod = get_qaf_module_for_input_stream(out); |
| 2179 | if (!qaf_mod) return (-EINVAL); |
| 2180 | |
| 2181 | //TODO: HDMI is connected but user doesn't want HDMI output, close both HDMI outputs. |
| 2182 | |
| 2183 | /* Setting new device information to the mm module input streams. |
| 2184 | * This is needed if QAF module output streams are not created yet. |
| 2185 | */ |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 2186 | out->devices = val; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2187 | |
| 2188 | if (val == AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) { |
| 2189 | //If device is BT then open the BT stream if not already opened. |
| 2190 | if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) == NULL |
| 2191 | && audio_extn_bt_hal_get_device(qaf_mod->bt_hdl) != NULL) { |
| 2192 | ret = audio_extn_bt_hal_open_output_stream(qaf_mod->bt_hdl, |
| 2193 | QAF_OUTPUT_SAMPLING_RATE, |
| 2194 | AUDIO_CHANNEL_OUT_STEREO, |
| 2195 | CODEC_BACKEND_DEFAULT_BIT_WIDTH); |
| 2196 | if (ret != 0) { |
| 2197 | ERROR_MSG("BT Output stream open failure!"); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2198 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2199 | } |
| 2200 | } else if (val != 0) { |
| 2201 | //If device is not BT then close the BT stream if already opened. |
| 2202 | if ( audio_extn_bt_hal_get_output_stream(qaf_mod->bt_hdl) != NULL) { |
| 2203 | audio_extn_bt_hal_close_output_stream(qaf_mod->bt_hdl); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2204 | } |
| 2205 | } |
| 2206 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2207 | if (p_qaf->passthrough_in == out) { //Device routing is received for QAF passthrough stream. |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2208 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2209 | if (!(val & AUDIO_DEVICE_OUT_AUX_DIGITAL)) { //HDMI route is disabled. |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2210 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2211 | //If QAF pasthrough output is enabled. Close it. |
| 2212 | close_qaf_passthrough_stream(); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2213 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2214 | //Send the routing information to mm module pcm output. |
| 2215 | if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) { |
| 2216 | ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters( |
| 2217 | (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs); |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2218 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2219 | //else: device info is updated in the input streams. |
| 2220 | } else { //HDMI route is enabled. |
| 2221 | |
| 2222 | //create the QAf passthrough stream, if not created already. |
| 2223 | ret = create_qaf_passthrough_stream(); |
| 2224 | |
| 2225 | if (p_qaf->passthrough_out != NULL) { //If QAF passthrough out is enabled then send routing information. |
| 2226 | ret = p_qaf->passthrough_out->stream.common.set_parameters( |
| 2227 | (struct audio_stream *)p_qaf->passthrough_out, kvpairs); |
| 2228 | } |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2229 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2230 | } else { |
| 2231 | //Send the routing information to mm module pcm output. |
| 2232 | if (qaf_mod->stream_out[QAF_OUT_OFFLOAD]) { |
| 2233 | ret = qaf_mod->stream_out[QAF_OUT_OFFLOAD]->stream.common.set_parameters( |
| 2234 | (struct audio_stream *)qaf_mod->stream_out[QAF_OUT_OFFLOAD], kvpairs); |
| 2235 | } |
| 2236 | //else: device info is updated in the input streams. |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2237 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2238 | str_parms_destroy(parms); |
| 2239 | |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2240 | return ret; |
| 2241 | } |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2242 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2243 | /* Checks if a stream is QAF stream or not. */ |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2244 | bool audio_extn_is_qaf_stream(struct stream_out *out) |
| 2245 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2246 | struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2247 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2248 | if (qaf_mod) { |
| 2249 | return true; |
| 2250 | } |
| 2251 | return false; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | /* API to send playback stream specific config parameters */ |
| 2255 | int audio_extn_qaf_out_set_param_data(struct stream_out *out, |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2256 | audio_extn_param_id param_id, |
| 2257 | audio_extn_param_payload *payload) |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2258 | { |
| 2259 | int ret = -EINVAL; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2260 | int index; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2261 | struct stream_out *new_out = NULL; |
| 2262 | struct audio_adsp_event *adsp_event; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2263 | struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2264 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2265 | if (!out || !qaf_mod || !payload) { |
| 2266 | ERROR_MSG("Invalid Param"); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2267 | return ret; |
| 2268 | } |
| 2269 | |
| 2270 | /* In qaf output render session may not be opened at this time. |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2271 | to handle it store adsp_hdlr param info so that it can be |
| 2272 | applied later after opening render session from ms12 callback |
| 2273 | */ |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2274 | if (param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2275 | index = get_input_stream_index(out); |
| 2276 | if (index < 0) { |
| 2277 | ERROR_MSG("Invalid stream"); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2278 | return ret; |
| 2279 | } |
| 2280 | adsp_event = (struct audio_adsp_event *)payload; |
| 2281 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2282 | if (payload->adsp_event_params.payload_length <= AUDIO_MAX_ADSP_STREAM_CMD_PAYLOAD_LEN) { |
| 2283 | pthread_mutex_lock(&p_qaf->lock); |
| 2284 | memcpy(qaf_mod->adsp_hdlr_config[index].event_payload, |
| 2285 | adsp_event->payload, |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2286 | adsp_event->payload_length); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2287 | qaf_mod->adsp_hdlr_config[index].event_params.payload = |
| 2288 | qaf_mod->adsp_hdlr_config[index].event_payload; |
| 2289 | qaf_mod->adsp_hdlr_config[index].event_params.payload_length = |
| 2290 | adsp_event->payload_length; |
| 2291 | qaf_mod->adsp_hdlr_config[index].adsp_hdlr_config_valid = true; |
| 2292 | pthread_mutex_unlock(&p_qaf->lock); |
| 2293 | } else { |
| 2294 | ERROR_MSG("Invalid adsp event length %d", adsp_event->payload_length); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2295 | return ret; |
| 2296 | } |
| 2297 | ret = 0; |
| 2298 | } |
| 2299 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2300 | /* apply param for all active out sessions */ |
| 2301 | for (index = 0; index < MAX_QAF_MODULE_OUT; index++) { |
| 2302 | new_out = qaf_mod->stream_out[index]; |
| 2303 | if (!new_out) continue; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2304 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2305 | /*ADSP event is not supported for passthrough*/ |
| 2306 | if ((param_id == AUDIO_EXTN_PARAM_ADSP_STREAM_CMD) |
| 2307 | && !(new_out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM)) continue; |
| 2308 | if (new_out->standby) |
| 2309 | new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0); |
| 2310 | lock_output_stream(new_out); |
| 2311 | ret = audio_extn_out_set_param_data(new_out, param_id, payload); |
| 2312 | if (ret) |
| 2313 | ERROR_MSG("audio_extn_out_set_param_data error %d", ret); |
| 2314 | unlock_output_stream(new_out); |
| 2315 | } |
| 2316 | return ret; |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | int audio_extn_qaf_out_get_param_data(struct stream_out *out, |
| 2320 | audio_extn_param_id param_id, |
| 2321 | audio_extn_param_payload *payload) |
| 2322 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2323 | int ret = -EINVAL, i; |
| 2324 | struct stream_out *new_out; |
| 2325 | struct audio_usecase *uc_info; |
| 2326 | struct qaf_module *qaf_mod = get_qaf_module_for_input_stream(out); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2327 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2328 | if (!out || !qaf_mod || !payload) { |
| 2329 | ERROR_MSG("Invalid Param"); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2330 | return ret; |
| 2331 | } |
| 2332 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2333 | if (!p_qaf->hdmi_connect) { |
| 2334 | ERROR_MSG("hdmi not connected"); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2335 | return ret; |
| 2336 | } |
| 2337 | |
| 2338 | /* get session which is routed to hdmi*/ |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2339 | if (p_qaf->passthrough_out) |
| 2340 | new_out = p_qaf->passthrough_out; |
| 2341 | else { |
| 2342 | for (i = 0; i < MAX_QAF_MODULE_OUT; i++) { |
| 2343 | if (qaf_mod->stream_out[i]) { |
| 2344 | new_out = qaf_mod->stream_out[i]; |
| 2345 | break; |
| 2346 | } |
| 2347 | } |
| 2348 | } |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2349 | |
| 2350 | if (!new_out) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2351 | ERROR_MSG("No stream active."); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2352 | return ret; |
| 2353 | } |
| 2354 | |
| 2355 | if (new_out->standby) |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2356 | new_out->stream.write((struct audio_stream_out *)new_out, NULL, 0); |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2357 | |
| 2358 | lock_output_stream(new_out); |
| 2359 | ret = audio_extn_out_get_param_data(new_out, param_id, payload); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2360 | if (ret) |
| 2361 | ERROR_MSG("audio_extn_out_get_param_data error %d", ret); |
| 2362 | unlock_output_stream(new_out); |
| 2363 | |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2364 | return ret; |
| 2365 | } |
| 2366 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2367 | /* To open a stream with QAF. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2368 | int audio_extn_qaf_open_output_stream(struct audio_hw_device *dev, |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2369 | audio_io_handle_t handle, |
| 2370 | audio_devices_t devices, |
| 2371 | audio_output_flags_t flags, |
| 2372 | struct audio_config *config, |
| 2373 | struct audio_stream_out **stream_out, |
| 2374 | const char *address) |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2375 | { |
| 2376 | int ret = 0; |
| 2377 | struct stream_out *out; |
| 2378 | |
| 2379 | ret = adev_open_output_stream(dev, handle, devices, flags, config, stream_out, address); |
| 2380 | if (*stream_out == NULL) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2381 | ERROR_MSG("Stream open failed %d", ret); |
Deepak Agarwal | 1e42b85 | 2017-02-11 17:57:04 +0530 | [diff] [blame] | 2382 | return ret; |
| 2383 | } |
| 2384 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2385 | out = (struct stream_out *)*stream_out; |
| 2386 | |
| 2387 | ret = qaf_stream_open(out, config, flags, devices); |
| 2388 | if (ret == -ENOTSUP) { |
| 2389 | //Stream not supported by QAF, Bypass QAF. |
| 2390 | return 0; |
| 2391 | } else if (ret < 0) { |
| 2392 | ERROR_MSG("Error opening QAF stream err[%d]!", ret); |
| 2393 | adev_close_output_stream(dev, *stream_out); |
| 2394 | return ret; |
| 2395 | } |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2396 | |
| 2397 | /* Override function pointers based on qaf definitions */ |
| 2398 | out->stream.set_volume = qaf_out_set_volume; |
| 2399 | out->stream.pause = qaf_out_pause; |
| 2400 | out->stream.resume = qaf_out_resume; |
| 2401 | out->stream.drain = qaf_out_drain; |
| 2402 | out->stream.flush = qaf_out_flush; |
| 2403 | |
| 2404 | out->stream.common.standby = qaf_out_standby; |
| 2405 | out->stream.common.set_parameters = qaf_out_set_parameters; |
| 2406 | out->stream.get_latency = qaf_out_get_latency; |
| 2407 | out->stream.write = qaf_out_write; |
| 2408 | out->stream.get_presentation_position = qaf_out_get_presentation_position; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2409 | out->platform_latency = 0; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2410 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2411 | /*TODO: Need to handle this for DTS*/ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2412 | if (out->usecase == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) { |
| 2413 | out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 2414 | out->config.period_size = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2415 | out->config.period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT; |
Varun B | 34da7a4 | 2017-02-13 16:16:53 +0530 | [diff] [blame] | 2416 | out->config.start_threshold = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4; |
| 2417 | out->config.avail_min = QAF_DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4; |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 2418 | } else if(out->flags & AUDIO_OUTPUT_FLAG_DIRECT_PCM) { |
| 2419 | out->compr_config.fragment_size = qaf_get_pcm_offload_input_buffer_size(&(config->offload_info)); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2420 | } |
Satish Babu Patakokila | a8c136d | 2017-04-21 12:48:19 +0530 | [diff] [blame] | 2421 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2422 | *stream_out = &out->stream; |
| 2423 | if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 2424 | qaf_create_offload_callback_thread(out); |
| 2425 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2426 | |
| 2427 | DEBUG_MSG("Exit"); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2428 | return 0; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2429 | } |
| 2430 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2431 | /* Close a QAF stream. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2432 | void audio_extn_qaf_close_output_stream(struct audio_hw_device *dev, |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2433 | struct audio_stream_out *stream) |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2434 | { |
| 2435 | struct stream_out *out = (struct stream_out *)stream; |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2436 | struct qaf_module* qaf_mod = get_qaf_module_for_input_stream(out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2437 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2438 | if (!qaf_mod) return; |
| 2439 | |
| 2440 | DEBUG_MSG("stream_handle(%p) format = %x", out, out->format); |
| 2441 | |
| 2442 | //If close is received for QAF passthrough stream then close the QAF passthrough output. |
| 2443 | if (p_qaf->passthrough_in == out) { |
| 2444 | if (p_qaf->passthrough_out) { |
| 2445 | ALOGD("%s %d closing stream handle %p", __func__, __LINE__, p_qaf->passthrough_out); |
| 2446 | pthread_mutex_lock(&p_qaf->lock); |
| 2447 | adev_close_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 2448 | (struct audio_stream_out *)(p_qaf->passthrough_out)); |
| 2449 | pthread_mutex_unlock(&p_qaf->lock); |
| 2450 | p_qaf->passthrough_out = NULL; |
| 2451 | } |
| 2452 | |
| 2453 | p_qaf->passthrough_in = NULL; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2454 | } |
| 2455 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2456 | if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 2457 | qaf_destroy_offload_callback_thread(out); |
| 2458 | } |
Ben Romberger | d771a7c | 2017-02-22 18:05:17 -0800 | [diff] [blame] | 2459 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2460 | qaf_stream_close(out); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2461 | |
| 2462 | adev_close_output_stream(dev, stream); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2463 | |
| 2464 | DEBUG_MSG("Exit"); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2465 | } |
| 2466 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2467 | /* Check if QAF is supported or not. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2468 | bool audio_extn_qaf_is_enabled() |
| 2469 | { |
| 2470 | bool prop_enabled = false; |
| 2471 | char value[PROPERTY_VALUE_MAX] = {0}; |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 2472 | property_get("vendor.audio.qaf.enabled", value, NULL); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2473 | prop_enabled = atoi(value) || !strncmp("true", value, 4); |
| 2474 | return (prop_enabled); |
| 2475 | } |
| 2476 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2477 | /* Query HDMI EDID and sets module output accordingly.*/ |
| 2478 | void set_hdmi_configuration_to_module() |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2479 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2480 | int channels = 0; |
| 2481 | char *format_params; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2482 | struct str_parms *qaf_params; |
Lakshman Chaluvaraju | b4ec870 | 2016-11-04 19:21:12 +0530 | [diff] [blame] | 2483 | char prop_value[PROPERTY_VALUE_MAX]; |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2484 | bool passth_support = false; |
| 2485 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2486 | DEBUG_MSG("Entry"); |
| 2487 | |
| 2488 | if (!p_qaf) { |
| 2489 | return; |
| 2490 | } |
| 2491 | |
| 2492 | if (!p_qaf->hdmi_connect) { |
| 2493 | DEBUG_MSG("HDMI is not connected."); |
| 2494 | return; |
| 2495 | } |
| 2496 | |
| 2497 | p_qaf->hdmi_sink_channels = 0; |
| 2498 | |
| 2499 | //QAF re-encoding and DSP offload passthrough is supported. |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 2500 | if (property_get_bool("vendor.audio.offload.passthrough", false) |
| 2501 | && property_get_bool("vendor.audio.qaf.reencode", false)) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2502 | |
| 2503 | //If MS12 session is active. |
| 2504 | if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) { |
| 2505 | |
| 2506 | bool do_setparam = false; |
| 2507 | qaf_params = str_parms_create(); |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 2508 | property_get("vendor.audio.qaf.hdmi.out", prop_value, NULL); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2509 | |
| 2510 | if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_E_AC3) |
| 2511 | && (strncmp(prop_value, "ddp", 3) == 0)) { |
| 2512 | do_setparam = true; |
| 2513 | if (qaf_params) { |
| 2514 | str_parms_add_str(qaf_params, |
| 2515 | AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT, |
| 2516 | AUDIO_QAF_PARAMETER_VALUE_REENCODE_EAC3); |
| 2517 | } |
| 2518 | } else if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_AC3)) { |
| 2519 | do_setparam = true; |
| 2520 | if (qaf_params) { |
| 2521 | str_parms_add_str(qaf_params, |
| 2522 | AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT, |
| 2523 | AUDIO_QAF_PARAMETER_VALUE_REENCODE_AC3); |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | if (do_setparam) { |
| 2528 | if (p_qaf->qaf_msmd_enabled) { |
| 2529 | str_parms_add_str(qaf_params, |
| 2530 | AUDIO_QAF_PARAMETER_KEY_DEVICE, |
| 2531 | AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement. |
| 2532 | } else { |
| 2533 | str_parms_add_str(qaf_params, |
| 2534 | AUDIO_QAF_PARAMETER_KEY_DEVICE, |
| 2535 | AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI); |
| 2536 | } |
| 2537 | format_params = str_parms_to_str(qaf_params); |
| 2538 | |
| 2539 | p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle, |
| 2540 | format_params); |
| 2541 | |
| 2542 | passth_support = true; |
| 2543 | } |
| 2544 | str_parms_destroy(qaf_params); |
| 2545 | } |
| 2546 | |
| 2547 | //DTS_M8 session is active. |
| 2548 | if (p_qaf->qaf_mod[DTS_M8].session_handle |
| 2549 | && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) { |
| 2550 | |
| 2551 | bool do_setparam = false; |
| 2552 | qaf_params = str_parms_create(); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2553 | if (platform_is_edid_supported_format(p_qaf->adev->platform, AUDIO_FORMAT_DTS)) { |
| 2554 | do_setparam = true; |
| 2555 | if (qaf_params) { |
| 2556 | str_parms_add_str(qaf_params, |
| 2557 | AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT, |
| 2558 | AUDIO_QAF_PARAMETER_VALUE_REENCODE_DTS); |
| 2559 | } |
| 2560 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2561 | |
| 2562 | if (do_setparam) { |
| 2563 | if (p_qaf->qaf_msmd_enabled) { |
| 2564 | str_parms_add_str(qaf_params, |
| 2565 | AUDIO_QAF_PARAMETER_KEY_DEVICE, |
| 2566 | AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); //TODO: Need enhancement. |
| 2567 | } else { |
| 2568 | str_parms_add_str(qaf_params, |
| 2569 | AUDIO_QAF_PARAMETER_KEY_DEVICE, |
| 2570 | AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI); |
| 2571 | } |
| 2572 | format_params = str_parms_to_str(qaf_params); |
| 2573 | |
| 2574 | p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle, |
| 2575 | format_params); |
| 2576 | |
| 2577 | passth_support = true; |
| 2578 | } |
| 2579 | str_parms_destroy(qaf_params); |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | //Compressed passthrough is not enabled. |
| 2584 | if (!passth_support) { |
| 2585 | |
| 2586 | channels = platform_edid_get_max_channels(p_qaf->adev->platform); |
| 2587 | |
| 2588 | qaf_params = str_parms_create(); |
Harsh Bansal | 431eede | 2017-04-28 21:22:30 +0530 | [diff] [blame] | 2589 | |
| 2590 | str_parms_add_str(qaf_params, |
| 2591 | AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT, |
| 2592 | AUDIO_QAF_PARAMETER_VALUE_PCM); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2593 | switch (channels) { |
| 2594 | case 8: |
| 2595 | DEBUG_MSG("Switching Qaf output to 7.1 channels"); |
| 2596 | str_parms_add_str(qaf_params, |
| 2597 | AUDIO_QAF_PARAMETER_KEY_CHANNELS, |
| 2598 | AUDIO_QAF_PARAMETER_VALUE_8_CHANNELS); |
| 2599 | if (p_qaf->qaf_msmd_enabled) { |
| 2600 | str_parms_add_str(qaf_params, |
| 2601 | AUDIO_QAF_PARAMETER_KEY_DEVICE, |
| 2602 | AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); |
| 2603 | } else { |
| 2604 | str_parms_add_str(qaf_params, |
| 2605 | AUDIO_QAF_PARAMETER_KEY_DEVICE, |
| 2606 | AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI); |
| 2607 | } |
| 2608 | p_qaf->hdmi_sink_channels = channels; |
| 2609 | break; |
| 2610 | case 6: |
| 2611 | DEBUG_MSG("Switching Qaf output to 5.1 channels"); |
| 2612 | str_parms_add_str(qaf_params, |
| 2613 | AUDIO_QAF_PARAMETER_KEY_CHANNELS, |
| 2614 | AUDIO_QAF_PARAMETER_VALUE_6_CHANNELS); |
| 2615 | if (p_qaf->qaf_msmd_enabled) { |
| 2616 | str_parms_add_str(qaf_params, |
| 2617 | AUDIO_QAF_PARAMETER_KEY_DEVICE, |
| 2618 | AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); |
| 2619 | } else { |
| 2620 | str_parms_add_str(qaf_params, |
| 2621 | AUDIO_QAF_PARAMETER_KEY_DEVICE, |
| 2622 | AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI); |
| 2623 | } |
| 2624 | p_qaf->hdmi_sink_channels = channels; |
| 2625 | break; |
| 2626 | default: |
| 2627 | DEBUG_MSG("Switching Qaf output to default channels"); |
| 2628 | str_parms_add_str(qaf_params, |
| 2629 | AUDIO_QAF_PARAMETER_KEY_CHANNELS, |
| 2630 | AUDIO_QAF_PARAMETER_VALUE_DEFAULT_CHANNELS); |
| 2631 | if (p_qaf->qaf_msmd_enabled) { |
| 2632 | str_parms_add_str(qaf_params, |
| 2633 | AUDIO_QAF_PARAMETER_KEY_DEVICE, |
| 2634 | AUDIO_QAF_PARAMETER_VALUE_DEVICE_HDMI_AND_SPK); |
| 2635 | } else { |
| 2636 | str_parms_add_str(qaf_params, |
| 2637 | AUDIO_QAF_PARAMETER_KEY_DEVICE, |
| 2638 | AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER); |
| 2639 | } |
| 2640 | p_qaf->hdmi_sink_channels = 2; |
| 2641 | break; |
| 2642 | } |
| 2643 | |
| 2644 | format_params = str_parms_to_str(qaf_params); |
| 2645 | |
| 2646 | if (p_qaf->qaf_mod[MS12].session_handle && p_qaf->qaf_mod[MS12].qaf_audio_session_set_param) { |
| 2647 | p_qaf->qaf_mod[MS12].qaf_audio_session_set_param(p_qaf->qaf_mod[MS12].session_handle, |
| 2648 | format_params); |
| 2649 | } |
| 2650 | if (p_qaf->qaf_mod[DTS_M8].session_handle |
| 2651 | && p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param) { |
| 2652 | p_qaf->qaf_mod[DTS_M8].qaf_audio_session_set_param(p_qaf->qaf_mod[DTS_M8].session_handle, |
| 2653 | format_params); |
| 2654 | } |
| 2655 | |
| 2656 | str_parms_destroy(qaf_params); |
| 2657 | } |
| 2658 | DEBUG_MSG("Exit"); |
| 2659 | } |
| 2660 | |
| 2661 | /* QAF set parameter function. For Device connect and disconnect. */ |
| 2662 | int audio_extn_qaf_set_parameters(struct audio_device *adev, struct str_parms *parms) |
| 2663 | { |
| 2664 | int status = 0, val = 0, k; |
| 2665 | char *format_params, *kv_parirs; |
| 2666 | struct str_parms *qaf_params; |
| 2667 | char value[32]; |
| 2668 | |
| 2669 | DEBUG_MSG("Entry"); |
| 2670 | |
| 2671 | if (!p_qaf) { |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2672 | return -EINVAL; |
| 2673 | } |
| 2674 | |
| 2675 | status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value)); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2676 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2677 | if (status >= 0) { |
| 2678 | val = atoi(value); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2679 | if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is connected. |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2680 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2681 | p_qaf->hdmi_connect = 1; |
| 2682 | p_qaf->hdmi_sink_channels = 0; |
| 2683 | |
| 2684 | if (p_qaf->passthrough_in) { //If QAF passthrough is already initialized. |
| 2685 | lock_output_stream(p_qaf->passthrough_in); |
| 2686 | if (platform_is_edid_supported_format(adev->platform, |
| 2687 | p_qaf->passthrough_in->format)) { |
| 2688 | //If passthrough format is supported by HDMI then create the QAF passthrough output if not created already. |
| 2689 | create_qaf_passthrough_stream(); |
| 2690 | //Ignoring the returned error, If error then QAF passthrough is disabled. |
| 2691 | } else { |
| 2692 | //If passthrough format is not supported by HDMI then close the QAF passthrough output if already created. |
| 2693 | close_qaf_passthrough_stream(); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2694 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2695 | unlock_output_stream(p_qaf->passthrough_in); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2696 | } |
| 2697 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2698 | set_hdmi_configuration_to_module(); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2699 | |
Bharath Gopal | 01310bb | 2016-12-05 15:39:32 +0530 | [diff] [blame] | 2700 | } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2701 | for (k = 0; k < MAX_MM_MODULE_TYPE; k++) { |
| 2702 | if (!p_qaf->qaf_mod[k].bt_hdl) { |
| 2703 | DEBUG_MSG("Opening a2dp output..."); |
| 2704 | status = audio_extn_bt_hal_load(&p_qaf->qaf_mod[k].bt_hdl); |
| 2705 | if (status != 0) { |
| 2706 | ERROR_MSG("Error opening BT module"); |
| 2707 | return status; |
| 2708 | } |
| 2709 | } |
Bharath Gopal | 01310bb | 2016-12-05 15:39:32 +0530 | [diff] [blame] | 2710 | } |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2711 | } |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2712 | //TODO else if: Need to consider other devices. |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | status = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value)); |
| 2716 | if (status >= 0) { |
| 2717 | val = atoi(value); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2718 | if (val & AUDIO_DEVICE_OUT_AUX_DIGITAL) { //HDMI is disconnected. |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2719 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2720 | qaf_params = str_parms_create(); |
| 2721 | str_parms_add_str(qaf_params, |
| 2722 | AUDIO_QAF_PARAMETER_KEY_DEVICE, |
| 2723 | AUDIO_QAF_PARAMETER_VALUE_DEVICE_SPEAKER); |
| 2724 | str_parms_add_str(qaf_params, |
| 2725 | AUDIO_QAF_PARAMETER_KEY_RENDER_FORMAT, |
| 2726 | AUDIO_QAF_PARAMETER_VALUE_PCM); |
| 2727 | p_qaf->hdmi_sink_channels = 0; |
| 2728 | |
| 2729 | p_qaf->passthrough_enabled = 0; |
| 2730 | p_qaf->mch_pcm_hdmi_enabled = 0; |
| 2731 | p_qaf->hdmi_connect = 0; |
| 2732 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2733 | format_params = str_parms_to_str(qaf_params); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2734 | |
| 2735 | for (k = 0; k < MAX_MM_MODULE_TYPE; k++) { |
| 2736 | if (p_qaf->qaf_mod[k].session_handle |
| 2737 | && p_qaf->qaf_mod[k].qaf_audio_session_set_param) { |
| 2738 | p_qaf->qaf_mod[k].qaf_audio_session_set_param( |
| 2739 | p_qaf->qaf_mod[k].session_handle, format_params); |
| 2740 | } |
| 2741 | } |
| 2742 | close_all_hdmi_output(); |
| 2743 | |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2744 | str_parms_destroy(qaf_params); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2745 | close_qaf_passthrough_stream(); |
Bharath Gopal | 01310bb | 2016-12-05 15:39:32 +0530 | [diff] [blame] | 2746 | } else if (val & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2747 | DEBUG_MSG("Closing a2dp output..."); |
| 2748 | for (k = 0; k < MAX_MM_MODULE_TYPE; k++) { |
| 2749 | if (p_qaf->qaf_mod[k].bt_hdl) { |
| 2750 | audio_extn_bt_hal_unload(p_qaf->qaf_mod[k].bt_hdl); |
| 2751 | p_qaf->qaf_mod[k].bt_hdl = NULL; |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | //TODO else if: Need to consider other devices. |
| 2756 | } |
| 2757 | |
| 2758 | for (k = 0; k < MAX_MM_MODULE_TYPE; k++) { |
| 2759 | kv_parirs = str_parms_to_str(parms); |
| 2760 | if (p_qaf->qaf_mod[k].session_handle && p_qaf->qaf_mod[k].qaf_audio_session_set_param) { |
| 2761 | p_qaf->qaf_mod[k].qaf_audio_session_set_param( |
| 2762 | p_qaf->qaf_mod[k].session_handle, kv_parirs); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2763 | } |
| 2764 | } |
| 2765 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2766 | DEBUG_MSG("Exit"); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2767 | return status; |
| 2768 | } |
| 2769 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2770 | /* Create the QAF. */ |
| 2771 | int audio_extn_qaf_init(struct audio_device *adev) |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2772 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2773 | DEBUG_MSG("Entry"); |
| 2774 | |
| 2775 | p_qaf = calloc(1, sizeof(struct qaf)); |
| 2776 | if (p_qaf == NULL) { |
| 2777 | ERROR_MSG("Out of memory"); |
| 2778 | return -ENOMEM; |
| 2779 | } |
| 2780 | |
| 2781 | p_qaf->adev = adev; |
| 2782 | |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 2783 | if (property_get_bool("vendor.audio.qaf.msmd", false)) { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2784 | p_qaf->qaf_msmd_enabled = 1; |
| 2785 | } |
| 2786 | pthread_mutex_init(&p_qaf->lock, (const pthread_mutexattr_t *) NULL); |
| 2787 | |
| 2788 | int i = 0; |
| 2789 | |
| 2790 | for (i = 0; i < MAX_MM_MODULE_TYPE; i++) { |
| 2791 | char value[PROPERTY_VALUE_MAX] = {0}; |
| 2792 | char lib_name[PROPERTY_VALUE_MAX] = {0}; |
| 2793 | struct qaf_module *qaf_mod = &(p_qaf->qaf_mod[i]); |
| 2794 | |
| 2795 | if (i == MS12) { |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 2796 | property_get("vendor.audio.qaf.library", value, NULL); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2797 | } else if (i == DTS_M8) { |
Aniket Kumar Lata | 8fc67e6 | 2017-05-02 12:33:46 -0700 | [diff] [blame] | 2798 | property_get("vendor.audio.qaf.m8.library", value, NULL); |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2799 | } else { |
| 2800 | continue; |
| 2801 | } |
| 2802 | |
| 2803 | snprintf(lib_name, PROPERTY_VALUE_MAX, "%s", value); |
| 2804 | |
| 2805 | qaf_mod->qaf_lib = dlopen(lib_name, RTLD_NOW); |
| 2806 | if (qaf_mod->qaf_lib == NULL) { |
| 2807 | ERROR_MSG("DLOPEN failed for %s", lib_name); |
| 2808 | continue; |
| 2809 | } |
| 2810 | |
| 2811 | DEBUG_MSG("DLOPEN successful for %s", lib_name); |
| 2812 | qaf_mod->qaf_audio_session_open = |
Sri Karri | 1fbfef5 | 2017-06-29 19:45:47 +0530 | [diff] [blame] | 2813 | (int (*)(audio_session_handle_t* session_handle, audio_session_type_t s_type, |
| 2814 | void *p_data, void* license_data))dlsym(qaf_mod->qaf_lib, |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2815 | "audio_session_open"); |
| 2816 | qaf_mod->qaf_audio_session_close = |
| 2817 | (int (*)(audio_session_handle_t session_handle))dlsym(qaf_mod->qaf_lib, |
| 2818 | "audio_session_close"); |
| 2819 | qaf_mod->qaf_audio_stream_open = |
| 2820 | (int (*)(audio_session_handle_t session_handle, audio_stream_handle_t* stream_handle, |
| 2821 | audio_stream_config_t input_config, audio_devices_t devices, stream_type_t flags))dlsym(qaf_mod->qaf_lib, |
| 2822 | "audio_stream_open"); |
| 2823 | qaf_mod->qaf_audio_stream_close = |
| 2824 | (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib, |
| 2825 | "audio_stream_close"); |
| 2826 | qaf_mod->qaf_audio_stream_set_param = |
| 2827 | (int (*)(audio_stream_handle_t stream_handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib, |
| 2828 | "audio_stream_set_param"); |
| 2829 | qaf_mod->qaf_audio_session_set_param = |
| 2830 | (int (*)(audio_session_handle_t handle, const char* kv_pairs))dlsym(qaf_mod->qaf_lib, |
| 2831 | "audio_session_set_param"); |
| 2832 | qaf_mod->qaf_audio_stream_get_param = |
| 2833 | (char* (*)(audio_stream_handle_t stream_handle, const char* key))dlsym(qaf_mod->qaf_lib, |
| 2834 | "audio_stream_get_param"); |
| 2835 | qaf_mod->qaf_audio_session_get_param = |
| 2836 | (char* (*)(audio_session_handle_t handle, const char* key))dlsym(qaf_mod->qaf_lib, |
| 2837 | "audio_session_get_param"); |
| 2838 | qaf_mod->qaf_audio_stream_start = |
| 2839 | (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib, |
| 2840 | "audio_stream_start"); |
| 2841 | qaf_mod->qaf_audio_stream_stop = |
| 2842 | (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib, |
| 2843 | "audio_stream_stop"); |
| 2844 | qaf_mod->qaf_audio_stream_pause = |
| 2845 | (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib, |
| 2846 | "audio_stream_pause"); |
| 2847 | qaf_mod->qaf_audio_stream_flush = |
| 2848 | (int (*)(audio_stream_handle_t stream_handle))dlsym(qaf_mod->qaf_lib, |
| 2849 | "audio_stream_flush"); |
| 2850 | qaf_mod->qaf_audio_stream_write = |
| 2851 | (int (*)(audio_stream_handle_t stream_handle, const void* buf, int size))dlsym(qaf_mod->qaf_lib, |
| 2852 | "audio_stream_write"); |
| 2853 | qaf_mod->qaf_register_event_callback = |
| 2854 | (void (*)(audio_session_handle_t session_handle, void *priv_data, notify_event_callback_t event_callback, |
| 2855 | audio_event_id_t event_id))dlsym(qaf_mod->qaf_lib, |
| 2856 | "register_event_callback"); |
| 2857 | } |
| 2858 | |
| 2859 | DEBUG_MSG("Exit"); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2860 | return 0; |
| 2861 | } |
| 2862 | |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2863 | /* Tear down the qaf extension. */ |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2864 | void audio_extn_qaf_deinit() |
| 2865 | { |
Harsh Bansal | 28718c5 | 2017-04-20 13:47:12 +0530 | [diff] [blame] | 2866 | int i; |
| 2867 | DEBUG_MSG("Entry"); |
| 2868 | |
| 2869 | if (p_qaf != NULL) { |
| 2870 | for (i = 0; i < MAX_MM_MODULE_TYPE; i++) { |
| 2871 | qaf_session_close(&p_qaf->qaf_mod[i]); |
| 2872 | |
| 2873 | if (p_qaf->qaf_mod[i].qaf_lib != NULL) { |
| 2874 | dlclose(p_qaf->qaf_mod[i].qaf_lib); |
| 2875 | p_qaf->qaf_mod[i].qaf_lib = NULL; |
| 2876 | } |
| 2877 | } |
| 2878 | if (p_qaf->passthrough_out) { |
| 2879 | adev_close_output_stream((struct audio_hw_device *)p_qaf->adev, |
| 2880 | (struct audio_stream_out *)(p_qaf->passthrough_out)); |
| 2881 | p_qaf->passthrough_out = NULL; |
| 2882 | } |
| 2883 | |
| 2884 | pthread_mutex_destroy(&p_qaf->lock); |
| 2885 | free(p_qaf); |
| 2886 | p_qaf = NULL; |
| 2887 | } |
| 2888 | DEBUG_MSG("Exit"); |
Lakshman Chaluvaraju | 22ba9f1 | 2016-09-12 11:42:10 +0530 | [diff] [blame] | 2889 | } |