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
20 #include <algorithm>
21
22 #include <fcntl.h>
23 #include <sys/prctl.h>
24 #include <sys/wait.h>
25 #include <cutils/properties.h>
26
27 #include <android/media/audio/common/AudioMMapPolicy.h>
28 #include <android/media/audio/common/AudioMMapPolicyInfo.h>
29 #include <android/media/audio/common/AudioMMapPolicyType.h>
30 #include <android/media/IAudioFlingerService.h>
31 #include <binder/IPCThreadState.h>
32 #include <binder/ProcessState.h>
33 #include <binder/IServiceManager.h>
34 #include <hidl/HidlTransportSupport.h>
35 #include <mediautils/LimitProcessMemory.h>
36 #include <utils/Log.h>
37
38 // from include_dirs
39 #include "AudioFlinger.h"
40 #include "AudioPolicyService.h"
41 #include "AAudioService.h"
42 #include "utility/AAudioUtilities.h"
43
44 using namespace android;
45
46 using android::media::audio::common::AudioMMapPolicy;
47 using android::media::audio::common::AudioMMapPolicyInfo;
48 using android::media::audio::common::AudioMMapPolicyType;
49
main(int argc __unused,char ** argv __unused)50 int main(int argc __unused, char **argv __unused)
51 {
52 ALOGD("%s: starting", __func__);
53 const auto startTime = std::chrono::steady_clock::now();
54 // TODO: update with refined parameters
55 limitProcessMemory(
56 "audio.maxmem", /* "ro.audio.maxmem", property that defines limit */
57 (size_t)512 * (1 << 20), /* SIZE_MAX, upper limit in bytes */
58 20 /* upper limit as percentage of physical RAM */);
59
60 signal(SIGPIPE, SIG_IGN);
61
62 android::hardware::configureRpcThreadpool(4, false /*callerWillJoin*/);
63
64 // Ensure threads for possible callbacks. Note that get_audio_flinger() does
65 // this automatically when called from AudioPolicy, but we do this anyways here.
66 ProcessState::self()->startThreadPool();
67
68 // Instantiating AudioFlinger (making it public, e.g. through ::initialize())
69 // and then instantiating AudioPolicy (and making it public)
70 // leads to situations where AudioFlinger is accessed remotely before
71 // AudioPolicy is initialized. Not only might this
72 // cause inaccurate results, but if AudioPolicy has slow audio HAL
73 // initialization, it can cause a TimeCheck abort to occur on an AudioFlinger
74 // call which tries to access AudioPolicy.
75 //
76 // We create AudioFlinger and AudioPolicy locally then make it public to ServiceManager.
77 // This requires both AudioFlinger and AudioPolicy to be in-proc.
78 //
79 const auto af = sp<AudioFlinger>::make();
80 const auto afAdapter = sp<AudioFlingerServerAdapter>::make(af);
81 ALOGD("%s: AudioFlinger created", __func__);
82 ALOGW_IF(AudioSystem::setLocalAudioFlinger(af) != OK,
83 "%s: AudioSystem already has an AudioFlinger instance!", __func__);
84 const auto aps = sp<AudioPolicyService>::make();
85 af->initAudioPolicyLocal(aps);
86 ALOGD("%s: AudioPolicy created", __func__);
87 ALOGW_IF(AudioSystem::setLocalAudioPolicyService(aps) != OK,
88 "%s: AudioSystem already has an AudioPolicyService instance!", __func__);
89
90 // Start initialization of internally managed audio objects such as Device Effects.
91 aps->onAudioSystemReady();
92
93 // Add AudioFlinger and AudioPolicy to ServiceManager.
94 sp<IServiceManager> sm = defaultServiceManager();
95 sm->addService(String16(IAudioFlinger::DEFAULT_SERVICE_NAME), afAdapter,
96 false /* allowIsolated */, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
97 sm->addService(String16(AudioPolicyService::getServiceName()), aps,
98 false /* allowIsolated */, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
99
100 // AAudioService should only be used in OC-MR1 and later.
101 // And only enable the AAudioService if the system MMAP policy explicitly allows it.
102 // This prevents a client from misusing AAudioService when it is not supported.
103 // If we cannot get audio flinger here, there must be some serious problems. In that case,
104 // attempting to call audio flinger on a null pointer could make the process crash
105 // and attract attentions.
106 std::vector<AudioMMapPolicyInfo> policyInfos;
107 status_t status = AudioSystem::getMmapPolicyInfos(
108 AudioMMapPolicyType::DEFAULT, &policyInfos);
109 // Initialize aaudio service when querying mmap policy succeeds and
110 // any of the policy supports MMAP.
111 if (status == NO_ERROR &&
112 std::any_of(policyInfos.begin(), policyInfos.end(), [](const auto& info) {
113 return info.mmapPolicy == AudioMMapPolicy::AUTO ||
114 info.mmapPolicy == AudioMMapPolicy::ALWAYS;
115 })) {
116 AAudioService::instantiate();
117 } else {
118 ALOGD("%s: Do not init aaudio service, status %d, policy info size %zu",
119 __func__, status, policyInfos.size());
120 }
121 const auto endTime = std::chrono::steady_clock::now();
122 af->startupFinished();
123 using FloatMillis = std::chrono::duration<float, std::milli>;
124 const float timeTaken = std::chrono::duration_cast<FloatMillis>(
125 endTime - startTime).count();
126 ALOGI("%s: initialization done in %.3f ms, joining thread pool", __func__, timeTaken);
127 IPCThreadState::self()->joinThreadPool();
128 }
129