Fred Oh | 144d874 | 2013-11-11 14:26:21 -0800 | [diff] [blame] | 1 | /* AudioDaemon.cpp |
| 2 | Copyright (c) 2012-2013, 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 | #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 | |
| 38 | int bootup_complete = 0; |
| 39 | |
| 40 | namespace android { |
| 41 | |
| 42 | AudioDaemon::AudioDaemon() : Thread(false) { |
| 43 | } |
| 44 | |
| 45 | AudioDaemon::~AudioDaemon() { |
| 46 | putStateFDs(mSndCardFd); |
| 47 | } |
| 48 | |
| 49 | void AudioDaemon::onFirstRef() { |
| 50 | ALOGV("Start audiod daemon"); |
Naresh Tanniru | 1aa072f | 2014-05-02 15:52:46 +0530 | [diff] [blame] | 51 | run("AudioDaemon", PRIORITY_URGENT_AUDIO); |
Fred Oh | 144d874 | 2013-11-11 14:26:21 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void AudioDaemon::binderDied(const wp<IBinder>& who) |
| 55 | { |
| 56 | requestExit(); |
| 57 | } |
| 58 | |
| 59 | bool AudioDaemon::getStateFDs(std::vector<std::pair<int,int> > &sndcardFdPair) |
| 60 | { |
| 61 | FILE *fp; |
| 62 | int fd; |
| 63 | char *ptr, *saveptr; |
| 64 | char buffer[128]; |
| 65 | int line = 0; |
| 66 | String8 path; |
| 67 | int sndcard; |
| 68 | const char* cards = "/proc/asound/cards"; |
| 69 | |
| 70 | if ((fp = fopen(cards, "r")) == NULL) { |
| 71 | ALOGE("Cannot open %s file to get list of sound cars", cards); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | sndcardFdPair.clear(); |
| 76 | memset(buffer, 0x0, sizeof(buffer)); |
| 77 | while ((fgets(buffer, sizeof(buffer), fp) != NULL)) { |
| 78 | if (line % 2) |
| 79 | continue; |
| 80 | ptr = strtok_r(buffer, " [", &saveptr); |
| 81 | if (ptr) { |
| 82 | path = "/proc/asound/card"; |
| 83 | path += ptr; |
| 84 | path += "/state"; |
| 85 | ALOGD("Opening sound card state : %s", path.string()); |
| 86 | fd = open(path.string(), O_RDONLY); |
| 87 | if (fd == -1) { |
| 88 | ALOGE("Open %s failed : %s", path.string(), strerror(errno)); |
| 89 | } else { |
| 90 | /* returns vector of pair<sndcard, fd> */ |
| 91 | sndcard = atoi(ptr); |
| 92 | sndcardFdPair.push_back(std::make_pair(sndcard, fd)); |
| 93 | } |
| 94 | } |
| 95 | line++; |
| 96 | } |
| 97 | |
| 98 | ALOGV("%s: %d sound cards detected", __func__, sndcardFdPair.size()); |
| 99 | fclose(fp); |
| 100 | |
| 101 | return sndcardFdPair.size() > 0 ? true : false; |
| 102 | } |
| 103 | |
| 104 | void AudioDaemon::putStateFDs(std::vector<std::pair<int,int> > &sndcardFdPair) |
| 105 | { |
| 106 | unsigned int i; |
| 107 | for (i = 0; i < sndcardFdPair.size(); i++) |
| 108 | close(sndcardFdPair[i].second); |
| 109 | sndcardFdPair.clear(); |
| 110 | } |
| 111 | |
| 112 | status_t AudioDaemon::readyToRun() { |
| 113 | |
| 114 | ALOGV("readyToRun: open snd card state node files"); |
| 115 | return NO_ERROR; |
| 116 | } |
| 117 | |
| 118 | #define MAX_SLEEP_RETRY 100 |
| 119 | #define AUDIO_INIT_SLEEP_WAIT 100 /* 100 ms */ |
| 120 | |
| 121 | bool AudioDaemon::threadLoop() |
| 122 | { |
| 123 | int max = -1; |
| 124 | unsigned int i; |
| 125 | bool ret = true; |
| 126 | snd_card_status cur_state = snd_card_offline; |
| 127 | struct pollfd *pfd = NULL; |
| 128 | char rd_buf[9]; |
| 129 | unsigned int sleepRetry = 0; |
| 130 | bool audioInitDone = false; |
| 131 | |
| 132 | ALOGV("Start threadLoop()"); |
| 133 | while (audioInitDone == false && sleepRetry < MAX_SLEEP_RETRY) { |
| 134 | if (mSndCardFd.empty() && !getStateFDs(mSndCardFd)) { |
| 135 | ALOGE("Sleeping for 100 ms"); |
| 136 | usleep(AUDIO_INIT_SLEEP_WAIT*1000); |
| 137 | sleepRetry++; |
| 138 | } else { |
| 139 | audioInitDone = true; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (audioInitDone == false) |
| 144 | ALOGE("Sound Card is empty!!!"); |
| 145 | |
| 146 | pfd = new pollfd[mSndCardFd.size()]; |
| 147 | bzero(pfd, sizeof(*pfd) * mSndCardFd.size()); |
| 148 | for (i = 0; i < mSndCardFd.size(); i++) { |
| 149 | pfd[i].fd = mSndCardFd[i].second; |
| 150 | pfd[i].events = POLLPRI; |
| 151 | } |
| 152 | |
| 153 | ALOGD("read for sound card state change before while"); |
| 154 | for (i = 0; i < mSndCardFd.size(); i++) { |
| 155 | if (!read(pfd[i].fd, (void *)rd_buf, 8)) { |
| 156 | ALOGE("Error receiving sound card state event (%s)", strerror(errno)); |
| 157 | ret = false; |
| 158 | } else { |
| 159 | rd_buf[8] = '\0'; |
| 160 | ALOGD("sound card state file content: %s before while",rd_buf); |
| 161 | lseek(pfd[i].fd, 0, SEEK_SET); |
| 162 | |
| 163 | if (strstr(rd_buf, "OFFLINE")) { |
| 164 | ALOGE("put cur_state to offline"); |
| 165 | cur_state = snd_card_offline; |
| 166 | } else if (strstr(rd_buf, "ONLINE")){ |
| 167 | ALOGE("put cur_state to online"); |
| 168 | cur_state = snd_card_online; |
| 169 | } else { |
| 170 | ALOGE("ERROR rd_buf %s", rd_buf); |
| 171 | } |
| 172 | |
| 173 | ALOGD("cur_state=%d, bootup_complete=%d", cur_state, cur_state ); |
| 174 | if (cur_state == snd_card_online && !bootup_complete) { |
| 175 | bootup_complete = 1; |
| 176 | ALOGE("sound card up is deteced before while"); |
| 177 | ALOGE("bootup_complete set to 1"); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | while (1) { |
| 183 | ALOGD("poll() for sound card state change "); |
| 184 | if (poll(pfd, mSndCardFd.size(), -1) < 0) { |
| 185 | ALOGE("poll() failed (%s)", strerror(errno)); |
| 186 | ret = false; |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | ALOGD("out of poll() for sound card state change, SNDCARD size=%d", mSndCardFd.size()); |
| 191 | for (i = 0; i < mSndCardFd.size(); i++) { |
| 192 | if (pfd[i].revents & POLLPRI) { |
| 193 | if (!read(pfd[i].fd, (void *)rd_buf, 8)) { |
| 194 | ALOGE("Error receiving sound card state event (%s)", strerror(errno)); |
| 195 | ret = false; |
| 196 | } else { |
| 197 | rd_buf[8] = '\0'; |
| 198 | ALOGV("sound card state file content: %s, bootup_complete=%d",rd_buf, bootup_complete); |
| 199 | lseek(pfd[i].fd, 0, SEEK_SET); |
| 200 | |
| 201 | if (strstr(rd_buf, "OFFLINE")) { |
| 202 | cur_state = snd_card_offline; |
| 203 | } else if (strstr(rd_buf, "ONLINE")){ |
| 204 | cur_state = snd_card_online; |
| 205 | } |
| 206 | |
| 207 | if (bootup_complete) { |
| 208 | ALOGV("bootup_complete, so NofityAudioSystem"); |
| 209 | notifyAudioSystem(mSndCardFd[i].first, cur_state); |
| 210 | } |
| 211 | |
| 212 | if (cur_state == snd_card_online && !bootup_complete) { |
| 213 | bootup_complete = 1; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | putStateFDs(mSndCardFd); |
| 221 | delete [] pfd; |
| 222 | |
| 223 | ALOGV("Exiting Poll ThreadLoop"); |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | void AudioDaemon::notifyAudioSystem(int snd_card, snd_card_status status) { |
| 228 | |
| 229 | String8 str; |
| 230 | char buf[4] = {0,}; |
| 231 | |
| 232 | str = "SND_CARD_STATUS="; |
| 233 | snprintf(buf, sizeof(buf), "%d", snd_card); |
| 234 | str += buf; |
| 235 | if (status == snd_card_online) |
| 236 | str += ",ONLINE"; |
| 237 | else |
| 238 | str += ",OFFLINE"; |
| 239 | ALOGV("%s: notifyAudioSystem : %s", __func__, str.string()); |
| 240 | AudioSystem::setParameters(0, str); |
| 241 | } |
| 242 | } |