• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "boot_video_player.h"
16 
17 #include "boot_animation_utils.h"
18 #include "log.h"
19 #include <media_errors.h>
20 #include "transaction/rs_interfaces.h"
21 #include <parameters.h>
22 #include <util.h>
23 
24 using namespace OHOS;
25 
26 namespace {
27     const std::vector<std::string> NORMAL_REBOOT_REASON_ARR = {"AP_S_COLDBOOT", "bootloader", "recovery", "fastbootd",
28         "resetfactory", "at2resetfactory", "atfactoryreset0", "resetuser", "sdupdate", "chargereboot", "resize",
29         "erecovery", "usbupdate", "cust", "oem_rtc", "UNKNOWN", "mountfail", "hungdetect", "COLDBOOT", "updatedataimg",
30         "AP_S_FASTBOOTFLASH", "gpscoldboot", "AP_S_COMBINATIONKEY", "CP_S_NORMALRESET", "IOM3_S_USER_EXCEPTION",
31         "BR_UPDATE_USB", "BR_UPDATA_SD_FORCE", "BR_KEY_VOLUMN_UP", "BR_PRESS_1S", "BR_CHECK_RECOVERY",
32         "BR_CHECK_ERECOVERY", "BR_CHECK_SDUPDATE", "BR_CHECK_USBUPDATE", "BR_CHECK_RESETFACTORY",
33         "BR_CHECK_HOTAUPDATE", "BR_POWERONNOBAT", "BR_NOGUI", "BR_FACTORY_VERSION", "BR_RESET_HAPPEN",
34         "BR_POWEROFF_ALARM", "BR_POWEROFF_CHARGE", "BR_POWERON_BY_SMPL", "BR_CHECK_UPDATEDATAIMG",
35         "BR_POWERON_CHARGE", "AP_S_PRESS6S", "BR_PRESS_10S"};
36 }
37 
BootVideoPlayer(const PlayerParams & params)38 BootVideoPlayer::BootVideoPlayer(const PlayerParams& params)
39 {
40     screenId_ = params.screenId;
41     resPath_ = params.resPath;
42 #ifdef PLAYER_FRAMEWORK_ENABLE
43     surface_ = params.surface;
44 #endif
45     SetCallback(params.callback);
46     isSoundEnabled_ = params.soundEnabled;
47 }
48 
Play()49 void BootVideoPlayer::Play()
50 {
51 #ifdef PLAYER_FRAMEWORK_ENABLE
52     LOGI("PlayVideo begin");
53     int64_t startTime = GetSystemCurrentTime();
54     int64_t endTime = startTime;
55     while ((endTime - startTime) < MAX_WAIT_MEDIA_CREATE_TIME
56         && (mediaPlayer_ = Media::PlayerFactory::CreatePlayer()) == nullptr) {
57         endTime = GetSystemCurrentTime();
58         usleep(SLEEP_TIME_US);
59         LOGI("mediaPlayer is nullptr, try create again");
60     }
61 
62     if (mediaPlayer_ == nullptr) {
63         LOGI("mediaPlayer create fail");
64         return;
65     }
66 
67     std::shared_ptr<VideoPlayerCallback> cb = std::make_shared<VideoPlayerCallback>(shared_from_this());
68     int32_t ret = mediaPlayer_->SetPlayerCallback(cb);
69     if (ret != 0) {
70         LOGE("PlayVideo SetPlayerCallback fail, errorCode: %{public}d", ret);
71         return;
72     }
73     std::string path = GetResPath(TYPE_VIDEO);
74     LOGI("video res path: %{public}s", path.c_str());
75     ret = mediaPlayer_->SetSource(path);
76     if (ret != 0) {
77         LOGE("PlayVideo SetSource fail, errorCode: %{public}d", ret);
78         return;
79     }
80     if (surface_ == nullptr) {
81         LOGE("PlayVideo surface is null");
82         return;
83     }
84     ret = mediaPlayer_->SetVideoSurface(surface_);
85     if (ret != 0) {
86         LOGE("PlayVideo SetVideoSurface fail, errorCode: %{public}d", ret);
87         return;
88     }
89 
90     if (!SetVideoSound()) {
91         LOGW("SetVideoSound failed");
92     }
93 
94     ret = mediaPlayer_->Prepare();
95     if (ret !=  0) {
96         LOGE("PlayVideo Prepare fail, errorCode: %{public}d", ret);
97         return;
98     }
99     LOGI("PlayVideo end");
100 #else
101     LOGI("player framework is disabled");
102 #endif
103 }
104 
SetVideoSound()105 bool BootVideoPlayer::SetVideoSound()
106 {
107 #ifdef PLAYER_FRAMEWORK_ENABLE
108     LOGI("SetVideoSound start");
109     if (!isSoundEnabled_) {
110         LOGI("sound disabled on screen: " BPUBU64 "", screenId_);
111         mediaPlayer_->SetVolume(0, 0);
112         return true;
113     }
114 
115     int ret = mediaPlayer_->SetParameter(buildMediaFormat());
116     if (ret != 0) {
117         LOGE("PlayVideo SetParameter fail, errorCode:%{public}d", ret);
118         return false;
119     }
120 
121     bool bootSoundEnabled = BootAnimationUtils::GetBootAnimationSoundEnabled();
122     if (!bootSoundEnabled || !IsNormalBoot()) {
123         if (!SetCustomizedVolume(0)) {
124             return false;
125         }
126     } else {
127         int customizedVolume = system::GetIntParameter(BOOT_SOUND, INVALID_VOLUME, MIN_VOLUME, MAX_VOLUME);
128         if (customizedVolume != INVALID_VOLUME && !SetCustomizedVolume(customizedVolume)) {
129             return false;
130         }
131     }
132     return true;
133 #endif
134 }
135 
SetCallback(const BootAnimationCallback * cb)136 void BootVideoPlayer::SetCallback(const BootAnimationCallback* cb)
137 {
138     vSyncCallback_ = cb->callback;
139     userData_ = cb->userData;
140 }
141 
142 #ifdef PLAYER_FRAMEWORK_ENABLE
GetMediaPlayer() const143 std::shared_ptr<Media::Player> BootVideoPlayer::GetMediaPlayer() const
144 {
145     return mediaPlayer_;
146 }
147 #endif
148 
StopVideo()149 void BootVideoPlayer::StopVideo()
150 {
151     vSyncCallback_(userData_);
152 }
153 
IsNormalBoot()154 bool BootVideoPlayer::IsNormalBoot()
155 {
156     std::string bootReason = system::GetParameter("ohos.boot.reboot_reason", "");
157     LOGI("bootReason: %{public}s", bootReason.c_str());
158     if (std::find(NORMAL_REBOOT_REASON_ARR.begin(), NORMAL_REBOOT_REASON_ARR.end(), bootReason)
159         != NORMAL_REBOOT_REASON_ARR.end()) {
160         LOGI("normal boot");
161         return true;
162     }
163     return false;
164 }
165 
166 #ifdef PLAYER_FRAMEWORK_ENABLE
167 // PlayerCallback override
OnError(int32_t errorCode,const std::string & errorMsg)168 void VideoPlayerCallback::OnError(int32_t errorCode, const std::string &errorMsg)
169 {
170     LOGE("PlayerCallbackError received, errorMsg:%{public}s", errorMsg.c_str());
171     auto boot = boot_.lock();
172     if (boot) {
173         boot->StopVideo();
174     }
175 }
176 #endif
177 
178 #ifdef PLAYER_FRAMEWORK_ENABLE
OnInfo(Media::PlayerOnInfoType type,int32_t extra,const Media::Format & infoBody)179 void VideoPlayerCallback::OnInfo(Media::PlayerOnInfoType type, int32_t extra, const Media::Format &infoBody)
180 {
181     switch (type) {
182         case Media::INFO_TYPE_SEEKDONE:
183             LOGI("PlayerCallback: OnSeekDone currentPositon is: %{public}d", extra);
184             break;
185         case Media::INFO_TYPE_SPEEDDONE:
186             LOGI("PlayerCallback: SpeedDone");
187             break;
188         case Media::INFO_TYPE_BITRATEDONE:
189             LOGI("PlayerCallback: BitRateDone");
190             break;
191         case Media::INFO_TYPE_BUFFERING_UPDATE:
192             LOGI("PlayerCallback: Buffering Update");
193             break;
194         case Media::INFO_TYPE_BITRATE_COLLECT:
195             LOGI("PlayerCallback: Bitrate Collect");
196             break;
197         case Media::INFO_TYPE_POSITION_UPDATE:
198             LOGD("PlayerCallback: Position Update");
199             break;
200         case Media::INFO_TYPE_RESOLUTION_CHANGE:
201             LOGI("PlayerCallback: Resolution Change");
202             break;
203         case Media::INFO_TYPE_VOLUME_CHANGE:
204             LOGI("PlayerCallback: Volume Changed");
205             break;
206         default:
207             OnOperateInfo(type, extra);
208             break;
209     }
210 }
211 #endif
212 
213 #ifdef PLAYER_FRAMEWORK_ENABLE
OnOperateInfo(Media::PlayerOnInfoType type,int32_t extra)214 void VideoPlayerCallback::OnOperateInfo(Media::PlayerOnInfoType type, int32_t extra)
215 {
216     auto boot = boot_.lock();
217     if (!boot) {
218         LOGI("PlayerCallback: boot error");
219         return;
220     }
221     switch (type) {
222         case Media::INFO_TYPE_EOS: {
223             LOGI("PlayerCallback: OnEndOfStream isLooping is: %{public}d", extra);
224             boot->StopVideo();
225             break;
226         }
227         case Media::INFO_TYPE_STATE_CHANGE:
228             LOGI("PlayerCallback: State Change, current state is: %{public}d", extra);
229             if (Media::PlayerStates::PLAYER_PREPARED == extra) {
230                 LOGI("Begin to play");
231                 boot->GetMediaPlayer()->Play();
232             }
233             break;
234         case Media::INFO_TYPE_MESSAGE:
235             LOGI("PlayerCallback: OnMessage is: %{public}d", extra);
236             if (!system::GetBoolParameter(BOOT_ANIMATION_STARTED, false)) {
237                 system::SetParameter(BOOT_ANIMATION_STARTED, "true");
238             }
239             break;
240         default:
241             LOGI("PlayerCallback: Default");
242             break;
243     }
244 }
245 #endif