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 <fcntl.h>
18 #include <sys/stat.h>
19 #include "media_errors.h"
20 #include "transaction/rs_transaction.h"
21 #include "ui/rs_surface_node.h"
22 #include "window_option.h"
23
24 using namespace OHOS::Media::PlayerTestParam;
25
26 namespace OHOS {
27 namespace Media {
SetState(PlayerStates state)28 void PlayerCallbackTest::SetState(PlayerStates state)
29 {
30 state_ = state;
31 }
32
SetSeekDoneFlag(bool seekDoneFlag)33 void PlayerCallbackTest::SetSeekDoneFlag(bool seekDoneFlag)
34 {
35 seekDoneFlag_ = seekDoneFlag;
36 }
37
SetSpeedDoneFlag(bool speedDoneFlag)38 void PlayerCallbackTest::SetSpeedDoneFlag(bool speedDoneFlag)
39 {
40 speedDoneFlag_ = speedDoneFlag;
41 }
42
SetSeekPosition(int32_t seekPosition)43 void PlayerCallbackTest::SetSeekPosition(int32_t seekPosition)
44 {
45 seekPosition_ = seekPosition;
46 }
47
SetTrackDoneFlag(bool trackDoneFlag)48 void PlayerCallbackTest::SetTrackDoneFlag(bool trackDoneFlag)
49 {
50 std::unique_lock<std::mutex> lockSpeed(mutexCond_);
51 trackDoneFlag_ = trackDoneFlag;
52 trackChange_ = trackDoneFlag;
53 }
54
PrepareSync()55 int32_t PlayerCallbackTest::PrepareSync()
56 {
57 if (state_ != PLAYER_PREPARED) {
58 std::unique_lock<std::mutex> lockPrepare(mutexCond_);
59 condVarPrepare_.wait_for(lockPrepare, std::chrono::seconds(WAITSECOND));
60 if (state_ != PLAYER_PREPARED) {
61 return -1;
62 }
63 }
64 return MSERR_OK;
65 }
66
PlaySync()67 int32_t PlayerCallbackTest::PlaySync()
68 {
69 if (state_ != PLAYER_STARTED) {
70 std::unique_lock<std::mutex> lockPlay(mutexCond_);
71 condVarPlay_.wait_for(lockPlay, std::chrono::seconds(WAITSECOND));
72 if (state_ != PLAYER_STARTED && state_ != PLAYER_PLAYBACK_COMPLETE) {
73 return -1;
74 }
75 }
76 return MSERR_OK;
77 }
78
PauseSync()79 int32_t PlayerCallbackTest::PauseSync()
80 {
81 if (state_ != PLAYER_PAUSED) {
82 std::unique_lock<std::mutex> lockPause(mutexCond_);
83 condVarPause_.wait_for(lockPause, std::chrono::seconds(WAITSECOND));
84 if (state_ != PLAYER_PAUSED) {
85 return -1;
86 }
87 }
88 return MSERR_OK;
89 }
90
StopSync()91 int32_t PlayerCallbackTest::StopSync()
92 {
93 if (state_ != PLAYER_STOPPED) {
94 std::unique_lock<std::mutex> lockStop(mutexCond_);
95 condVarStop_.wait_for(lockStop, std::chrono::seconds(WAITSECOND));
96 if (state_ != PLAYER_STOPPED) {
97 return -1;
98 }
99 }
100 return MSERR_OK;
101 }
102
ResetSync()103 int32_t PlayerCallbackTest::ResetSync()
104 {
105 if (state_ != PLAYER_IDLE) {
106 std::unique_lock<std::mutex> lockReset(mutexCond_);
107 condVarReset_.wait_for(lockReset, std::chrono::seconds(WAITSECOND));
108 if (state_ != PLAYER_IDLE) {
109 return -1;
110 }
111 }
112 return MSERR_OK;
113 }
114
SeekSync()115 int32_t PlayerCallbackTest::SeekSync()
116 {
117 if (seekDoneFlag_ == false) {
118 std::unique_lock<std::mutex> lockSeek(mutexCond_);
119 condVarSeek_.wait_for(lockSeek, std::chrono::seconds(WAITSECOND));
120 if (seekDoneFlag_ == false) {
121 return -1;
122 }
123 }
124 return MSERR_OK;
125 }
126
SpeedSync()127 int32_t PlayerCallbackTest::SpeedSync()
128 {
129 if (speedDoneFlag_ == false) {
130 std::unique_lock<std::mutex> lockSpeed(mutexCond_);
131 condVarSpeed_.wait_for(lockSpeed, std::chrono::seconds(WAITSECOND));
132 if (speedDoneFlag_ == false) {
133 return -1;
134 }
135 }
136 return MSERR_OK;
137 }
138
TrackSync(bool & trackChange)139 int32_t PlayerCallbackTest::TrackSync(bool &trackChange)
140 {
141 if (trackDoneFlag_ == false) {
142 std::unique_lock<std::mutex> lockTrackDone(mutexCond_);
143 condVarTrackDone_.wait_for(lockTrackDone, std::chrono::seconds(WAITSECOND));
144 if (trackDoneFlag_ == false) {
145 return -1;
146 }
147 }
148
149 trackChange = trackChange_;
150
151 return MSERR_OK;
152 }
153
TrackInfoUpdateSync()154 int32_t PlayerCallbackTest::TrackInfoUpdateSync()
155 {
156 if (trackInfoUpdate_ == false) {
157 std::unique_lock<std::mutex> lock(mutexCond_);
158 condVarTrackInfoUpdate_.wait_for(lock, std::chrono::seconds(WAITSECOND), [this]() {
159 return trackInfoUpdate_;
160 });
161 if (trackInfoUpdate_ == false) {
162 return -1;
163 }
164 }
165 trackInfoUpdate_ = false;
166 return MSERR_OK;
167 }
168
HandleSubtitleCallback(int32_t extra,const Format & infoBody)169 void PlayerCallbackTest::HandleSubtitleCallback(int32_t extra, const Format &infoBody)
170 {
171 (void)extra;
172 infoBody.GetStringValue(std::string(PlayerKeys::SUBTITLE_TEXT), text_);
173 std::cout << "text = " << text_ << std::endl;
174 textUpdate_ = true;
175 condVarText_.notify_all();
176 }
177
HandleTrackInfoCallback(int32_t extra,const Format & infoBody)178 void PlayerCallbackTest::HandleTrackInfoCallback(int32_t extra, const Format &infoBody)
179 {
180 (void)extra;
181 std::cout << "track info update" << std::endl;
182 trackInfoUpdate_ = true;
183 condVarTrackInfoUpdate_.notify_all();
184 }
185
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)186 void PlayerCallbackTest::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
187 {
188 int32_t index;
189 int32_t isSelect;
190 switch (type) {
191 case INFO_TYPE_SEEKDONE:
192 SetSeekDoneFlag(true);
193 SeekNotify(extra, infoBody);
194 break;
195 case INFO_TYPE_STATE_CHANGE:
196 state_ = static_cast<PlayerStates>(extra);
197 SetState(state_);
198 Notify(state_);
199 break;
200 case INFO_TYPE_SPEEDDONE:
201 SetSpeedDoneFlag(true);
202 condVarSpeed_.notify_all();
203 break;
204 case INFO_TYPE_POSITION_UPDATE:
205 std::cout << "cur position is " << static_cast<uint64_t>(extra) << std::endl;
206 break;
207 case INFO_TYPE_BITRATE_COLLECT:
208 std::cout << "INFO_TYPE_BITRATE_COLLECT: " << extra << std::endl;
209 break;
210 case INFO_TYPE_INTERRUPT_EVENT:
211 std::cout << "INFO_TYPE_INTERRUPT_EVENT: " << extra << std::endl;
212 break;
213 case INFO_TYPE_RESOLUTION_CHANGE:
214 std::cout << "INFO_TYPE_RESOLUTION_CHANGE: " << extra << std::endl;
215 break;
216 case INFO_TYPE_TRACKCHANGE:
217 trackChange_ = true;
218 trackDoneFlag_ = true;
219 infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_TRACK_INDEX), index);
220 infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_IS_SELECT), isSelect);
221 std::cout << "INFO_TYPE_TRACKCHANGE: index " << index << " isSelect " << isSelect << std::endl;
222 condVarTrackDone_.notify_all();
223 break;
224 case INFO_TYPE_SUBTITLE_UPDATE: {
225 HandleSubtitleCallback(extra, infoBody);
226 break;
227 }
228 case INFO_TYPE_TRACK_INFO_UPDATE: {
229 HandleTrackInfoCallback(extra, infoBody);
230 break;
231 }
232 default:
233 break;
234 }
235 }
236
SubtitleTextUpdate(std::string text)237 std::string PlayerCallbackTest::SubtitleTextUpdate(std::string text)
238 {
239 std::unique_lock<std::mutex> lock(subtitleMutex_);
240 std::cout << "wait for text update" <<std::endl;
241 condVarText_.wait_for(lock, std::chrono::seconds(WAITSECOND), [&, this]() {
242 if (text_ != text) {
243 return textUpdate_ = false;
244 }
245 return textUpdate_;
246 });
247 std::cout << "text updated" <<std::endl;
248 textUpdate_ = false;
249 return text_;
250 }
251
OnError(int32_t errorCode,const std::string & errorMsg)252 void PlayerCallbackTest::OnError(int32_t errorCode, const std::string &errorMsg)
253 {
254 if (!trackDoneFlag_) {
255 trackDoneFlag_ = true;
256 condVarTrackDone_.notify_all();
257 }
258 std::cout << "Error received, errorCode: " << errorCode << " errorMsg: " << errorMsg << std::endl;
259 }
260
Notify(PlayerStates currentState)261 void PlayerCallbackTest::Notify(PlayerStates currentState)
262 {
263 if (currentState == PLAYER_PREPARED) {
264 condVarPrepare_.notify_all();
265 } else if (currentState == PLAYER_STARTED) {
266 condVarPlay_.notify_all();
267 } else if (currentState == PLAYER_PAUSED) {
268 condVarPause_.notify_all();
269 } else if (currentState == PLAYER_STOPPED) {
270 condVarStop_.notify_all();
271 } else if (currentState == PLAYER_IDLE) {
272 condVarReset_.notify_all();
273 }
274 }
275
SeekNotify(int32_t extra,const Format & infoBody)276 void PlayerCallbackTest::SeekNotify(int32_t extra, const Format &infoBody)
277 {
278 if (seekMode_ == PlayerSeekMode::SEEK_CLOSEST) {
279 if (seekPosition_ == extra) {
280 condVarSeek_.notify_all();
281 }
282 } else if (seekMode_ == PlayerSeekMode::SEEK_PREVIOUS_SYNC) {
283 if (seekPosition_ - extra < DELTA_TIME && extra - seekPosition_ >= 0) {
284 condVarSeek_.notify_all();
285 }
286 } else if (seekMode_ == PlayerSeekMode::SEEK_NEXT_SYNC) {
287 if (extra - seekPosition_ < DELTA_TIME && seekPosition_ - extra >= 0) {
288 condVarSeek_.notify_all();
289 }
290 } else if (abs(seekPosition_ - extra) <= DELTA_TIME) {
291 condVarSeek_.notify_all();
292 } else {
293 SetSeekDoneFlag(false);
294 }
295 }
296
GetVideoSurface()297 sptr<Surface> PlayerMock::GetVideoSurface()
298 {
299 sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
300 option->SetWindowRect({ 0, 0, width_, height_ });
301 option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_LAUNCHING);
302 option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
303 previewWindow_ = Rosen::Window::Create("xcomponent_window", option);
304 if (previewWindow_ == nullptr || previewWindow_->GetSurfaceNode() == nullptr) {
305 return nullptr;
306 }
307
308 previewWindow_->Show();
309 auto surfaceNode = previewWindow_->GetSurfaceNode();
310 surfaceNode->SetFrameGravity(Rosen::Gravity::RESIZE);
311 Rosen::RSTransaction::FlushImplicitTransaction();
312 return surfaceNode->GetSurface();
313 }
314
PlayerMock(std::shared_ptr<PlayerCallbackTest> & callback)315 PlayerMock::PlayerMock(std::shared_ptr<PlayerCallbackTest> &callback)
316 {
317 callback_ = callback;
318 }
319
~PlayerMock()320 PlayerMock::~PlayerMock()
321 {
322 if (previewWindow_ != nullptr) {
323 previewWindow_->Destroy();
324 previewWindow_ = nullptr;
325 }
326 }
327
CreatePlayer()328 bool PlayerMock::CreatePlayer()
329 {
330 player_ = PlayerFactory::CreatePlayer();
331 return player_ != nullptr;
332 }
333
SetSource(const std::string url)334 int32_t PlayerMock::SetSource(const std::string url)
335 {
336 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
337 std::unique_lock<std::mutex> lock(mutex_);
338 int32_t ret = player_->SetSource(url);
339 return ret;
340 }
341
SetSource(int32_t fd,int64_t offset,int64_t size)342 int32_t PlayerMock::SetSource(int32_t fd, int64_t offset, int64_t size)
343 {
344 std::unique_lock<std::mutex> lock(mutex_);
345 return player_->SetSource(fd, offset, size);
346 }
347
SetSource(const std::string & path,int64_t offset,int64_t size)348 int32_t PlayerMock::SetSource(const std::string &path, int64_t offset, int64_t size)
349 {
350 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
351 std::string rawFile = path.substr(strlen("file://"));
352 int32_t fd = open(rawFile.c_str(), O_RDONLY);
353 if (fd <= 0) {
354 std::cout << "Open file failed" << std::endl;
355 return -1;
356 }
357
358 struct stat64 st;
359 if (fstat64(fd, &st) != 0) {
360 std::cout << "Get file state failed" << std::endl;
361 (void)close(fd);
362 return -1;
363 }
364 int64_t length = static_cast<int64_t>(st.st_size);
365 if (size > 0) {
366 length = size;
367 }
368 int32_t ret = player_->SetSource(fd, offset, length);
369 if (ret != 0) {
370 (void)close(fd);
371 return -1;
372 }
373
374 (void)close(fd);
375 return ret;
376 }
377
SetDataSrc(const std::string & path,int32_t size,bool seekable)378 int32_t PlayerMock::SetDataSrc(const std::string &path, int32_t size, bool seekable)
379 {
380 if (seekable) {
381 dataSrc_ = MediaDataSourceTestSeekable::Create(path, size);
382 } else {
383 dataSrc_ = MediaDataSourceTestNoSeek::Create(path, size);
384 }
385 return player_->SetSource(dataSrc_);
386 }
387
Prepare()388 int32_t PlayerMock::Prepare()
389 {
390 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
391 std::unique_lock<std::mutex> lock(mutex_);
392 int32_t ret = player_->Prepare();
393 if (ret == MSERR_OK) {
394 return callback_->PrepareSync();
395 }
396 return ret;
397 }
398
PrepareAsync()399 int32_t PlayerMock::PrepareAsync()
400 {
401 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
402 std::unique_lock<std::mutex> lock(mutex_);
403 int ret = player_->PrepareAsync();
404 if (ret == MSERR_OK) {
405 return callback_->PrepareSync();
406 }
407 return ret;
408 }
409
Play()410 int32_t PlayerMock::Play()
411 {
412 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
413 std::unique_lock<std::mutex> lock(mutex_);
414 int32_t ret = player_->Play();
415 if (ret == MSERR_OK) {
416 return callback_->PlaySync();
417 }
418 return ret;
419 }
420
Pause()421 int32_t PlayerMock::Pause()
422 {
423 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
424 std::unique_lock<std::mutex> lock(mutex_);
425 int32_t ret = player_->Pause();
426 if (ret == MSERR_OK) {
427 return callback_->PauseSync();
428 }
429 return ret;
430 }
431
Stop()432 int32_t PlayerMock::Stop()
433 {
434 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
435 std::unique_lock<std::mutex> lock(mutex_);
436 int32_t ret = player_->Stop();
437 if (ret == MSERR_OK) {
438 if (dataSrc_ != nullptr) {
439 dataSrc_->Reset();
440 }
441 return callback_->StopSync();
442 }
443 return ret;
444 }
445
SeekPrepare(int32_t & mseconds,PlayerSeekMode & mode)446 void PlayerMock::SeekPrepare(int32_t &mseconds, PlayerSeekMode &mode)
447 {
448 UNITTEST_CHECK_AND_RETURN_LOG(player_ != nullptr && callback_ != nullptr, "player or callback is nullptr");
449 int32_t duration = 0;
450 int32_t seekPosition = 0;
451 callback_->SetSeekDoneFlag(false);
452 player_->GetDuration(duration);
453 if (mseconds < 0) {
454 seekPosition = 0;
455 } else if (mseconds > duration) {
456 seekPosition = duration;
457 } else {
458 seekPosition = mseconds;
459 }
460 callback_->SetSeekPosition(seekPosition);
461 }
462
Seek(int32_t mseconds,PlayerSeekMode mode)463 int32_t PlayerMock::Seek(int32_t mseconds, PlayerSeekMode mode)
464 {
465 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
466 std::unique_lock<std::mutex> lock(mutex_);
467 SeekPrepare(mseconds, mode);
468 int32_t ret = player_->Seek(mseconds, mode);
469 if (ret == MSERR_OK) {
470 return callback_->SeekSync();
471 }
472 return ret;
473 }
474
Reset()475 int32_t PlayerMock::Reset()
476 {
477 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
478 int32_t ret = player_->Reset();
479 if (ret == MSERR_OK) {
480 return callback_->ResetSync();
481 }
482 return ret;
483 }
484
Release()485 int32_t PlayerMock::Release()
486 {
487 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
488 if (previewWindow_ != nullptr) {
489 previewWindow_->Destroy();
490 previewWindow_ = nullptr;
491 }
492 callback_ = nullptr;
493 return player_->Release();
494 }
495
ReleaseSync()496 int32_t PlayerMock::ReleaseSync()
497 {
498 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
499 if (previewWindow_ != nullptr) {
500 previewWindow_->Destroy();
501 previewWindow_ = nullptr;
502 }
503 callback_ = nullptr;
504 return player_->ReleaseSync();
505 }
506
SetVolume(float leftVolume,float rightVolume)507 int32_t PlayerMock::SetVolume(float leftVolume, float rightVolume)
508 {
509 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
510 std::unique_lock<std::mutex> lock(mutex_);
511 return player_->SetVolume(leftVolume, rightVolume);
512 }
513
SetLooping(bool loop)514 int32_t PlayerMock::SetLooping(bool loop)
515 {
516 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
517 std::unique_lock<std::mutex> lock(mutex_);
518 return player_->SetLooping(loop);
519 }
520
GetCurrentTime(int32_t & currentTime)521 int32_t PlayerMock::GetCurrentTime(int32_t ¤tTime)
522 {
523 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
524 std::unique_lock<std::mutex> lock(mutex_);
525 return player_->GetCurrentTime(currentTime);
526 }
527
GetVideoTrackInfo(std::vector<Format> & videoTrack)528 int32_t PlayerMock::GetVideoTrackInfo(std::vector<Format> &videoTrack)
529 {
530 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
531 return player_->GetVideoTrackInfo(videoTrack);
532 }
533
GetAudioTrackInfo(std::vector<Format> & audioTrack)534 int32_t PlayerMock::GetAudioTrackInfo(std::vector<Format> &audioTrack)
535 {
536 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
537 return player_->GetAudioTrackInfo(audioTrack);
538 }
539
GetVideoWidth()540 int32_t PlayerMock::GetVideoWidth()
541 {
542 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
543 return player_->GetVideoWidth();
544 }
545
GetVideoHeight()546 int32_t PlayerMock::GetVideoHeight()
547 {
548 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
549 return player_->GetVideoHeight();
550 }
551
GetDuration(int32_t & duration)552 int32_t PlayerMock::GetDuration(int32_t &duration)
553 {
554 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
555 return player_->GetDuration(duration);
556 }
557
SetPlaybackSpeed(PlaybackRateMode mode)558 int32_t PlayerMock::SetPlaybackSpeed(PlaybackRateMode mode)
559 {
560 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
561 callback_->SetSpeedDoneFlag(false);
562 int32_t ret = player_->SetPlaybackSpeed(mode);
563 if (ret == MSERR_OK) {
564 return callback_->SpeedSync();
565 }
566 return player_->SetPlaybackSpeed(mode);
567 }
568
GetPlaybackSpeed(PlaybackRateMode & mode)569 int32_t PlayerMock::GetPlaybackSpeed(PlaybackRateMode &mode)
570 {
571 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
572 return player_->GetPlaybackSpeed(mode);
573 }
574
SelectBitRate(uint32_t bitRate)575 int32_t PlayerMock::SelectBitRate(uint32_t bitRate)
576 {
577 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
578 return player_->SelectBitRate(bitRate);
579 }
580
IsPlaying()581 bool PlayerMock::IsPlaying()
582 {
583 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
584 std::unique_lock<std::mutex> lock(mutex_);
585 return player_->IsPlaying();
586 }
587
IsLooping()588 bool PlayerMock::IsLooping()
589 {
590 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
591 std::unique_lock<std::mutex> lock(mutex_);
592 return player_->IsLooping();
593 }
594
SetParameter(const Format & param)595 int32_t PlayerMock::SetParameter(const Format ¶m)
596 {
597 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
598 return player_->SetParameter(param);
599 }
600
SetPlayerCallback(const std::shared_ptr<PlayerCallback> & callback)601 int32_t PlayerMock::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback)
602 {
603 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
604 std::unique_lock<std::mutex> lock(mutex_);
605 return player_->SetPlayerCallback(callback);
606 }
607
SetVideoSurface(sptr<Surface> surface)608 int32_t PlayerMock::SetVideoSurface(sptr<Surface> surface)
609 {
610 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
611 std::unique_lock<std::mutex> lock(mutex_);
612 return player_->SetVideoSurface(surface);
613 }
614
SelectTrack(int32_t index,bool & trackChange)615 int32_t PlayerMock::SelectTrack(int32_t index, bool &trackChange)
616 {
617 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
618 std::unique_lock<std::mutex> lock(mutex_);
619 callback_->SetTrackDoneFlag(false);
620 int32_t ret = player_->SelectTrack(index);
621 if (callback_->TrackSync(trackChange) != MSERR_OK) {
622 return -1;
623 }
624 return ret;
625 }
626
DeselectTrack(int32_t index,bool & trackChange)627 int32_t PlayerMock::DeselectTrack(int32_t index, bool &trackChange)
628 {
629 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
630 std::unique_lock<std::mutex> lock(mutex_);
631 callback_->SetTrackDoneFlag(false);
632 int32_t ret = player_->DeselectTrack(index);
633 if (callback_->TrackSync(trackChange) != MSERR_OK) {
634 return -1;
635 }
636 return ret;
637 }
638
GetCurrentTrack(int32_t trackType,int32_t & index)639 int32_t PlayerMock::GetCurrentTrack(int32_t trackType, int32_t &index)
640 {
641 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
642 std::unique_lock<std::mutex> lock(mutex_);
643 return player_->GetCurrentTrack(trackType, index);
644 }
645
AddSubSource(const std::string & url)646 int32_t PlayerMock::AddSubSource(const std::string &url)
647 {
648 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
649 (void)player_->AddSubSource(url);
650 std::cout << "wait for track info callback" << std::endl;
651 return callback_->TrackInfoUpdateSync();
652 }
653
AddSubSource(const std::string & path,int64_t offset,int64_t size)654 int32_t PlayerMock::AddSubSource(const std::string &path, int64_t offset, int64_t size)
655 {
656 UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
657 std::string rawFile = path.substr(strlen("file://"));
658 int32_t fd = open(rawFile.c_str(), O_RDONLY);
659 if (fd <= 0) {
660 std::cout << "Open file failed" << std::endl;
661 return -1;
662 }
663
664 struct stat64 st;
665 if (fstat64(fd, &st) != 0) {
666 std::cout << "Get file state failed" << std::endl;
667 (void)close(fd);
668 return -1;
669 }
670 int64_t length = static_cast<int64_t>(st.st_size);
671 if (size > 0) {
672 length = size;
673 }
674 int32_t ret = player_->AddSubSource(fd, offset, length);
675 if (ret != 0) {
676 (void)close(fd);
677 return -1;
678 }
679 (void)close(fd);
680 std::cout << "wait for track info callback" << std::endl;
681 return callback_->TrackInfoUpdateSync();
682 }
683
GetSubtitleText(std::string text)684 std::string PlayerMock::GetSubtitleText(std::string text)
685 {
686 return callback_->SubtitleTextUpdate(text);
687 }
688 } // namespace Media
689 } // namespace OHOS
690