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