1 /*
2 * Copyright (c) 2023 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_animation.h"
16 #include "boot_videoplayer.h"
17 #include "log.h"
18 #include "util.h"
19
20 using namespace OHOS;
21
SetVideoPath(const std::string & path)22 void BootVideoPlayer::SetVideoPath(const std::string& path)
23 {
24 if (path.empty()) {
25 LOGE("set video path is empty");
26 return;
27 }
28 videopath_ = path;
29 }
30
SetPlayerWindow(const OHOS::sptr<OHOS::Rosen::Window> & window)31 void BootVideoPlayer::SetPlayerWindow(const OHOS::sptr<OHOS::Rosen::Window>& window)
32 {
33 if (window == nullptr) {
34 LOGE("SetPlayerWindow window is nullptr");
35 return;
36 }
37 window_ = window;
38 }
39
SetPlayerSurface(const sptr<Surface> & surface)40 void BootVideoPlayer::SetPlayerSurface(const sptr<Surface>& surface)
41 {
42 if (surface == nullptr) {
43 LOGE("SetPlayerSurface surface is nullptr");
44 return;
45 }
46 surface_ = surface;
47 }
48
PlayVideo()49 bool BootVideoPlayer::PlayVideo()
50 {
51 LOGI("PlayVideo begin");
52 if (mediaPlayer_ == nullptr) {
53 mediaPlayer_ = Media::PlayerFactory::CreatePlayer();
54 }
55 while (mediaPlayer_ == nullptr) {
56 LOGE("CreatePlayer fail, mediaPlayer_ is nullptr");
57 mediaPlayer_ = Media::PlayerFactory::CreatePlayer();
58 usleep(SLEEP_TIME_US);
59 }
60
61 std::shared_ptr<VideoPlayerCallback> cb = std::make_shared<VideoPlayerCallback>(shared_from_this());
62 int32_t ret = mediaPlayer_->SetPlayerCallback(cb);
63 if (ret != 0) {
64 LOGE("PlayVideo SetPlayerCallback fail, errorCode:%{public}d", ret);
65 return false;
66 }
67
68 std::string uri = "file:/" + videopath_;
69 ret = mediaPlayer_->SetSource(uri);
70 if (ret != 0) {
71 LOGE("PlayVideo SetSource fail, uri:%{public}s, errorCode:%{public}d", uri.c_str(), ret);
72 return false;
73 }
74 if (surface_ == nullptr) {
75 LOGE("PlayVideo surface is null");
76 return false;
77 }
78 ret = mediaPlayer_->SetVideoSurface(surface_);
79 if (ret != 0) {
80 LOGE("PlayVideo SetVideoSurface fail, errorCode:%{public}d", ret);
81 return false;
82 }
83 ret = mediaPlayer_->Prepare();
84 if (ret != 0) {
85 LOGE("PlayVideo Prepare fail, errorCode:%{public}d", ret);
86 return false;
87 }
88 mediaPlayer_->Play();
89 LOGI("PlayVideo end");
90 return true;
91 }
92
StopVideo()93 void BootVideoPlayer::StopVideo()
94 {
95 mediaPlayer_->Stop();
96 vsyncCallbacks_(userData_);
97 }
98
99 // PlayerCallback override
OnError(int32_t errorCode,const std::string & errorMsg)100 void VideoPlayerCallback::OnError(int32_t errorCode, const std::string &errorMsg)
101 {
102 std::string err = Media::MSErrorToString(static_cast<Media::MediaServiceErrCode>(errorCode));
103 LOGE("PlayerCallbackError received, errorCode:%{public}s", err.c_str());
104 boot_->StopVideo();
105 }
106
OnInfo(Media::PlayerOnInfoType type,int32_t extra,const Media::Format & infoBody)107 void VideoPlayerCallback::OnInfo(Media::PlayerOnInfoType type, int32_t extra, const Media::Format &infoBody)
108 {
109 switch (type) {
110 case Media::INFO_TYPE_SEEKDONE:
111 LOGI("PlayerCallback: OnSeekDone currentPositon is:%{public}d", extra);
112 break;
113 case Media::INFO_TYPE_SPEEDDONE:
114 LOGI("PlayerCallback: SpeedDone");
115 break;
116 case Media::INFO_TYPE_BITRATEDONE:
117 LOGI("PlayerCallback: BitRateDone");
118 break;
119 case Media::INFO_TYPE_EOS: {
120 LOGI("PlayerCallback: OnEndOfStream isLooping is:%{public}d", extra);
121 boot_->StopVideo();
122 break;
123 }
124 case Media::INFO_TYPE_BUFFERING_UPDATE:
125 LOGI("PlayerCallback: Buffering Update");
126 break;
127 case Media::INFO_TYPE_BITRATE_COLLECT:
128 LOGI("PlayerCallback: Bitrate Collect");
129 break;
130 case Media::INFO_TYPE_STATE_CHANGE:
131 LOGI("PlayerCallback: State Change");
132 break;
133 case Media::INFO_TYPE_POSITION_UPDATE: {
134 LOGI("PlayerCallback: Position Update");
135 break;
136 }
137 case Media::INFO_TYPE_MESSAGE:
138 LOGI("PlayerCallback: OnMessage is:%{public}d", extra);
139 break;
140 case Media::INFO_TYPE_RESOLUTION_CHANGE:
141 LOGI("PlayerCallback: Resolution Change");
142 break;
143 case Media::INFO_TYPE_VOLUME_CHANGE:
144 LOGI("PlayerCallback: Volume Changed");
145 break;
146 default:
147 LOGI("PlayerCallback: Default");
148 break;
149 }
150 }
151