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