Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "audioserver" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <sys/prctl.h> |
| 24 | #include <sys/wait.h> |
| 25 | #include <cutils/properties.h> |
| 26 | |
jiabin | e99d088 | 2021-09-17 05:21:25 +0000 | [diff] [blame] | 27 | #include <android/media/audio/common/AudioMMapPolicy.h> |
| 28 | #include <android/media/audio/common/AudioMMapPolicyInfo.h> |
| 29 | #include <android/media/audio/common/AudioMMapPolicyType.h> |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 30 | #include <android/media/IAudioFlingerService.h> |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 31 | #include <binder/IPCThreadState.h> |
| 32 | #include <binder/ProcessState.h> |
| 33 | #include <binder/IServiceManager.h> |
Mikhail Naganov | 00a21a3 | 2017-11-16 08:57:44 -0800 | [diff] [blame] | 34 | #include <hidl/HidlTransportSupport.h> |
Andy Hung | d7e0dd4 | 2020-03-27 16:42:34 -0700 | [diff] [blame] | 35 | #include <mediautils/LimitProcessMemory.h> |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 36 | #include <utils/Log.h> |
| 37 | |
Glenn Kasten | 1688e1d | 2020-04-06 12:58:24 -0700 | [diff] [blame] | 38 | // from include_dirs |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 39 | #include "AudioFlinger.h" |
| 40 | #include "AudioPolicyService.h" |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 41 | #include "AAudioService.h" |
Phil Burk | 5b38eb3 | 2017-09-29 15:06:40 -0700 | [diff] [blame] | 42 | #include "utility/AAudioUtilities.h" |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 43 | #include "MediaLogService.h" |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 44 | |
| 45 | using namespace android; |
| 46 | |
jiabin | e99d088 | 2021-09-17 05:21:25 +0000 | [diff] [blame] | 47 | using android::media::audio::common::AudioMMapPolicy; |
| 48 | using android::media::audio::common::AudioMMapPolicyInfo; |
| 49 | using android::media::audio::common::AudioMMapPolicyType; |
| 50 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 51 | int main(int argc __unused, char **argv) |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 52 | { |
Andy Hung | 1d312b9 | 2023-01-03 16:12:50 -0800 | [diff] [blame] | 53 | ALOGD("%s: starting", __func__); |
| 54 | const auto startTime = std::chrono::steady_clock::now(); |
Andy Hung | 2105271 | 2017-12-07 11:45:37 -0800 | [diff] [blame] | 55 | // TODO: update with refined parameters |
| 56 | limitProcessMemory( |
| 57 | "audio.maxmem", /* "ro.audio.maxmem", property that defines limit */ |
| 58 | (size_t)512 * (1 << 20), /* SIZE_MAX, upper limit in bytes */ |
| 59 | 20 /* upper limit as percentage of physical RAM */); |
| 60 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 61 | signal(SIGPIPE, SIG_IGN); |
| 62 | |
Glenn Kasten | e2cf829 | 2020-09-14 11:40:32 -0700 | [diff] [blame] | 63 | #if 1 |
| 64 | // FIXME See bug 165702394 and bug 168511485 |
| 65 | const bool doLog = false; |
| 66 | #else |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 67 | bool doLog = (bool) property_get_bool("ro.test_harness", 0); |
Glenn Kasten | e2cf829 | 2020-09-14 11:40:32 -0700 | [diff] [blame] | 68 | #endif |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 69 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 70 | pid_t childPid; |
| 71 | // FIXME The advantage of making the process containing media.log service the parent process of |
| 72 | // the process that contains the other audio services, is that it allows us to collect more |
| 73 | // detailed information such as signal numbers, stop and continue, resource usage, etc. |
| 74 | // But it is also more complex. Consider replacing this by independent processes, and using |
| 75 | // binder on death notification instead. |
| 76 | if (doLog && (childPid = fork()) != 0) { |
| 77 | // media.log service |
| 78 | //prctl(PR_SET_NAME, (unsigned long) "media.log", 0, 0, 0); |
| 79 | // unfortunately ps ignores PR_SET_NAME for the main thread, so use this ugly hack |
| 80 | strcpy(argv[0], "media.log"); |
| 81 | sp<ProcessState> proc(ProcessState::self()); |
| 82 | MediaLogService::instantiate(); |
| 83 | ProcessState::self()->startThreadPool(); |
Eric Laurent | 9c0b3a3 | 2016-03-24 12:08:27 -0700 | [diff] [blame] | 84 | IPCThreadState::self()->joinThreadPool(); |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 85 | for (;;) { |
| 86 | siginfo_t info; |
Keith Mok | 7b3d5c3 | 2021-11-13 04:56:36 +0000 | [diff] [blame] | 87 | int ret = TEMP_FAILURE_RETRY(waitid(P_PID, childPid, &info, |
| 88 | WEXITED | WSTOPPED | WCONTINUED)); |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 89 | if (ret < 0) { |
| 90 | break; |
| 91 | } |
| 92 | char buffer[32]; |
| 93 | const char *code; |
| 94 | switch (info.si_code) { |
| 95 | case CLD_EXITED: |
| 96 | code = "CLD_EXITED"; |
| 97 | break; |
| 98 | case CLD_KILLED: |
| 99 | code = "CLD_KILLED"; |
| 100 | break; |
| 101 | case CLD_DUMPED: |
| 102 | code = "CLD_DUMPED"; |
| 103 | break; |
| 104 | case CLD_STOPPED: |
| 105 | code = "CLD_STOPPED"; |
| 106 | break; |
| 107 | case CLD_TRAPPED: |
| 108 | code = "CLD_TRAPPED"; |
| 109 | break; |
| 110 | case CLD_CONTINUED: |
| 111 | code = "CLD_CONTINUED"; |
| 112 | break; |
| 113 | default: |
| 114 | snprintf(buffer, sizeof(buffer), "unknown (%d)", info.si_code); |
| 115 | code = buffer; |
| 116 | break; |
| 117 | } |
| 118 | struct rusage usage; |
| 119 | getrusage(RUSAGE_CHILDREN, &usage); |
| 120 | ALOG(LOG_ERROR, "media.log", "pid %d status %d code %s user %ld.%03lds sys %ld.%03lds", |
| 121 | info.si_pid, info.si_status, code, |
| 122 | usage.ru_utime.tv_sec, usage.ru_utime.tv_usec / 1000, |
| 123 | usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 1000); |
| 124 | sp<IServiceManager> sm = defaultServiceManager(); |
| 125 | sp<IBinder> binder = sm->getService(String16("media.log")); |
| 126 | if (binder != 0) { |
| 127 | Vector<String16> args; |
| 128 | binder->dump(-1, args); |
| 129 | } |
| 130 | switch (info.si_code) { |
| 131 | case CLD_EXITED: |
| 132 | case CLD_KILLED: |
| 133 | case CLD_DUMPED: { |
| 134 | ALOG(LOG_INFO, "media.log", "exiting"); |
| 135 | _exit(0); |
| 136 | // not reached |
| 137 | } |
| 138 | default: |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | } else { |
| 143 | // all other services |
| 144 | if (doLog) { |
| 145 | prctl(PR_SET_PDEATHSIG, SIGKILL); // if parent media.log dies before me, kill me also |
| 146 | setpgid(0, 0); // but if I die first, don't kill my parent |
| 147 | } |
Mikhail Naganov | 00a21a3 | 2017-11-16 08:57:44 -0800 | [diff] [blame] | 148 | android::hardware::configureRpcThreadpool(4, false /*callerWillJoin*/); |
Andy Hung | 1d312b9 | 2023-01-03 16:12:50 -0800 | [diff] [blame] | 149 | |
| 150 | // Ensure threads for possible callbacks. Note that get_audio_flinger() does |
| 151 | // this automatically when called from AudioPolicy, but we do this anyways here. |
| 152 | ProcessState::self()->startThreadPool(); |
| 153 | |
| 154 | // Instantiating AudioFlinger (making it public, e.g. through ::initialize()) |
| 155 | // and then instantiating AudioPolicy (and making it public) |
| 156 | // leads to situations where AudioFlinger is accessed remotely before |
| 157 | // AudioPolicy is initialized. Not only might this |
| 158 | // cause inaccurate results, but if AudioPolicy has slow audio HAL |
| 159 | // initialization, it can cause a TimeCheck abort to occur on an AudioFlinger |
| 160 | // call which tries to access AudioPolicy. |
| 161 | // |
| 162 | // We create AudioFlinger and AudioPolicy locally then make it public to ServiceManager. |
| 163 | // This requires both AudioFlinger and AudioPolicy to be in-proc. |
| 164 | // |
| 165 | const auto af = sp<AudioFlinger>::make(); |
| 166 | const auto afAdapter = sp<AudioFlingerServerAdapter>::make(af); |
| 167 | ALOGD("%s: AudioFlinger created", __func__); |
Andy Hung | 66fc0bd | 2023-01-04 11:45:50 -0800 | [diff] [blame] | 168 | ALOGW_IF(AudioSystem::setLocalAudioFlinger(af) != OK, |
| 169 | "%s: AudioSystem already has an AudioFlinger instance!", __func__); |
Andy Hung | 1d312b9 | 2023-01-03 16:12:50 -0800 | [diff] [blame] | 170 | const auto aps = sp<AudioPolicyService>::make(); |
| 171 | ALOGD("%s: AudioPolicy created", __func__); |
| 172 | |
| 173 | // Add AudioFlinger and AudioPolicy to ServiceManager. |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 174 | sp<IServiceManager> sm = defaultServiceManager(); |
Andy Hung | 1d312b9 | 2023-01-03 16:12:50 -0800 | [diff] [blame] | 175 | sm->addService(String16(IAudioFlinger::DEFAULT_SERVICE_NAME), afAdapter, |
| 176 | false /* allowIsolated */, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT); |
| 177 | sm->addService(String16(AudioPolicyService::getServiceName()), aps, |
| 178 | false /* allowIsolated */, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT); |
Phil Burk | 5b38eb3 | 2017-09-29 15:06:40 -0700 | [diff] [blame] | 179 | |
| 180 | // AAudioService should only be used in OC-MR1 and later. |
| 181 | // And only enable the AAudioService if the system MMAP policy explicitly allows it. |
| 182 | // This prevents a client from misusing AAudioService when it is not supported. |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 183 | // If we cannot get audio flinger here, there must be some serious problems. In that case, |
| 184 | // attempting to call audio flinger on a null pointer could make the process crash |
| 185 | // and attract attentions. |
jiabin | e99d088 | 2021-09-17 05:21:25 +0000 | [diff] [blame] | 186 | std::vector<AudioMMapPolicyInfo> policyInfos; |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 187 | status_t status = af->getMmapPolicyInfos( |
jiabin | e99d088 | 2021-09-17 05:21:25 +0000 | [diff] [blame] | 188 | AudioMMapPolicyType::DEFAULT, &policyInfos); |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 189 | // Initialize aaudio service when querying mmap policy succeeds and |
| 190 | // any of the policy supports MMAP. |
| 191 | if (status == NO_ERROR && |
| 192 | std::any_of(policyInfos.begin(), policyInfos.end(), [](const auto& info) { |
jiabin | e99d088 | 2021-09-17 05:21:25 +0000 | [diff] [blame] | 193 | return info.mmapPolicy == AudioMMapPolicy::AUTO || |
| 194 | info.mmapPolicy == AudioMMapPolicy::ALWAYS; |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 195 | })) { |
Phil Burk | 5b38eb3 | 2017-09-29 15:06:40 -0700 | [diff] [blame] | 196 | AAudioService::instantiate(); |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 197 | } else { |
Andy Hung | 1d312b9 | 2023-01-03 16:12:50 -0800 | [diff] [blame] | 198 | ALOGD("%s: Do not init aaudio service, status %d, policy info size %zu", |
| 199 | __func__, status, policyInfos.size()); |
Phil Burk | 5b38eb3 | 2017-09-29 15:06:40 -0700 | [diff] [blame] | 200 | } |
Andy Hung | 1d312b9 | 2023-01-03 16:12:50 -0800 | [diff] [blame] | 201 | const auto endTime = std::chrono::steady_clock::now(); |
| 202 | using FloatMillis = std::chrono::duration<float, std::milli>; |
| 203 | const float timeTaken = std::chrono::duration_cast<FloatMillis>( |
| 204 | endTime - startTime).count(); |
| 205 | ALOGI("%s: initialization done in %.3f ms, joining thread pool", __func__, timeTaken); |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 206 | IPCThreadState::self()->joinThreadPool(); |
| 207 | } |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 208 | } |