• 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 OHOS_AVCAST_CONTROLLER_ITEM_H
17 #define OHOS_AVCAST_CONTROLLER_ITEM_H
18 
19 #include "i_avcast_controller_proxy.h"
20 #include "avcast_controller_callback_proxy.h"
21 #include "avcast_controller_stub.h"
22 #include "avcast_control_command.h"
23 #include "av_shared_memory_base.h"
24 
25 namespace OHOS::AVSession {
26 class AVCastControllerItem : public AVCastControllerStub, public IAVCastControllerProxyListener,
27     public std::enable_shared_from_this<IAVCastControllerProxyListener> {
28 public:
29     AVCastControllerItem();
30 
31     ~AVCastControllerItem();
32 
33     void Init(std::shared_ptr<IAVCastControllerProxy> castControllerProxy,
34         const std::function<void(int32_t, std::vector<int32_t>&)>& validCommandsChangecallback,
35         const std::function<void()>& preparecallback);
36 
37     void OnCastPlaybackStateChange(const AVPlaybackState& state) override;
38 
39     void OnMediaItemChange(const AVQueueItem& avQueueItem) override;
40 
41     void OnPlayNext() override;
42 
43     void OnPlayPrevious() override;
44 
45     void OnSeekDone(const int32_t seekNumber) override;
46 
47     void OnVideoSizeChange(const int32_t width, const int32_t height) override;
48 
49     void OnPlayerError(const int32_t errorCode, const std::string& errorMsg) override;
50 
51     void OnEndOfStream(const int32_t isLooping) override;
52 
53     void OnPlayRequest(const AVQueueItem& avQueueItem) override;
54 
55     void OnKeyRequest(const std::string& assetId, const std::vector<uint8_t>& keyRequestData) override;
56 
57     void OnValidCommandChange(const std::vector<int32_t> &cmds) override;
58 
59     int32_t onDataSrcRead(std::shared_ptr<AVSharedMemory> mem, uint32_t length, int64_t pos) override;
60 
61     int32_t SendControlCommand(const AVCastControlCommand& cmd) override;
62 
63     int32_t Start(const AVQueueItem& avQueueItem) override;
64 
65     int32_t Prepare(const AVQueueItem& avQueueItem) override;
66 
67     int32_t GetDuration(int32_t& duration) override;
68 
69     int32_t GetCastAVPlaybackState(AVPlaybackState& avPlaybackState) override;
70 
71     int32_t GetSupportedDecoders(std::vector<std::string>& decoderTypes) override;
72 
73     int32_t GetRecommendedResolutionLevel(std::string& decoderType, ResolutionLevel& resolutionLevel) override;
74 
75     int32_t GetSupportedHdrCapabilities(std::vector<HDRFormat>& hdrFormats) override;
76 
77     int32_t GetSupportedPlaySpeeds(std::vector<float>& playSpeeds) override;
78 
79     int32_t GetCurrentItem(AVQueueItem& currentItem) override;
80 
81     int32_t GetValidCommands(std::vector<int32_t>& cmds) override;
82 
83     int32_t SetDisplaySurface(std::string& surfaceId) override;
84 
85     int32_t SetCastPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter) override;
86 
87     int32_t ProcessMediaKeyResponse(const std::string& assetId, const std::vector<uint8_t>& response) override;
88 
89     bool RegisterControllerListener(std::shared_ptr<IAVCastControllerProxy> castControllerProxy);
90 
91     int32_t AddAvailableCommand(const int32_t cmd) override;
92 
93     int32_t RemoveAvailableCommand(const int32_t cmd) override;
94 
95     int32_t HandleCastValidCommandChange(const std::vector<int32_t> &cmds);
96 
97     void SetSessionTag(const std::string& tag);
98 
99     void SetSessionId(const std::string id);
100 
101     void SetUserId(const int32_t userId);
102 
103     int32_t Destroy() override;
104 
105     void SetSessionCallbackForCastCap(const std::function<void(std::string, bool, bool)>& callback);
106 
107     void SetQueueItemDataSrc(const AVQueueItem& avQueueItem);
108 
109 protected:
110     int32_t RegisterCallbackInner(const sptr<IRemoteObject>& callback) override;
111 
112 private:
113     bool IsStopState(int32_t playbackState);
114     void CheckIfCancelCastCapsule();
115     void ReportPrepare(int32_t preRet, const AVQueueItem& avQueueItem);
116 
117     std::shared_ptr<IAVCastControllerProxy> castControllerProxy_;
118     sptr<IAVCastControllerCallback> callback_;
119     AVPlaybackState::PlaybackStateMaskType castPlaybackMask_;
120     std::function<void(int32_t, std::vector<int32_t>&)> validCommandsChangecallback_;
121     std::function<void()> preparecallback_;
122     int32_t removeCmdStep_ = 1000;
123     int32_t currentState_ = AVPlaybackState::PLAYBACK_STATE_INITIAL;
124     std::string sessionTag_;
125     std::string sessionId_;
126     int32_t userId_ = 0;
127     bool isSessionCallbackAvailable_ = true;
128     std::recursive_mutex castControllerLock_;
129     std::recursive_mutex castControllerCallbackLock_;
130     std::mutex callbackToSessionLock_;
131     std::function<void(std::string&, bool, bool)> sessionCallbackForCastNtf_;
132     bool isPlayingState_ = false;
133     const int32_t cancelTimeout = 5000;
134 };
135 } // namespace OHOS::AVSession
136 #endif // OHOS_AVCAST_CONTROLLER_ITEM_H
137