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