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
23 using namespace OHOS;
24 #ifdef PLAYER_FRAMEWORK_ENABLE
25 static const int CONTENT_TYPE_UNKNOWN = 0;
26 static const int STREAM_USAGE_ENFORCED_TONE = 15;
27 #endif
28
BootVideoPlayer(const PlayerParams & params)29 BootVideoPlayer::BootVideoPlayer(const PlayerParams& params)
30 {
31 screenId_ = params.screenId;
32 resPath_ = params.resPath;
33 #ifdef PLAYER_FRAMEWORK_ENABLE
34 surface_ = params.surface;
35 #endif
36 SetCallback(params.callback);
37 isSoundEnabled_ = params.soundEnabled;
38 }
39
Play()40 void BootVideoPlayer::Play()
41 {
42 #ifdef PLAYER_FRAMEWORK_ENABLE
43 LOGI("PlayVideo begin");
44 while ((mediaPlayer_ = Media::PlayerFactory::CreatePlayer()) == nullptr) {
45 LOGI("mediaPlayer is nullptr, try create again");
46 usleep(SLEEP_TIME_US);
47 }
48
49 std::shared_ptr<VideoPlayerCallback> cb = std::make_shared<VideoPlayerCallback>(shared_from_this());
50 int32_t ret = mediaPlayer_->SetPlayerCallback(cb);
51 if (ret != 0) {
52 LOGE("PlayVideo SetPlayerCallback fail, errorCode: %{public}d", ret);
53 return;
54 }
55 std::string path = GetResPath(TYPE_VIDEO);
56 LOGI("video res path: %{public}s", path.c_str());
57 ret = mediaPlayer_->SetSource(path);
58 if (ret != 0) {
59 LOGE("PlayVideo SetSource fail, errorCode: %{public}d", ret);
60 return;
61 }
62 if (surface_ == nullptr) {
63 LOGE("PlayVideo surface is null");
64 return;
65 }
66 ret = mediaPlayer_->SetVideoSurface(surface_);
67 if (ret != 0) {
68 LOGE("PlayVideo SetVideoSurface fail, errorCode: %{public}d", ret);
69 return;
70 }
71
72 if (!SetVideoSound()) {
73 LOGW("SetVideoSound failed");
74 }
75
76 ret = mediaPlayer_->Prepare();
77 if (ret != 0) {
78 LOGE("PlayVideo Prepare fail, errorCode: %{public}d", ret);
79 return;
80 }
81 LOGI("PlayVideo end");
82 #else
83 LOGI("player framework is disabled");
84 #endif
85 }
86
SetVideoSound()87 bool BootVideoPlayer::SetVideoSound()
88 {
89 #ifdef PLAYER_FRAMEWORK_ENABLE
90 LOGI("SetVideoSound start");
91 if (!isSoundEnabled_) {
92 LOGI("sound disabled on screen: " BPUBU64 "", screenId_);
93 mediaPlayer_->SetVolume(0, 0);
94 return true;
95 }
96 Media::Format format;
97 format.PutIntValue(Media::PlayerKeys::CONTENT_TYPE, CONTENT_TYPE_UNKNOWN);
98 format.PutIntValue(Media::PlayerKeys::STREAM_USAGE, STREAM_USAGE_ENFORCED_TONE);
99 format.PutIntValue(Media::PlayerKeys::RENDERER_FLAG, 0);
100 int ret = mediaPlayer_->SetParameter(format);
101 if (ret != 0) {
102 LOGE("PlayVideo SetParameter fail, errorCode:%{public}d", ret);
103 return false;
104 }
105
106 bool bootSoundEnabled = BootAnimationUtils::GetBootAnimationSoundEnabled();
107 if (!bootSoundEnabled) {
108 ret = mediaPlayer_->SetVolume(0, 0);
109 if (ret != 0) {
110 LOGE("PlayVideo SetVolume fail, errorCode:%{public}d", ret);
111 return false;
112 }
113 }
114 return true;
115 #endif
116 }
117
SetCallback(const BootAnimationCallback * cb)118 void BootVideoPlayer::SetCallback(const BootAnimationCallback* cb)
119 {
120 vSyncCallback_ = cb->callback;
121 userData_ = cb->userData;
122 }
123
124 #ifdef PLAYER_FRAMEWORK_ENABLE
GetMediaPlayer() const125 std::shared_ptr<Media::Player> BootVideoPlayer::GetMediaPlayer() const
126 {
127 return mediaPlayer_;
128 }
129 #endif
130
StopVideo()131 void BootVideoPlayer::StopVideo()
132 {
133 vSyncCallback_(userData_);
134 }
135
136 #ifdef PLAYER_FRAMEWORK_ENABLE
137 // PlayerCallback override
OnError(int32_t errorCode,const std::string & errorMsg)138 void VideoPlayerCallback::OnError(int32_t errorCode, const std::string &errorMsg)
139 {
140 LOGE("PlayerCallbackError received, errorMsg:%{public}s", errorMsg.c_str());
141 boot_->StopVideo();
142 }
143 #endif
144
145 #ifdef PLAYER_FRAMEWORK_ENABLE
OnInfo(Media::PlayerOnInfoType type,int32_t extra,const Media::Format & infoBody)146 void VideoPlayerCallback::OnInfo(Media::PlayerOnInfoType type, int32_t extra, const Media::Format &infoBody)
147 {
148 switch (type) {
149 case Media::INFO_TYPE_SEEKDONE:
150 LOGI("PlayerCallback: OnSeekDone currentPositon is: %{public}d", extra);
151 break;
152 case Media::INFO_TYPE_SPEEDDONE:
153 LOGI("PlayerCallback: SpeedDone");
154 break;
155 case Media::INFO_TYPE_BITRATEDONE:
156 LOGI("PlayerCallback: BitRateDone");
157 break;
158 case Media::INFO_TYPE_EOS: {
159 LOGI("PlayerCallback: OnEndOfStream isLooping is: %{public}d", extra);
160 boot_->StopVideo();
161 break;
162 }
163 case Media::INFO_TYPE_BUFFERING_UPDATE:
164 LOGI("PlayerCallback: Buffering Update");
165 break;
166 case Media::INFO_TYPE_BITRATE_COLLECT:
167 LOGI("PlayerCallback: Bitrate Collect");
168 break;
169 case Media::INFO_TYPE_STATE_CHANGE:
170 LOGI("PlayerCallback: State Change, current state is: %{public}d", extra);
171 if (Media::PlayerStates::PLAYER_PREPARED == extra) {
172 LOGI("Begin to play");
173 boot_->GetMediaPlayer()->Play();
174 }
175 break;
176 case Media::INFO_TYPE_POSITION_UPDATE: {
177 LOGD("PlayerCallback: Position Update");
178 break;
179 }
180 case Media::INFO_TYPE_MESSAGE:
181 LOGI("PlayerCallback: OnMessage is: %{public}d", extra);
182 if (!system::GetBoolParameter(BOOT_ANIMATION_STARTED, false)) {
183 system::SetParameter(BOOT_ANIMATION_STARTED, "true");
184 }
185 break;
186 case Media::INFO_TYPE_RESOLUTION_CHANGE:
187 LOGI("PlayerCallback: Resolution Change");
188 break;
189 case Media::INFO_TYPE_VOLUME_CHANGE:
190 LOGI("PlayerCallback: Volume Changed");
191 break;
192 default:
193 LOGI("PlayerCallback: Default");
194 break;
195 }
196 }
197 #endif
198