• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_videoplayer.h"
16 
17 #include "boot_animation.h"
18 #include "boot_animation_utils.h"
19 #include "log.h"
20 #include "util.h"
21 
22 using namespace OHOS;
23 #ifdef PLAYER_FRAMEWORK_ENABLE
24 static const int CONTENT_TYPE_UNKNOWN = 0;
25 static const int STREAM_USAGE_RINGTONE = 6;
26 #endif
27 
SetVideoPath(const std::string & path)28 void BootVideoPlayer::SetVideoPath(const std::string& path)
29 {
30     if (path.empty()) {
31         LOGE("set video path is empty");
32         return;
33     }
34     videopath_ = path;
35 }
36 
37 #ifdef PLAYER_FRAMEWORK_ENABLE
SetPlayerSurface(const sptr<Surface> & surface)38 void BootVideoPlayer::SetPlayerSurface(const sptr<Surface>& surface)
39 {
40     if (surface == nullptr) {
41         LOGE("SetPlayerSurface surface is nullptr");
42         return;
43     }
44     surface_ = surface;
45 }
46 #endif
47 
PlayVideo()48 bool BootVideoPlayer::PlayVideo()
49 {
50 #ifdef PLAYER_FRAMEWORK_ENABLE
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 
84     SetVideoSound();
85 
86     ret = mediaPlayer_->Prepare();
87     if (ret !=  0) {
88         LOGE("PlayVideo Prepare fail, errorCode:%{public}d", ret);
89         return false;
90     }
91     mediaPlayer_->Play();
92     LOGI("PlayVideo end");
93     return true;
94 #else
95     LOGI("player_framework part is not enabled.");
96     return false;
97 #endif
98 }
99 
StopVideo()100 void BootVideoPlayer::StopVideo()
101 {
102     LOGI("BootVideoPlayer StopVideo");
103     vsyncCallbacks_(userData_);
104 }
105 
SetVideoSound()106 void BootVideoPlayer::SetVideoSound()
107 {
108 #ifdef PLAYER_FRAMEWORK_ENABLE
109     LOGI("BootVideoPlayer SetVideoSound");
110     Media::Format format;
111     format.PutIntValue(Media::PlayerKeys::CONTENT_TYPE, CONTENT_TYPE_UNKNOWN);
112     format.PutIntValue(Media::PlayerKeys::STREAM_USAGE, STREAM_USAGE_RINGTONE);
113     format.PutIntValue(Media::PlayerKeys::RENDERER_FLAG, 0);
114     int ret = mediaPlayer_->SetParameter(format);
115     if (ret !=  0) {
116         LOGE("PlayVideo SetParameter fail, errorCode:%{public}d", ret);
117     }
118 
119     bool bootSoundEnabled = BootAnimationUtils::GetBootAnimationSoundEnabled();
120     if (!bootSoundEnabled) {
121         ret = mediaPlayer_->SetVolume(0, 0);
122         if (ret !=  0) {
123             LOGE("PlayVideo SetVolume fail, errorCode:%{public}d", ret);
124         }
125     }
126 #endif
127 }
128 
129 #ifdef PLAYER_FRAMEWORK_ENABLE
130 // PlayerCallback override
OnError(int32_t errorCode,const std::string & errorMsg)131 void VideoPlayerCallback::OnError(int32_t errorCode, const std::string &errorMsg)
132 {
133     LOGE("PlayerCallbackError received, errorMsg:%{public}s", errorMsg.c_str());
134     boot_->StopVideo();
135 }
136 #endif
137 
138 #ifdef PLAYER_FRAMEWORK_ENABLE
OnInfo(Media::PlayerOnInfoType type,int32_t extra,const Media::Format & infoBody)139 void VideoPlayerCallback::OnInfo(Media::PlayerOnInfoType type, int32_t extra, const Media::Format &infoBody)
140 {
141     switch (type) {
142         case Media::INFO_TYPE_SEEKDONE:
143             LOGI("PlayerCallback: OnSeekDone currentPositon is:%{public}d", extra);
144             break;
145         case Media::INFO_TYPE_SPEEDDONE:
146             LOGI("PlayerCallback: SpeedDone");
147             break;
148         case Media::INFO_TYPE_BITRATEDONE:
149             LOGI("PlayerCallback: BitRateDone");
150             break;
151         case Media::INFO_TYPE_EOS: {
152             LOGI("PlayerCallback: OnEndOfStream isLooping is:%{public}d", extra);
153             boot_->StopVideo();
154             break;
155         }
156         case Media::INFO_TYPE_BUFFERING_UPDATE:
157             LOGI("PlayerCallback: Buffering Update");
158             break;
159         case Media::INFO_TYPE_BITRATE_COLLECT:
160             LOGI("PlayerCallback: Bitrate Collect");
161             break;
162         case Media::INFO_TYPE_STATE_CHANGE:
163             LOGI("PlayerCallback: State Change");
164             break;
165         case Media::INFO_TYPE_POSITION_UPDATE: {
166             LOGD("PlayerCallback: Position Update");
167             break;
168         }
169         case Media::INFO_TYPE_MESSAGE:
170             LOGI("PlayerCallback: OnMessage is:%{public}d", extra);
171             system::SetParameter("bootevent.bootanimation.started", "true");
172             break;
173         case Media::INFO_TYPE_RESOLUTION_CHANGE:
174             LOGI("PlayerCallback: Resolution Change");
175             break;
176         case Media::INFO_TYPE_VOLUME_CHANGE:
177             LOGI("PlayerCallback: Volume Changed");
178             break;
179         default:
180             LOGI("PlayerCallback: Default");
181             break;
182     }
183 }
184 #endif
185