blob: d87303b8d45321e6acff7629f3109628591b95e9 [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>
40
41static const audio_format_t audio_passthru_formats[] = {
42 AUDIO_FORMAT_AC3,
43 AUDIO_FORMAT_E_AC3,
44 AUDIO_FORMAT_DTS,
45 AUDIO_FORMAT_DTS_HD
46};
47
48/*
49 * This atomic var is incremented/decremented by the offload stream to notify
50 * other pcm playback streams that a pass thru session is about to start or has
51 * finished. This hint can be used by the other streams to move to standby or
52 * start calling pcm_write respectively.
53 * This behavior is necessary as the DSP backend can only be configured to one
54 * of PCM or compressed.
55 */
56static volatile int32_t compress_passthru_active;
57
58bool audio_extn_passthru_is_supported_format(audio_format_t format)
59{
60 int32_t num_passthru_formats = sizeof(audio_passthru_formats) /
61 sizeof(audio_passthru_formats[0]);
62 int32_t i;
63
64 for (i = 0; i < num_passthru_formats; i++) {
65 if (format == audio_passthru_formats[i]) {
66 return true;
67 }
68 }
69 return false;
70}
71
72/*
73 * must be called with stream lock held
74 * This function decides based on some rules whether the data
75 * coming on stream out must be rendered or dropped.
76 */
77bool audio_extn_passthru_should_drop_data(struct stream_out * out)
78{
79 /* Make this product specific */
80 if (!(out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
81 ALOGI("drop data as end device 0x%x is unsupported", out->devices);
82 return true;
83 }
84
85 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
86 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
87 ALOGI("drop data as pass thru is active");
88 return true;
89 }
90 }
91
92 return false;
93}
94
95/* called with adev lock held */
96void audio_extn_passthru_on_start(struct stream_out * out)
97{
98
99 uint64_t max_period_us = 0;
100 uint64_t temp;
101 struct audio_usecase * usecase;
102 struct listnode *node;
103 struct stream_out * o;
104 struct audio_device *adev = out->dev;
105
106 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
107 ALOGI("pass thru is already active");
108 return;
109 }
110
111 ALOGV("inc pass thru count to notify other streams");
112 android_atomic_inc(&compress_passthru_active);
113
114 ALOGV("keep_alive_stop");
115 audio_extn_keep_alive_stop();
116
117 while (true) {
118 /* find max period time among active playback use cases */
119 list_for_each(node, &adev->usecase_list) {
120 usecase = node_to_item(node, struct audio_usecase, list);
121 if (usecase->type == PCM_PLAYBACK &&
122 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
123 o = usecase->stream.out;
124 temp = o->config.period_size * 1000000LL / o->sample_rate;
125 if (temp > max_period_us)
126 max_period_us = temp;
127 }
128 }
129
130 if (max_period_us) {
131 pthread_mutex_unlock(&adev->lock);
132 usleep(2*max_period_us);
133 max_period_us = 0;
134 pthread_mutex_lock(&adev->lock);
135 } else
136 break;
137 }
138}
139
140/* called with adev lock held */
141void audio_extn_passthru_on_stop(struct stream_out * out)
142{
Mingming Yin21854652016-04-13 11:54:02 -0700143 if (android_atomic_acquire_load(&compress_passthru_active) > 0) {
144 /*
145 * its possible the count is already zero if pause was called before
146 * stop output stream
147 */
148 android_atomic_dec(&compress_passthru_active);
149 }
150
151 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
152 ALOGI("passthru on aux digital, start keep alive");
153 audio_extn_keep_alive_start();
154 }
155}
156
157void audio_extn_passthru_on_pause(struct stream_out * out __unused)
158{
159 if (android_atomic_acquire_load(&compress_passthru_active) == 0)
160 return;
161
162 android_atomic_dec(&compress_passthru_active);
163}
164
165int audio_extn_passthru_set_parameters(struct audio_device *adev __unused,
166 struct str_parms *parms __unused)
167{
168 return 0;
169}
170
171bool audio_extn_passthru_is_active()
172{
173 return android_atomic_acquire_load(&compress_passthru_active) > 0;
174}
175
176bool audio_extn_passthru_is_enabled() { return true; }
177
178void audio_extn_passthru_init(struct audio_device *adev __unused)
179{
180}
181
182bool audio_extn_passthru_should_standby(struct stream_out * out __unused)
183{
184 return true;
185}