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 | 57c44c0 | 2021-08-31 23:18:35 +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 | 57c44c0 | 2021-08-31 23:18:35 +0000 | [diff] [blame^] | 27 | #include <android/media/AudioMMapPolicy.h> |
| 28 | #include <android/media/AudioMMapPolicyInfo.h> |
| 29 | #include <android/media/AudioMMapPolicyType.h> |
| 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> |
jiabin | 57c44c0 | 2021-08-31 23:18:35 +0000 | [diff] [blame^] | 34 | #include <binder/IInterface.h> |
Mikhail Naganov | 00a21a3 | 2017-11-16 08:57:44 -0800 | [diff] [blame] | 35 | #include <hidl/HidlTransportSupport.h> |
Andy Hung | d7e0dd4 | 2020-03-27 16:42:34 -0700 | [diff] [blame] | 36 | #include <mediautils/LimitProcessMemory.h> |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 37 | #include <utils/Log.h> |
| 38 | |
Glenn Kasten | 1688e1d | 2020-04-06 12:58:24 -0700 | [diff] [blame] | 39 | // from include_dirs |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 40 | #include "AudioFlinger.h" |
| 41 | #include "AudioPolicyService.h" |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 42 | #include "AAudioService.h" |
Phil Burk | 5b38eb3 | 2017-09-29 15:06:40 -0700 | [diff] [blame] | 43 | #include "utility/AAudioUtilities.h" |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 44 | #include "MediaLogService.h" |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 45 | |
| 46 | using namespace android; |
| 47 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 48 | int main(int argc __unused, char **argv) |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 49 | { |
Andy Hung | 2105271 | 2017-12-07 11:45:37 -0800 | [diff] [blame] | 50 | // TODO: update with refined parameters |
| 51 | limitProcessMemory( |
| 52 | "audio.maxmem", /* "ro.audio.maxmem", property that defines limit */ |
| 53 | (size_t)512 * (1 << 20), /* SIZE_MAX, upper limit in bytes */ |
| 54 | 20 /* upper limit as percentage of physical RAM */); |
| 55 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 56 | signal(SIGPIPE, SIG_IGN); |
| 57 | |
Glenn Kasten | e2cf829 | 2020-09-14 11:40:32 -0700 | [diff] [blame] | 58 | #if 1 |
| 59 | // FIXME See bug 165702394 and bug 168511485 |
| 60 | const bool doLog = false; |
| 61 | #else |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 62 | bool doLog = (bool) property_get_bool("ro.test_harness", 0); |
Glenn Kasten | e2cf829 | 2020-09-14 11:40:32 -0700 | [diff] [blame] | 63 | #endif |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 64 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 65 | pid_t childPid; |
| 66 | // FIXME The advantage of making the process containing media.log service the parent process of |
| 67 | // the process that contains the other audio services, is that it allows us to collect more |
| 68 | // detailed information such as signal numbers, stop and continue, resource usage, etc. |
| 69 | // But it is also more complex. Consider replacing this by independent processes, and using |
| 70 | // binder on death notification instead. |
| 71 | if (doLog && (childPid = fork()) != 0) { |
| 72 | // media.log service |
| 73 | //prctl(PR_SET_NAME, (unsigned long) "media.log", 0, 0, 0); |
| 74 | // unfortunately ps ignores PR_SET_NAME for the main thread, so use this ugly hack |
| 75 | strcpy(argv[0], "media.log"); |
| 76 | sp<ProcessState> proc(ProcessState::self()); |
| 77 | MediaLogService::instantiate(); |
| 78 | ProcessState::self()->startThreadPool(); |
Eric Laurent | 9c0b3a3 | 2016-03-24 12:08:27 -0700 | [diff] [blame] | 79 | IPCThreadState::self()->joinThreadPool(); |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 80 | for (;;) { |
| 81 | siginfo_t info; |
| 82 | int ret = waitid(P_PID, childPid, &info, WEXITED | WSTOPPED | WCONTINUED); |
| 83 | if (ret == EINTR) { |
| 84 | continue; |
| 85 | } |
| 86 | if (ret < 0) { |
| 87 | break; |
| 88 | } |
| 89 | char buffer[32]; |
| 90 | const char *code; |
| 91 | switch (info.si_code) { |
| 92 | case CLD_EXITED: |
| 93 | code = "CLD_EXITED"; |
| 94 | break; |
| 95 | case CLD_KILLED: |
| 96 | code = "CLD_KILLED"; |
| 97 | break; |
| 98 | case CLD_DUMPED: |
| 99 | code = "CLD_DUMPED"; |
| 100 | break; |
| 101 | case CLD_STOPPED: |
| 102 | code = "CLD_STOPPED"; |
| 103 | break; |
| 104 | case CLD_TRAPPED: |
| 105 | code = "CLD_TRAPPED"; |
| 106 | break; |
| 107 | case CLD_CONTINUED: |
| 108 | code = "CLD_CONTINUED"; |
| 109 | break; |
| 110 | default: |
| 111 | snprintf(buffer, sizeof(buffer), "unknown (%d)", info.si_code); |
| 112 | code = buffer; |
| 113 | break; |
| 114 | } |
| 115 | struct rusage usage; |
| 116 | getrusage(RUSAGE_CHILDREN, &usage); |
| 117 | ALOG(LOG_ERROR, "media.log", "pid %d status %d code %s user %ld.%03lds sys %ld.%03lds", |
| 118 | info.si_pid, info.si_status, code, |
| 119 | usage.ru_utime.tv_sec, usage.ru_utime.tv_usec / 1000, |
| 120 | usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 1000); |
| 121 | sp<IServiceManager> sm = defaultServiceManager(); |
| 122 | sp<IBinder> binder = sm->getService(String16("media.log")); |
| 123 | if (binder != 0) { |
| 124 | Vector<String16> args; |
| 125 | binder->dump(-1, args); |
| 126 | } |
| 127 | switch (info.si_code) { |
| 128 | case CLD_EXITED: |
| 129 | case CLD_KILLED: |
| 130 | case CLD_DUMPED: { |
| 131 | ALOG(LOG_INFO, "media.log", "exiting"); |
| 132 | _exit(0); |
| 133 | // not reached |
| 134 | } |
| 135 | default: |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | } else { |
| 140 | // all other services |
| 141 | if (doLog) { |
| 142 | prctl(PR_SET_PDEATHSIG, SIGKILL); // if parent media.log dies before me, kill me also |
| 143 | setpgid(0, 0); // but if I die first, don't kill my parent |
| 144 | } |
Mikhail Naganov | 00a21a3 | 2017-11-16 08:57:44 -0800 | [diff] [blame] | 145 | android::hardware::configureRpcThreadpool(4, false /*callerWillJoin*/); |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 146 | sp<ProcessState> proc(ProcessState::self()); |
| 147 | sp<IServiceManager> sm = defaultServiceManager(); |
| 148 | ALOGI("ServiceManager: %p", sm.get()); |
| 149 | AudioFlinger::instantiate(); |
| 150 | AudioPolicyService::instantiate(); |
Phil Burk | 5b38eb3 | 2017-09-29 15:06:40 -0700 | [diff] [blame] | 151 | |
| 152 | // AAudioService should only be used in OC-MR1 and later. |
| 153 | // And only enable the AAudioService if the system MMAP policy explicitly allows it. |
| 154 | // This prevents a client from misusing AAudioService when it is not supported. |
jiabin | 57c44c0 | 2021-08-31 23:18:35 +0000 | [diff] [blame^] | 155 | // If we cannot get audio flinger here, there must be some serious problems. In that case, |
| 156 | // attempting to call audio flinger on a null pointer could make the process crash |
| 157 | // and attract attentions. |
| 158 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 159 | std::vector<media::AudioMMapPolicyInfo> policyInfos; |
| 160 | status_t status = af->getMmapPolicyInfos( |
| 161 | media::AudioMMapPolicyType::DEFAULT, &policyInfos); |
| 162 | // Initialize aaudio service when querying mmap policy succeeds and |
| 163 | // any of the policy supports MMAP. |
| 164 | if (status == NO_ERROR || |
| 165 | std::any_of(policyInfos.begin(), policyInfos.end(), [](const auto& info) { |
| 166 | return info.mmapPolicy == media::AudioMMapPolicy::AUTO || |
| 167 | info.mmapPolicy == media::AudioMMapPolicy::ALWAYS; |
| 168 | })) { |
Phil Burk | 5b38eb3 | 2017-09-29 15:06:40 -0700 | [diff] [blame] | 169 | AAudioService::instantiate(); |
jiabin | 57c44c0 | 2021-08-31 23:18:35 +0000 | [diff] [blame^] | 170 | } else { |
| 171 | ALOGD("Do not init aaudio service, status %d, policy info size %zu", |
| 172 | status, policyInfos.size()); |
Phil Burk | 5b38eb3 | 2017-09-29 15:06:40 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 175 | ProcessState::self()->startThreadPool(); |
| 176 | IPCThreadState::self()->joinThreadPool(); |
| 177 | } |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 178 | } |