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 
24 namespace OHOS::AVSession {
25 class AVCastControllerItem : public AVCastControllerStub, public IAVCastControllerProxyListener,
26     public std::enable_shared_from_this<IAVCastControllerProxyListener> {
27 public:
28     AVCastControllerItem();
29 
30     ~AVCastControllerItem();
31 
32     void Init(std::shared_ptr<IAVCastControllerProxy> castControllerProxy,
33         const std::function<void(int32_t, std::vector<int32_t>&)>& validCommandsChangecallback,
34         const std::function<void()>& preparecallback);
35 
36     void OnCastPlaybackStateChange(const AVPlaybackState& state) override;
37 
38     void OnMediaItemChange(const AVQueueItem& avQueueItem) override;
39 
40     void OnPlayNext() override;
41 
42     void OnPlayPrevious() override;
43 
44     void OnSeekDone(const int32_t seekNumber) override;
45 
46     void OnVideoSizeChange(const int32_t width, const int32_t height) override;
47 
48     void OnPlayerError(const int32_t errorCode, const std::string& errorMsg) override;
49 
50     void OnEndOfStream(const int32_t isLooping) override;
51 
52     void OnPlayRequest(const AVQueueItem& avQueueItem) override;
53 
54     void OnKeyRequest(const std::string& assetId, const std::vector<uint8_t>& keyRequestData) override;
55 
56     void OnValidCommandChange(const std::vector<int32_t> &cmds) override;
57 
58     int32_t SendControlCommand(const AVCastControlCommand& cmd) override;
59 
60     int32_t Start(const AVQueueItem& avQueueItem) override;
61 
62     int32_t Prepare(const AVQueueItem& avQueueItem) override;
63 
64     int32_t GetDuration(int32_t& duration) override;
65 
66     int32_t GetCastAVPlaybackState(AVPlaybackState& avPlaybackState) override;
67 
68     int32_t GetCurrentItem(AVQueueItem& currentItem) override;
69 
70     int32_t GetValidCommands(std::vector<int32_t>& cmds) override;
71 
72     int32_t SetDisplaySurface(std::string& surfaceId) override;
73 
74     int32_t SetCastPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter) override;
75 
76     int32_t ProcessMediaKeyResponse(const std::string& assetId, const std::vector<uint8_t>& response) override;
77 
78     bool RegisterControllerListener(std::shared_ptr<IAVCastControllerProxy> castControllerProxy);
79 
80     int32_t AddAvailableCommand(const int32_t cmd) override;
81 
82     int32_t RemoveAvailableCommand(const int32_t cmd) override;
83 
84     int32_t HandleCastValidCommandChange(const std::vector<int32_t> &cmds);
85 
86     void SetSessionTag(const std::string& tag);
87 
88     int32_t Destroy() override;
89 
90 protected:
91     int32_t RegisterCallbackInner(const sptr<IRemoteObject>& callback) override;
92 
93 private:
94     std::shared_ptr<IAVCastControllerProxy> castControllerProxy_;
95     sptr<IAVCastControllerCallback> callback_;
96     std::recursive_mutex itemCallbackLock_;
97     AVPlaybackState::PlaybackStateMaskType castPlaybackMask_;
98     AVQueueItem currentAVQueueItem_;
99     std::vector<int32_t> supportedCastCmds_;
100     std::function<void(int32_t, std::vector<int32_t>&)> validCommandsChangecallback_;
101     std::function<void()> preparecallback_;
102     int32_t removeCmdStep_ = 1000;
103     int32_t currentState_ = AVPlaybackState::PLAYBACK_STATE_INITIAL;
104     std::string sessionTag_;
105     bool isSessionCallbackAvailable_ = true;
106     std::mutex callbackToSessionLock_;
107 };
108 } // namespace OHOS::AVSession
109 #endif // OHOS_AVCAST_CONTROLLER_ITEM_H
110