blob: 2bfe3910d7c639b461137d81fedfe5da36a7dcf0 [file] [log] [blame]
Fred Oh144d8742013-11-11 14:26:21 -08001/* AudioDaemon.cpp
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -07002Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
Fred Oh144d8742013-11-11 14:26:21 -08003
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
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
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
28
29#define LOG_TAG "AudioDaemon"
30#define LOG_NDEBUG 0
31#define LOG_NDDEBUG 0
32
33#include <media/AudioSystem.h>
34#include <sys/poll.h>
35
36#include "AudioDaemon.h"
37
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -070038#define CPE_MAGIC_NUM 0x2000
39#define MAX_CPE_SLEEP_RETRY 2
40#define CPE_SLEEP_WAIT 100
41
42#define MAX_SLEEP_RETRY 100
43#define AUDIO_INIT_SLEEP_WAIT 100 /* 100 ms */
44
Fred Oh144d8742013-11-11 14:26:21 -080045int bootup_complete = 0;
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -070046bool cpe_bootup_complete = false;
Fred Oh144d8742013-11-11 14:26:21 -080047
48namespace android {
49
50 AudioDaemon::AudioDaemon() : Thread(false) {
51 }
52
53 AudioDaemon::~AudioDaemon() {
54 putStateFDs(mSndCardFd);
55 }
56
57 void AudioDaemon::onFirstRef() {
58 ALOGV("Start audiod daemon");
Naresh Tanniru1aa072f2014-05-02 15:52:46 +053059 run("AudioDaemon", PRIORITY_URGENT_AUDIO);
Fred Oh144d8742013-11-11 14:26:21 -080060 }
61
62 void AudioDaemon::binderDied(const wp<IBinder>& who)
63 {
64 requestExit();
65 }
66
67 bool AudioDaemon::getStateFDs(std::vector<std::pair<int,int> > &sndcardFdPair)
68 {
69 FILE *fp;
70 int fd;
71 char *ptr, *saveptr;
72 char buffer[128];
73 int line = 0;
74 String8 path;
75 int sndcard;
76 const char* cards = "/proc/asound/cards";
77
78 if ((fp = fopen(cards, "r")) == NULL) {
79 ALOGE("Cannot open %s file to get list of sound cars", cards);
80 return false;
81 }
82
83 sndcardFdPair.clear();
84 memset(buffer, 0x0, sizeof(buffer));
85 while ((fgets(buffer, sizeof(buffer), fp) != NULL)) {
86 if (line % 2)
87 continue;
88 ptr = strtok_r(buffer, " [", &saveptr);
89 if (ptr) {
90 path = "/proc/asound/card";
91 path += ptr;
92 path += "/state";
93 ALOGD("Opening sound card state : %s", path.string());
94 fd = open(path.string(), O_RDONLY);
95 if (fd == -1) {
96 ALOGE("Open %s failed : %s", path.string(), strerror(errno));
97 } else {
98 /* returns vector of pair<sndcard, fd> */
99 sndcard = atoi(ptr);
100 sndcardFdPair.push_back(std::make_pair(sndcard, fd));
101 }
102 }
103 line++;
104 }
105
106 ALOGV("%s: %d sound cards detected", __func__, sndcardFdPair.size());
107 fclose(fp);
108
109 return sndcardFdPair.size() > 0 ? true : false;
110 }
111
112 void AudioDaemon::putStateFDs(std::vector<std::pair<int,int> > &sndcardFdPair)
113 {
114 unsigned int i;
115 for (i = 0; i < sndcardFdPair.size(); i++)
116 close(sndcardFdPair[i].second);
117 sndcardFdPair.clear();
118 }
119
120 status_t AudioDaemon::readyToRun() {
121
122 ALOGV("readyToRun: open snd card state node files");
123 return NO_ERROR;
124 }
125
Fred Oh144d8742013-11-11 14:26:21 -0800126 bool AudioDaemon::threadLoop()
127 {
128 int max = -1;
129 unsigned int i;
130 bool ret = true;
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700131 notify_status cur_state = snd_card_offline;
Fred Oh144d8742013-11-11 14:26:21 -0800132 struct pollfd *pfd = NULL;
133 char rd_buf[9];
134 unsigned int sleepRetry = 0;
135 bool audioInitDone = false;
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700136 int fd = 0;
137 char path[50];
138 notify_status cur_cpe_state = cpe_offline;
139 notify_status prev_cpe_state = cpe_offline;
140 unsigned int cpe_cnt = CPE_MAGIC_NUM;
141 unsigned int num_snd_cards = 0;
Fred Oh144d8742013-11-11 14:26:21 -0800142
143 ALOGV("Start threadLoop()");
144 while (audioInitDone == false && sleepRetry < MAX_SLEEP_RETRY) {
145 if (mSndCardFd.empty() && !getStateFDs(mSndCardFd)) {
146 ALOGE("Sleeping for 100 ms");
147 usleep(AUDIO_INIT_SLEEP_WAIT*1000);
148 sleepRetry++;
149 } else {
150 audioInitDone = true;
151 }
152 }
153
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700154 if (audioInitDone == false) {
Fred Oh144d8742013-11-11 14:26:21 -0800155 ALOGE("Sound Card is empty!!!");
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700156 goto thread_exit;
157 }
158
159 /* soundcards are opened, now get the cpe state nodes */
160 num_snd_cards = mSndCardFd.size();
161 for (i = 0; i < num_snd_cards; i++) {
162 snprintf(path, sizeof(path), "/proc/asound/card%d/cpe0_state", mSndCardFd[i].first);
163 ALOGD("Opening cpe0_state : %s", path);
164 sleepRetry = 0;
165 do {
166 fd = open(path, O_RDONLY);
167 if (fd == -1) {
168 sleepRetry++;
169 ALOGE("CPE state open %s failed %s, Retrying %d",
170 path, strerror(errno), sleepRetry);
171 usleep(CPE_SLEEP_WAIT*1000);
172 } else {
173 ALOGD("cpe state opened: %s", path);
174 mSndCardFd.push_back(std::make_pair(cpe_cnt++, fd));
175 }
176 }while ((fd == -1) && sleepRetry < MAX_CPE_SLEEP_RETRY);
177 }
178 ALOGD("number of sndcards %d CPEs %d", i, cpe_cnt - CPE_MAGIC_NUM);
Fred Oh144d8742013-11-11 14:26:21 -0800179
180 pfd = new pollfd[mSndCardFd.size()];
181 bzero(pfd, sizeof(*pfd) * mSndCardFd.size());
182 for (i = 0; i < mSndCardFd.size(); i++) {
183 pfd[i].fd = mSndCardFd[i].second;
184 pfd[i].events = POLLPRI;
185 }
186
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700187 ALOGD("read for sound card state change before while");
188 for (i = 0; i < mSndCardFd.size(); i++) {
189 if (!read(pfd[i].fd, (void *)rd_buf, 8)) {
Fred Oh144d8742013-11-11 14:26:21 -0800190 ALOGE("Error receiving sound card state event (%s)", strerror(errno));
191 ret = false;
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700192 } else {
Fred Oh144d8742013-11-11 14:26:21 -0800193 rd_buf[8] = '\0';
Fred Oh144d8742013-11-11 14:26:21 -0800194 lseek(pfd[i].fd, 0, SEEK_SET);
195
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700196 if(mSndCardFd[i].first >= CPE_MAGIC_NUM) {
197 ALOGD("CPE %d state file content: %s before while",
198 mSndCardFd[i].first - CPE_MAGIC_NUM, rd_buf);
199 if (strstr(rd_buf, "OFFLINE")) {
200 ALOGD("CPE state offline");
201 cur_cpe_state = cpe_offline;
202 } else if (strstr(rd_buf, "ONLINE")){
203 ALOGD("CPE state online");
204 cur_cpe_state = cpe_online;
205 } else {
206 ALOGE("ERROR CPE rd_buf %s", rd_buf);
207 }
208 if (cur_cpe_state == cpe_online && !cpe_bootup_complete) {
209 cpe_bootup_complete = true;
210 ALOGD("CPE boot up completed before polling");
211 }
212 prev_cpe_state = cur_cpe_state;
Fred Oh144d8742013-11-11 14:26:21 -0800213 }
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700214 else {
215 ALOGD("sound card state file content: %s before while",rd_buf);
216 if (strstr(rd_buf, "OFFLINE")) {
217 ALOGE("put cur_state to offline");
218 cur_state = snd_card_offline;
219 } else if (strstr(rd_buf, "ONLINE")){
220 ALOGE("put cur_state to online");
221 cur_state = snd_card_online;
222 } else {
223 ALOGE("ERROR rd_buf %s", rd_buf);
224 }
Fred Oh144d8742013-11-11 14:26:21 -0800225
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700226 ALOGD("cur_state=%d, bootup_complete=%d", cur_state, cur_state );
227 if (cur_state == snd_card_online && !bootup_complete) {
228 bootup_complete = 1;
229 ALOGE("sound card up is deteced before while");
230 ALOGE("bootup_complete set to 1");
231 }
Fred Oh144d8742013-11-11 14:26:21 -0800232 }
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700233 }
234 }
Fred Oh144d8742013-11-11 14:26:21 -0800235
236 while (1) {
237 ALOGD("poll() for sound card state change ");
238 if (poll(pfd, mSndCardFd.size(), -1) < 0) {
239 ALOGE("poll() failed (%s)", strerror(errno));
240 ret = false;
241 break;
242 }
243
244 ALOGD("out of poll() for sound card state change, SNDCARD size=%d", mSndCardFd.size());
245 for (i = 0; i < mSndCardFd.size(); i++) {
246 if (pfd[i].revents & POLLPRI) {
247 if (!read(pfd[i].fd, (void *)rd_buf, 8)) {
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700248 ALOGE("Error receiving sound card %d state event (%s)",
249 mSndCardFd[i].first, strerror(errno));
Fred Oh144d8742013-11-11 14:26:21 -0800250 ret = false;
251 } else {
252 rd_buf[8] = '\0';
Fred Oh144d8742013-11-11 14:26:21 -0800253 lseek(pfd[i].fd, 0, SEEK_SET);
254
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700255 if(mSndCardFd[i].first >= CPE_MAGIC_NUM) {
256 if (strstr(rd_buf, "OFFLINE"))
257 cur_cpe_state = cpe_offline;
258 else if (strstr(rd_buf, "ONLINE"))
259 cur_cpe_state = cpe_online;
260 else
261 ALOGE("ERROR CPE rd_buf %s", rd_buf);
Fred Oh144d8742013-11-11 14:26:21 -0800262
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700263 if (cpe_bootup_complete && (prev_cpe_state != cur_cpe_state)) {
264 ALOGD("CPE state is %d, nofity AudioSystem", cur_cpe_state);
265 notifyAudioSystem(mSndCardFd[i].first, cur_cpe_state, CPE_STATE);
266 }
267 if (!cpe_bootup_complete && (cur_cpe_state == cpe_online)) {
268 cpe_bootup_complete = true;
269 ALOGD("CPE boot up completed");
270 }
271 prev_cpe_state = cur_cpe_state;
Fred Oh144d8742013-11-11 14:26:21 -0800272 }
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700273 else {
274 ALOGV("sound card state file content: %s, bootup_complete=%d",rd_buf, bootup_complete);
275 if (strstr(rd_buf, "OFFLINE")) {
276 cur_state = snd_card_offline;
277 } else if (strstr(rd_buf, "ONLINE")){
278 cur_state = snd_card_online;
279 }
Fred Oh144d8742013-11-11 14:26:21 -0800280
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700281 if (bootup_complete) {
282 ALOGV("bootup_complete, so NofityAudioSystem");
283 notifyAudioSystem(mSndCardFd[i].first, cur_state, SND_CARD_STATE);
284 }
285
286 if (cur_state == snd_card_online && !bootup_complete) {
287 bootup_complete = 1;
288 }
Fred Oh144d8742013-11-11 14:26:21 -0800289 }
290 }
291 }
292 }
293 }
294
295 putStateFDs(mSndCardFd);
296 delete [] pfd;
297
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700298 thread_exit:
Fred Oh144d8742013-11-11 14:26:21 -0800299 ALOGV("Exiting Poll ThreadLoop");
300 return ret;
301 }
302
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700303 void AudioDaemon::notifyAudioSystem(int snd_card,
304 notify_status status,
305 notify_status_type type)
306 {
Fred Oh144d8742013-11-11 14:26:21 -0800307
308 String8 str;
309 char buf[4] = {0,};
310
Bharath Ramachandramurthy2b4f0c42014-06-24 13:20:50 -0700311 if (type == CPE_STATE) {
312 str = "CPE_STATUS=";
313 snprintf(buf, sizeof(buf), "%d", snd_card - CPE_MAGIC_NUM);
314 str += buf;
315 if (status == cpe_online)
316 str += ",ONLINE";
317 else
318 str += ",OFFLINE";
319 }
320 else {
321 str = "SND_CARD_STATUS=";
322 snprintf(buf, sizeof(buf), "%d", snd_card);
323 str += buf;
324 if (status == snd_card_online)
325 str += ",ONLINE";
326 else
327 str += ",OFFLINE";
328 }
Fred Oh144d8742013-11-11 14:26:21 -0800329 ALOGV("%s: notifyAudioSystem : %s", __func__, str.string());
330 AudioSystem::setParameters(0, str);
331 }
332}