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