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