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