blob: e6ac4dd55252fefc92c60d76f8cb8e775a3fa3d5 [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{
Mingming Yin21854652016-04-13 11:54:02 -070085
86 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
87 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
88 ALOGI("drop data as pass thru is active");
89 return true;
90 }
91 }
92
93 return false;
94}
95
96/* called with adev lock held */
97void audio_extn_passthru_on_start(struct stream_out * out)
98{
99
100 uint64_t max_period_us = 0;
101 uint64_t temp;
102 struct audio_usecase * usecase;
103 struct listnode *node;
104 struct stream_out * o;
105 struct audio_device *adev = out->dev;
106
107 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
108 ALOGI("pass thru is already active");
109 return;
110 }
111
112 ALOGV("inc pass thru count to notify other streams");
113 android_atomic_inc(&compress_passthru_active);
114
115 ALOGV("keep_alive_stop");
116 audio_extn_keep_alive_stop();
117
118 while (true) {
119 /* find max period time among active playback use cases */
120 list_for_each(node, &adev->usecase_list) {
121 usecase = node_to_item(node, struct audio_usecase, list);
122 if (usecase->type == PCM_PLAYBACK &&
123 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
124 o = usecase->stream.out;
125 temp = o->config.period_size * 1000000LL / o->sample_rate;
126 if (temp > max_period_us)
127 max_period_us = temp;
128 }
129 }
130
131 if (max_period_us) {
132 pthread_mutex_unlock(&adev->lock);
133 usleep(2*max_period_us);
134 max_period_us = 0;
135 pthread_mutex_lock(&adev->lock);
136 } else
137 break;
138 }
139}
140
141/* called with adev lock held */
142void audio_extn_passthru_on_stop(struct stream_out * out)
143{
Mingming Yin21854652016-04-13 11:54:02 -0700144 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
145 /*
146 * its possible the count is already zero if pause was called before
147 * stop output stream
148 */
149 android_atomic_dec(&compress_passthru_active);
150 }
151
152 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
153 ALOGI("passthru on aux digital, start keep alive");
154 audio_extn_keep_alive_start();
155 }
156}
157
158void audio_extn_passthru_on_pause(struct stream_out * out __unused)
159{
160 if (android_atomic_acquire_load(&compress_passthru_active) == 0)
161 return;
Mingming Yin21854652016-04-13 11:54:02 -0700162}
163
164int audio_extn_passthru_set_parameters(struct audio_device *adev __unused,
165 struct str_parms *parms __unused)
166{
167 return 0;
168}
169
170bool audio_extn_passthru_is_active()
171{
172 return android_atomic_acquire_load(&compress_passthru_active) > 0;
173}
174
175bool audio_extn_passthru_is_enabled() { return true; }
176
177void audio_extn_passthru_init(struct audio_device *adev __unused)
178{
179}
180
181bool audio_extn_passthru_should_standby(struct stream_out * out __unused)
182{
183 return true;
184}
Satish Babu Patakokila1caa1b72016-05-24 13:47:08 +0530185
186bool audio_extn_passthru_is_convert_supported(struct audio_device *adev,
187 struct stream_out *out)
188{
189
190 bool convert = false;
191 switch (out->format) {
192 case AUDIO_FORMAT_E_AC3:
193 case AUDIO_FORMAT_E_AC3_JOC:
194 case AUDIO_FORMAT_DTS_HD:
195 if (!platform_is_edid_supported_format(adev->platform,
196 out->format)) {
197 ALOGD("%s:PASSTHROUGH_CONVERT supported", __func__);
198 convert = true;
199 }
200 break;
201 default:
202 ALOGD("%s: PASSTHROUGH_CONVERT not supported for format 0x%x",
203 __func__, out->format);
204 break;
205 }
206 ALOGD("%s: convert %d", __func__, convert);
207 return convert;
208}
209
210bool audio_extn_passthru_is_passt_supported(struct audio_device *adev,
211 struct stream_out *out)
212{
213 bool passt = false;
214 switch (out->format) {
215 case AUDIO_FORMAT_E_AC3:
216 if (platform_is_edid_supported_format(adev->platform, out->format)) {
217 ALOGV("%s:PASSTHROUGH supported for format %x",
218 __func__, out->format);
219 passt = true;
220 }
221 break;
222 case AUDIO_FORMAT_AC3:
223 if (platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_AC3)
224 || platform_is_edid_supported_format(adev->platform,
225 AUDIO_FORMAT_E_AC3)) {
226 ALOGV("%s:PASSTHROUGH supported for format %x",
227 __func__, out->format);
228 passt = true;
229 }
230 break;
231 case AUDIO_FORMAT_E_AC3_JOC:
232 /* Check for DDP capability in edid for JOC contents.*/
233 if (platform_is_edid_supported_format(adev->platform,
234 AUDIO_FORMAT_E_AC3)) {
235 ALOGV("%s:PASSTHROUGH supported for format %x",
236 __func__, out->format);
237 passt = true;
238 }
239 break;
240 case AUDIO_FORMAT_DTS:
241 if (platform_is_edid_supported_format(adev->platform, AUDIO_FORMAT_DTS)
242 || platform_is_edid_supported_format(adev->platform,
243 AUDIO_FORMAT_DTS_HD)) {
244 ALOGV("%s:PASSTHROUGH supported for format %x",
245 __func__, out->format);
246 passt = true;
247 }
248 break;
249 case AUDIO_FORMAT_DTS_HD:
250 if (platform_is_edid_supported_format(adev->platform, out->format)) {
251 ALOGV("%s:PASSTHROUGH supported for format %x",
252 __func__, out->format);
253 passt = true;
254 }
255 break;
256 default:
257 ALOGV("%s:Passthrough not supported", __func__);
258 }
259 return passt;
260}
261
262void audio_extn_passthru_update_stream_configuration(
263 struct audio_device *adev, struct stream_out *out)
264{
265 if (audio_extn_passthru_is_passt_supported(adev, out)) {
266 ALOGV("%s:PASSTHROUGH", __func__);
267 out->compr_config.codec->compr_passthr = PASSTHROUGH;
268 } else if (audio_extn_passthru_is_convert_supported(adev, out)){
269 ALOGV("%s:PASSTHROUGH CONVERT", __func__);
270 out->compr_config.codec->compr_passthr = PASSTHROUGH_CONVERT;
271 } else {
272 ALOGV("%s:NO PASSTHROUGH", __func__);
273 out->compr_config.codec->compr_passthr = LEGACY_PCM;
274 }
275}
276
277bool audio_extn_passthru_is_passthrough_stream(struct stream_out *out)
278{
279 //check passthrough system property
280 if (!property_get_bool("audio.offload.passthrough", false)) {
281 return false;
282 }
283
284 //check supported device, currently only on HDMI.
285 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
286 //passthrough flag
287 if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_PASSTHROUGH)
288 return true;
289 //direct flag, check supported formats.
290 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
291 if (audio_extn_passthru_is_supported_format(out->format)) {
292 if (platform_is_edid_supported_format(out->dev->platform,
293 out->format)) {
294 ALOGV("%s : return true",__func__);
295 return true;
296 } else if (audio_extn_is_dolby_format(out->format) &&
297 platform_is_edid_supported_format(out->dev->platform,
298 AUDIO_FORMAT_AC3)){
299 //return true for EAC3/EAC3_JOC formats
300 //if sink supports only AC3
301 ALOGV("%s : return true",__func__);
302 return true;
303 }
304 }
305 }
306 }
307 ALOGV("%s : return false",__func__);
308 return false;
309}
310
311int audio_extn_passthru_get_buffer_size(audio_offload_info_t* info)
312{
313 return platform_get_compress_passthrough_buffer_size(info);
314}
315
316int audio_extn_passthru_set_volume(struct stream_out *out, int mute)
317{
318 return platform_set_device_params(out, DEVICE_PARAM_MUTE_ID, mute);
319}
320
321int audio_extn_passthru_set_latency(struct stream_out *out, int latency)
322{
323 return platform_set_device_params(out, DEVICE_PARAM_LATENCY_ID, latency);
324}