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 "dcamera_sink_frame_info.h" 17 #include "nlohmann/json.hpp" 18 #include "distributed_camera_errno.h" 19 #include "distributed_hardware_log.h" 20 using json = nlohmann::json; 21 22 namespace OHOS { 23 namespace DistributedHardware { Marshal(std::string & jsonStr)24void DCameraSinkFrameInfo::Marshal(std::string& jsonStr) 25 { 26 json frameInfo; 27 frameInfo[FRAME_INFO_TYPE] = type_; 28 frameInfo[FRAME_INFO_INDEX] = index_; 29 frameInfo[FRAME_INFO_PTS] = pts_; 30 frameInfo[FRAME_INFO_START_ENCODE] = startEncodeT_; 31 frameInfo[FRAME_INFO_FINISH_ENCODE] = finishEncodeT_; 32 frameInfo[FRAME_INFO_SENDT] = sendT_; 33 frameInfo[FRAME_INFO_VERSION] = ver_; 34 jsonStr = frameInfo.dump(); 35 } 36 Unmarshal(const std::string & jsonStr)37int32_t DCameraSinkFrameInfo::Unmarshal(const std::string& jsonStr) 38 { 39 json frameInfo = json::parse(jsonStr, nullptr, false); 40 if (frameInfo.is_discarded()) { 41 DHLOGE("FrameInfo json::parse error."); 42 return DCAMERA_BAD_VALUE; 43 } 44 45 if (!frameInfo.contains(FRAME_INFO_TYPE) || !frameInfo[FRAME_INFO_TYPE].is_number_integer()) { 46 return DCAMERA_BAD_VALUE; 47 } 48 type_ = frameInfo[FRAME_INFO_TYPE].get<std::int8_t>(); 49 50 if (!frameInfo.contains(FRAME_INFO_INDEX) || !frameInfo[FRAME_INFO_INDEX].is_number_integer()) { 51 return DCAMERA_BAD_VALUE; 52 } 53 index_ = frameInfo[FRAME_INFO_INDEX].get<std::int32_t>(); 54 55 if (!frameInfo.contains(FRAME_INFO_PTS) || !frameInfo[FRAME_INFO_PTS].is_number_integer()) { 56 return DCAMERA_BAD_VALUE; 57 } 58 pts_ = frameInfo[FRAME_INFO_PTS].get<std::int64_t>(); 59 60 if (!frameInfo.contains(FRAME_INFO_START_ENCODE) || !frameInfo[FRAME_INFO_START_ENCODE].is_number_integer()) { 61 return DCAMERA_BAD_VALUE; 62 } 63 startEncodeT_ = frameInfo[FRAME_INFO_START_ENCODE].get<std::int64_t>(); 64 65 if (!frameInfo.contains(FRAME_INFO_FINISH_ENCODE) || !frameInfo[FRAME_INFO_FINISH_ENCODE].is_number_integer()) { 66 return DCAMERA_BAD_VALUE; 67 } 68 finishEncodeT_ = frameInfo[FRAME_INFO_FINISH_ENCODE].get<std::int64_t>(); 69 70 if (!frameInfo.contains(FRAME_INFO_SENDT) || !frameInfo[FRAME_INFO_SENDT].is_number_integer()) { 71 return DCAMERA_BAD_VALUE; 72 } 73 sendT_ = frameInfo[FRAME_INFO_SENDT].get<std::int64_t>(); 74 75 if (!frameInfo.contains(FRAME_INFO_VERSION) || !frameInfo[FRAME_INFO_VERSION].is_string()) { 76 return DCAMERA_BAD_VALUE; 77 } 78 ver_ = frameInfo[FRAME_INFO_VERSION].get<std::string>(); 79 return DCAMERA_OK; 80 } 81 } // namespace DistributedHardware 82 } // namespace OHOS