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 #include <nlohmann/json.hpp> 21 22 #include "pixel_map.h" 23 #include "cast_engine_common.h" 24 #include "i_stream_player.h" 25 #include "i_avcast_controller_proxy.h" 26 #include "avsession_pixel_map_adapter.h" 27 #include "hw_cast_data_source_descriptor.h" 28 29 namespace OHOS::AVSession { 30 struct JsonCapabilities { 31 std::vector<std::string> decoderTypes_; 32 std::vector<HDRFormat> hdrFormats_; 33 std::vector<float> playSpeeds_; 34 std::vector<std::map<std::string, ResolutionLevel>> decoderSupportResolutions_; 35 }; 36 37 class HwCastStreamPlayer : public IAVCastControllerProxy, public CastEngine::IStreamPlayerListener, 38 public std::enable_shared_from_this<HwCastStreamPlayer> { 39 public: HwCastStreamPlayer(std::shared_ptr<CastEngine::IStreamPlayer> streamPlayer)40 explicit HwCastStreamPlayer(std::shared_ptr<CastEngine::IStreamPlayer> streamPlayer) 41 : streamPlayer_(streamPlayer) {} 42 ~HwCastStreamPlayer() override; 43 int32_t Init(); 44 void Release() override; 45 void SendControlCommand(const AVCastControlCommand castControlCommand) override; 46 AVQueueItem GetCurrentItem() override; 47 int32_t Start(const AVQueueItem& avQueueItem) override; 48 int32_t Prepare(const AVQueueItem& avQueueItem) override; 49 int32_t GetDuration(int32_t &duration) override; 50 int32_t GetCastAVPlaybackState(AVPlaybackState& avPlaybackState) override; 51 int32_t GetSupportedDecoders(std::vector<std::string>& decoderTypes) override; 52 int32_t GetRecommendedResolutionLevel(std::string& decoderType, ResolutionLevel& resolutionLevel) override; 53 int32_t GetSupportedHdrCapabilities(std::vector<HDRFormat>& hdrFormats) override; 54 int32_t GetSupportedPlaySpeeds(std::vector<float>& playSpeeds) override; 55 int32_t SetDisplaySurface(std::string &surfaceId) override; 56 int32_t ProcessMediaKeyResponse(const std::string& assetId, const std::vector<uint8_t>& response) override; 57 int32_t RegisterControllerListener(const std::shared_ptr<IAVCastControllerProxyListener>) override; 58 int32_t UnRegisterControllerListener(const std::shared_ptr<IAVCastControllerProxyListener>) override; 59 int32_t SetValidAbility(const std::vector<int32_t>& validCmdList) override; 60 int32_t GetValidAbility(std::vector<int32_t>& validCmdList) override; 61 62 void OnStateChanged(const CastEngine::PlayerStates playbackState, bool isPlayWhenReady) override; 63 void OnPositionChanged(int position, int bufferPosition, int duration) override; 64 void OnMediaItemChanged(const CastEngine::MediaInfo &mediaInfo) override; 65 void OnVolumeChanged(int volume, int maxVolume) override; 66 void OnLoopModeChanged(const CastEngine::LoopMode loopMode) override; 67 void OnNextRequest() override; 68 void OnPreviousRequest() override; 69 void OnPlaySpeedChanged(const CastEngine::PlaybackSpeed speed) override; 70 void OnPlayerError(int errorCode, const std::string &errorMsg) override; 71 void OnSeekDone(int32_t seekNumber) override; 72 void OnVideoSizeChanged(int width, int height) override; 73 void OnEndOfStream(int isLooping) override; 74 void OnPlayRequest(const CastEngine::MediaInfo &mediaInfo) override; 75 void OnImageChanged(std::shared_ptr<Media::PixelMap> pixelMap) override; 76 void OnAlbumCoverChanged(std::shared_ptr<Media::PixelMap> pixelMap) override; 77 void OnAvailableCapabilityChanged(const CastEngine::StreamCapability &streamCapability) override; 78 void OnKeyRequest(const std::string& assetId, const std::vector<uint8_t>& keyRequestData) override; 79 80 void SendControlCommandWithParams(const AVCastControlCommand castControlCommand); 81 82 private: 83 int32_t CheckCastTime(int32_t castTime); 84 void checkCmdsFromAbility(const CastEngine::StreamCapability &streamCapability, 85 std::vector<int32_t> &supportedCastCmds); 86 void checkAbilityFromCmds( 87 const std::vector<int32_t>& supportedCastCmds, CastEngine::StreamCapability& streamCapability); 88 int32_t RefreshCurrentAVQueueItem(const AVQueueItem& avQueueItem) override; 89 bool RepeatPrepare(std::shared_ptr<AVMediaDescription>& mediaDescription); 90 int32_t GetMediaCapabilities(); 91 void GetMediaCapabilitiesOfVideo(nlohmann::json& videoValue); 92 void GetMediaCapabilitiesOfAudio(nlohmann::json& audioValue); 93 94 std::shared_ptr<JsonCapabilities> jsonCapabilitiesSptr_ = std::make_shared<JsonCapabilities>(); 95 const std::string videoStr_ = "video"; 96 const std::string audioStr_ = "audio"; 97 const std::string decodeTypeStr_ = "decodeType"; 98 const std::string hdrFormatStr_ = "HDRFormat"; 99 const std::string decodeSupportResolutionStr_ = "decodeSupportResolution"; 100 const std::string decodeOfVideoHevcStr_ = "video/hevc"; 101 const std::string decodeOfVideoAvcStr_ = "video/avc"; 102 const std::string decodeOfAudioStr_ = "audio/av3a"; 103 const std::string speedStr_ = "speed"; 104 int32_t castMinTime = 1000; 105 std::recursive_mutex streamPlayerLock_; 106 std::recursive_mutex curItemLock_; 107 std::shared_ptr<CastEngine::IStreamPlayer> streamPlayer_; 108 std::recursive_mutex streamPlayerListenerLock_; 109 std::recursive_mutex streamPlayerListenerListLock_; 110 std::vector<std::shared_ptr<IAVCastControllerProxyListener>> streamPlayerListenerList_; 111 AVQueueItem currentAVQueueItem_; 112 std::shared_ptr<HwCastDataSourceDescriptor> castDataSrc_ = nullptr; 113 std::map<CastEngine::PlayerStates, int32_t> castPlusStateToString_ = { 114 {CastEngine::PlayerStates::PLAYER_STATE_ERROR, AVPlaybackState::PLAYBACK_STATE_ERROR}, 115 {CastEngine::PlayerStates::PLAYER_IDLE, AVPlaybackState::PLAYBACK_STATE_INITIAL}, 116 {CastEngine::PlayerStates::PLAYER_INITIALIZED, AVPlaybackState::PLAYBACK_STATE_INITIAL}, 117 {CastEngine::PlayerStates::PLAYER_PREPARING, AVPlaybackState::PLAYBACK_STATE_PREPARE}, 118 {CastEngine::PlayerStates::PLAYER_PREPARED, AVPlaybackState::PLAYBACK_STATE_PREPARE}, 119 {CastEngine::PlayerStates::PLAYER_STARTED, AVPlaybackState::PLAYBACK_STATE_PLAY}, 120 {CastEngine::PlayerStates::PLAYER_PAUSED, AVPlaybackState::PLAYBACK_STATE_PAUSE}, 121 {CastEngine::PlayerStates::PLAYER_STOPPED, AVPlaybackState::PLAYBACK_STATE_STOP}, 122 {CastEngine::PlayerStates::PLAYER_PLAYBACK_COMPLETE, AVPlaybackState::PLAYBACK_STATE_COMPLETED}, 123 {CastEngine::PlayerStates::PLAYER_RELEASED, AVPlaybackState::PLAYBACK_STATE_RELEASED}, 124 {CastEngine::PlayerStates::PLAYER_BUFFERING, AVPlaybackState::PLAYBACK_STATE_BUFFERING} 125 }; 126 std::map<CastEngine::PlaybackSpeed, double> castPlusSpeedToDouble_ = { 127 {CastEngine::PlaybackSpeed::SPEED_FORWARD_0_75_X, 0.75}, 128 {CastEngine::PlaybackSpeed::SPEED_FORWARD_1_00_X, 1.00}, 129 {CastEngine::PlaybackSpeed::SPEED_FORWARD_1_25_X, 1.25}, 130 {CastEngine::PlaybackSpeed::SPEED_FORWARD_1_75_X, 1.75}, 131 {CastEngine::PlaybackSpeed::SPEED_FORWARD_2_00_X, 2.00}, 132 {CastEngine::PlaybackSpeed::SPEED_FORWARD_0_50_X, 0.50}, 133 {CastEngine::PlaybackSpeed::SPEED_FORWARD_1_50_X, 1.50} 134 }; 135 std::map<CastEngine::LoopMode, int32_t> castPlusLoopModeToInt_ = { 136 {CastEngine::LoopMode::LOOP_MODE_SEQUENCE, AVPlaybackState::LOOP_MODE_SEQUENCE}, 137 {CastEngine::LoopMode::LOOP_MODE_SINGLE, AVPlaybackState::LOOP_MODE_SINGLE}, 138 {CastEngine::LoopMode::LOOP_MODE_LIST, AVPlaybackState::LOOP_MODE_LIST}, 139 {CastEngine::LoopMode::LOOP_MODE_SHUFFLE, AVPlaybackState::LOOP_MODE_SHUFFLE}, 140 }; 141 std::map<int32_t, CastEngine::LoopMode> intLoopModeToCastPlus_ = { 142 {AVPlaybackState::LOOP_MODE_SEQUENCE, CastEngine::LoopMode::LOOP_MODE_SEQUENCE}, 143 {AVPlaybackState::LOOP_MODE_SINGLE, CastEngine::LoopMode::LOOP_MODE_SINGLE}, 144 {AVPlaybackState::LOOP_MODE_LIST, CastEngine::LoopMode::LOOP_MODE_LIST}, 145 {AVPlaybackState::LOOP_MODE_SHUFFLE, CastEngine::LoopMode::LOOP_MODE_SHUFFLE}, 146 }; 147 }; 148 } // namespace OHOS::AVSession 149 150 #endif