blob: 8501982d071c4d25906164387f9407ecc6ad2a2e [file] [log] [blame]
Mathias Agopian627e7b52009-05-21 19:21:59 -07001/*
2 * Copyright (C) 2007 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 "BootAnimation"
18
Keun-young Park721c9dc2017-05-19 17:13:04 -070019#include <stdint.h>
20#include <inttypes.h>
21
Mathias Agopianac31a3b2009-05-21 19:59:24 -070022#include <binder/IPCThreadState.h>
23#include <binder/ProcessState.h>
24#include <binder/IServiceManager.h>
Yabin Cui16104862015-01-26 19:43:58 -080025#include <cutils/properties.h>
26#include <sys/resource.h>
Mathias Agopian627e7b52009-05-21 19:21:59 -070027#include <utils/Log.h>
Keun-young Park721c9dc2017-05-19 17:13:04 -070028#include <utils/SystemClock.h>
Mathias Agopian627e7b52009-05-21 19:21:59 -070029#include <utils/threads.h>
Ed Coyne2c9e94a2017-05-31 10:08:28 -070030#include <android-base/properties.h>
Mathias Agopian627e7b52009-05-21 19:21:59 -070031
Mathias Agopian627e7b52009-05-21 19:21:59 -070032#include "BootAnimation.h"
Ed Coyne7464ac92017-06-08 12:26:48 -070033#include "BootAnimationUtil.h"
Ed Coyne2c9e94a2017-05-31 10:08:28 -070034#include "audioplay.h"
Mathias Agopian627e7b52009-05-21 19:21:59 -070035
36using namespace android;
37
38// ---------------------------------------------------------------------------
39
Ed Coyne2c9e94a2017-05-31 10:08:28 -070040namespace {
41
42// Create a typedef for readability.
43typedef android::BootAnimation::Animation Animation;
44
45static const char PLAY_SOUND_PROP_NAME[] = "persist.sys.bootanim.play_sound";
46static const char BOOT_COMPLETED_PROP_NAME[] = "sys.boot_completed";
47static const char POWER_CTL_PROP_NAME[] = "sys.powerctl";
48static const char BOOTREASON_PROP_NAME[] = "ro.boot.bootreason";
49static const std::vector<std::string> PLAY_SOUND_BOOTREASON_BLACKLIST {
50 "kernel_panic",
51 "Panic",
52 "Watchdog",
53};
54
55class InitAudioThread : public Thread {
56public:
57 InitAudioThread(uint8_t* exampleAudioData, int exampleAudioLength)
58 : Thread(false),
59 mExampleAudioData(exampleAudioData),
60 mExampleAudioLength(exampleAudioLength) {}
61private:
62 virtual bool threadLoop() {
63 audioplay::create(mExampleAudioData, mExampleAudioLength);
64 // Exit immediately
65 return false;
66 }
67
68 uint8_t* mExampleAudioData;
69 int mExampleAudioLength;
70};
71
72bool playSoundsAllowed() {
73 // Only play sounds for system boots, not runtime restarts.
74 if (android::base::GetBoolProperty(BOOT_COMPLETED_PROP_NAME, false)) {
75 return false;
76 }
77 // no audio while shutting down
78 if (!android::base::GetProperty(POWER_CTL_PROP_NAME, "").empty()) {
79 return false;
80 }
81 // Read the system property to see if we should play the sound.
82 // If it's not present, default to allowed.
83 if (!property_get_bool(PLAY_SOUND_PROP_NAME, 1)) {
84 return false;
85 }
86
87 // Don't play sounds if this is a reboot due to an error.
88 char bootreason[PROPERTY_VALUE_MAX];
89 if (property_get(BOOTREASON_PROP_NAME, bootreason, nullptr) > 0) {
90 for (const auto& str : PLAY_SOUND_BOOTREASON_BLACKLIST) {
91 if (strcasecmp(str.c_str(), bootreason) == 0) {
92 return false;
93 }
94 }
95 }
96 return true;
97}
98
Ed Coyne7464ac92017-06-08 12:26:48 -070099class AudioAnimationCallbacks : public android::BootAnimation::Callbacks {
100public:
101 void init(const Vector<Animation::Part>& parts) override {
102 const Animation::Part* partWithAudio = nullptr;
103 for (const Animation::Part& part : parts) {
104 if (part.audioData != nullptr) {
105 partWithAudio = &part;
106 }
107 }
108
109 if (partWithAudio == nullptr) {
110 return;
111 }
112
113 ALOGD("found audio.wav, creating playback engine");
114 initAudioThread = new InitAudioThread(partWithAudio->audioData,
115 partWithAudio->audioLength);
116 initAudioThread->run("BootAnimation::InitAudioThread", PRIORITY_NORMAL);
117 };
118
119 void playPart(int partNumber, const Animation::Part& part, int playNumber) override {
120 // only play audio file the first time we animate the part
121 if (playNumber == 0 && part.audioData && playSoundsAllowed()) {
122 ALOGD("playing clip for part%d, size=%d",
123 partNumber, part.audioLength);
124 // Block until the audio engine is finished initializing.
125 if (initAudioThread != nullptr) {
126 initAudioThread->join();
127 }
128 audioplay::playClip(part.audioData, part.audioLength);
129 }
130 };
131
132 void shutdown() override {
133 // we've finally played everything we're going to play
134 audioplay::setPlaying(false);
135 audioplay::destroy();
136 };
137
138private:
139 sp<InitAudioThread> initAudioThread = nullptr;
140};
141
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700142} // namespace
143
144
Andreas Gampecfedceb2014-09-30 21:48:18 -0700145int main()
Mathias Agopian627e7b52009-05-21 19:21:59 -0700146{
Mathias Agopian627e7b52009-05-21 19:21:59 -0700147 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700148
Ed Coyne7464ac92017-06-08 12:26:48 -0700149 bool noBootAnimation = bootAnimationDisabled();
Steve Block6215d3f2012-01-04 20:05:49 +0000150 ALOGI_IF(noBootAnimation, "boot animation disabled");
Mathias Agopiande363132009-07-28 15:27:39 -0700151 if (!noBootAnimation) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700152
Mathias Agopiande363132009-07-28 15:27:39 -0700153 sp<ProcessState> proc(ProcessState::self());
154 ProcessState::self()->startThreadPool();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700155
Ed Coyne7464ac92017-06-08 12:26:48 -0700156 waitForSurfaceFlinger();
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700157
Mathias Agopiande363132009-07-28 15:27:39 -0700158 // create the boot animation object
Ed Coyne7464ac92017-06-08 12:26:48 -0700159 sp<BootAnimation> boot = new BootAnimation(new AudioAnimationCallbacks());
Kalle Raita88efa562017-07-14 16:18:16 -0700160 ALOGV("Boot animation set up. Joining pool.");
Mathias Agopiande363132009-07-28 15:27:39 -0700161
162 IPCThreadState::self()->joinThreadPool();
Mathias Agopiande363132009-07-28 15:27:39 -0700163 }
Kalle Raita88efa562017-07-14 16:18:16 -0700164 ALOGV("Boot animation exit");
Mathias Agopian627e7b52009-05-21 19:21:59 -0700165 return 0;
166}