• 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     CheckAndCreateMedia();
54 
55     if (mediaPlayer_ == nullptr) {
56         LOGI("mediaPlayer create fail");
57         return;
58     }
59 
60     std::shared_ptr<VideoPlayerCallback> cb = std::make_shared<VideoPlayerCallback>(shared_from_this());
61     int32_t ret = mediaPlayer_->SetPlayerCallback(cb);
62     if (ret != 0) {
63         LOGE("PlayVideo SetPlayerCallback fail, errorCode: %{public}d", ret);
64         return;
65     }
66     std::string path = GetResPath(TYPE_VIDEO);
67     ret = mediaPlayer_->SetSource(path);
68     if (ret != 0) {
69         LOGE("PlayVideo SetSource fail, errorCode: %{public}d", ret);
70         return;
71     }
72     if (surface_ == nullptr) {
73         LOGE("PlayVideo surface is null");
74         return;
75     }
76     ret = mediaPlayer_->SetVideoSurface(surface_);
77     if (ret != 0) {
78         LOGE("PlayVideo SetVideoSurface fail, errorCode: %{public}d", ret);
79         return;
80     }
81 
82     if (!SetVideoSound()) {
83         LOGW("SetVideoSound failed");
84     }
85 
86     ret = mediaPlayer_->Prepare();
87     if (ret !=  0) {
88         LOGE("PlayVideo Prepare fail, errorCode: %{public}d", ret);
89         return;
90     }
91     LOGI("PlayVideo end");
92 #else
93     LOGI("player framework is disabled");
94 #endif
95 }
96 
SetVideoSound()97 bool BootVideoPlayer::SetVideoSound()
98 {
99 #ifdef PLAYER_FRAMEWORK_ENABLE
100     LOGI("SetVideoSound start");
101     if (!isSoundEnabled_) {
102         LOGI("sound disabled on screen: " BPUBU64 "", screenId_);
103         mediaPlayer_->SetVolume(0, 0);
104         return true;
105     }
106 
107     int ret = mediaPlayer_->SetParameter(buildMediaFormat());
108     if (ret != 0) {
109         LOGE("PlayVideo SetParameter fail, errorCode:%{public}d", ret);
110         return false;
111     }
112 
113     bool bootSoundEnabled = BootAnimationUtils::GetBootAnimationSoundEnabled();
114     if (!bootSoundEnabled || !IsNormalBoot()) {
115         if (!SetCustomizedVolume(0)) {
116             return false;
117         }
118     } else {
119         int customizedVolume = system::GetIntParameter(BOOT_SOUND, INVALID_VOLUME, MIN_VOLUME, MAX_VOLUME);
120         if (customizedVolume != INVALID_VOLUME && !SetCustomizedVolume(customizedVolume)) {
121             return false;
122         }
123     }
124     return true;
125 #endif
126 }
127 
SetCallback(const BootAnimationCallback * cb)128 void BootVideoPlayer::SetCallback(const BootAnimationCallback* cb)
129 {
130     vSyncCallback_ = cb->callback;
131     userData_ = cb->userData;
132 }
133 
134 #ifdef PLAYER_FRAMEWORK_ENABLE
GetMediaPlayer() const135 std::shared_ptr<Media::Player> BootVideoPlayer::GetMediaPlayer() const
136 {
137     return mediaPlayer_;
138 }
139 #endif
140 
StopVideo()141 void BootVideoPlayer::StopVideo()
142 {
143     vSyncCallback_(userData_);
144 }
145 
IsNormalBoot()146 bool BootVideoPlayer::IsNormalBoot()
147 {
148     std::string bootReason = system::GetParameter("ohos.boot.reboot_reason", "");
149     LOGI("bootReason: %{public}s", bootReason.c_str());
150     if (std::find(NORMAL_REBOOT_REASON_ARR.begin(), NORMAL_REBOOT_REASON_ARR.end(), bootReason)
151         != NORMAL_REBOOT_REASON_ARR.end()) {
152         LOGI("normal boot");
153         return true;
154     }
155     return false;
156 }
157 
158 #ifdef PLAYER_FRAMEWORK_ENABLE
159 // PlayerCallback override
OnError(int32_t errorCode,const std::string & errorMsg)160 void VideoPlayerCallback::OnError(int32_t errorCode, const std::string &errorMsg)
161 {
162     LOGE("PlayerCallbackError received, errorMsg:%{public}s", errorMsg.c_str());
163     auto boot = boot_.lock();
164     if (boot) {
165         boot->StopVideo();
166     }
167 }
168 #endif
169 
170 #ifdef PLAYER_FRAMEWORK_ENABLE
OnInfo(Media::PlayerOnInfoType type,int32_t extra,const Media::Format & infoBody)171 void VideoPlayerCallback::OnInfo(Media::PlayerOnInfoType type, int32_t extra, const Media::Format &infoBody)
172 {
173     switch (type) {
174         case Media::INFO_TYPE_SEEKDONE:
175             LOGI("PlayerCallback: OnSeekDone currentPositon is: %{public}d", extra);
176             break;
177         case Media::INFO_TYPE_SPEEDDONE:
178             LOGI("PlayerCallback: SpeedDone");
179             break;
180         case Media::INFO_TYPE_BITRATEDONE:
181             LOGI("PlayerCallback: BitRateDone");
182             break;
183         case Media::INFO_TYPE_BUFFERING_UPDATE:
184             LOGI("PlayerCallback: Buffering Update");
185             break;
186         case Media::INFO_TYPE_BITRATE_COLLECT:
187             LOGI("PlayerCallback: Bitrate Collect");
188             break;
189         case Media::INFO_TYPE_POSITION_UPDATE:
190             LOGD("PlayerCallback: Position Update");
191             break;
192         case Media::INFO_TYPE_RESOLUTION_CHANGE:
193             LOGI("PlayerCallback: Resolution Change");
194             break;
195         case Media::INFO_TYPE_VOLUME_CHANGE:
196             LOGI("PlayerCallback: Volume Changed");
197             break;
198         default:
199             OnOperateInfo(type, extra);
200             break;
201     }
202 }
203 #endif
204 
205 #ifdef PLAYER_FRAMEWORK_ENABLE
OnOperateInfo(Media::PlayerOnInfoType type,int32_t extra)206 void VideoPlayerCallback::OnOperateInfo(Media::PlayerOnInfoType type, int32_t extra)
207 {
208     auto boot = boot_.lock();
209     if (!boot) {
210         LOGI("PlayerCallback: boot error");
211         return;
212     }
213     switch (type) {
214         case Media::INFO_TYPE_EOS: {
215             LOGI("PlayerCallback: OnEndOfStream isLooping is: %{public}d", extra);
216             boot->StopVideo();
217             break;
218         }
219         case Media::INFO_TYPE_STATE_CHANGE:
220             LOGI("PlayerCallback: State Change, current state is: %{public}d", extra);
221             if (Media::PlayerStates::PLAYER_PREPARED == extra) {
222                 LOGI("Begin to play");
223                 boot->GetMediaPlayer()->Play();
224             }
225             break;
226         case Media::INFO_TYPE_MESSAGE:
227             LOGI("PlayerCallback: OnMessage is: %{public}d", extra);
228             if (!system::GetBoolParameter(BOOT_ANIMATION_STARTED, false)) {
229                 system::SetParameter(BOOT_ANIMATION_STARTED, "true");
230             }
231             break;
232         default:
233             LOGI("PlayerCallback: Default");
234             break;
235     }
236 }
237 #endif