• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 
SetRateDoneFlag(bool rateDoneFlag)43 void PlayerCallbackTest::SetRateDoneFlag(bool rateDoneFlag)
44 {
45     rateDoneFlag_ = rateDoneFlag;
46 }
47 
SetSeekPosition(int32_t seekPosition)48 void PlayerCallbackTest::SetSeekPosition(int32_t seekPosition)
49 {
50     seekPosition_ = seekPosition;
51 }
52 
SetTrackDoneFlag(bool trackDoneFlag)53 void PlayerCallbackTest::SetTrackDoneFlag(bool trackDoneFlag)
54 {
55     std::unique_lock<std::mutex> lockSpeed(mutexCond_);
56     trackDoneFlag_ = trackDoneFlag;
57     trackChange_ = trackDoneFlag;
58 }
59 
PrepareSync()60 int32_t PlayerCallbackTest::PrepareSync()
61 {
62     if (state_ != PLAYER_PREPARED) {
63         std::unique_lock<std::mutex> lockPrepare(mutexCond_);
64         condVarPrepare_.wait_for(lockPrepare, std::chrono::seconds(waitsecond));
65         if (state_ != PLAYER_PREPARED) {
66             return -1;
67         }
68     }
69     return MSERR_OK;
70 }
71 
PlaySync()72 int32_t PlayerCallbackTest::PlaySync()
73 {
74     if (state_ != PLAYER_STARTED) {
75         std::unique_lock<std::mutex> lockPlay(mutexCond_);
76         condVarPlay_.wait_for(lockPlay, std::chrono::seconds(waitsecond));
77         if (state_ != PLAYER_STARTED && state_ != PLAYER_PLAYBACK_COMPLETE) {
78             return -1;
79         }
80     }
81     return MSERR_OK;
82 }
83 
PauseSync()84 int32_t PlayerCallbackTest::PauseSync()
85 {
86     if (state_ != PLAYER_PAUSED) {
87         std::unique_lock<std::mutex> lockPause(mutexCond_);
88         condVarPause_.wait_for(lockPause, std::chrono::seconds(waitsecond));
89         if (state_ != PLAYER_PAUSED) {
90             return -1;
91         }
92     }
93     return MSERR_OK;
94 }
95 
StopSync()96 int32_t PlayerCallbackTest::StopSync()
97 {
98     if (state_ != PLAYER_STOPPED) {
99         std::unique_lock<std::mutex> lockStop(mutexCond_);
100         condVarStop_.wait_for(lockStop, std::chrono::seconds(waitsecond));
101         if (state_ != PLAYER_STOPPED) {
102             return -1;
103         }
104     }
105     return MSERR_OK;
106 }
107 
ResetSync()108 int32_t PlayerCallbackTest::ResetSync()
109 {
110     if (state_ != PLAYER_IDLE) {
111         std::unique_lock<std::mutex> lockReset(mutexCond_);
112         condVarReset_.wait_for(lockReset, std::chrono::seconds(waitsecond));
113         if (state_ != PLAYER_IDLE) {
114             return -1;
115         }
116     }
117     return MSERR_OK;
118 }
119 
SeekSync()120 int32_t PlayerCallbackTest::SeekSync()
121 {
122     if (seekDoneFlag_ == false) {
123         std::unique_lock<std::mutex> lockSeek(mutexCond_);
124         condVarSeek_.wait_for(lockSeek, std::chrono::seconds(waitsecond));
125         if (seekDoneFlag_ == false) {
126             return -1;
127         }
128     }
129     return MSERR_OK;
130 }
131 
SpeedSync()132 int32_t PlayerCallbackTest::SpeedSync()
133 {
134     if (speedDoneFlag_ == false) {
135         std::unique_lock<std::mutex> lockSpeed(mutexCond_);
136         condVarSpeed_.wait_for(lockSpeed, std::chrono::seconds(waitsecond));
137         if (speedDoneFlag_ == false) {
138             return -1;
139         }
140     }
141     return MSERR_OK;
142 }
143 
RateSync()144 int32_t PlayerCallbackTest::RateSync()
145 {
146     if (rateDoneFlag_ == false) {
147         std::unique_lock<std::mutex> lockRate(mutexCond_);
148         condVarRate_.wait_for(lockRate, std::chrono::seconds(waitsecond));
149         if (rateDoneFlag_ == false) {
150             return -1;
151         }
152     }
153     return MSERR_OK;
154 }
155 
TrackSync(bool & trackChange)156 int32_t PlayerCallbackTest::TrackSync(bool &trackChange)
157 {
158     if (trackDoneFlag_ == false) {
159         std::unique_lock<std::mutex> lockTrackDone(mutexCond_);
160         condVarTrackDone_.wait_for(lockTrackDone, std::chrono::seconds(waitsecond));
161         if (trackDoneFlag_ == false) {
162             return -1;
163         }
164     }
165 
166     trackChange = trackChange_;
167 
168     return MSERR_OK;
169 }
170 
TrackInfoUpdateSync()171 int32_t PlayerCallbackTest::TrackInfoUpdateSync()
172 {
173     if (trackInfoUpdate_ == false) {
174         std::unique_lock<std::mutex> lock(mutexCond_);
175         condVarTrackInfoUpdate_.wait_for(lock, std::chrono::seconds(waitsecond), [this]() {
176             return trackInfoUpdate_;
177         });
178         if (trackInfoUpdate_ == false) {
179             return -1;
180         }
181     }
182     trackInfoUpdate_ = false;
183     return MSERR_OK;
184 }
185 
HandleTrackChangeCallback(int32_t extra,const Format & infoBody)186 void PlayerCallbackTest::HandleTrackChangeCallback(int32_t extra, const Format &infoBody)
187 {
188     (void)extra;
189     trackChange_ = true;
190     trackDoneFlag_ = true;
191     int32_t index;
192     int32_t isSelect;
193     infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_TRACK_INDEX), index);
194     infoBody.GetIntValue(std::string(PlayerKeys::PLAYER_IS_SELECT), isSelect);
195     std::cout << "INFO_TYPE_TRACKCHANGE: index " << index << " isSelect " << isSelect << std::endl;
196     condVarTrackDone_.notify_all();
197 }
198 
HandleSubtitleCallback(int32_t extra,const Format & infoBody)199 void PlayerCallbackTest::HandleSubtitleCallback(int32_t extra, const Format &infoBody)
200 {
201     (void)extra;
202     infoBody.GetStringValue(std::string(PlayerKeys::SUBTITLE_TEXT), text_);
203     std::cout << "text = " << text_ << std::endl;
204     textUpdate_ = true;
205     condVarText_.notify_all();
206 }
207 
HandleTrackInfoCallback(int32_t extra,const Format & infoBody)208 void PlayerCallbackTest::HandleTrackInfoCallback(int32_t extra, const Format &infoBody)
209 {
210     (void)extra;
211     std::cout << "track info update" << std::endl;
212     trackInfoUpdate_ = true;
213     condVarTrackInfoUpdate_.notify_all();
214 }
215 
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)216 void PlayerCallbackTest::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
217 {
218     switch (type) {
219         case INFO_TYPE_SEEKDONE:
220             SetSeekDoneFlag(true);
221             SeekNotify(extra, infoBody);
222             break;
223         case INFO_TYPE_STATE_CHANGE:
224             state_ = static_cast<PlayerStates>(extra);
225             SetState(state_);
226             Notify(state_);
227             break;
228         case INFO_TYPE_RATEDONE:
229             SetRateDoneFlag(true);
230             condVarRate_.notify_all();
231             break;
232         case INFO_TYPE_SPEEDDONE:
233             SetSpeedDoneFlag(true);
234             condVarSpeed_.notify_all();
235             break;
236         case INFO_TYPE_POSITION_UPDATE:
237             std::cout << "cur position is " << static_cast<uint64_t>(extra) << std::endl;
238             break;
239         case INFO_TYPE_BITRATE_COLLECT:
240             std::cout << "INFO_TYPE_BITRATE_COLLECT: " << extra << std::endl;
241             break;
242         case INFO_TYPE_INTERRUPT_EVENT:
243             std::cout << "INFO_TYPE_INTERRUPT_EVENT: " << extra << std::endl;
244             break;
245         case INFO_TYPE_RESOLUTION_CHANGE:
246             std::cout << "INFO_TYPE_RESOLUTION_CHANGE: " << extra << std::endl;
247             break;
248         case INFO_TYPE_TRACKCHANGE:
249             HandleTrackChangeCallback(extra, infoBody);
250             break;
251         case INFO_TYPE_SUBTITLE_UPDATE: {
252             HandleSubtitleCallback(extra, infoBody);
253             break;
254         }
255         case INFO_TYPE_TRACK_INFO_UPDATE: {
256             HandleTrackInfoCallback(extra, infoBody);
257             break;
258         }
259         case INFO_TYPE_AUDIO_DEVICE_CHANGE: {
260             std::cout << "device change reason is " << extra;
261             break;
262         }
263         default:
264             break;
265     }
266 }
267 
SubtitleTextUpdate(std::string text)268 std::string PlayerCallbackTest::SubtitleTextUpdate(std::string text)
269 {
270     std::unique_lock<std::mutex> lock(subtitleMutex_);
271     std::cout << "wait for text update" <<std::endl;
272     condVarText_.wait_for(lock, std::chrono::seconds(waitsecond), [&, this]() {
273         if (text_ != text) {
274             return textUpdate_ = false;
275         }
276         return textUpdate_;
277     });
278     std::cout << "text updated" <<std::endl;
279     textUpdate_ = false;
280     return text_;
281 }
282 
GetState()283 PlayerStates PlayerCallbackTest::GetState()
284 {
285     return state_;
286 }
287 
OnError(int32_t errorCode,const std::string & errorMsg)288 void PlayerCallbackTest::OnError(int32_t errorCode, const std::string &errorMsg)
289 {
290     if (!trackDoneFlag_) {
291         trackDoneFlag_ = true;
292         condVarTrackDone_.notify_all();
293     }
294     std::cout << "Error received, errorCode: " << errorCode << " errorMsg: " << errorMsg << std::endl;
295 }
296 
Notify(PlayerStates currentState)297 void PlayerCallbackTest::Notify(PlayerStates currentState)
298 {
299     if (currentState == PLAYER_PREPARED) {
300         condVarPrepare_.notify_all();
301     } else if (currentState == PLAYER_STARTED) {
302         condVarPlay_.notify_all();
303     } else if (currentState == PLAYER_PAUSED) {
304         condVarPause_.notify_all();
305     } else if (currentState == PLAYER_STOPPED) {
306         condVarStop_.notify_all();
307     } else if (currentState == PLAYER_IDLE) {
308         condVarReset_.notify_all();
309     }
310 }
311 
SeekNotify(int32_t extra,const Format & infoBody)312 void PlayerCallbackTest::SeekNotify(int32_t extra, const Format &infoBody)
313 {
314     if (seekMode_ == PlayerSeekMode::SEEK_CLOSEST) {
315         if (seekPosition_ == extra) {
316             condVarSeek_.notify_all();
317         }
318     } else if (seekMode_ == PlayerSeekMode::SEEK_PREVIOUS_SYNC) {
319         if (seekPosition_ - extra < DELTA_TIME && extra - seekPosition_ >= 0) {
320             condVarSeek_.notify_all();
321         }
322     } else if (seekMode_ == PlayerSeekMode::SEEK_NEXT_SYNC) {
323         if (extra - seekPosition_ < DELTA_TIME && seekPosition_ - extra >= 0) {
324             condVarSeek_.notify_all();
325         }
326     } else if (abs(seekPosition_ - extra) <= DELTA_TIME) {
327         condVarSeek_.notify_all();
328     } else {
329         SetSeekDoneFlag(false);
330     }
331 }
332 
GetVideoSurface()333 sptr<Surface> PlayerMock::GetVideoSurface()
334 {
335     sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
336     option->SetWindowRect({ 0, 0, width_, height_ });
337     option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_TOAST);
338     option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
339     previewWindow_ = Rosen::Window::Create("xcomponent_window", option);
340     if (previewWindow_ == nullptr || previewWindow_->GetSurfaceNode() == nullptr) {
341         return nullptr;
342     }
343 
344     previewWindow_->Show();
345     auto surfaceNode = previewWindow_->GetSurfaceNode();
346     surfaceNode->SetFrameGravity(Rosen::Gravity::RESIZE);
347     Rosen::RSTransaction::FlushImplicitTransaction();
348     return surfaceNode->GetSurface();
349 }
350 
PlayerMock(std::shared_ptr<PlayerCallbackTest> & callback)351 PlayerMock::PlayerMock(std::shared_ptr<PlayerCallbackTest> &callback)
352 {
353     callback_ = callback;
354 }
355 
~PlayerMock()356 PlayerMock::~PlayerMock()
357 {
358     if (previewWindow_ != nullptr) {
359         previewWindow_->Destroy();
360         previewWindow_ = nullptr;
361     }
362 
363     if (previewWindowNext_ != nullptr) {
364         previewWindowNext_->Destroy();
365         previewWindowNext_ = nullptr;
366     }
367 }
368 
CreatePlayer()369 bool PlayerMock::CreatePlayer()
370 {
371     player_ = PlayerFactory::CreatePlayer();
372     return player_ != nullptr;
373 }
374 
SetMediaSource(const std::shared_ptr<AVMediaSource> & mediaSource,AVPlayStrategy strategy)375 int32_t PlayerMock::SetMediaSource(const std::shared_ptr<AVMediaSource> &mediaSource, AVPlayStrategy strategy)
376 {
377     std::unique_lock<std::mutex> lock(mutex_);
378     return player_->SetMediaSource(mediaSource, strategy);
379 }
380 
SetSource(const std::string url)381 int32_t PlayerMock::SetSource(const std::string url)
382 {
383     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
384     std::unique_lock<std::mutex> lock(mutex_);
385     int32_t ret = player_->SetSource(url);
386     return ret;
387 }
388 
SetSource(int32_t fd,int64_t offset,int64_t size)389 int32_t PlayerMock::SetSource(int32_t fd, int64_t offset, int64_t size)
390 {
391     std::unique_lock<std::mutex> lock(mutex_);
392     return player_->SetSource(fd, offset, size);
393 }
394 
SetSource(const std::string & path,int64_t offset,int64_t size)395 int32_t PlayerMock::SetSource(const std::string &path, int64_t offset, int64_t size)
396 {
397     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
398     std::string rawFile = path.substr(strlen("file://"));
399     int32_t fd = open(rawFile.c_str(), O_RDONLY);
400     if (fd <= 0) {
401         std::cout << "Open file failed" << std::endl;
402         return -1;
403     }
404 
405     struct stat64 st;
406     if (fstat64(fd, &st) != 0) {
407         std::cout << "Get file state failed" << std::endl;
408         (void)close(fd);
409         return -1;
410     }
411     int64_t length = static_cast<int64_t>(st.st_size);
412     if (size > 0) {
413         length = size;
414     }
415     int32_t ret = player_->SetSource(fd, offset, length);
416     if (ret != 0) {
417         (void)close(fd);
418         return -1;
419     }
420 
421     (void)close(fd);
422     return ret;
423 }
424 
SetDataSrc(const std::string & path,int32_t size,bool seekable)425 int32_t PlayerMock::SetDataSrc(const std::string &path, int32_t size, bool seekable)
426 {
427     if (seekable) {
428         dataSrc_ = MediaDataSourceTestSeekable::Create(path, size);
429     } else {
430         dataSrc_ = MediaDataSourceTestNoSeek::Create(path, size);
431     }
432     return player_->SetSource(dataSrc_);
433 }
434 
Prepare()435 int32_t PlayerMock::Prepare()
436 {
437     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
438     std::unique_lock<std::mutex> lock(mutex_);
439     int32_t ret = player_->Prepare();
440     if (ret == MSERR_OK) {
441         return callback_->PrepareSync();
442     }
443     return ret;
444 }
445 
PrepareAsync()446 int32_t PlayerMock::PrepareAsync()
447 {
448     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
449     std::unique_lock<std::mutex> lock(mutex_);
450     int ret = player_->PrepareAsync();
451     if (ret == MSERR_OK) {
452         return callback_->PrepareSync();
453     }
454     return ret;
455 }
456 
Play()457 int32_t PlayerMock::Play()
458 {
459     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
460     std::unique_lock<std::mutex> lock(mutex_);
461     int32_t ret = player_->Play();
462     if (ret == MSERR_OK) {
463         return callback_->PlaySync();
464     }
465     return ret;
466 }
467 
Pause()468 int32_t PlayerMock::Pause()
469 {
470     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
471     std::unique_lock<std::mutex> lock(mutex_);
472     int32_t ret = player_->Pause();
473     if (ret == MSERR_OK) {
474         return callback_->PauseSync();
475     }
476     return ret;
477 }
478 
Stop()479 int32_t PlayerMock::Stop()
480 {
481     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
482     std::unique_lock<std::mutex> lock(mutex_);
483     int32_t ret = player_->Stop();
484     if (ret == MSERR_OK) {
485         if (dataSrc_ != nullptr) {
486             dataSrc_->Reset();
487         }
488         return callback_->StopSync();
489     }
490     return ret;
491 }
492 
SeekPrepare(int32_t & mseconds,PlayerSeekMode & mode)493 void PlayerMock::SeekPrepare(int32_t &mseconds, PlayerSeekMode &mode)
494 {
495     UNITTEST_CHECK_AND_RETURN_LOG(player_ != nullptr && callback_ != nullptr, "player or callback is nullptr");
496     int32_t duration = 0;
497     int32_t seekPosition = 0;
498     callback_->SetSeekDoneFlag(false);
499     player_->GetDuration(duration);
500     if (mseconds < 0) {
501         seekPosition = 0;
502     } else if (mseconds > duration) {
503         seekPosition = duration;
504     } else {
505         seekPosition = mseconds;
506     }
507     callback_->SetSeekPosition(seekPosition);
508 }
509 
Seek(int32_t mseconds,PlayerSeekMode mode)510 int32_t PlayerMock::Seek(int32_t mseconds, PlayerSeekMode mode)
511 {
512     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
513     std::unique_lock<std::mutex> lock(mutex_);
514     SeekPrepare(mseconds, mode);
515     int32_t ret = player_->Seek(mseconds, mode);
516     if (ret == MSERR_OK) {
517         return callback_->SeekSync();
518     }
519     return ret;
520 }
521 
Reset()522 int32_t PlayerMock::Reset()
523 {
524     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
525     int32_t ret = player_->Reset();
526     if (ret == MSERR_OK) {
527         return callback_->ResetSync();
528     }
529     return ret;
530 }
531 
Release()532 int32_t PlayerMock::Release()
533 {
534     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
535     if (previewWindow_ != nullptr) {
536         previewWindow_->Destroy();
537         previewWindow_ = nullptr;
538     }
539     callback_ = nullptr;
540     return player_->Release();
541 }
542 
ReleaseSync()543 int32_t PlayerMock::ReleaseSync()
544 {
545     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
546     if (previewWindow_ != nullptr) {
547         previewWindow_->Destroy();
548         previewWindow_ = nullptr;
549     }
550     callback_ = nullptr;
551     return player_->ReleaseSync();
552 }
553 
SetVolume(float leftVolume,float rightVolume)554 int32_t PlayerMock::SetVolume(float leftVolume, float rightVolume)
555 {
556     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
557     std::unique_lock<std::mutex> lock(mutex_);
558     return player_->SetVolume(leftVolume, rightVolume);
559 }
560 
SetLooping(bool loop)561 int32_t PlayerMock::SetLooping(bool loop)
562 {
563     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
564     std::unique_lock<std::mutex> lock(mutex_);
565     return player_->SetLooping(loop);
566 }
567 
GetCurrentTime(int32_t & currentTime)568 int32_t PlayerMock::GetCurrentTime(int32_t &currentTime)
569 {
570     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
571     std::unique_lock<std::mutex> lock(mutex_);
572     return player_->GetCurrentTime(currentTime);
573 }
574 
GetVideoTrackInfo(std::vector<Format> & videoTrack)575 int32_t PlayerMock::GetVideoTrackInfo(std::vector<Format> &videoTrack)
576 {
577     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
578     return player_->GetVideoTrackInfo(videoTrack);
579 }
580 
GetPlaybackInfo(Format & playbackInfo)581 int32_t PlayerMock::GetPlaybackInfo(Format &playbackInfo)
582 {
583     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
584     return player_->GetPlaybackInfo(playbackInfo);
585 }
586 
GetAudioTrackInfo(std::vector<Format> & audioTrack)587 int32_t PlayerMock::GetAudioTrackInfo(std::vector<Format> &audioTrack)
588 {
589     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
590     return player_->GetAudioTrackInfo(audioTrack);
591 }
592 
GetVideoWidth()593 int32_t PlayerMock::GetVideoWidth()
594 {
595     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
596     return player_->GetVideoWidth();
597 }
598 
GetVideoHeight()599 int32_t PlayerMock::GetVideoHeight()
600 {
601     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
602     return player_->GetVideoHeight();
603 }
604 
GetDuration(int32_t & duration)605 int32_t PlayerMock::GetDuration(int32_t &duration)
606 {
607     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
608     return player_->GetDuration(duration);
609 }
610 
SetPlaybackSpeed(PlaybackRateMode mode)611 int32_t PlayerMock::SetPlaybackSpeed(PlaybackRateMode mode)
612 {
613     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
614     callback_->SetSpeedDoneFlag(false);
615     int32_t ret = player_->SetPlaybackSpeed(mode);
616     if (ret == MSERR_OK) {
617         return callback_->SpeedSync();
618     }
619     return player_->SetPlaybackSpeed(mode);
620 }
621 
GetPlaybackSpeed(PlaybackRateMode & mode)622 int32_t PlayerMock::GetPlaybackSpeed(PlaybackRateMode &mode)
623 {
624     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
625     return player_->GetPlaybackSpeed(mode);
626 }
627 
SetPlaybackRate(float rate)628 int32_t PlayerMock::SetPlaybackRate(float rate)
629 {
630     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
631     callback_->SetRateDoneFlag(false);
632     int32_t ret = player_->SetPlaybackRate(rate);
633     if (ret == MSERR_OK) {
634         return callback_->RateSync();
635     }
636     return player_->SetPlaybackRate(rate);
637 }
638 
SelectBitRate(uint32_t bitRate)639 int32_t PlayerMock::SelectBitRate(uint32_t bitRate)
640 {
641     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
642     return player_->SelectBitRate(bitRate);
643 }
644 
IsPlaying()645 bool PlayerMock::IsPlaying()
646 {
647     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
648     std::unique_lock<std::mutex> lock(mutex_);
649     return player_->IsPlaying();
650 }
651 
IsLooping()652 bool PlayerMock::IsLooping()
653 {
654     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
655     std::unique_lock<std::mutex> lock(mutex_);
656     return player_->IsLooping();
657 }
658 
SetVolumeMode(int32_t mode)659 int32_t PlayerMock::SetVolumeMode(int32_t mode)
660 {
661     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
662     return player_->SetVolumeMode(mode);
663 }
664 
SetParameter(const Format & param)665 int32_t PlayerMock::SetParameter(const Format &param)
666 {
667     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
668     return player_->SetParameter(param);
669 }
670 
SetPlayerCallback(const std::shared_ptr<PlayerCallback> & callback)671 int32_t PlayerMock::SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback)
672 {
673     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
674     std::unique_lock<std::mutex> lock(mutex_);
675     return player_->SetPlayerCallback(callback);
676 }
677 
SetVideoSurface(sptr<Surface> surface)678 int32_t PlayerMock::SetVideoSurface(sptr<Surface> surface)
679 {
680     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
681     std::unique_lock<std::mutex> lock(mutex_);
682     return player_->SetVideoSurface(surface);
683 }
684 
SelectTrack(int32_t index,bool & trackChange)685 int32_t PlayerMock::SelectTrack(int32_t index, bool &trackChange)
686 {
687     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
688     std::unique_lock<std::mutex> lock(mutex_);
689     callback_->SetTrackDoneFlag(false);
690     int32_t ret = player_->SelectTrack(index, PlayerSwitchMode::SWITCH_SMOOTH);
691     if (callback_->TrackSync(trackChange) != MSERR_OK) {
692         return -1;
693     }
694     return ret;
695 }
696 
DeselectTrack(int32_t index,bool & trackChange)697 int32_t PlayerMock::DeselectTrack(int32_t index, bool &trackChange)
698 {
699     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
700     std::unique_lock<std::mutex> lock(mutex_);
701     callback_->SetTrackDoneFlag(false);
702     int32_t ret = player_->DeselectTrack(index);
703     if (callback_->TrackSync(trackChange) != MSERR_OK) {
704         return -1;
705     }
706     return ret;
707 }
708 
GetCurrentTrack(int32_t trackType,int32_t & index)709 int32_t PlayerMock::GetCurrentTrack(int32_t trackType, int32_t &index)
710 {
711     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
712     std::unique_lock<std::mutex> lock(mutex_);
713     return player_->GetCurrentTrack(trackType, index);
714 }
715 
AddSubSource(const std::string & url)716 int32_t PlayerMock::AddSubSource(const std::string &url)
717 {
718     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
719     (void)player_->AddSubSource(url);
720     std::cout << "wait for track info callback" << std::endl;
721     return callback_->TrackInfoUpdateSync();
722 }
723 
AddSubSource(const std::string & path,int64_t offset,int64_t size)724 int32_t PlayerMock::AddSubSource(const std::string &path, int64_t offset, int64_t size)
725 {
726     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr && callback_ != nullptr, -1, "player or callback is nullptr");
727     std::string rawFile = path.substr(strlen("file://"));
728     int32_t fileDescriptor = open(rawFile.c_str(), O_RDONLY);
729     if (fileDescriptor <= 0) {
730         std::cout << "Open file failed." << std::endl;
731         return -1;
732     }
733 
734     struct stat64 st;
735     if (fstat64(fileDescriptor, &st) != 0) {
736         std::cout << "Get file state failed" << std::endl;
737         (void)close(fileDescriptor);
738         return -1;
739     }
740     int64_t stLen = static_cast<int64_t>(st.st_size);
741     if (size > 0) {
742         stLen = size;
743     }
744     int32_t ret = player_->AddSubSource(fileDescriptor, offset, stLen);
745     if (ret != 0) {
746         (void)close(fileDescriptor);
747         return -1;
748     }
749     (void)close(fileDescriptor);
750     std::cout << "wait for track info callback" << std::endl;
751     return callback_->TrackInfoUpdateSync();
752 }
753 
GetSubtitleText(std::string text)754 std::string PlayerMock::GetSubtitleText(std::string text)
755 {
756     return callback_->SubtitleTextUpdate(text);
757 }
758 
GetVideoSurfaceNext()759 sptr<Surface> PlayerMock::GetVideoSurfaceNext()
760 {
761     sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
762     option->SetWindowRect({ 0, 0, nextSurfaceWidth_, nextSurfaceHeight_ });
763     option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_TOAST);
764     option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
765     previewWindowNext_ = Rosen::Window::Create("xcomponent_window_next", option);
766     if (previewWindowNext_ == nullptr || previewWindowNext_->GetSurfaceNode() == nullptr) {
767         return nullptr;
768     }
769 
770     previewWindowNext_->Show();
771     auto surfaceNode = previewWindowNext_->GetSurfaceNode();
772     surfaceNode->SetFrameGravity(Rosen::Gravity::RESIZE);
773     Rosen::RSTransaction::FlushImplicitTransaction();
774     return surfaceNode->GetSurface();
775 }
776 
GetState()777 PlayerStates PlayerMock::GetState()
778 {
779     return callback_->GetState();
780 }
781 
SetPlayRange(int64_t start,int64_t end)782 int32_t PlayerMock::SetPlayRange(int64_t start, int64_t end)
783 {
784     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
785     std::unique_lock<std::mutex> lock(mutex_);
786     return player_->SetPlayRange(start, end);
787 }
788 
SeekContinuous(int32_t mseconds)789 int32_t PlayerMock::SeekContinuous(int32_t mseconds)
790 {
791     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
792     std::unique_lock<std::mutex> lock(mutex_);
793     return player_->Seek(mseconds, PlayerSeekMode::SEEK_CONTINOUS);
794 }
795 
SetMaxAmplitudeCbStatus(bool status)796 int32_t PlayerMock::SetMaxAmplitudeCbStatus(bool status)
797 {
798     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
799     return player_->SetMaxAmplitudeCbStatus(status);
800 }
801 
SetPlaybackStrategy(AVPlayStrategy strategy)802 int32_t PlayerMock::SetPlaybackStrategy(AVPlayStrategy strategy)
803 {
804     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
805     std::unique_lock<std::mutex> lock(mutex_);
806     return player_->SetPlaybackStrategy(strategy);
807 }
808 
SetMediaMuted(OHOS::Media::MediaType mediaType,bool isMuted)809 int32_t PlayerMock::SetMediaMuted(OHOS::Media::MediaType mediaType, bool isMuted)
810 {
811     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
812     std::unique_lock<std::mutex> lock(mutex_);
813     return player_->SetMediaMuted(mediaType, isMuted);
814 }
815 
SetDeviceChangeCbStatus(bool status)816 int32_t PlayerMock::SetDeviceChangeCbStatus(bool status)
817 {
818     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
819     return player_->SetDeviceChangeCbStatus(status);
820 }
821 
SetSuperResolution(bool enabled)822 int32_t PlayerMock::SetSuperResolution(bool enabled)
823 {
824     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
825     return player_->SetSuperResolution(enabled);
826 }
827 
SetVideoWindowSize(int32_t width,int32_t height)828 int32_t PlayerMock::SetVideoWindowSize(int32_t width, int32_t height)
829 {
830     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
831     return player_->SetVideoWindowSize(width, height);
832 }
833 
EnableReportMediaProgress(bool enable)834 int32_t PlayerMock::EnableReportMediaProgress(bool enable)
835 {
836     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
837     return player_->EnableReportMediaProgress(enable);
838 }
839 
EnableReportAudioInterrupt(bool enable)840 int32_t PlayerMock::EnableReportAudioInterrupt(bool enable)
841 {
842     UNITTEST_CHECK_AND_RETURN_RET_LOG(player_ != nullptr, -1, "player_ == nullptr");
843     return player_->EnableReportAudioInterrupt(enable);
844 }
845 
ReleaseClientListener()846 void PlayerMock::ReleaseClientListener()
847 {
848     UNITTEST_CHECK_AND_RETURN_LOG(player_ != nullptr, "player_ == nullptr");
849     player_->ReleaseClientListener();
850 }
851 } // namespace Media
852 } // namespace OHOS