main_audioserver.cpp revision bf1c3e0ae1ae1ea015957b7b73362326f8a4a1d4
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 <fcntl.h>
21#include <sys/prctl.h>
22#include <sys/wait.h>
23#include <cutils/properties.h>
24
25#include <binder/IPCThreadState.h>
26#include <binder/ProcessState.h>
27#include <binder/IServiceManager.h>
28#include <utils/Log.h>
29
30// from LOCAL_C_INCLUDES
31#include "aaudio/AAudioTesting.h"
32#include "AudioFlinger.h"
33#include "AudioPolicyService.h"
34#include "AAudioService.h"
35#include "utility/AAudioUtilities.h"
36#include "MediaLogService.h"
37#include "SoundTriggerHwService.h"
38
39using namespace android;
40
41int main(int argc __unused, char **argv)
42{
43    signal(SIGPIPE, SIG_IGN);
44
45    bool doLog = (bool) property_get_bool("ro.test_harness", 0);
46
47    pid_t childPid;
48    // FIXME The advantage of making the process containing media.log service the parent process of
49    // the process that contains the other audio services, is that it allows us to collect more
50    // detailed information such as signal numbers, stop and continue, resource usage, etc.
51    // But it is also more complex.  Consider replacing this by independent processes, and using
52    // binder on death notification instead.
53    if (doLog && (childPid = fork()) != 0) {
54        // media.log service
55        //prctl(PR_SET_NAME, (unsigned long) "media.log", 0, 0, 0);
56        // unfortunately ps ignores PR_SET_NAME for the main thread, so use this ugly hack
57        strcpy(argv[0], "media.log");
58        sp<ProcessState> proc(ProcessState::self());
59        MediaLogService::instantiate();
60        ProcessState::self()->startThreadPool();
61        IPCThreadState::self()->joinThreadPool();
62        for (;;) {
63            siginfo_t info;
64            int ret = waitid(P_PID, childPid, &info, WEXITED | WSTOPPED | WCONTINUED);
65            if (ret == EINTR) {
66                continue;
67            }
68            if (ret < 0) {
69                break;
70            }
71            char buffer[32];
72            const char *code;
73            switch (info.si_code) {
74            case CLD_EXITED:
75                code = "CLD_EXITED";
76                break;
77            case CLD_KILLED:
78                code = "CLD_KILLED";
79                break;
80            case CLD_DUMPED:
81                code = "CLD_DUMPED";
82                break;
83            case CLD_STOPPED:
84                code = "CLD_STOPPED";
85                break;
86            case CLD_TRAPPED:
87                code = "CLD_TRAPPED";
88                break;
89            case CLD_CONTINUED:
90                code = "CLD_CONTINUED";
91                break;
92            default:
93                snprintf(buffer, sizeof(buffer), "unknown (%d)", info.si_code);
94                code = buffer;
95                break;
96            }
97            struct rusage usage;
98            getrusage(RUSAGE_CHILDREN, &usage);
99            ALOG(LOG_ERROR, "media.log", "pid %d status %d code %s user %ld.%03lds sys %ld.%03lds",
100                    info.si_pid, info.si_status, code,
101                    usage.ru_utime.tv_sec, usage.ru_utime.tv_usec / 1000,
102                    usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 1000);
103            sp<IServiceManager> sm = defaultServiceManager();
104            sp<IBinder> binder = sm->getService(String16("media.log"));
105            if (binder != 0) {
106                Vector<String16> args;
107                binder->dump(-1, args);
108            }
109            switch (info.si_code) {
110            case CLD_EXITED:
111            case CLD_KILLED:
112            case CLD_DUMPED: {
113                ALOG(LOG_INFO, "media.log", "exiting");
114                _exit(0);
115                // not reached
116                }
117            default:
118                break;
119            }
120        }
121    } else {
122        // all other services
123        if (doLog) {
124            prctl(PR_SET_PDEATHSIG, SIGKILL);   // if parent media.log dies before me, kill me also
125            setpgid(0, 0);                      // but if I die first, don't kill my parent
126        }
127        sp<ProcessState> proc(ProcessState::self());
128        sp<IServiceManager> sm = defaultServiceManager();
129        ALOGI("ServiceManager: %p", sm.get());
130        AudioFlinger::instantiate();
131        AudioPolicyService::instantiate();
132
133        // AAudioService should only be used in OC-MR1 and later.
134        // And only enable the AAudioService if the system MMAP policy explicitly allows it.
135        // This prevents a client from misusing AAudioService when it is not supported.
136        aaudio_policy_t mmapPolicy = property_get_int32(AAUDIO_PROP_MMAP_POLICY,
137                                                        AAUDIO_POLICY_NEVER);
138        if (mmapPolicy == AAUDIO_POLICY_AUTO || mmapPolicy == AAUDIO_POLICY_ALWAYS) {
139            AAudioService::instantiate();
140        }
141
142        SoundTriggerHwService::instantiate();
143        ProcessState::self()->startThreadPool();
144        IPCThreadState::self()->joinThreadPool();
145    }
146}
147