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
16 #include "player_framework_adapter_impl.h"
17
18 #include "foundation/multimedia/player_framework/interfaces/inner_api/native/media_errors.h"
19 #include "nweb_log.h"
20 #include "surface_adapter_impl.h"
21
22 namespace OHOS::NWeb {
23 namespace {
IsUnsupportType(int32_t errorCode)24 bool IsUnsupportType(int32_t errorCode)
25 {
26 switch (errorCode) {
27 case Media::MSERR_UNSUPPORT:
28 case Media::MSERR_UNSUPPORT_AUD_SRC_TYPE:
29 case Media::MSERR_UNSUPPORT_AUD_SAMPLE_RATE:
30 case Media::MSERR_UNSUPPORT_AUD_CHANNEL_NUM:
31 case Media::MSERR_UNSUPPORT_AUD_ENC_TYPE:
32 case Media::MSERR_UNSUPPORT_AUD_PARAMS:
33 case Media::MSERR_UNSUPPORT_VID_SRC_TYPE:
34 case Media::MSERR_UNSUPPORT_VID_ENC_TYPE:
35 case Media::MSERR_UNSUPPORT_VID_PARAMS:
36 case Media::MSERR_UNSUPPORT_CONTAINER_TYPE:
37 case Media::MSERR_UNSUPPORT_PROTOCOL_TYPE:
38 case Media::MSERR_UNSUPPORT_VID_DEC_TYPE:
39 case Media::MSERR_UNSUPPORT_AUD_DEC_TYPE:
40 case Media::MSERR_UNSUPPORT_STREAM:
41 case Media::MSERR_UNSUPPORT_FILE:
42 case Media::MSERR_UNSUPPORT_SOURCE:
43 return true;
44 default:
45 return false;
46 }
47 }
48
IsFatalError(int32_t errorCode)49 bool IsFatalError(int32_t errorCode)
50 {
51 switch (errorCode) {
52 case Media::MSERR_EXT_NO_MEMORY:
53 case Media::MSERR_EXT_SERVICE_DIED:
54 case Media::MSERR_CREATE_PLAYER_ENGINE_FAILED:
55 case Media::MSERR_CREATE_AVMETADATAHELPER_ENGINE_FAILED:
56 case Media::MSERR_AUD_DEC_FAILED:
57 case Media::MSERR_VID_DEC_FAILED:
58 case Media::MSERR_FILE_ACCESS_FAILED:
59 return true;
60 default:
61 return false;
62 }
63 }
64
ConvertInfoType(Media::PlayerOnInfoType infoType)65 NWeb::PlayerOnInfoType ConvertInfoType(Media::PlayerOnInfoType infoType)
66 {
67 NWeb::PlayerOnInfoType ret = NWeb::PlayerOnInfoType::INFO_TYPE_UNSET;
68 switch (infoType) {
69 case Media::INFO_TYPE_SEEKDONE:
70 ret = NWeb::PlayerOnInfoType::INFO_TYPE_SEEKDONE;
71 break;
72 case Media::INFO_TYPE_EOS:
73 ret = NWeb::PlayerOnInfoType::INFO_TYPE_EOS;
74 break;
75 case Media::INFO_TYPE_STATE_CHANGE:
76 ret = NWeb::PlayerOnInfoType::INFO_TYPE_STATE_CHANGE;
77 break;
78 case Media::INFO_TYPE_POSITION_UPDATE:
79 ret = NWeb::PlayerOnInfoType::INFO_TYPE_POSITION_UPDATE;
80 break;
81 case Media::INFO_TYPE_MESSAGE:
82 ret = NWeb::PlayerOnInfoType::INFO_TYPE_MESSAGE;
83 break;
84 case Media::INFO_TYPE_INTERRUPT_EVENT:
85 ret = NWeb::PlayerOnInfoType::INFO_TYPE_INTERRUPT_EVENT;
86 break;
87 default:
88 break;
89 }
90 return ret;
91 }
92
ConvertSeekMode(NWeb::PlayerSeekMode mode)93 Media::PlayerSeekMode ConvertSeekMode(NWeb::PlayerSeekMode mode)
94 {
95 Media::PlayerSeekMode ret = Media::SEEK_CLOSEST;
96 switch (mode) {
97 case NWeb::PlayerSeekMode::SEEK_NEXT_SYNC:
98 ret = Media::SEEK_NEXT_SYNC;
99 break;
100 case NWeb::PlayerSeekMode::SEEK_PREVIOUS_SYNC:
101 ret = Media::SEEK_PREVIOUS_SYNC;
102 break;
103 case NWeb::PlayerSeekMode::SEEK_CLOSEST_SYNC:
104 ret = Media::SEEK_CLOSEST_SYNC;
105 break;
106 case NWeb::PlayerSeekMode::SEEK_CLOSEST:
107 ret = Media::SEEK_CLOSEST;
108 break;
109 default:
110 break;
111 }
112 return ret;
113 }
114
ConvertRateMode(NWeb::PlaybackRateMode mode)115 Media::PlaybackRateMode ConvertRateMode(NWeb::PlaybackRateMode mode)
116 {
117 Media::PlaybackRateMode ret = Media::SPEED_FORWARD_1_00_X;
118 switch (mode) {
119 case NWeb::PlaybackRateMode::SPEED_FORWARD_0_75_X:
120 ret = Media::SPEED_FORWARD_0_75_X;
121 break;
122 case NWeb::PlaybackRateMode::SPEED_FORWARD_1_00_X:
123 ret = Media::SPEED_FORWARD_1_00_X;
124 break;
125 case NWeb::PlaybackRateMode::SPEED_FORWARD_1_25_X:
126 ret = Media::SPEED_FORWARD_1_25_X;
127 break;
128 case NWeb::PlaybackRateMode::SPEED_FORWARD_1_75_X:
129 ret = Media::SPEED_FORWARD_1_75_X;
130 break;
131 case NWeb::PlaybackRateMode::SPEED_FORWARD_2_00_X:
132 ret = Media::SPEED_FORWARD_2_00_X;
133 break;
134 default:
135 break;
136 }
137 return ret;
138 }
139 } // namespace
140
PlayerCallbackImpl(std::unique_ptr<PlayerCallbackAdapter> callback)141 PlayerCallbackImpl::PlayerCallbackImpl(std::unique_ptr<PlayerCallbackAdapter> callback)
142 : callbackAdapter_(std::move(callback))
143 {}
144
OnInfo(Media::PlayerOnInfoType type,int32_t extra,const Media::Format & infoBody)145 void PlayerCallbackImpl::OnInfo(Media::PlayerOnInfoType type, int32_t extra, const Media::Format& infoBody)
146 {
147 int32_t hintValue = -1;
148 if (type == Media::INFO_TYPE_INTERRUPT_EVENT) {
149 infoBody.GetIntValue(OHOS::Media::PlayerKeys::AUDIO_INTERRUPT_HINT, hintValue);
150 }
151 if (callbackAdapter_) {
152 callbackAdapter_->OnInfo(ConvertInfoType(type), extra, hintValue);
153 }
154 }
155
OnError(int32_t errorCode,const std::string & errorMsg)156 void PlayerCallbackImpl::OnError(int32_t errorCode, const std::string& errorMsg)
157 {
158 (void)errorMsg;
159 WVLOG_E("media player error, errorCode=%{public}d", errorCode);
160 if (callbackAdapter_) {
161 auto errorType = PlayerAdapterErrorType::INVALID_CODE;
162 if (IsUnsupportType(errorCode)) {
163 errorType = PlayerAdapterErrorType::UNSUPPORT_TYPE;
164 } else if (IsFatalError(errorCode)) {
165 errorType = PlayerAdapterErrorType::FATAL_ERROR;
166 }
167 callbackAdapter_->OnError(errorType);
168 }
169 }
170
PlayerAdapterImpl()171 PlayerAdapterImpl::PlayerAdapterImpl()
172 {
173 player_ = Media::PlayerFactory::CreatePlayer();
174 }
175
~PlayerAdapterImpl()176 PlayerAdapterImpl::~PlayerAdapterImpl()
177 {
178 if (player_) {
179 player_->Release();
180 WVLOG_I("player release");
181 }
182 }
183
SetPlayerCallback(std::unique_ptr<PlayerCallbackAdapter> callbackAdapter)184 int32_t PlayerAdapterImpl::SetPlayerCallback(std::unique_ptr<PlayerCallbackAdapter> callbackAdapter)
185 {
186 if (!player_ || !callbackAdapter) {
187 WVLOG_E("player_ or callbackAdapter is nullptr");
188 return -1;
189 }
190 auto callback = std::make_shared<PlayerCallbackImpl>(std::move(callbackAdapter));
191 return player_->SetPlayerCallback(callback);
192 }
193
SetSource(const std::string & url)194 int32_t PlayerAdapterImpl::SetSource(const std::string& url)
195 {
196 if (!player_) {
197 WVLOG_E("player_ is nullptr");
198 return -1;
199 }
200 return player_->SetSource(url);
201 }
202
SetSource(int32_t fd,int64_t offset,int64_t size)203 int32_t PlayerAdapterImpl::SetSource(int32_t fd, int64_t offset, int64_t size)
204 {
205 if (!player_) {
206 WVLOG_E("player_ is nullptr");
207 return -1;
208 }
209 return player_->SetSource(fd, offset, size);
210 }
211
SetVideoSurface(IConsumerSurfaceAdapter * cSurfaceAdapter)212 int32_t PlayerAdapterImpl::SetVideoSurface(IConsumerSurfaceAdapter* cSurfaceAdapter)
213 {
214 if (!player_ || !cSurfaceAdapter) {
215 WVLOG_E("player_ or cSurfaceAdapter is nullptr");
216 return -1;
217 }
218 auto cSurface = static_cast<ConsumerSurfaceAdapterImpl*>(cSurfaceAdapter)->GetConsumerSurface();
219 sptr<IBufferProducer> producer = cSurface->GetProducer();
220 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
221 return player_->SetVideoSurface(pSurface);
222 }
223
SetVolume(float leftVolume,float rightVolume)224 int32_t PlayerAdapterImpl::SetVolume(float leftVolume, float rightVolume)
225 {
226 if (!player_) {
227 WVLOG_E("player_ is nullptr");
228 return -1;
229 }
230 return player_->SetVolume(leftVolume, rightVolume);
231 }
232
Seek(int32_t mSeconds,PlayerSeekMode mode)233 int32_t PlayerAdapterImpl::Seek(int32_t mSeconds, PlayerSeekMode mode)
234 {
235 if (!player_) {
236 WVLOG_E("player_ is nullptr");
237 return -1;
238 }
239 return player_->Seek(mSeconds, ConvertSeekMode(mode));
240 }
241
Play()242 int32_t PlayerAdapterImpl::Play()
243 {
244 if (!player_) {
245 WVLOG_E("player_ is nullptr");
246 return -1;
247 }
248 return player_->Play();
249 }
250
Pause()251 int32_t PlayerAdapterImpl::Pause()
252 {
253 if (!player_) {
254 WVLOG_E("player_ is nullptr");
255 return -1;
256 }
257 return player_->Pause();
258 }
259
PrepareAsync()260 int32_t PlayerAdapterImpl::PrepareAsync()
261 {
262 if (!player_) {
263 WVLOG_E("player_ is nullptr");
264 return -1;
265 }
266 return player_->PrepareAsync();
267 }
268
GetCurrentTime(int32_t & currentTime)269 int32_t PlayerAdapterImpl::GetCurrentTime(int32_t& currentTime)
270 {
271 if (!player_) {
272 WVLOG_E("player_ is nullptr");
273 return -1;
274 }
275 return player_->GetCurrentTime(currentTime);
276 }
277
GetDuration(int32_t & duration)278 int32_t PlayerAdapterImpl::GetDuration(int32_t& duration)
279 {
280 if (!player_) {
281 WVLOG_E("player_ is nullptr");
282 return -1;
283 }
284 return player_->GetDuration(duration);
285 }
286
SetPlaybackSpeed(PlaybackRateMode mode)287 int32_t PlayerAdapterImpl::SetPlaybackSpeed(PlaybackRateMode mode)
288 {
289 if (!player_) {
290 WVLOG_E("player_ is nullptr");
291 return -1;
292 }
293 return player_->SetPlaybackSpeed(ConvertRateMode(mode));
294 }
295 } // namespace OHOS::NWeb
296