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