blob: e3db5b45b909b960978d295b003146b52588934d [file] [log] [blame]
Marco Nelissendcb346b2015-09-09 10:47:29 -07001/*
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 Huangebe64102021-09-07 20:01:07 +000020#include <algorithm>
21
Glenn Kastenae0cff12016-02-24 13:57:49 -080022#include <fcntl.h>
23#include <sys/prctl.h>
24#include <sys/wait.h>
25#include <cutils/properties.h>
26
jiabine99d0882021-09-17 05:21:25 +000027#include <android/media/audio/common/AudioMMapPolicy.h>
28#include <android/media/audio/common/AudioMMapPolicyInfo.h>
29#include <android/media/audio/common/AudioMMapPolicyType.h>
Jiabin Huangebe64102021-09-07 20:01:07 +000030#include <android/media/IAudioFlingerService.h>
Marco Nelissendcb346b2015-09-09 10:47:29 -070031#include <binder/IPCThreadState.h>
32#include <binder/ProcessState.h>
33#include <binder/IServiceManager.h>
Mikhail Naganov00a21a32017-11-16 08:57:44 -080034#include <hidl/HidlTransportSupport.h>
Andy Hungd7e0dd42020-03-27 16:42:34 -070035#include <mediautils/LimitProcessMemory.h>
Marco Nelissendcb346b2015-09-09 10:47:29 -070036#include <utils/Log.h>
37
Glenn Kasten1688e1d2020-04-06 12:58:24 -070038// from include_dirs
Marco Nelissendcb346b2015-09-09 10:47:29 -070039#include "AudioFlinger.h"
40#include "AudioPolicyService.h"
Phil Burk7f6b40d2017-02-09 13:18:38 -080041#include "AAudioService.h"
Phil Burk5b38eb32017-09-29 15:06:40 -070042#include "utility/AAudioUtilities.h"
Glenn Kastenae0cff12016-02-24 13:57:49 -080043#include "MediaLogService.h"
Marco Nelissendcb346b2015-09-09 10:47:29 -070044
45using namespace android;
46
jiabine99d0882021-09-17 05:21:25 +000047using android::media::audio::common::AudioMMapPolicy;
48using android::media::audio::common::AudioMMapPolicyInfo;
49using android::media::audio::common::AudioMMapPolicyType;
50
Glenn Kastenae0cff12016-02-24 13:57:49 -080051int main(int argc __unused, char **argv)
Marco Nelissendcb346b2015-09-09 10:47:29 -070052{
Andy Hung21052712017-12-07 11:45:37 -080053 // TODO: update with refined parameters
54 limitProcessMemory(
55 "audio.maxmem", /* "ro.audio.maxmem", property that defines limit */
56 (size_t)512 * (1 << 20), /* SIZE_MAX, upper limit in bytes */
57 20 /* upper limit as percentage of physical RAM */);
58
Marco Nelissendcb346b2015-09-09 10:47:29 -070059 signal(SIGPIPE, SIG_IGN);
60
Glenn Kastene2cf8292020-09-14 11:40:32 -070061#if 1
62 // FIXME See bug 165702394 and bug 168511485
63 const bool doLog = false;
64#else
Glenn Kastenae0cff12016-02-24 13:57:49 -080065 bool doLog = (bool) property_get_bool("ro.test_harness", 0);
Glenn Kastene2cf8292020-09-14 11:40:32 -070066#endif
Marco Nelissendcb346b2015-09-09 10:47:29 -070067
Glenn Kastenae0cff12016-02-24 13:57:49 -080068 pid_t childPid;
69 // FIXME The advantage of making the process containing media.log service the parent process of
70 // the process that contains the other audio services, is that it allows us to collect more
71 // detailed information such as signal numbers, stop and continue, resource usage, etc.
72 // But it is also more complex. Consider replacing this by independent processes, and using
73 // binder on death notification instead.
74 if (doLog && (childPid = fork()) != 0) {
75 // media.log service
76 //prctl(PR_SET_NAME, (unsigned long) "media.log", 0, 0, 0);
77 // unfortunately ps ignores PR_SET_NAME for the main thread, so use this ugly hack
78 strcpy(argv[0], "media.log");
79 sp<ProcessState> proc(ProcessState::self());
80 MediaLogService::instantiate();
81 ProcessState::self()->startThreadPool();
Eric Laurent9c0b3a32016-03-24 12:08:27 -070082 IPCThreadState::self()->joinThreadPool();
Glenn Kastenae0cff12016-02-24 13:57:49 -080083 for (;;) {
84 siginfo_t info;
Keith Mok7b3d5c32021-11-13 04:56:36 +000085 int ret = TEMP_FAILURE_RETRY(waitid(P_PID, childPid, &info,
86 WEXITED | WSTOPPED | WCONTINUED));
Glenn Kastenae0cff12016-02-24 13:57:49 -080087 if (ret < 0) {
88 break;
89 }
90 char buffer[32];
91 const char *code;
92 switch (info.si_code) {
93 case CLD_EXITED:
94 code = "CLD_EXITED";
95 break;
96 case CLD_KILLED:
97 code = "CLD_KILLED";
98 break;
99 case CLD_DUMPED:
100 code = "CLD_DUMPED";
101 break;
102 case CLD_STOPPED:
103 code = "CLD_STOPPED";
104 break;
105 case CLD_TRAPPED:
106 code = "CLD_TRAPPED";
107 break;
108 case CLD_CONTINUED:
109 code = "CLD_CONTINUED";
110 break;
111 default:
112 snprintf(buffer, sizeof(buffer), "unknown (%d)", info.si_code);
113 code = buffer;
114 break;
115 }
116 struct rusage usage;
117 getrusage(RUSAGE_CHILDREN, &usage);
118 ALOG(LOG_ERROR, "media.log", "pid %d status %d code %s user %ld.%03lds sys %ld.%03lds",
119 info.si_pid, info.si_status, code,
120 usage.ru_utime.tv_sec, usage.ru_utime.tv_usec / 1000,
121 usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 1000);
122 sp<IServiceManager> sm = defaultServiceManager();
123 sp<IBinder> binder = sm->getService(String16("media.log"));
124 if (binder != 0) {
125 Vector<String16> args;
126 binder->dump(-1, args);
127 }
128 switch (info.si_code) {
129 case CLD_EXITED:
130 case CLD_KILLED:
131 case CLD_DUMPED: {
132 ALOG(LOG_INFO, "media.log", "exiting");
133 _exit(0);
134 // not reached
135 }
136 default:
137 break;
138 }
139 }
140 } else {
141 // all other services
142 if (doLog) {
143 prctl(PR_SET_PDEATHSIG, SIGKILL); // if parent media.log dies before me, kill me also
144 setpgid(0, 0); // but if I die first, don't kill my parent
145 }
Mikhail Naganov00a21a32017-11-16 08:57:44 -0800146 android::hardware::configureRpcThreadpool(4, false /*callerWillJoin*/);
Glenn Kastenae0cff12016-02-24 13:57:49 -0800147 sp<ProcessState> proc(ProcessState::self());
148 sp<IServiceManager> sm = defaultServiceManager();
149 ALOGI("ServiceManager: %p", sm.get());
150 AudioFlinger::instantiate();
151 AudioPolicyService::instantiate();
Phil Burk5b38eb32017-09-29 15:06:40 -0700152
153 // AAudioService should only be used in OC-MR1 and later.
154 // And only enable the AAudioService if the system MMAP policy explicitly allows it.
155 // This prevents a client from misusing AAudioService when it is not supported.
Jiabin Huangebe64102021-09-07 20:01:07 +0000156 // If we cannot get audio flinger here, there must be some serious problems. In that case,
157 // attempting to call audio flinger on a null pointer could make the process crash
158 // and attract attentions.
159 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
jiabine99d0882021-09-17 05:21:25 +0000160 std::vector<AudioMMapPolicyInfo> policyInfos;
Jiabin Huangebe64102021-09-07 20:01:07 +0000161 status_t status = af->getMmapPolicyInfos(
jiabine99d0882021-09-17 05:21:25 +0000162 AudioMMapPolicyType::DEFAULT, &policyInfos);
Jiabin Huangebe64102021-09-07 20:01:07 +0000163 // Initialize aaudio service when querying mmap policy succeeds and
164 // any of the policy supports MMAP.
165 if (status == NO_ERROR &&
166 std::any_of(policyInfos.begin(), policyInfos.end(), [](const auto& info) {
jiabine99d0882021-09-17 05:21:25 +0000167 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
168 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
Jiabin Huangebe64102021-09-07 20:01:07 +0000169 })) {
Phil Burk5b38eb32017-09-29 15:06:40 -0700170 AAudioService::instantiate();
Jiabin Huangebe64102021-09-07 20:01:07 +0000171 } else {
172 ALOGD("Do not init aaudio service, status %d, policy info size %zu",
173 status, policyInfos.size());
Phil Burk5b38eb32017-09-29 15:06:40 -0700174 }
175
Glenn Kastenae0cff12016-02-24 13:57:49 -0800176 ProcessState::self()->startThreadPool();
177 IPCThreadState::self()->joinThreadPool();
178 }
Marco Nelissendcb346b2015-09-09 10:47:29 -0700179}