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 #include "av_trans_meta.h"
17
18 #include "av_trans_utils.h"
19 #include "cJSON.h"
20
21 namespace OHOS {
22 namespace DistributedHardware {
23 const std::string META_DATA_TYPE = "meta_data_type";
24 const std::string META_TIMESTAMP = "meta_timestamp";
25 const std::string META_FRAME_NUMBER = "meta_frame_number";
26 const std::string META_EXT_TIMESTAMP = "meta_ext_timestamp";
27 const std::string META_EXT_FRAME_NUMBER = "meta_ext_frame_number";
28
Clone()29 std::shared_ptr<OHOS::Media::Plugin::BufferMeta> AVTransAudioBufferMeta::Clone()
30 {
31 auto bufferMeta = std::make_shared<AVTransAudioBufferMeta>();
32 bufferMeta->pts_ = pts_;
33 bufferMeta->cts_ = cts_;
34 bufferMeta->format_ = format_;
35 bufferMeta->dataType_ = dataType_;
36 bufferMeta->frameNum_ = frameNum_;
37 bufferMeta->channels_ = channels_;
38 bufferMeta->sampleRate_ = sampleRate_;
39 bufferMeta->Update(*this);
40 return bufferMeta;
41 }
42
MarshalAudioMeta()43 std::string AVTransAudioBufferMeta::MarshalAudioMeta()
44 {
45 cJSON *metaJson = cJSON_CreateObject();
46 if (metaJson == nullptr) {
47 return "";
48 }
49 cJSON_AddNumberToObject(metaJson, META_DATA_TYPE.c_str(), static_cast<uint32_t>(dataType_));
50 cJSON_AddNumberToObject(metaJson, META_TIMESTAMP.c_str(), pts_);
51 cJSON_AddNumberToObject(metaJson, META_FRAME_NUMBER.c_str(), frameNum_);
52 char *data = cJSON_PrintUnformatted(metaJson);
53 if (data == nullptr) {
54 cJSON_Delete(metaJson);
55 return "";
56 }
57 std::string jsonstr(data);
58 cJSON_free(data);
59 cJSON_Delete(metaJson);
60 return jsonstr;
61 }
62
UnmarshalAudioMeta(const std::string & jsonStr)63 bool AVTransAudioBufferMeta::UnmarshalAudioMeta(const std::string& jsonStr)
64 {
65 cJSON *metaJson = cJSON_Parse(jsonStr.c_str());
66 if (metaJson == nullptr) {
67 return false;
68 }
69 if (!IsUInt32(metaJson, META_DATA_TYPE) || !IsInt64(metaJson, META_TIMESTAMP) ||
70 !IsUInt32(metaJson, META_FRAME_NUMBER)) {
71 cJSON_Delete(metaJson);
72 return false;
73 }
74 cJSON *typeObj = cJSON_GetObjectItemCaseSensitive(metaJson, META_DATA_TYPE.c_str());
75 if (typeObj == nullptr || !cJSON_IsNumber(typeObj)) {
76 cJSON_Delete(metaJson);
77 return false;
78 }
79 cJSON *ptsObj = cJSON_GetObjectItemCaseSensitive(metaJson, META_TIMESTAMP.c_str());
80 if (ptsObj == nullptr || !cJSON_IsNumber(ptsObj)) {
81 cJSON_Delete(metaJson);
82 return false;
83 }
84 cJSON *frameObj = cJSON_GetObjectItemCaseSensitive(metaJson, META_FRAME_NUMBER.c_str());
85 if (frameObj == nullptr || !cJSON_IsNumber(frameObj)) {
86 cJSON_Delete(metaJson);
87 return false;
88 }
89 dataType_ = static_cast<BufferDataType>(typeObj->valueint);
90 pts_ = static_cast<int64_t>(ptsObj->valueint);
91 frameNum_ = static_cast<uint32_t>(frameObj->valueint);
92 cJSON_Delete(metaJson);
93 return true;
94 }
95
Clone()96 std::shared_ptr<OHOS::Media::Plugin::BufferMeta> AVTransVideoBufferMeta::Clone()
97 {
98 auto bufferMeta = std::make_shared<AVTransVideoBufferMeta>();
99 bufferMeta->pts_ = pts_;
100 bufferMeta->cts_ = cts_;
101 bufferMeta->width_ = width_;
102 bufferMeta->height_ = height_;
103 bufferMeta->format_ = format_;
104 bufferMeta->dataType_ = dataType_;
105 bufferMeta->frameNum_ = frameNum_;
106 bufferMeta->extPts_ = extPts_;
107 bufferMeta->extFrameNum_ = extFrameNum_;
108 bufferMeta->Update(*this);
109 return bufferMeta;
110 }
111
MarshalVideoMeta()112 std::string AVTransVideoBufferMeta::MarshalVideoMeta()
113 {
114 cJSON *metaJson = cJSON_CreateObject();
115 if (metaJson == nullptr) {
116 return "";
117 }
118 cJSON_AddNumberToObject(metaJson, META_DATA_TYPE.c_str(), static_cast<uint32_t>(dataType_));
119 cJSON_AddNumberToObject(metaJson, META_TIMESTAMP.c_str(), pts_);
120 cJSON_AddNumberToObject(metaJson, META_FRAME_NUMBER.c_str(), frameNum_);
121 if (extPts_ > 0) {
122 cJSON_AddNumberToObject(metaJson, META_EXT_TIMESTAMP.c_str(), extPts_);
123 }
124 if (extFrameNum_ > 0) {
125 cJSON_AddNumberToObject(metaJson, META_EXT_FRAME_NUMBER.c_str(), extFrameNum_);
126 }
127 char *data = cJSON_PrintUnformatted(metaJson);
128 if (data == nullptr) {
129 cJSON_Delete(metaJson);
130 return "";
131 }
132 std::string jsonstr(data);
133 cJSON_Delete(metaJson);
134 cJSON_free(data);
135 return jsonstr;
136 }
137
UnmarshalVideoMeta(const std::string & jsonStr)138 bool AVTransVideoBufferMeta::UnmarshalVideoMeta(const std::string& jsonStr)
139 {
140 cJSON *metaJson = cJSON_Parse(jsonStr.c_str());
141 if (metaJson == nullptr) {
142 return false;
143 }
144
145 cJSON *typeObj = cJSON_GetObjectItemCaseSensitive(metaJson, META_DATA_TYPE.c_str());
146 if (typeObj != nullptr && IsUInt32(metaJson, META_DATA_TYPE)) {
147 dataType_ = static_cast<BufferDataType>(typeObj->valueint);
148 }
149
150 cJSON *timeStampObj = cJSON_GetObjectItemCaseSensitive(metaJson, META_TIMESTAMP.c_str());
151 if (timeStampObj != nullptr && IsInt64(metaJson, META_TIMESTAMP)) {
152 pts_ = static_cast<int64_t>(timeStampObj->valueint);
153 }
154
155 cJSON *numberObj = cJSON_GetObjectItemCaseSensitive(metaJson, META_FRAME_NUMBER.c_str());
156 if (numberObj != nullptr && IsUInt32(metaJson, META_FRAME_NUMBER)) {
157 frameNum_ = static_cast<uint32_t>(numberObj->valueint);
158 }
159
160 cJSON *extTimeStampObj = cJSON_GetObjectItemCaseSensitive(metaJson, META_EXT_TIMESTAMP.c_str());
161 if (extTimeStampObj != nullptr && IsInt64(metaJson, META_EXT_TIMESTAMP)) {
162 extPts_ = static_cast<int64_t>(extTimeStampObj->valueint);
163 }
164
165 cJSON *extNumberObj = cJSON_GetObjectItemCaseSensitive(metaJson, META_EXT_FRAME_NUMBER.c_str());
166 if (extNumberObj != nullptr && IsUInt32(metaJson, META_EXT_FRAME_NUMBER)) {
167 extFrameNum_ = static_cast<uint32_t>(extNumberObj->valueint);
168 }
169 cJSON_Delete(metaJson);
170 return true;
171 }
172 } // namespace DistributedHardware
173 } // namespace OHOS