1 /*
2 * Copyright (c) 2022 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_mock.h"
17 #include <sys/stat.h>
18 #include "media_errors.h"
19 #include "transaction/rs_transaction.h"
20 #include "ui/rs_surface_node.h"
21 #include "window_option.h"
22
23 using namespace OHOS::Media::PlayerTestParam;
24
25 namespace OHOS {
26 namespace Media {
SetState(PlayerStates state)27 void PlayerCallbackTest::SetState(PlayerStates state)
28 {
29 state_ = state;
30 }
31
SetSeekDoneFlag(bool seekDoneFlag)32 void PlayerCallbackTest::SetSeekDoneFlag(bool seekDoneFlag)
33 {
34 seekDoneFlag_ = seekDoneFlag;
35 }
36
SetSpeedDoneFlag(bool speedDoneFlag)37 void PlayerCallbackTest::SetSpeedDoneFlag(bool speedDoneFlag)
38 {
39 speedDoneFlag_ = speedDoneFlag;
40 }
41
SetSeekPosition(int32_t seekPosition)42 void PlayerCallbackTest::SetSeekPosition(int32_t seekPosition)
43 {
44 seekPosition_ = seekPosition;
45 }
46
PrepareSync()47 int32_t PlayerCallbackTest::PrepareSync()
48 {
49 if (state_ != PLAYER_PREPARED) {
50 std::unique_lock<std::mutex> lockPrepare(mutexCond_);
51 condVarPrepare_.wait_for(lockPrepare, std::chrono::seconds(WAITSECOND));
52 if (state_ != PLAYER_PREPARED) {
53 return -1;
54 }
55 }
56 return MSERR_OK;
57 }
58
PlaySync()59 int32_t PlayerCallbackTest::PlaySync()
60 {
61 if (state_ != PLAYER_STARTED) {
62 std::unique_lock<std::mutex> lockPlay(mutexCond_);
63 condVarPlay_.wait_for(lockPlay, std::chrono::seconds(WAITSECOND));
64 if (state_ != PLAYER_STARTED && state_ != PLAYER_PLAYBACK_COMPLETE) {
65 return -1;
66 }
67 }
68 return MSERR_OK;
69 }
70
PauseSync()71 int32_t PlayerCallbackTest::PauseSync()
72 {
73 if (state_ != PLAYER_PAUSED) {
74 std::unique_lock<std::mutex> lockPause(mutexCond_);
75 condVarPause_.wait_for(lockPause, std::chrono::seconds(WAITSECOND));
76 if (state_ != PLAYER_PAUSED) {
77 return -1;
78 }
79 }
80 return MSERR_OK;
81 }
82
StopSync()83 int32_t PlayerCallbackTest::StopSync()
84 {
85 if (state_ != PLAYER_STOPPED) {
86 std::unique_lock<std::mutex> lockStop(mutexCond_);
87 condVarStop_.wait_for(lockStop, std::chrono::seconds(WAITSECOND));
88 if (state_ != PLAYER_STOPPED) {
89 return -1;
90 }
91 }
92 return MSERR_OK;
93 }
94
ResetSync()95 int32_t PlayerCallbackTest::ResetSync()
96 {
97 if (state_ != PLAYER_IDLE) {
98 std::unique_lock<std::mutex> lockReset(mutexCond_);
99 condVarReset_.wait_for(lockReset, std::chrono::seconds(WAITSECOND));
100 if (state_ != PLAYER_IDLE) {
101 return -1;
102 }
103 }
104 return MSERR_OK;
105 }
106
SeekSync()107 int32_t PlayerCallbackTest::SeekSync()
108 {
109 if (seekDoneFlag_ == false) {
110 std::unique_lock<std::mutex> lockSeek(mutexCond_);
111 condVarSeek_.wait_for(lockSeek, std::chrono::seconds(WAITSECOND));
112 if (seekDoneFlag_ == false) {
113 return -1;
114 }
115 }
116 return MSERR_OK;
117 }
118
SpeedSync()119 int32_t PlayerCallbackTest::SpeedSync()
120 {
121 if (speedDoneFlag_ == false) {
122 std::unique_lock<std::mutex> lockSpeed(mutexCond_);
123 condVarSpeed_.wait_for(lockSpeed, std::chrono::seconds(WAITSECOND));
124 if (speedDoneFlag_ == false) {
125 return -1;
126 }
127 }
128 return MSERR_OK;
129 }
130
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)131 void PlayerCallbackTest::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
132 {
133 switch (type) {
134 case INFO_TYPE_SEEKDONE:
135 SetSeekDoneFlag(true);
136 SeekNotify(extra, infoBody);
137 break;
138 case INFO_TYPE_STATE_CHANGE:
139 state_ = static_cast<PlayerStates>(extra);
140 SetState(state_);
141 Notify(state_);
142 break;
143 case INFO_TYPE_SPEEDDONE:
144 SetSpeedDoneFlag(true);
145 condVarSpeed_.notify_all();
146 break;
147 case INFO_TYPE_POSITION_UPDATE:
148 break;
149 case INFO_TYPE_BITRATE_COLLECT:
150 std::cout << "INFO_TYPE_BITRATE_COLLECT: " << extra << std::endl;
151 break;
152 case INFO_TYPE_INTERRUPT_EVENT:
153 std::cout << "INFO_TYPE_INTERRUPT_EVENT: " << extra << std::endl;
154 break;
155 case INFO_TYPE_RESOLUTION_CHANGE:
156 std::cout << "INFO_TYPE_RESOLUTION_CHANGE: " << extra << std::endl;
157 break;
158 default:
159 break;
160 }
161 }
162
OnError(int32_t errorCode,const std::string & errorMsg)163 void PlayerCallbackTest::OnError(int32_t errorCode, const std::string &errorMsg)
164 {
165 std::cout << "Error received, errorCode: " << errorCode << "errorMsg: " << errorMsg << std::endl;
166 }
167
Notify(PlayerStates currentState)168 void PlayerCallbackTest::Notify(PlayerStates currentState)
169 {
170 if (currentState == PLAYER_PREPARED) {
171 condVarPrepare_.notify_all();
172 } else if (currentState == PLAYER_STARTED) {
173 condVarPlay_.notify_all();
174 } else if (currentState == PLAYER_PAUSED) {
175 condVarPause_.notify_all();
176 } else if (currentState == PLAYER_STOPPED) {
177 condVarStop_.notify_all();
178 } else if (currentState == PLAYER_IDLE) {
179 condVarReset_.notify_all();
180 }
181 }
182
SeekNotify(int32_t extra,const Format & infoBody)183 void PlayerCallbackTest::SeekNotify(int32_t extra, const Format &infoBody)
184 {
185 if (seekMode_ == PlayerSeekMode::SEEK_CLOSEST) {
186 if (seekPosition_ == extra) {
187 condVarSeek_.notify_all();
188 }
189 } else if (seekMode_ == PlayerSeekMode::SEEK_PREVIOUS_SYNC) {
190 if (seekPosition_ - extra < DELTA_TIME && extra - seekPosition_ >= 0) {
191 condVarSeek_.notify_all();
192 }
193 } else if (seekMode_ == PlayerSeekMode::SEEK_NEXT_SYNC) {
194 if (extra - seekPosition_ < DELTA_TIME && seekPosition_ - extra >= 0) {
195 condVarSeek_.notify_all();
196 }
197 } else if (abs(seekPosition_ - extra) <= DELTA_TIME) {
198 condVarSeek_.notify_all();
199 } else {
200 SetSeekDoneFlag(false);
201 }
202 }
203
GetVideoSurface()204 sptr<Surface> PlayerMock::GetVideoSurface()
205 {
206 sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
207 option->SetWindowRect({ 0, 0, width_, height_ });
208 option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_LAUNCHING);
209 option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
210 previewWindow_ = Rosen::Window::Create("xcomponent_window", option);
211 if (previewWindow_ == nullptr || previewWindow_->GetSurfaceNode() == nullptr) {
212 return nullptr;
213 }
214
215 previewWindow_->Show();
216 auto surfaceNode = previewWindow_->GetSurfaceNode();
217 surfaceNode->SetFrameGravity(Rosen::Gravity::RESIZE);
218 Rosen::RSTransaction::FlushImplicitTransaction();
219 return surfaceNode->GetSurface();
220 }
221
PlayerMock(std::shared_ptr<PlayerCallbackTest> & callback)222 PlayerMock::PlayerMock(std::shared_ptr<PlayerCallbackTest> &callback)
223 {
224 callback_ = callback;
225 }
226
~PlayerMock()227 PlayerMock::~PlayerMock()
228 {
229 if (previewWindow_ != nullptr) {
230 previewWindow_->Destroy();
231 previewWindow_ = nullptr;
232 }
233 }
234
CreatePlayer()235 bool PlayerMock::CreatePlayer()
236 {
237 player_ = PlayerFactory::CreatePlayer();
238 return player_ != nullptr;
239 }
240
SetSource(const std::string url)241 int32_t PlayerMock::SetSource(const std::string url)
242 {
243 std::unique_lock<std::mutex> lock(mutex_);
244 int32_t ret = player_->SetSource(url);
245 return ret;
246 }
247
SetSource(const std::string & path,int64_t offset,int64_t size)248 int32_t PlayerMock::SetSource(const std::string &path, int64_t offset, int64_t size)
249 {
250 std::string rawFile = path.substr(strlen("file://"));
251 int32_t fd = open(rawFile.c_str(), O_RDONLY);
252 if (fd <= 0) {
253 std::cout << "Open file failed" << std::endl;
254 return -1;
255 }
256
257 struct stat64 st;
258 if (fstat64(fd, &st) != 0) {
259 std::cout << "Get file state failed" << std::endl;
260 (void)close(fd);
261 return -1;
262 }
263 int64_t length = static_cast<int64_t>(st.st_size);
264 if (size > 0) {
265 length = size;
266 }
267 int32_t ret = player_->SetSource(fd, offset, length);
268 if (ret != 0) {
269 (void)close(fd);
270 return -1;
271 }
272
273 (void)close(fd);
274 return ret;
275 }
276
SetDataSrc(const std::string & path,int32_t size,bool seekable)277 int32_t PlayerMock::SetDataSrc(const std::string &path, int32_t size, bool seekable)
278 {
279 if (seekable) {
280 dataSrc_ = MediaDataSourceTestSeekable::Create(path, size);
281 } else {
282 dataSrc_ = MediaDataSourceTestNoSeek::Create(path, size);
283 }
284 return player_->SetSource(dataSrc_);
285 }
286
Prepare()287 int32_t PlayerMock::Prepare()
288 {
289 std::unique_lock<std::mutex> lock(mutex_);
290 int32_t ret = player_->Prepare();
291 if (ret == MSERR_OK) {
292 return callback_->PrepareSync();
293 }
294 return ret;
295 }
296
PrepareAsync()297 int32_t PlayerMock::PrepareAsync()
298 {
299 std::unique_lock<std::mutex> lock(mutex_);
300 int ret = player_->PrepareAsync();
301 if (ret == MSERR_OK) {
302 return callback_->PrepareSync();
303 }
304 return ret;
305 }
306
Play()307 int32_t PlayerMock::Play()
308 {
309 std::unique_lock<std::mutex> lock(mutex_);
310 int32_t ret = player_->Play();
311 if (ret == MSERR_OK) {
312 return callback_->PlaySync();
313 }
314 return ret;
315 }
316
Pause()317 int32_t PlayerMock::Pause()
318 {
319 std::unique_lock<std::mutex> lock(mutex_);
320 int32_t ret = player_->Pause();
321 if (ret == MSERR_OK) {
322 return callback_->PauseSync();
323 }
324 return ret;
325 }
326
Stop()327 int32_t PlayerMock::Stop()
328 {
329 std::unique_lock<std::mutex> lock(mutex_);
330 int32_t ret = player_->Stop();
331 if (ret == MSERR_OK) {
332 if (dataSrc_ != nullptr) {
333 dataSrc_->Reset();
334 }
335 return callback_->StopSync();
336 }
337 return ret;
338 }
339
SeekPrepare(int32_t & mseconds,PlayerSeekMode & mode)340 void PlayerMock::SeekPrepare(int32_t &mseconds, PlayerSeekMode &mode)
341 {
342 int32_t duration = 0;
343 int32_t seekPosition = 0;
344 callback_->SetSeekDoneFlag(false);
345 player_->GetDuration(duration);
346 if (mseconds < 0) {
347 seekPosition = 0;
348 } else if (mseconds > duration) {
349 seekPosition = duration;
350 } else {
351 seekPosition = mseconds;
352 }
353 callback_->SetSeekPosition(seekPosition);
354 }
355
Seek(int32_t mseconds,PlayerSeekMode mode)356 int32_t PlayerMock::Seek(int32_t mseconds, PlayerSeekMode mode)
357 {
358 std::unique_lock<std::mutex> lock(mutex_);
359 SeekPrepare(mseconds, mode);
360 int32_t ret = player_->Seek(mseconds, mode);
361 if (ret == MSERR_OK) {
362 return callback_->SeekSync();
363 }
364 return ret;
365 }
366
Reset()367 int32_t PlayerMock::Reset()
368 {
369 int32_t ret = player_->Reset();
370 if (ret == MSERR_OK) {
371 return callback_->ResetSync();
372 }
373 return ret;
374 }
375
Release()376 int32_t PlayerMock::Release()
377 {
378 if (previewWindow_ != nullptr) {
379 previewWindow_->Destroy();
380 previewWindow_ = nullptr;
381 }
382 callback_ = nullptr;
383 return player_->Release();
384 }
385
ReleaseSync()386 int32_t PlayerMock::ReleaseSync()
387 {
388 if (previewWindow_ != nullptr) {
389 previewWindow_->Destroy();
390 previewWindow_ = nullptr;
391 }
392 callback_ = nullptr;
393 return player_->ReleaseSync();
394 }
395
SetVolume(float leftVolume,float rightVolume)396 int32_t PlayerMock::SetVolume(float leftVolume, float rightVolume)
397 {
398 std::unique_lock<std::mutex> lock(mutex_);
399 return player_->SetVolume(leftVolume, rightVolume);
400 }
401
SetLooping(bool loop)402 int32_t PlayerMock::SetLooping(bool loop)
403 {
404 std::unique_lock<std::mutex> lock(mutex_);
405 return player_->SetLooping(loop);
406 }
407
GetCurrentTime(int32_t & currentTime)408 int32_t PlayerMock::GetCurrentTime(int32_t ¤tTime)
409 {
410 std::unique_lock<std::mutex> lock(mutex_);
411 return player_->GetCurrentTime(currentTime);
412 }
413
GetVideoTrackInfo(std::vector<Format> & videoTrack)414 int32_t PlayerMock::GetVideoTrackInfo(std::vector<Format> &videoTrack)
415 {
416 return player_->GetVideoTrackInfo(videoTrack);
417 }
418
GetAudioTrackInfo(std::vector<Format> & audioTrack)419 int32_t PlayerMock::GetAudioTrackInfo(std::vector<Format> &audioTrack)
420 {
421 return player_->GetAudioTrackInfo(audioTrack);
422 }
423
GetVideoWidth()424 int32_t PlayerMock::GetVideoWidth()
425 {
426 return player_->GetVideoWidth();
427 }
428
GetVideoHeight()429 int32_t PlayerMock::GetVideoHeight()
430 {
431 return player_->GetVideoHeight();
432 }
433
GetDuration(int32_t & duration)434 int32_t PlayerMock::GetDuration(int32_t &duration)
435 {
436 return player_->GetDuration(duration);
437 }
438
SetPlaybackSpeed(PlaybackRateMode mode)439 int32_t PlayerMock::SetPlaybackSpeed(PlaybackRateMode mode)
440 {
441 callback_->SetSpeedDoneFlag(false);
442 int32_t ret = player_->SetPlaybackSpeed(mode);
443 if (ret == MSERR_OK) {
444 return callback_->SpeedSync();
445 }
446 return player_->SetPlaybackSpeed(mode);
447 }
448
GetPlaybackSpeed(PlaybackRateMode & mode)449 int32_t PlayerMock::GetPlaybackSpeed(PlaybackRateMode &mode)
450 {
451 return player_->GetPlaybackSpeed(mode);
452 }
453
SelectBitRate(uint32_t bitRate)454 int32_t PlayerMock::SelectBitRate(uint32_t bitRate)
455 {
456 return player_->SelectBitRate(bitRate);
457 }
458
IsPlaying()459 bool PlayerMock::IsPlaying()
460 {
461 std::unique_lock<std::mutex> lock(mutex_);
462 return player_->IsPlaying();
463 }
464
IsLooping()465 bool PlayerMock::IsLooping()
466 {
467 std::unique_lock<std::mutex> lock(mutex_);
468 return player_->IsLooping();
469 }
470
SetParameter(const Format & param)471 int32_t PlayerMock::SetParameter(const Format ¶m)
472 {
473 return player_->SetParameter(param);
474 }
475
SetPlayerCallback(const std::shared_ptr<PlayerCallback> & callback)476 int32_t PlayerMock::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback)
477 {
478 std::unique_lock<std::mutex> lock(mutex_);
479 return player_->SetPlayerCallback(callback);
480 }
481
SetVideoSurface(sptr<Surface> surface)482 int32_t PlayerMock::SetVideoSurface(sptr<Surface> surface)
483 {
484 std::unique_lock<std::mutex> lock(mutex_);
485 return player_->SetVideoSurface(surface);
486 }
487 } // namespace Media
488 } // namespace OHOS
489