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_UTILS_H
17 #define OHOS_CJ_AVSESSION_UTILS_H
18
19 #include "want_agent.h"
20 #include "avcall_state.h"
21 #include "avmeta_data.h"
22 #include "avcall_meta_data.h"
23 #include "avsession_pixel_map.h"
24 #include "avsession_descriptor.h"
25 #include "avqueue_item.h"
26 #include "avmedia_description.h"
27 #include "avplayback_state.h"
28 #include "avsession_errors.h"
29 #include "avsession_log.h"
30 #include "avcast_control_command.h"
31 #include "avcontrol_command.h"
32 #include "cj_avsession_prototypes.h"
33 #include "key_event.h"
34
35 namespace OHOS::AVSession {
36 constexpr int32_t CJNO_ERROR = 0;
37
38 constexpr int32_t STR_TYPE = 0;
39 constexpr int32_t I32_TYPE = 1;
40 constexpr int32_t I64_TYPE = 2;
41 constexpr int32_t BOOL_TYPE = 3;
42 constexpr int32_t FD_TYPE = 4;
43 constexpr int32_t STR_PTR_TYPE = 5;
44 constexpr int32_t I32_PTR_TYPE = 6;
45 constexpr int32_t I64_PTR_TYPE = 7;
46 constexpr int32_t BOOL_PTR_TYPE = 8;
47 constexpr int32_t DOUBLE_PTR_TYPE = 9;
48 constexpr int32_t FD_PTR_TYPE = 10;
49 constexpr int32_t DOUBLE_TYPE = 11;
50 constexpr int32_t PIXEL_MAP_TYPE = 13;
51 constexpr int32_t HASH_MAP_TYPE = 15;
52 constexpr int32_t NONE_VALUE = -1;
53
54 /* Native => Cangjie FFI Object*/
55 /*Primitive Type*/
56 int32_t ConvertNativeToCJStruct(const std::string& native, char*& cj);
57 int32_t ConvertNativeToCJStruct(const std::vector<std::string>& native, CArray& cj);
58 int32_t ConvertNativeToCJStruct(const int32_t& native, int32_t& cj);
59 int32_t ConvertNativeToCJStruct(const int64_t& native, int64_t& cj);
60 int32_t ConvertNativeToCJStruct(const bool& native, bool& cj);
61 int32_t ConvertNativeToCJStruct(const std::vector<int32_t>& native, CArray& cj);
62 int32_t ConvertNativeToCJStruct(const std::vector<uint8_t>& native, CArray& cj);
63 int32_t ConvertNativeToCJStruct(const AAFwk::WantParams& native, CArray& cj);
64
65 /* String | PixelMap*/
66 int32_t ConvertNativeToCJStruct(const std::shared_ptr<AVSessionPixelMap>& native, int64_t& cj);
67 int32_t ConvertNativeToCJStruct(const std::shared_ptr<AVSessionPixelMap>& native, StringPixelMapParameter& cj);
68 int32_t ConvertNativeToCJStruct(const std::string& native, StringPixelMapParameter& cj);
69
70 int32_t ConvertNativeToCJStruct(const AVCallState& native, CAVCallState& cj);
71 int32_t ConvertNativeToCJStruct(const AVCallMetaData& native, CAVCallMetaData& cj);
72 int32_t ConvertNativeToCJStruct(const AVPlaybackState::Position& native, CPlaybackPosition& cj);
73 int32_t ConvertNativeToCJStruct(const AVPlaybackState& native, CAVPlaybackState& cj);
74 int32_t ConvertNativeToCJStruct(const AVMetaData& native, CAVMetaData& cj);
75
76 int32_t ConvertNativeToCJStruct(const DeviceInfo& native, CDeviceInfo& cj);
77 int32_t ConvertNativeToCJStruct(const OutputDeviceInfo& native, COutputDeviceInfo& cj);
78
79 int32_t ConvertNativeToCJStruct(const AVQueueItem& native, CAVQueueItem& cj);
80 int32_t ConvertNativeToCJStruct(const std::vector<AVQueueItem>& native, CArray& cj);
81 int32_t ConvertNativeToCJStruct(const AVMediaDescription& native, CAVMediaDescription& cj);
82 int32_t ConvertNativeToCJStruct(const AVFileDescriptor& native, CAVFileDescriptor& cj);
83
84 int32_t ConvertNativeToCJStruct(const std::vector<CastDisplayInfo>& native, CArray& cj);
85 int32_t ConvertNativeToCJStruct(const CastDisplayInfo& native, CCastDisplayInfo& cj);
86
87 int32_t ConvertNativeToCJStruct(const MMI::KeyEvent::KeyItem& native, CKey& cj);
88 int32_t ConvertNativeToCJStruct(const std::vector<MMI::KeyEvent::KeyItem>& native, CKey*& cj);
89 int32_t ConvertNativeToCJStruct(const MMI::KeyEvent& native, CInputEvent& cj);
90 int32_t ConvertNativeToCJStruct(const MMI::KeyEvent& native, CKeyEvent& cj);
91
92 int32_t ConvertNativeToCJStruct(const AbilityRuntime::WantAgent::WantAgent& native, int64_t& cj);
93
94 /* Canjie FFI Object => Native*/
95 int32_t convertCJStructToNative(const int32_t& cj, int32_t& native);
96 int32_t convertCJStructToNative(const CKeyEvent& cj, MMI::KeyEvent& native);
97 int32_t convertCJStructToNative(const CKey& cj, MMI::KeyEvent::KeyItem& native);
98
99 int32_t convertCJStructToNative(const CArray& cj, AAFwk::WantParams& native);
100 int32_t convertCJStructToNative(const CAVMetaData& cj, AVMetaData &native);
101 int32_t convertCJStructToNative(const CArray& cj, std::vector<std::string>& native);
102 int32_t convertCJStructToNative(const CAVCallMetaData& cj, AVCallMetaData& native);
103 int32_t convertCJStructToNative(const CAVCallState& cj, AVCallState& native);
104 int32_t convertCJStructToNative(const CAVPlaybackState& cj, AVPlaybackState& native);
105 int32_t convertCJStructToNative(const CArray& cj, std::vector<AVQueueItem>& native);
106 int32_t convertCJStructToNative(const CAVQueueItem& cj, AVQueueItem& native);
107 int32_t convertCJStructToNative(const CAVMediaDescription& cj, AVMediaDescription& native);
108 int32_t convertCJStructToNative(const CAVSessionCommand& cj, AVControlCommand& native);
109 int32_t convertCJStructToNative(const CAVSessionCommand& cj, AVCastControlCommand& native);
110 int32_t convertCJStructToNative(const CArray& cj, std::vector<uint8_t>& native);
111 int32_t convertCJStructToNative(const int64_t& cj, std::shared_ptr<AbilityRuntime::WantAgent::WantAgent>& native);
112
113 /* Free cjObject */
114 void cjStructHeapFree(COutputDeviceInfo& cj);
115 void cjStructHeapFree(CArray& cj);
116 void cjStructHeapFree(CCastDisplayInfo& cj);
117 void cjStructHeapFree(CAVCallMetaData& cj);
118 void cjStructHeapFree(CAVPlaybackState& cj);
119 void cjStructHeapFree(CAVMetaData& cj);
120 void cjStructHeapFreeWant(CArray& cj);
121 void cjStructHeapFreeAVQueueItem(CArray& cj);
122
123
124 /* Common Methods */
125 int32_t CJExecMethod(std::function<int32_t()> method, std::string methodName);
126 void ParseParameters(const AAFwk::WantParams &wantP, CArray &cArray, int32_t &code);
127
128 template<class NT, class CJT>
CJControllerGetterCStruct(std::function<int32_t (NT &)> method,CJT & cj,std::string method_name)129 int32_t CJControllerGetterCStruct(
130 std::function<int32_t(NT&)> method, CJT& cj, std::string method_name)
131 {
132 NT native;
133 int32_t ret = method(native);
134 if (ret != AVSESSION_SUCCESS) {
135 SLOGE("%{public}s failed:%{public}d", method_name.c_str(), ret);
136 } else {
137 ret = ConvertNativeToCJStruct(native, cj);
138 if (ret != CJNO_ERROR) {
139 SLOGE("%{public}s failed:%{public}d", method_name.c_str(), ret);
140 }
141 }
142 return ret;
143 }
144
145 template<class NT, class CJT>
CJAVSessionSetterCStruct(std::function<int32_t (NT &)> method,CJT & cj,std::string method_name)146 int32_t CJAVSessionSetterCStruct(
147 std::function<int32_t(NT&)> method, CJT& cj, std::string method_name)
148 {
149 NT native;
150 int32_t ret = convertCJStructToNative(cj, native);
151 if (ret != AVSESSION_SUCCESS) {
152 SLOGE("%{public}s failed:%{public}d", method_name.c_str(), ret);
153 } else {
154 ret = method(native);
155 if (ret != CJNO_ERROR) {
156 SLOGE("%{public}s failed:%{public}d", method_name.c_str(), ret);
157 }
158 }
159 return ret;
160 }
161 } // namespace AVSession::OHOS
162
163 #endif // OHOS_CJ_AVSESSION_UTILS_H