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