blob: 24208abaaf76d1b6df4d3740c0c94d84cbaed92c [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,
55 AUDIO_FORMAT_DOLBY_TRUEHD
Mingming Yin21854652016-04-13 11:54:02 -070056};
57
58/*
59 * This atomic var is incremented/decremented by the offload stream to notify
60 * other pcm playback streams that a pass thru session is about to start or has
61 * finished. This hint can be used by the other streams to move to standby or
62 * start calling pcm_write respectively.
63 * This behavior is necessary as the DSP backend can only be configured to one
64 * of PCM or compressed.
65 */
66static volatile int32_t compress_passthru_active;
67
68bool audio_extn_passthru_is_supported_format(audio_format_t format)
69{
70 int32_t num_passthru_formats = sizeof(audio_passthru_formats) /
71 sizeof(audio_passthru_formats[0]);
72 int32_t i;
73
74 for (i = 0; i < num_passthru_formats; i++) {
75 if (format == audio_passthru_formats[i]) {
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +053076 ALOGD("%s : pass through format is true", __func__);
Mingming Yin21854652016-04-13 11:54:02 -070077 return true;
78 }
79 }
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +053080 ALOGD("%s : pass through format is false", __func__);
Mingming Yin21854652016-04-13 11:54:02 -070081 return false;
82}
83
84/*
85 * must be called with stream lock held
86 * This function decides based on some rules whether the data
87 * coming on stream out must be rendered or dropped.
88 */
89bool audio_extn_passthru_should_drop_data(struct stream_out * out)
90{
Ashish Jaind84fd6a2016-07-27 12:33:25 +053091 /*Drop data only
92 *stream is routed to HDMI and
93 *stream has PCM format or
94 *if a compress offload (DSP decode) session
95 */
96 if ((out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) &&
97 (((out->format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) ||
98 ((out->compr_config.codec != NULL) && (out->compr_config.codec->compr_passthr == LEGACY_PCM)))) {
Mingming Yin21854652016-04-13 11:54:02 -070099 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
100 ALOGI("drop data as pass thru is active");
101 return true;
102 }
103 }
104
105 return false;
106}
107
108/* called with adev lock held */
109void audio_extn_passthru_on_start(struct stream_out * out)
110{
111
112 uint64_t max_period_us = 0;
113 uint64_t temp;
114 struct audio_usecase * usecase;
115 struct listnode *node;
116 struct stream_out * o;
117 struct audio_device *adev = out->dev;
118
119 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
120 ALOGI("pass thru is already active");
121 return;
122 }
123
124 ALOGV("inc pass thru count to notify other streams");
125 android_atomic_inc(&compress_passthru_active);
126
Mingming Yin21854652016-04-13 11:54:02 -0700127 while (true) {
128 /* find max period time among active playback use cases */
129 list_for_each(node, &adev->usecase_list) {
130 usecase = node_to_item(node, struct audio_usecase, list);
131 if (usecase->type == PCM_PLAYBACK &&
132 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
133 o = usecase->stream.out;
134 temp = o->config.period_size * 1000000LL / o->sample_rate;
135 if (temp > max_period_us)
136 max_period_us = temp;
137 }
138 }
139
140 if (max_period_us) {
141 pthread_mutex_unlock(&adev->lock);
142 usleep(2*max_period_us);
143 max_period_us = 0;
144 pthread_mutex_lock(&adev->lock);
145 } else
146 break;
147 }
148}
149
150/* called with adev lock held */
151void audio_extn_passthru_on_stop(struct stream_out * out)
152{
Mingming Yin21854652016-04-13 11:54:02 -0700153 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
154 /*
155 * its possible the count is already zero if pause was called before
156 * stop output stream
157 */
158 android_atomic_dec(&compress_passthru_active);
159 }
160
161 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
162 ALOGI("passthru on aux digital, start keep alive");
163 audio_extn_keep_alive_start();
164 }
165}
166
167void audio_extn_passthru_on_pause(struct stream_out * out __unused)
168{
169 if (android_atomic_acquire_load(&compress_passthru_active) == 0)
170 return;
Mingming Yin21854652016-04-13 11:54:02 -0700171}
172
173int audio_extn_passthru_set_parameters(struct audio_device *adev __unused,
174 struct str_parms *parms __unused)
175{
176 return 0;
177}
178
179bool audio_extn_passthru_is_active()
180{
181 return android_atomic_acquire_load(&compress_passthru_active) > 0;
182}
183
184bool audio_extn_passthru_is_enabled() { return true; }
185
186void audio_extn_passthru_init(struct audio_device *adev __unused)
187{
188}
189
190bool audio_extn_passthru_should_standby(struct stream_out * out __unused)
191{
192 return true;
193}
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530194
195bool audio_extn_passthru_is_convert_supported(struct audio_device *adev,
196 struct stream_out *out)
197{
198
199 bool convert = false;
200 switch (out->format) {
201 case AUDIO_FORMAT_E_AC3:
202 case AUDIO_FORMAT_E_AC3_JOC:
203 case AUDIO_FORMAT_DTS_HD:
204 if (!platform_is_edid_supported_format(adev->platform,
205 out->format)) {
206 ALOGD("%s:PASSTHROUGH_CONVERT supported", __func__);
207 convert = true;
208 }
209 break;
210 default:
211 ALOGD("%s: PASSTHROUGH_CONVERT not supported for format 0x%x",
212 __func__, out->format);
213 break;
214 }
215 ALOGD("%s: convert %d", __func__, convert);
216 return convert;
217}
218
219bool audio_extn_passthru_is_passt_supported(struct audio_device *adev,
220 struct stream_out *out)
221{
222 bool passt = false;
223 switch (out->format) {
224 case AUDIO_FORMAT_E_AC3:
Ben Romberger1aaaf862017-04-06 17:49:46 -0700225 case AUDIO_FORMAT_DTS_HD:
226 case AUDIO_FORMAT_DOLBY_TRUEHD:
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530227 if (platform_is_edid_supported_format(adev->platform, out->format)) {
228 ALOGV("%s:PASSTHROUGH supported for format %x",
229 __func__, out->format);
230 passt = true;
231 }
232 break;
233 case AUDIO_FORMAT_AC3:
234 if (platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_AC3)
235 || platform_is_edid_supported_format(adev->platform,
236 AUDIO_FORMAT_E_AC3)) {
237 ALOGV("%s:PASSTHROUGH supported for format %x",
238 __func__, out->format);
239 passt = true;
240 }
241 break;
242 case AUDIO_FORMAT_E_AC3_JOC:
243 /* Check for DDP capability in edid for JOC contents.*/
244 if (platform_is_edid_supported_format(adev->platform,
245 AUDIO_FORMAT_E_AC3)) {
246 ALOGV("%s:PASSTHROUGH supported for format %x",
247 __func__, out->format);
248 passt = true;
249 }
250 break;
251 case AUDIO_FORMAT_DTS:
252 if (platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_DTS)
253 || platform_is_edid_supported_format(adev->platform,
254 AUDIO_FORMAT_DTS_HD)) {
255 ALOGV("%s:PASSTHROUGH supported for format %x",
256 __func__, out->format);
257 passt = true;
258 }
259 break;
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530260 default:
261 ALOGV("%s:Passthrough not supported", __func__);
262 }
263 return passt;
264}
265
266void audio_extn_passthru_update_stream_configuration(
267 struct audio_device *adev, struct stream_out *out)
268{
269 if (audio_extn_passthru_is_passt_supported(adev, out)) {
270 ALOGV("%s:PASSTHROUGH", __func__);
271 out->compr_config.codec->compr_passthr = PASSTHROUGH;
272 } else if (audio_extn_passthru_is_convert_supported(adev, out)){
273 ALOGV("%s:PASSTHROUGH CONVERT", __func__);
274 out->compr_config.codec->compr_passthr = PASSTHROUGH_CONVERT;
275 } else {
276 ALOGV("%s:NO PASSTHROUGH", __func__);
277 out->compr_config.codec->compr_passthr = LEGACY_PCM;
278 }
279}
280
281bool audio_extn_passthru_is_passthrough_stream(struct stream_out *out)
282{
283 //check passthrough system property
284 if (!property_get_bool("audio.offload.passthrough", false)) {
285 return false;
286 }
287
288 //check supported device, currently only on HDMI.
289 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
290 //passthrough flag
291 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)
292 return true;
293 //direct flag, check supported formats.
294 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
295 if (audio_extn_passthru_is_supported_format(out->format)) {
296 if (platform_is_edid_supported_format(out->dev->platform,
297 out->format)) {
298 ALOGV("%s : return true",__func__);
299 return true;
300 } else if (audio_extn_is_dolby_format(out->format) &&
301 platform_is_edid_supported_format(out->dev->platform,
302 AUDIO_FORMAT_AC3)){
303 //return true for EAC3/EAC3_JOC formats
304 //if sink supports only AC3
305 ALOGV("%s : return true",__func__);
306 return true;
307 }
308 }
309 }
310 }
311 ALOGV("%s : return false",__func__);
312 return false;
313}
314
315int audio_extn_passthru_get_buffer_size(audio_offload_info_t* info)
316{
317 return platform_get_compress_passthrough_buffer_size(info);
318}
319
320int audio_extn_passthru_set_volume(struct stream_out *out, int mute)
321{
322 return platform_set_device_params(out, DEVICE_PARAM_MUTE_ID, mute);
323}
324
325int audio_extn_passthru_set_latency(struct stream_out *out, int latency)
326{
327 return platform_set_device_params(out, DEVICE_PARAM_LATENCY_ID, latency);
328}