1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HW_CAST_STREAM_PLAYER_H 17 #define HW_CAST_STREAM_PLAYER_H 18 19 #include <mutex> 20 21 #include "cast_engine_common.h" 22 #include "i_stream_player.h" 23 #include "i_avcast_controller_proxy.h" 24 25 namespace OHOS::AVSession { 26 class HwCastStreamPlayer : public IAVCastControllerProxy, public CastEngine::IStreamPlayerListener, 27 public std::enable_shared_from_this<HwCastStreamPlayer> { 28 public: HwCastStreamPlayer(std::shared_ptr<CastEngine::IStreamPlayer> streamPlayer)29 explicit HwCastStreamPlayer(std::shared_ptr<CastEngine::IStreamPlayer> streamPlayer) 30 : streamPlayer_(streamPlayer) {} 31 ~HwCastStreamPlayer() override; 32 void Init(); 33 void Release() override; 34 void SendControlCommand(const AVCastControlCommand castControlCommand) override; 35 AVQueueItem GetCurrentItem() override; 36 int32_t Start(const AVQueueItem& avQueueItem) override; 37 int32_t Prepare(const AVQueueItem& avQueueItem) override; 38 int32_t GetDuration(int32_t &duration) override; 39 int32_t GetCastAVPlaybackState(AVPlaybackState& avPlaybackState) override; 40 int32_t SetDisplaySurface(std::string &surfaceId) override; 41 int32_t RegisterControllerListener(const std::shared_ptr<IAVCastControllerProxyListener>) override; 42 int32_t UnRegisterControllerListener(const std::shared_ptr<IAVCastControllerProxyListener>) override; 43 44 void OnStateChanged(const CastEngine::PlayerStates playbackState, bool isPlayWhenReady) override; 45 void OnPositionChanged(int position, int bufferPosition, int duration) override; 46 void OnMediaItemChanged(const CastEngine::MediaInfo &mediaInfo) override; 47 void OnVolumeChanged(int volume, int maxVolume) override; 48 void OnLoopModeChanged(const CastEngine::LoopMode loopMode) override; 49 void OnNextRequest() override; 50 void OnPreviousRequest() override; 51 void OnPlaySpeedChanged(const CastEngine::PlaybackSpeed speed) override; 52 void OnPlayerError(int errorCode, const std::string &errorMsg) override; 53 void OnSeekDone(int32_t seekNumber) override; 54 void OnVideoSizeChanged(int width, int height) override; 55 void OnEndOfStream(int isLooping) override; 56 57 void SendControlCommandWithParams(const AVCastControlCommand castControlCommand); 58 59 private: 60 std::recursive_mutex streamPlayerLock_; 61 std::shared_ptr<CastEngine::IStreamPlayer> streamPlayer_; 62 std::vector<std::shared_ptr<IAVCastControllerProxyListener>> streamPlayerListenerList_; 63 AVQueueItem currentAVQueueItem_; 64 std::map<CastEngine::PlayerStates, int32_t> castPlusStateToString_ = { 65 {CastEngine::PlayerStates::PLAYER_STATE_ERROR, AVPlaybackState::PLAYBACK_STATE_ERROR}, 66 {CastEngine::PlayerStates::PLAYER_IDLE, AVPlaybackState::PLAYBACK_STATE_INITIAL}, 67 {CastEngine::PlayerStates::PLAYER_INITIALIZED, AVPlaybackState::PLAYBACK_STATE_INITIAL}, 68 {CastEngine::PlayerStates::PLAYER_PREPARING, AVPlaybackState::PLAYBACK_STATE_PREPARE}, 69 {CastEngine::PlayerStates::PLAYER_PREPARED, AVPlaybackState::PLAYBACK_STATE_PREPARE}, 70 {CastEngine::PlayerStates::PLAYER_STARTED, AVPlaybackState::PLAYBACK_STATE_PLAY}, 71 {CastEngine::PlayerStates::PLAYER_PAUSED, AVPlaybackState::PLAYBACK_STATE_PAUSE}, 72 {CastEngine::PlayerStates::PLAYER_STOPPED, AVPlaybackState::PLAYBACK_STATE_STOP}, 73 {CastEngine::PlayerStates::PLAYER_PLAYBACK_COMPLETE, AVPlaybackState::PLAYBACK_STATE_COMPLETED}, 74 {CastEngine::PlayerStates::PLAYER_RELEASED, AVPlaybackState::PLAYBACK_STATE_RELEASED}, 75 }; 76 std::map<CastEngine::PlaybackSpeed, double> castPlusSpeedToDouble_ = { 77 {CastEngine::PlaybackSpeed::SPEED_FORWARD_0_75_X, 0.75}, 78 {CastEngine::PlaybackSpeed::SPEED_FORWARD_1_00_X, 1.00}, 79 {CastEngine::PlaybackSpeed::SPEED_FORWARD_1_25_X, 1.25}, 80 {CastEngine::PlaybackSpeed::SPEED_FORWARD_1_75_X, 1.75}, 81 {CastEngine::PlaybackSpeed::SPEED_FORWARD_2_00_X, 2.00}, 82 }; 83 std::map<CastEngine::LoopMode, int32_t> castPlusLoopModeToInt_ = { 84 {CastEngine::LoopMode::LOOP_MODE_SEQUENCE, AVPlaybackState::LOOP_MODE_SEQUENCE}, 85 {CastEngine::LoopMode::LOOP_MODE_SINGLE, AVPlaybackState::LOOP_MODE_SINGLE}, 86 {CastEngine::LoopMode::LOOP_MODE_LIST, AVPlaybackState::LOOP_MODE_LIST}, 87 {CastEngine::LoopMode::LOOP_MODE_SHUFFLE, AVPlaybackState::LOOP_MODE_SHUFFLE}, 88 }; 89 }; 90 } // namespace OHOS::AVSession 91 92 #endif