blob: 61575ddf5d5093480a7be11906110c858dffa6dd [file] [log] [blame]
Mingming Yin21854652016-04-13 11:54:02 -07001/*
Ben Romberger1aaaf862017-04-06 17:49:46 -07002* Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
Mingming Yin21854652016-04-13 11:54:02 -07003*
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 "passthru"
31/*#define LOG_NDEBUG 0*/
32#include <stdlib.h>
33#include <cutils/atomic.h>
34#include <cutils/str_parms.h>
35#include <cutils/log.h>
36#include "audio_hw.h"
37#include "audio_extn.h"
38#include "platform_api.h"
39#include <platform.h>
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +053040#include <cutils/properties.h>
41
42#include "sound/compress_params.h"
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053043#ifdef DYNAMIC_LOG_ENABLED
44#include <log_xml_parser.h>
45#define LOG_MASK HAL_MOD_FILE_PASSTH
46#include <log_utils.h>
47#endif
Mingming Yin21854652016-04-13 11:54:02 -070048
49static const audio_format_t audio_passthru_formats[] = {
50 AUDIO_FORMAT_AC3,
51 AUDIO_FORMAT_E_AC3,
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +053052 AUDIO_FORMAT_E_AC3_JOC,
Mingming Yin21854652016-04-13 11:54:02 -070053 AUDIO_FORMAT_DTS,
Ben Romberger1aaaf862017-04-06 17:49:46 -070054 AUDIO_FORMAT_DTS_HD,
Naresh Tanniru928f0862017-04-07 16:44:23 -070055 AUDIO_FORMAT_DOLBY_TRUEHD,
56 AUDIO_FORMAT_IEC61937
Mingming Yin21854652016-04-13 11:54:02 -070057};
58
59/*
60 * This atomic var is incremented/decremented by the offload stream to notify
61 * other pcm playback streams that a pass thru session is about to start or has
62 * finished. This hint can be used by the other streams to move to standby or
63 * start calling pcm_write respectively.
64 * This behavior is necessary as the DSP backend can only be configured to one
65 * of PCM or compressed.
66 */
67static volatile int32_t compress_passthru_active;
68
69bool audio_extn_passthru_is_supported_format(audio_format_t format)
70{
71 int32_t num_passthru_formats = sizeof(audio_passthru_formats) /
72 sizeof(audio_passthru_formats[0]);
73 int32_t i;
74
75 for (i = 0; i < num_passthru_formats; i++) {
76 if (format == audio_passthru_formats[i]) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +053077 ALOGD("%s : pass through format is true", __func__);
Mingming Yin21854652016-04-13 11:54:02 -070078 return true;
79 }
80 }
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +053081 ALOGD("%s : pass through format is false", __func__);
Mingming Yin21854652016-04-13 11:54:02 -070082 return false;
83}
84
85/*
86 * must be called with stream lock held
87 * This function decides based on some rules whether the data
88 * coming on stream out must be rendered or dropped.
89 */
90bool audio_extn_passthru_should_drop_data(struct stream_out * out)
91{
Ashish Jaind84fd6a2016-07-27 12:33:25 +053092 /*Drop data only
93 *stream is routed to HDMI and
94 *stream has PCM format or
95 *if a compress offload (DSP decode) session
96 */
97 if ((out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
98 (((out->format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) ||
99 ((out->compr_config.codec != NULL) && (out->compr_config.codec->compr_passthr == LEGACY_PCM)))) {
Mingming Yin21854652016-04-13 11:54:02 -0700100 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
101 ALOGI("drop data as pass thru is active");
102 return true;
103 }
104 }
105
106 return false;
107}
108
109/* called with adev lock held */
110void audio_extn_passthru_on_start(struct stream_out * out)
111{
112
113 uint64_t max_period_us = 0;
114 uint64_t temp;
115 struct audio_usecase * usecase;
116 struct listnode *node;
117 struct stream_out * o;
118 struct audio_device *adev = out->dev;
119
120 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
121 ALOGI("pass thru is already active");
122 return;
123 }
124
125 ALOGV("inc pass thru count to notify other streams");
126 android_atomic_inc(&compress_passthru_active);
127
Mingming Yin21854652016-04-13 11:54:02 -0700128 while (true) {
129 /* find max period time among active playback use cases */
130 list_for_each(node, &adev->usecase_list) {
131 usecase = node_to_item(node, struct audio_usecase, list);
132 if (usecase->type == PCM_PLAYBACK &&
133 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
134 o = usecase->stream.out;
135 temp = o->config.period_size * 1000000LL / o->sample_rate;
136 if (temp > max_period_us)
137 max_period_us = temp;
138 }
139 }
140
141 if (max_period_us) {
142 pthread_mutex_unlock(&adev->lock);
143 usleep(2*max_period_us);
144 max_period_us = 0;
145 pthread_mutex_lock(&adev->lock);
146 } else
147 break;
148 }
149}
150
151/* called with adev lock held */
152void audio_extn_passthru_on_stop(struct stream_out * out)
153{
Mingming Yin21854652016-04-13 11:54:02 -0700154 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
155 /*
156 * its possible the count is already zero if pause was called before
157 * stop output stream
158 */
159 android_atomic_dec(&compress_passthru_active);
160 }
161
162 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
163 ALOGI("passthru on aux digital, start keep alive");
164 audio_extn_keep_alive_start();
165 }
166}
167
168void audio_extn_passthru_on_pause(struct stream_out * out __unused)
169{
170 if (android_atomic_acquire_load(&compress_passthru_active) == 0)
171 return;
Mingming Yin21854652016-04-13 11:54:02 -0700172}
173
174int audio_extn_passthru_set_parameters(struct audio_device *adev __unused,
175 struct str_parms *parms __unused)
176{
177 return 0;
178}
179
180bool audio_extn_passthru_is_active()
181{
182 return android_atomic_acquire_load(&compress_passthru_active) > 0;
183}
184
185bool audio_extn_passthru_is_enabled() { return true; }
186
187void audio_extn_passthru_init(struct audio_device *adev __unused)
188{
189}
190
191bool audio_extn_passthru_should_standby(struct stream_out * out __unused)
192{
193 return true;
194}
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530195
196bool audio_extn_passthru_is_convert_supported(struct audio_device *adev,
197 struct stream_out *out)
198{
199
200 bool convert = false;
201 switch (out->format) {
202 case AUDIO_FORMAT_E_AC3:
203 case AUDIO_FORMAT_E_AC3_JOC:
204 case AUDIO_FORMAT_DTS_HD:
205 if (!platform_is_edid_supported_format(adev->platform,
206 out->format)) {
207 ALOGD("%s:PASSTHROUGH_CONVERT supported", __func__);
208 convert = true;
209 }
210 break;
211 default:
212 ALOGD("%s: PASSTHROUGH_CONVERT not supported for format 0x%x",
213 __func__, out->format);
214 break;
215 }
216 ALOGD("%s: convert %d", __func__, convert);
217 return convert;
218}
219
220bool audio_extn_passthru_is_passt_supported(struct audio_device *adev,
221 struct stream_out *out)
222{
223 bool passt = false;
224 switch (out->format) {
225 case AUDIO_FORMAT_E_AC3:
Ben Romberger1aaaf862017-04-06 17:49:46 -0700226 case AUDIO_FORMAT_DTS_HD:
227 case AUDIO_FORMAT_DOLBY_TRUEHD:
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530228 if (platform_is_edid_supported_format(adev->platform, out->format)) {
229 ALOGV("%s:PASSTHROUGH supported for format %x",
230 __func__, out->format);
231 passt = true;
232 }
233 break;
234 case AUDIO_FORMAT_AC3:
235 if (platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_AC3)
236 || platform_is_edid_supported_format(adev->platform,
237 AUDIO_FORMAT_E_AC3)) {
238 ALOGV("%s:PASSTHROUGH supported for format %x",
239 __func__, out->format);
240 passt = true;
241 }
242 break;
243 case AUDIO_FORMAT_E_AC3_JOC:
244 /* Check for DDP capability in edid for JOC contents.*/
245 if (platform_is_edid_supported_format(adev->platform,
246 AUDIO_FORMAT_E_AC3)) {
247 ALOGV("%s:PASSTHROUGH supported for format %x",
248 __func__, out->format);
249 passt = true;
250 }
251 break;
252 case AUDIO_FORMAT_DTS:
253 if (platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_DTS)
254 || platform_is_edid_supported_format(adev->platform,
255 AUDIO_FORMAT_DTS_HD)) {
256 ALOGV("%s:PASSTHROUGH supported for format %x",
257 __func__, out->format);
258 passt = true;
259 }
260 break;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530261 default:
262 ALOGV("%s:Passthrough not supported", __func__);
263 }
264 return passt;
265}
266
267void audio_extn_passthru_update_stream_configuration(
268 struct audio_device *adev, struct stream_out *out)
269{
270 if (audio_extn_passthru_is_passt_supported(adev, out)) {
271 ALOGV("%s:PASSTHROUGH", __func__);
272 out->compr_config.codec->compr_passthr = PASSTHROUGH;
Naresh Tanniru928f0862017-04-07 16:44:23 -0700273 } else if (audio_extn_passthru_is_convert_supported(adev, out)) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530274 ALOGV("%s:PASSTHROUGH CONVERT", __func__);
275 out->compr_config.codec->compr_passthr = PASSTHROUGH_CONVERT;
Naresh Tanniru928f0862017-04-07 16:44:23 -0700276 } else if (out->format == AUDIO_FORMAT_IEC61937) {
277 ALOGV("%s:PASSTHROUGH IEC61937", __func__);
278 out->compr_config.codec->compr_passthr = PASSTHROUGH_IEC61937;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530279 } else {
280 ALOGV("%s:NO PASSTHROUGH", __func__);
281 out->compr_config.codec->compr_passthr = LEGACY_PCM;
282 }
283}
284
285bool audio_extn_passthru_is_passthrough_stream(struct stream_out *out)
286{
287 //check passthrough system property
288 if (!property_get_bool("audio.offload.passthrough", false)) {
289 return false;
290 }
291
292 //check supported device, currently only on HDMI.
293 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
294 //passthrough flag
295 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)
296 return true;
297 //direct flag, check supported formats.
298 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
299 if (audio_extn_passthru_is_supported_format(out->format)) {
300 if (platform_is_edid_supported_format(out->dev->platform,
301 out->format)) {
302 ALOGV("%s : return true",__func__);
303 return true;
304 } else if (audio_extn_is_dolby_format(out->format) &&
305 platform_is_edid_supported_format(out->dev->platform,
306 AUDIO_FORMAT_AC3)){
307 //return true for EAC3/EAC3_JOC formats
308 //if sink supports only AC3
309 ALOGV("%s : return true",__func__);
310 return true;
311 }
312 }
313 }
314 }
315 ALOGV("%s : return false",__func__);
316 return false;
317}
318
319int audio_extn_passthru_get_buffer_size(audio_offload_info_t* info)
320{
321 return platform_get_compress_passthrough_buffer_size(info);
322}
323
324int audio_extn_passthru_set_volume(struct stream_out *out, int mute)
325{
326 return platform_set_device_params(out, DEVICE_PARAM_MUTE_ID, mute);
327}
328
329int audio_extn_passthru_set_latency(struct stream_out *out, int latency)
330{
331 return platform_set_device_params(out, DEVICE_PARAM_LATENCY_ID, latency);
332}