• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_CJ_AVSESSION_CAST_CONTROLLER_IMPL_H
17 #define OHOS_CJ_AVSESSION_CAST_CONTROLLER_IMPL_H
18 
19 #include <functional>
20 #include <map>
21 #include <optional>
22 #include <string>
23 #include <mutex>
24 #include <memory>
25 
26 #include "singleton.h"
27 #include "avcast_controller.h"
28 #include "avsession_errors.h"
29 #include "cj_avsession_cast_controller_ffi.h"
30 #include "cj_avsession_cast_controller_callback.h"
31 
32 namespace OHOS::AVSession {
33 
34 class CJAVCastControllerBase {
35 public:
~CJAVCastControllerBase()36     virtual ~CJAVCastControllerBase() {}
37     virtual int32_t SubscribeCallback(int32_t type, CParameters* extras, DoCallFunc doCall) = 0;
38     virtual int32_t Start(CAVQueueItem& item) = 0;
39     virtual int32_t Prepare(CAVQueueItem& item) = 0;
40     virtual int32_t SendControlCommand(CAVSessionCommand& command) = 0;
41     virtual int32_t GetAVPlaybackState(CAVPlaybackState& avPlaybackState) = 0;
42     virtual int32_t GetCurrentItem(CAVQueueItem& item) = 0;
43     virtual int32_t GetValidCommands(CArray& commands) = 0;
44     virtual int32_t Release() = 0;
45     virtual int32_t ProcessMediaKeyResponse(char*& assetId, CArray& response) = 0;
46     virtual bool Exists() = 0;
47 };
48 
49 class CJAVCastControllerImpl : public CJAVCastControllerBase {
50     friend class CJAVSessionImpl;
51 protected:
52     CJAVCastControllerImpl(std::shared_ptr<AVCastController>& nativeController, const std::string& sessionId);
53     static std::shared_ptr<CJAVCastControllerImpl>
54         NewInstance(std::shared_ptr<AVCastController>& nativeController, const std::string& sessionId);
55     int32_t SetCastPlaybackStateFilter(CParameters* extras);
56 
57 public:
58     ~CJAVCastControllerImpl();
59     static std::shared_ptr<CJAVCastControllerBase> GetInstance(const std::string &sessionId);
60     int32_t SubscribeCallback(int32_t type, CParameters* extras, DoCallFunc doCall);
61     int32_t Start(CAVQueueItem& item);
62     int32_t Prepare(CAVQueueItem& item);
63     int32_t SendControlCommand(CAVSessionCommand& command);
64     int32_t GetAVPlaybackState(CAVPlaybackState& avPlaybackState);
65     int32_t GetCurrentItem(CAVQueueItem& item);
66     int32_t GetValidCommands(CArray& commands);
67     int32_t Release();
68     int32_t ProcessMediaKeyResponse(char*& assetId, CArray& response);
Exists()69     bool Exists() { return true; }
70 
71 private:
72     std::string sessionId_;
73     std::shared_ptr<AVCastController> controller_;
74     static std::mutex controllerListMutex_;
75 
76     static std::mutex callbackMutex_;
77     std::shared_ptr<CJAVCastControllerCallback> callback_;
78 
79     static std::map<std::string, std::shared_ptr<CJAVCastControllerImpl>> ControllerList_;
80 };
81 
82 class CJAVCastControllerInvalidImpl : public CJAVCastControllerBase {
83     DECLARE_DELAYED_SINGLETON(CJAVCastControllerInvalidImpl);
84 public:
85     DISALLOW_COPY_AND_MOVE(CJAVCastControllerInvalidImpl);
SubscribeCallback(int32_t type,CParameters * extras,DoCallFunc doCall)86     int32_t SubscribeCallback(int32_t type, CParameters* extras, DoCallFunc doCall) {return ERR_CONTROLLER_NOT_EXIST; }
Start(CAVQueueItem & item)87     int32_t Start(CAVQueueItem& item) {return ERR_CONTROLLER_NOT_EXIST; }
Prepare(CAVQueueItem & item)88     int32_t Prepare(CAVQueueItem& item) {return ERR_CONTROLLER_NOT_EXIST; }
SendControlCommand(CAVSessionCommand & command)89     int32_t SendControlCommand(CAVSessionCommand& command) {return ERR_CONTROLLER_NOT_EXIST; }
GetAVPlaybackState(CAVPlaybackState & avPlaybackState)90     int32_t GetAVPlaybackState(CAVPlaybackState& avPlaybackState) {return ERR_CONTROLLER_NOT_EXIST; }
GetCurrentItem(CAVQueueItem & item)91     int32_t GetCurrentItem(CAVQueueItem& item) {return ERR_CONTROLLER_NOT_EXIST; }
GetValidCommands(CArray & commands)92     int32_t GetValidCommands(CArray& commands) {return ERR_CONTROLLER_NOT_EXIST; }
Release()93     int32_t Release() {return ERR_CONTROLLER_NOT_EXIST; }
ProcessMediaKeyResponse(char * & assetId,CArray & response)94     int32_t ProcessMediaKeyResponse(char*& assetId, CArray& response) {return ERR_CONTROLLER_NOT_EXIST; }
Exists()95     bool Exists() { return false; }
96 };
97 
98 #define CJ_CAST_CONTROLLER_INVALID_IMPL OHOS::DelayedSingleton<CJAVCastControllerInvalidImpl>::GetInstance()
99 
100 } // namespace AVSession::OHOS
101 
102 #endif // OHOS_CJ_AVSESSION_CAST_CONTROLLER_IMPL_H