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 #include "avsession_pixel_map_adapter.h" 25 26 namespace OHOS::AVSession { 27 class HwCastStreamPlayer : public IAVCastControllerProxy, public CastEngine::IStreamPlayerListener, 28 public std::enable_shared_from_this<HwCastStreamPlayer> { 29 public: HwCastStreamPlayer(std::shared_ptr<CastEngine::IStreamPlayer> streamPlayer)30 explicit HwCastStreamPlayer(std::shared_ptr<CastEngine::IStreamPlayer> streamPlayer) 31 : streamPlayer_(streamPlayer) {} 32 ~HwCastStreamPlayer() override; 33 void Init(); 34 void Release() override; 35 void SendControlCommand(const AVCastControlCommand castControlCommand) override; 36 AVQueueItem GetCurrentItem() override; 37 int32_t Start(const AVQueueItem& avQueueItem) override; 38 int32_t Prepare(const AVQueueItem& avQueueItem) override; 39 int32_t GetDuration(int32_t &duration) override; 40 int32_t GetCastAVPlaybackState(AVPlaybackState& avPlaybackState) override; 41 int32_t SetDisplaySurface(std::string &surfaceId) override; 42 int32_t RegisterControllerListener(const std::shared_ptr<IAVCastControllerProxyListener>) override; 43 int32_t UnRegisterControllerListener(const std::shared_ptr<IAVCastControllerProxyListener>) override; 44 int32_t SetValidAbility(const std::vector<int32_t>& validAbilityList) override; 45 int32_t GetValidAbility(const std::vector<int32_t>& validAbilityList) override; 46 47 void OnStateChanged(const CastEngine::PlayerStates playbackState, bool isPlayWhenReady) override; 48 void OnPositionChanged(int position, int bufferPosition, int duration) override; 49 void OnMediaItemChanged(const CastEngine::MediaInfo &mediaInfo) override; 50 void OnVolumeChanged(int volume, int maxVolume) override; 51 void OnLoopModeChanged(const CastEngine::LoopMode loopMode) override; 52 void OnNextRequest() override; 53 void OnPreviousRequest() override; 54 void OnPlaySpeedChanged(const CastEngine::PlaybackSpeed speed) override; 55 void OnPlayerError(int errorCode, const std::string &errorMsg) override; 56 void OnSeekDone(int32_t seekNumber) override; 57 void OnVideoSizeChanged(int width, int height) override; 58 void OnEndOfStream(int isLooping) override; 59 void OnPlayRequest(const CastEngine::MediaInfo &mediaInfo) override; 60 void OnImageChanged(std::shared_ptr<Media::PixelMap> pixelMap) override; 61 void OnAlbumCoverChanged(std::shared_ptr<Media::PixelMap> pixelMap) override; 62 void OnAvailableCapabilityChanged(const CastEngine::StreamCapability &mediaInfo) override; 63 64 void SendControlCommandWithParams(const AVCastControlCommand castControlCommand); 65 66 private: 67 int32_t CheckCastTime(int32_t castTime); 68 void checkCmdsFromAbility(const CastEngine::StreamCapability &streamCapability, 69 std::vector<int32_t> supportedCastCmds); 70 void checkAbilityFromCmds(std::vector<int32_t> supportedCastCmds, 71 const CastEngine::StreamCapability &streamCapability); 72 73 int32_t castMinTime = 1000; 74 std::recursive_mutex streamPlayerLock_; 75 std::shared_ptr<CastEngine::IStreamPlayer> streamPlayer_; 76 std::recursive_mutex streamPlayerListenerListLock_; 77 std::vector<std::shared_ptr<IAVCastControllerProxyListener>> streamPlayerListenerList_; 78 AVQueueItem currentAVQueueItem_; 79 std::map<CastEngine::PlayerStates, int32_t> castPlusStateToString_ = { 80 {CastEngine::PlayerStates::PLAYER_STATE_ERROR, AVPlaybackState::PLAYBACK_STATE_ERROR}, 81 {CastEngine::PlayerStates::PLAYER_IDLE, AVPlaybackState::PLAYBACK_STATE_INITIAL}, 82 {CastEngine::PlayerStates::PLAYER_INITIALIZED, AVPlaybackState::PLAYBACK_STATE_INITIAL}, 83 {CastEngine::PlayerStates::PLAYER_PREPARING, AVPlaybackState::PLAYBACK_STATE_PREPARE}, 84 {CastEngine::PlayerStates::PLAYER_PREPARED, AVPlaybackState::PLAYBACK_STATE_PREPARE}, 85 {CastEngine::PlayerStates::PLAYER_STARTED, AVPlaybackState::PLAYBACK_STATE_PLAY}, 86 {CastEngine::PlayerStates::PLAYER_PAUSED, AVPlaybackState::PLAYBACK_STATE_PAUSE}, 87 {CastEngine::PlayerStates::PLAYER_STOPPED, AVPlaybackState::PLAYBACK_STATE_STOP}, 88 {CastEngine::PlayerStates::PLAYER_PLAYBACK_COMPLETE, AVPlaybackState::PLAYBACK_STATE_COMPLETED}, 89 {CastEngine::PlayerStates::PLAYER_RELEASED, AVPlaybackState::PLAYBACK_STATE_RELEASED}, 90 {CastEngine::PlayerStates::PLAYER_BUFFERING, AVPlaybackState::PLAYBACK_STATE_BUFFERING} 91 }; 92 std::map<CastEngine::PlaybackSpeed, double> castPlusSpeedToDouble_ = { 93 {CastEngine::PlaybackSpeed::SPEED_FORWARD_0_75_X, 0.75}, 94 {CastEngine::PlaybackSpeed::SPEED_FORWARD_1_00_X, 1.00}, 95 {CastEngine::PlaybackSpeed::SPEED_FORWARD_1_25_X, 1.25}, 96 {CastEngine::PlaybackSpeed::SPEED_FORWARD_1_75_X, 1.75}, 97 {CastEngine::PlaybackSpeed::SPEED_FORWARD_2_00_X, 2.00}, 98 }; 99 std::map<CastEngine::LoopMode, int32_t> castPlusLoopModeToInt_ = { 100 {CastEngine::LoopMode::LOOP_MODE_SEQUENCE, AVPlaybackState::LOOP_MODE_SEQUENCE}, 101 {CastEngine::LoopMode::LOOP_MODE_SINGLE, AVPlaybackState::LOOP_MODE_SINGLE}, 102 {CastEngine::LoopMode::LOOP_MODE_LIST, AVPlaybackState::LOOP_MODE_LIST}, 103 {CastEngine::LoopMode::LOOP_MODE_SHUFFLE, AVPlaybackState::LOOP_MODE_SHUFFLE}, 104 }; 105 std::map<int32_t, CastEngine::LoopMode> intLoopModeToCastPlus_ = { 106 {AVPlaybackState::LOOP_MODE_SEQUENCE, CastEngine::LoopMode::LOOP_MODE_SEQUENCE}, 107 {AVPlaybackState::LOOP_MODE_SINGLE, CastEngine::LoopMode::LOOP_MODE_SINGLE}, 108 {AVPlaybackState::LOOP_MODE_LIST, CastEngine::LoopMode::LOOP_MODE_LIST}, 109 {AVPlaybackState::LOOP_MODE_SHUFFLE, CastEngine::LoopMode::LOOP_MODE_SHUFFLE}, 110 }; 111 }; 112 } // namespace OHOS::AVSession 113 114 #endif