From a6d02fb30f7280767ff245f60e21312a79710191 Mon Sep 17 00:00:00 2001 From: w30042960 Date: Tue, 5 Mar 2024 20:03:16 +0800 Subject: [PATCH] modify cjson Signed-off-by: w30042960 --- .../services/src/av_sync_manager.cpp | 84 ++++++++++--- .../dsoftbus_input/dsoftbus_input_plugin.cpp | 24 +++- .../dsoftbus_input/dsoftbus_input_plugin.h | 2 +- .../dsoftbus_input_audio_plugin.cpp | 24 +++- .../dsoftbus_input_audio_plugin.h | 2 +- .../dsoftbus_output_plugin.cpp | 20 ++- .../dsoftbus_output_audio_plugin.cpp | 20 ++- av_transport/common/include/av_trans_utils.h | 6 +- av_transport/common/src/av_sync_utils.cpp | 69 +++++++--- av_transport/common/src/av_trans_message.cpp | 33 +++-- av_transport/common/src/av_trans_meta.cpp | 97 +++++++++++--- av_transport/common/src/av_trans_utils.cpp | 118 +++++++++++++++--- 12 files changed, 395 insertions(+), 104 deletions(-) diff --git a/av_transport/av_trans_control_center/services/src/av_sync_manager.cpp b/av_transport/av_trans_control_center/services/src/av_sync_manager.cpp index 4a61c407..18233191 100644 --- a/av_transport/av_trans_control_center/services/src/av_sync_manager.cpp +++ b/av_transport/av_trans_control_center/services/src/av_sync_manager.cpp @@ -16,7 +16,8 @@ #include "av_sync_manager.h" #include -#include "nlohmann/json.hpp" +// #include "nlohmann/json.hpp" +#include "cJSON.h" #include "av_trans_control_center.h" #include "av_trans_log.h" @@ -170,26 +171,79 @@ bool AVSyncManager::MergeGroupInfo(std::string &syncGroupInfo) std::set groupInfoSet; for (const auto &item : streamInfoList_) { if ((item.sceneType == SCENE_TYPE_D_MIC) || (item.sceneType == SCENE_TYPE_D_SPEAKER)) { - nlohmann::json masterStr; - masterStr[KEY_SCENE_TYPE] = item.sceneType; - masterStr[KEY_PEER_DEV_ID] = item.peerDevId; - masterStr[KEY_START_FRAME_NUM] = 0; - masterStr[KEY_AV_SYNC_FLAG] = AvSyncFlag::MASTER; - groupInfoSet.insert(masterStr.dump()); + // nlohmann::json masterStr; + // masterStr[KEY_SCENE_TYPE] = item.sceneType; + // masterStr[KEY_PEER_DEV_ID] = item.peerDevId; + // masterStr[KEY_START_FRAME_NUM] = 0; + // masterStr[KEY_AV_SYNC_FLAG] = AvSyncFlag::MASTER; + // groupInfoSet.insert(masterStr.dump()); + cJSON *masterStr = cJSON_CreateObject(); + if (masterStr == nullptr) { + retrun false; + } + cJSON_AddStringToObject(masterStr, KEY_SCENE_TYPE.c_str(), item.sceneType.c_str()); + cJSON_AddStringToObject(masterStr, KEY_PEER_DEV_ID.c_str(), item.peerDevId.c_str()); + cJSON_AddNumberToObject(masterStr, KEY_START_FRAME_NUM.c_str(), 0); + cJSON_AddNumberToObject(masterStr, KEY_AV_SYNC_FLAG.c_str(), AvSyncFlag::MASTER); + char *jsonstr = cJSON_Print(masterStr); + if (jsonstr == nullptr) { + cJSON_Delete(masterStr); + return false; + } + groupInfoSet.insert(std::string(jsonstr)); + cJSON_Delete(masterStr); + cJSON_free(jsonstr); } else if ((item.sceneType == SCENE_TYPE_D_SCREEN) || (item.sceneType == SCENE_TYPE_D_CAMERA_STR)) { - nlohmann::json slaveStr; - slaveStr[KEY_SCENE_TYPE] = item.sceneType; - slaveStr[KEY_PEER_DEV_ID] = item.peerDevId; - slaveStr[KEY_START_FRAME_NUM] = 0; - slaveStr[KEY_AV_SYNC_FLAG] = AvSyncFlag::SLAVE; - groupInfoSet.insert(slaveStr.dump()); + // nlohmann::json slaveStr; + // slaveStr[KEY_SCENE_TYPE] = item.sceneType; + // slaveStr[KEY_PEER_DEV_ID] = item.peerDevId; + // slaveStr[KEY_START_FRAME_NUM] = 0; + // slaveStr[KEY_AV_SYNC_FLAG] = AvSyncFlag::SLAVE; + // groupInfoSet.insert(slaveStr.dump()); + cJSON *slaveStr = cJSON_CreateObject(); + if (slaveStr == nullptr) { + retrun false; + } + cJSON_AddStringToObject(slaveStr, KEY_SCENE_TYPE.c_str(), item.sceneType.c_str()); + cJSON_AddStringToObject(slaveStr, KEY_PEER_DEV_ID.c_str(), item.peerDevId.c_str()); + cJSON_AddNumberToObject(slaveStr, KEY_START_FRAME_NUM.c_str(), 0); + cJSON_AddNumberToObject(slaveStr, KEY_AV_SYNC_FLAG.c_str(), AvSyncFlag::SLAVE); + char *jsonstr = cJSON_Print(slaveStr); + if (jsonstr == nullptr) { + cJSON_Delete(slaveStr); + return false; + } + groupInfoSet.insert(std::string(jsonstr)); + cJSON_Delete(slaveStr); + cJSON_free(jsonstr); } else { continue; } } - nlohmann::json jsonStr = { { KEY_MY_DEV_ID, "" }, { KEY_GROUP_INFO_ARRAY, groupInfoSet }, }; - syncGroupInfo = jsonStr.dump(); + // nlohmann::json jsonStr = { { KEY_MY_DEV_ID, "" }, { KEY_GROUP_INFO_ARRAY, groupInfoSet }, }; + cJSON *jsonStr = cJSON_CreateObject(); + if (jsonStr == nullptr) { + retrun false; + } + cJSON_AddStringToObject(jsonStr, KEY_MY_DEV_ID.c_str(), ""); + cJSON *array = cJSON_CreateArray(); + if (array == nullptr) { + retrun false; + } + for (auto &info : groupInfoSet) { + cJSON_AddItemToArray(array, cJSON_CreateString(info.c_str())); + } + cJSON_AddItemToObject(jsonStr, KEY_GROUP_INFO_ARRAY.c_str(), array); + char *jsonstr = cJSON_Print(jsonStr); + if (jsonstr == nullptr) { + cJSON_Delete(jsonStr); + return false; + } + cJSON_Delete(jsonStr); + cJSON_free(jsonstr); + syncGroupInfo = jsonstr; + //syncGroupInfo = jsonStr.dump(); return true; } } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp index 61785037..3ec5fd68 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.cpp @@ -246,11 +246,20 @@ void DsoftbusInputPlugin::OnStreamReceived(const StreamData *data, const StreamD std::string message(reinterpret_cast(ext->buf), ext->bufLen); AVTRANS_LOGI("Receive message : %s", message.c_str()); - json resMsg = json::parse(message, nullptr, false); + // json resMsg = json::parse(message, nullptr, false); + cJSON *resMsg = cJSON_Parse(message.c_str()); + if (resMsg == nullptr) { + return; + } TRUE_RETURN(resMsg.is_discarded(), "The resMsg parse failed"); TRUE_RETURN(!IsUInt32(resMsg, AVT_DATA_META_TYPE), "invalid data type"); - uint32_t metaType = resMsg[AVT_DATA_META_TYPE]; - + //uint32_t metaType = resMsg[AVT_DATA_META_TYPE]; + cJSON *typeItem = cJSON_GetObjectItem(resMsg, AVT_DATA_META_TYPE); + if (typeItem == NULL) { + cJSON_Delete(resMsg); + return; + } + uint32_t metaType = typeItem->valueInt; auto buffer = CreateBuffer(metaType, data, resMsg); if (buffer != nullptr) { DataEnqueue(buffer); @@ -258,7 +267,7 @@ void DsoftbusInputPlugin::OnStreamReceived(const StreamData *data, const StreamD } std::shared_ptr DsoftbusInputPlugin::CreateBuffer(uint32_t metaType, - const StreamData *data, const json &resMsg) + const StreamData *data, const cJSON *resMsg) { auto buffer = Buffer::CreateDefaultBuffer(static_cast(metaType), data->bufLen); auto bufData = buffer->GetMemory(); @@ -268,9 +277,12 @@ std::shared_ptr DsoftbusInputPlugin::CreateBuffer(uint32_t metaType, AVTRANS_LOGE("write buffer data failed."); return buffer; } - + cJSON *paramItem = cJSON_GetObjectItem(resMsg, AVT_DATA_PARAM); + if (paramItem == NULL) { + return nullptr; + } auto meta = std::make_shared(); - if (!meta->UnmarshalVideoMeta(resMsg[AVT_DATA_PARAM])) { + if (!meta->UnmarshalVideoMeta(std::string(paramItem->valuestring))) { AVTRANS_LOGE("Unmarshal video buffer eta failed."); return nullptr; } diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h index ff5c8605..ee8d3842 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input/dsoftbus_input_plugin.h @@ -80,7 +80,7 @@ private: void HandleData(); void DataEnqueue(std::shared_ptr &buffer); void DataQueueClear(std::queue> &queue); - std::shared_ptr CreateBuffer(uint32_t metaType, const StreamData *data, const json &resMsg); + std::shared_ptr CreateBuffer(uint32_t metaType, const StreamData *data, const cJSON *resMsg); State GetCurrentState() { std::lock_guard lock(stateMutex_); diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp index c13fb2fa..93928226 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.cpp @@ -230,17 +230,26 @@ void DsoftbusInputAudioPlugin::OnStreamReceived(const StreamData *data, const St std::string message(reinterpret_cast(ext->buf), ext->bufLen); AVTRANS_LOGI("Receive message : %s", message.c_str()); - json resMsg = json::parse(message, nullptr, false); + //json resMsg = json::parse(message, nullptr, false); + cJSON *resMsg = cJSON_Parse(message.c_str()); + if (resMsg == nullptr) { + return; + } TRUE_RETURN(resMsg.is_discarded(), "The resMsg parse failed"); TRUE_RETURN(!IsUInt32(resMsg, AVT_DATA_META_TYPE), "invalid data type"); - uint32_t metaType = resMsg[AVT_DATA_META_TYPE]; - + // uint32_t metaType = resMsg[AVT_DATA_META_TYPE]; + cJSON *typeItem = cJSON_GetObjectItem(resMsg, AVT_DATA_META_TYPE); + if (typeItem == NULL) { + cJSON_Delete(resMsg); + return; + } + uint32_t metaType = typeItem->valueInt; auto buffer = CreateBuffer(metaType, data, resMsg); DataEnqueue(buffer); } std::shared_ptr DsoftbusInputAudioPlugin::CreateBuffer(uint32_t metaType, - const StreamData *data, const json &resMsg) + const StreamData *data, const cJSON *resMsg) { auto buffer = Buffer::CreateDefaultBuffer(static_cast(metaType), data->bufLen); auto bufData = buffer->GetMemory(); @@ -250,9 +259,12 @@ std::shared_ptr DsoftbusInputAudioPlugin::CreateBuffer(uint32_t metaType AVTRANS_LOGE("write buffer data failed."); return buffer; } - + cJSON *paramItem = cJSON_GetObjectItem(resMsg, AVT_DATA_PARAM); + if (paramItem == NULL) { + return nullptr; + } auto meta = std::make_shared(); - meta->UnmarshalAudioMeta(resMsg[AVT_DATA_PARAM]); + meta->UnmarshalAudioMeta(std::string(paramItem->valuestring)); buffer->pts = meta->pts_; buffer->GetBufferMeta()->SetMeta(Tag::USER_FRAME_PTS, meta->pts_); buffer->GetBufferMeta()->SetMeta(Tag::USER_FRAME_NUMBER, meta->frameNum_); diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h index 9ea9d88c..b4e3f056 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_input/dsoftbus_input_audio/dsoftbus_input_audio_plugin.h @@ -74,7 +74,7 @@ private: void HandleData(); void DataEnqueue(std::shared_ptr &buffer); void DataQueueClear(std::queue> &queue); - std::shared_ptr CreateBuffer(uint32_t metaType, const StreamData *data, const json &resMsg); + std::shared_ptr CreateBuffer(uint32_t metaType, const StreamData *data, const cJSON *resMsg); State GetCurrentState() { std::lock_guard lock(stateMutex_); diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp index 6fa15b8f..7261f45b 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output/dsoftbus_output_plugin.cpp @@ -297,10 +297,15 @@ void DsoftbusOutputPlugin::FeedChannelData() void DsoftbusOutputPlugin::SendDataToSoftbus(std::shared_ptr &buffer) { - json jsonObj; + //json jsonObj; + cJSON *jsonObj = cJSON_CreateObject(); + if (jsonObj == nullptr) { + retrun; + } auto bufferMeta = buffer->GetBufferMeta(); BufferMetaType metaType = bufferMeta->GetType(); - jsonObj[AVT_DATA_META_TYPE] = metaType; + //jsonObj[AVT_DATA_META_TYPE] = metaType; + cJSON_AddNumberToObject(jsonObj, AVT_DATA_META_TYPE.c_str(), metaType); if (metaType != BufferMetaType::VIDEO) { AVTRANS_LOGE("metaType is wrong"); return; @@ -316,9 +321,16 @@ void DsoftbusOutputPlugin::SendDataToSoftbus(std::shared_ptr &buffer) if (bufferMeta->IsExist(Tag::AUDIO_SAMPLE_PER_FRAME)) { hisAMeta->extFrameNum_ = Plugin::AnyCast(bufferMeta->GetMeta(Tag::AUDIO_SAMPLE_PER_FRAME)); } - jsonObj[AVT_DATA_PARAM] = hisAMeta->MarshalVideoMeta(); + // jsonObj[AVT_DATA_PARAM] = hisAMeta->MarshalVideoMeta(); + cJSON_AddStringToObject(jsonObj, AVT_DATA_PARAM.c_str(), hisAMeta->MarshalVideoMeta().c_str()); - std::string jsonStr = jsonObj.dump(); + // std::string jsonStr = jsonObj.dump(); + char *jsonstr = cJSON_Print(jsonObj); + if (jsonstr == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr = jsonstr; AVTRANS_LOGI("jsonStr->bufLen %zu, jsonStR: %s", jsonStr.length(), jsonStr.c_str()); auto bufferData = buffer->GetMemory(); diff --git a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp index 8d7b7f62..c375f37b 100644 --- a/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp +++ b/av_transport/av_trans_engine/plugin/plugins/av_trans_output/dsoftbus_output_audio/dsoftbus_output_audio_plugin.cpp @@ -290,10 +290,15 @@ void DsoftbusOutputAudioPlugin::FeedChannelData() void DsoftbusOutputAudioPlugin::SendDataToSoftbus(std::shared_ptr &buffer) { - json jsonObj; + // json jsonObj; + cJSON *jsonObj = cJSON_CreateObject(); + if (jsonObj == nullptr) { + retrun; + } auto bufferMeta = buffer->GetBufferMeta(); BufferMetaType metaType = bufferMeta->GetType(); - jsonObj[AVT_DATA_META_TYPE] = metaType; + // jsonObj[AVT_DATA_META_TYPE] = metaType; + cJSON_AddNumberToObject(jsonObj, AVT_DATA_META_TYPE.c_str(), metaType); if (metaType != BufferMetaType::AUDIO) { AVTRANS_LOGE("metaType is wrong"); return; @@ -309,10 +314,17 @@ void DsoftbusOutputAudioPlugin::SendDataToSoftbus(std::shared_ptr &buffe } else { hisAMeta->pts_ = Plugin::AnyCast(buffer->GetBufferMeta()->GetMeta(Tag::USER_FRAME_PTS)); } - jsonObj[AVT_DATA_PARAM] = hisAMeta->MarshalAudioMeta(); + // jsonObj[AVT_DATA_PARAM] = hisAMeta->MarshalAudioMeta(); + cJSON_AddStringToObject(jsonObj, AVT_DATA_PARAM.c_str(), hisAMeta->MarshalVideoMeta().c_str()); auto bufferData = buffer->GetMemory(); - std::string jsonStr = jsonObj.dump(); + // std::string jsonStr = jsonObj.dump(); + char *jsonstr = cJSON_Print(jsonObj); + if (jsonstr == nullptr) { + cJSON_Delete(jsonObj); + return; + } + std::string jsonStr = jsonstr; AVTRANS_LOGI("buffer data len = %zu, ext data len = %zu, ext data = %s", bufferData->GetSize(), jsonStr.length(), jsonStr.c_str()); diff --git a/av_transport/common/include/av_trans_utils.h b/av_transport/common/include/av_trans_utils.h index 0ca6694a..2ade76e2 100644 --- a/av_transport/common/include/av_trans_utils.h +++ b/av_transport/common/include/av_trans_utils.h @@ -53,9 +53,9 @@ void ParseChannelDescription(const std::string &descJsonStr, std::string &ownerN EventType CastEventType(Plugin::PluginEventType type, bool isAbnormal); void DumpBufferToFile(std::string fileName, uint8_t *buffer, int32_t bufSize); -bool IsUInt32(const nlohmann::json &jsonObj, const std::string &key); -bool IsInt64(const nlohmann::json &jsonObj, const std::string &key); -bool IsString(const nlohmann::json &jsonObj, const std::string &key); +bool IsUInt32(cJSON *jsonObj, const std::string &key); +bool IsInt64(cJSON *jsonObj, const std::string &key); +bool IsString(cJSON *jsonObj, const std::string &key); int64_t GetCurrentTime(); diff --git a/av_transport/common/src/av_sync_utils.cpp b/av_transport/common/src/av_sync_utils.cpp index 79915ed2..75bf71d5 100644 --- a/av_transport/common/src/av_sync_utils.cpp +++ b/av_transport/common/src/av_sync_utils.cpp @@ -259,35 +259,68 @@ bool IsInValidClockUnit(const AVSyncClockUnit &clockUnit) std::string MarshalSharedMemory(const AVTransSharedMemory &memory) { - nlohmann::json memoryJson; - - memoryJson[KEY_SHARED_MEM_FD] = memory.fd; - memoryJson[KEY_SHARED_MEM_SIZE] = memory.size; - memoryJson[KEY_SHARED_MEM_NAME] = memory.name; + // nlohmann::json memoryJson; + cJSON *memoryJson = cJSON_CreateObject(); + if (memoryJson == nullptr) { + return ""; + } - return memoryJson.dump(); + // memoryJson[KEY_SHARED_MEM_FD] = memory.fd; + // memoryJson[KEY_SHARED_MEM_SIZE] = memory.size; + // memoryJson[KEY_SHARED_MEM_NAME] = memory.name; + cJSON_AddNumberToObject(memoryJson, KEY_SHARED_MEM_FD.c_str(), memory.fd); + cJSON_AddNumberToObject(memoryJson, KEY_SHARED_MEM_SIZE.c_str(), memory.size); + cJSON_AddStringToObject(memoryJson, KEY_SHARED_MEM_NAME.c_str(), memory.name.c_str()); + + char *jsonstr = cJSON_Print(memoryJson); + if (jsonstr == nullptr) { + cJSON_Delete(jsonObj); + return ""; + } + cJSON_Delete(jsonObj); + cJSON_free(jsonstr); + return std::string(jsonstr); } AVTransSharedMemory UnmarshalSharedMemory(const std::string &jsonStr) { - nlohmann::json paramJson = nlohmann::json::parse(jsonStr, nullptr, false); - if (paramJson.is_discarded()) { + // nlohmann::json paramJson = nlohmann::json::parse(jsonStr, nullptr, false); + // if (paramJson.is_discarded()) { + // return AVTransSharedMemory{0, 0, ""}; + // } + + // if (!paramJson.contains(KEY_SHARED_MEM_FD) || !paramJson.contains(KEY_SHARED_MEM_SIZE) || + // !paramJson.contains(KEY_SHARED_MEM_NAME)) { + // return AVTransSharedMemory{0, 0, ""}; + // } + + // if (!paramJson[KEY_SHARED_MEM_FD].is_number_integer() || !paramJson[KEY_SHARED_MEM_SIZE].is_number_integer() || + // !paramJson[KEY_SHARED_MEM_NAME].is_string()) { + // return AVTransSharedMemory{0, 0, ""}; + // } + + // int32_t fd = paramJson[KEY_SHARED_MEM_FD].get(); + // int32_t size = paramJson[KEY_SHARED_MEM_SIZE].get(); + // std::string name = paramJson[KEY_SHARED_MEM_NAME].get(); + cJSON *paramJson = cJSON_Parse(jsonStr.c_str()); + if (paramJson == nullptr || !cJSON_IsObject(paramJson)) { return AVTransSharedMemory{0, 0, ""}; } - - if (!paramJson.contains(KEY_SHARED_MEM_FD) || !paramJson.contains(KEY_SHARED_MEM_SIZE) || - !paramJson.contains(KEY_SHARED_MEM_NAME)) { + cJSON *fdObj = cJSON_GetObjectItemCaseSensitive(paramJson, KEY_SHARED_MEM_FD.c_str()); + if (fdObj == nullptr || !cJSON_IsNumber(fdObj)) { return AVTransSharedMemory{0, 0, ""}; } - - if (!paramJson[KEY_SHARED_MEM_FD].is_number_integer() || !paramJson[KEY_SHARED_MEM_SIZE].is_number_integer() || - !paramJson[KEY_SHARED_MEM_NAME].is_string()) { + int32_t fd = fdObj->valueint; + cJSON *sizeObj = cJSON_GetObjectItemCaseSensitive(paramJson, KEY_SHARED_MEM_SIZE.c_str()); + if (sizeObj == nullptr || !cJSON_IsNumber(sizeObj)) { return AVTransSharedMemory{0, 0, ""}; } - - int32_t fd = paramJson[KEY_SHARED_MEM_FD].get(); - int32_t size = paramJson[KEY_SHARED_MEM_SIZE].get(); - std::string name = paramJson[KEY_SHARED_MEM_NAME].get(); + int32_t size = sizeObj->valueint; + cJSON *nameObj = cJSON_GetObjectItemCaseSensitive(paramJson, KEY_SHARED_MEM_NAME.c_str()); + if (nameObj == nullptr || !cJSON_IsNumber(nameObj)) { + return AVTransSharedMemory{0, 0, ""}; + } + std::string name = nameObj->valuestring; return AVTransSharedMemory{ fd, size, name }; } diff --git a/av_transport/common/src/av_trans_message.cpp b/av_transport/common/src/av_trans_message.cpp index db3eda7a..43093c42 100644 --- a/av_transport/common/src/av_trans_message.cpp +++ b/av_transport/common/src/av_trans_message.cpp @@ -39,17 +39,36 @@ AVTransMessage::~AVTransMessage() std::string AVTransMessage::MarshalMessage() { - nlohmann::json msgJson; - msgJson[KEY_TYPE] = type_; - msgJson[KEY_CONTENT] = content_; - msgJson[KEY_DST_DEVID] = dstDevId_; - return msgJson.dump(); + // nlohmann::json msgJson; + // msgJson[KEY_TYPE] = type_; + // msgJson[KEY_CONTENT] = content_; + // msgJson[KEY_DST_DEVID] = dstDevId_; + // return msgJson.dump(); + cJSON *msgJson = cJSON_CreateObject(); + if (msgJson == nullptr) { + return ""; + } + cJSON_AddNumberToObject(msgJson, KEY_TYPE.c_str(), type_); + cJSON_AddStringToObject(msgJson, KEY_CONTENT.c_str(), content_); + cJSON_AddStringToObject(msgJson, KEY_DST_DEVID.c_str(), dstDevId_); + char *jsonstr = cJSON_Print(msgJson); + if (jsonstr == nullptr) { + cJSON_Delete(msgJson); + return ""; + } + cJSON_Delete(msgJson); + cJSON_free(jsonstr); + return std::string(jsonstr); } bool AVTransMessage::UnmarshalMessage(const std::string& jsonStr, const std::string &peerDevId) { - nlohmann::json msgJson = nlohmann::json::parse(jsonStr, nullptr, false); - if (msgJson.is_discarded()) { + // nlohmann::json msgJson = nlohmann::json::parse(jsonStr, nullptr, false); + // if (msgJson.is_discarded()) { + // return false; + // } + cJSON *metaJson = cJSON_Parse(jsonStr.c_str()); + if (metaJson == nullptr || !cJSON_IsObject(metaJson)) { return false; } if (!IsUInt32(msgJson, KEY_TYPE) || !IsString(msgJson, KEY_CONTENT)) { diff --git a/av_transport/common/src/av_trans_meta.cpp b/av_transport/common/src/av_trans_meta.cpp index 37adf6fc..0c1ef9a6 100644 --- a/av_transport/common/src/av_trans_meta.cpp +++ b/av_transport/common/src/av_trans_meta.cpp @@ -42,26 +42,60 @@ std::shared_ptr AVTransAudioBufferMeta::Clone() std::string AVTransAudioBufferMeta::MarshalAudioMeta() { - nlohmann::json metaJson; - metaJson[META_DATA_TYPE] = dataType_; - metaJson[META_TIMESTAMP] = pts_; - metaJson[META_FRAME_NUMBER] = frameNum_; - return metaJson.dump(); + // nlohmann::json metaJson; + // metaJson[META_DATA_TYPE] = dataType_; + // metaJson[META_TIMESTAMP] = pts_; + // metaJson[META_FRAME_NUMBER] = frameNum_; + // return metaJson.dump(); + cJSON *metaJson = cJSON_CreateObject(); + if (metaJson == nullptr) { + return ""; + } + cJSON_AddNumberToObject(metaJson, META_DATA_TYPE.c_str(), dataType_); + cJSON_AddNumberToObject(metaJson, META_TIMESTAMP.c_str(), pts_); + cJSON_AddNumberToObject(metaJson, META_FRAME_NUMBER.c_str(), frameNum_); + char *jsonstr = cJSON_Print(metaJson); + if (jsonstr == nullptr) { + cJSON_Delete(metaJson); + return ""; + } + cJSON_Delete(metaJson); + cJSON_free(jsonstr); + return std::string(jsonstr); } bool AVTransAudioBufferMeta::UnmarshalAudioMeta(const std::string& jsonStr) { - nlohmann::json metaJson = nlohmann::json::parse(jsonStr, nullptr, false); - if (metaJson.is_discarded()) { - return false; + // nlohmann::json metaJson = nlohmann::json::parse(jsonStr, nullptr, false); + // if (metaJson.is_discarded()) { + // return false; + // } + cJSON *metaJson = cJSON_Parse(jsonStr.c_str()); + if (metaJson == nullptr || !cJSON_IsObject(metaJson)) { + return ; } if (!IsUInt32(metaJson, META_DATA_TYPE) || !IsInt64(metaJson, META_TIMESTAMP) || !IsUInt32(metaJson, META_FRAME_NUMBER)) { return false; } dataType_ = metaJson[META_DATA_TYPE].get(); - pts_ = metaJson[META_TIMESTAMP].get(); - frameNum_ = metaJson[META_FRAME_NUMBER].get(); + // pts_ = metaJson[META_TIMESTAMP].get(); + // frameNum_ = metaJson[META_FRAME_NUMBER].get(); + cJSON *typeObj = cJSON_GetObjectItemCaseSensitive(metaJson, META_DATA_TYPE.c_str()); + if (typeObj == nullptr || !cJSON_IsNumber(typeObj)) { + return false; + } + cJSON *ptsObj = cJSON_GetObjectItemCaseSensitive(metaJson, META_TIMESTAMP.c_str()); + if (ptsObj == nullptr || !cJSON_IsNumber(ptsObj)) { + return false; + } + cJSON *frameObj = cJSON_GetObjectItemCaseSensitive(metaJson, META_FRAME_NUMBER.c_str()); + if (frameObj == nullptr || !cJSON_IsNumber(frameObj)) { + return false; + } + dataType_ = typeObj->valueint; + pts_ = ptsObj->valueint; + frameNum_ = frameObj->valueint; return true; } @@ -83,23 +117,48 @@ std::shared_ptr AVTransVideoBufferMeta::Clone() std::string AVTransVideoBufferMeta::MarshalVideoMeta() { - nlohmann::json metaJson; - metaJson[META_DATA_TYPE] = dataType_; - metaJson[META_TIMESTAMP] = pts_; - metaJson[META_FRAME_NUMBER] = frameNum_; + // nlohmann::json metaJson; + // metaJson[META_DATA_TYPE] = dataType_; + // metaJson[META_TIMESTAMP] = pts_; + // metaJson[META_FRAME_NUMBER] = frameNum_; + // if (extPts_ > 0) { + // metaJson[META_EXT_TIMESTAMP] = extPts_; + // } + // if (extFrameNum_ > 0) { + // metaJson[META_EXT_FRAME_NUMBER] = extFrameNum_; + // } + // return metaJson.dump(); + cJSON *metaJson = cJSON_CreateObject(); + if (metaJson == nullptr) { + return ""; + } + cJSON_AddNumberToObject(metaJson, META_DATA_TYPE.c_str(), dataType_); + cJSON_AddNumberToObject(metaJson, META_TIMESTAMP.c_str(), pts_); + cJSON_AddNumberToObject(metaJson, META_FRAME_NUMBER.c_str(), frameNum_); if (extPts_ > 0) { - metaJson[META_EXT_TIMESTAMP] = extPts_; + //metaJson[META_EXT_TIMESTAMP] = extPts_; + cJSON_AddNumberToObject(metaJson, META_EXT_TIMESTAMP.c_str(), extPts_); } if (extFrameNum_ > 0) { - metaJson[META_EXT_FRAME_NUMBER] = extFrameNum_; + // metaJson[META_EXT_FRAME_NUMBER] = extFrameNum_; + cJSON_AddNumberToObject(metaJson, META_EXT_FRAME_NUMBER.c_str(), extFrameNum_); + } + char *jsonstr = cJSON_Print(metaJson); + if (jsonstr == nullptr) { + cJSON_Delete(metaJson); + return ""; } - return metaJson.dump(); + return std::string(metaJson); } bool AVTransVideoBufferMeta::UnmarshalVideoMeta(const std::string& jsonStr) { - nlohmann::json metaJson = nlohmann::json::parse(jsonStr, nullptr, false); - if (metaJson.is_discarded()) { + // nlohmann::json metaJson = nlohmann::json::parse(jsonStr, nullptr, false); + // if (metaJson.is_discarded()) { + // return false; + // } + cJSON *metaJson = cJSON_Parse(jsonStr.c_str()); + if (metaJson == nullptr || !cJSON_IsObject(metaJson)) { return false; } if (IsUInt32(metaJson, META_DATA_TYPE)) { diff --git a/av_transport/common/src/av_trans_utils.cpp b/av_transport/common/src/av_trans_utils.cpp index 4bc65ba8..a805ee51 100644 --- a/av_transport/common/src/av_trans_utils.cpp +++ b/av_transport/common/src/av_trans_utils.cpp @@ -70,26 +70,55 @@ OHOS::Media::Plugin::MediaType TransName2MediaType(const std::string &ownerName) std::string BuildChannelDescription(const std::string &ownerName, const std::string &peerDevId) { - nlohmann::json descJson; - descJson[KEY_OWNER_NAME] = ownerName; - descJson[KEY_PEER_DEVID] = peerDevId; - return descJson.dump(); + // nlohmann::json descJson; + // descJson[KEY_OWNER_NAME] = ownerName; + // descJson[KEY_PEER_DEVID] = peerDevId; + cJSON *descJson = cJSON_CreateObject(); + if (descJson == nullptr) { + return ""; + } + cJSON_AddStringToObject(descJson, KEY_OWNER_NAME.c_str(), ownerName.c_str()); + cJSON_AddStringToObject(descJson, KEY_PEER_DEVID.c_str(), peerDevId.c_str()); + char *jsonstr = cJSON_Print(descJson); + if (jsonstr == nullptr) { + cJSON_Delete(descJson); + return ""; + } + cJSON_Delete(descJson); + cJSON_free(jsonstr); + return std::string(jsonstr); } void ParseChannelDescription(const std::string &descJsonStr, std::string &ownerName, std::string &peerDevId) { - nlohmann::json descJson = nlohmann::json::parse(descJsonStr, nullptr, false); - if (descJson.is_discarded()) { - return; + // nlohmann::json descJson = nlohmann::json::parse(descJsonStr, nullptr, false); + // if (descJson.is_discarded()) { + // return; + // } + // if (!descJson.contains(KEY_OWNER_NAME) || !descJson.contains(KEY_PEER_DEVID)) { + // return; + // } + // if (!IsString(descJson, KEY_OWNER_NAME) || !IsString(descJson, KEY_PEER_DEVID)) { + // return; + // } + // ownerName = descJson[KEY_OWNER_NAME].get(); + // peerDevId = descJson[KEY_PEER_DEVID].get(); + + cJSON *descJson = cJSON_Parse(descJsonStr.c_str()); + if (descJson == nullptr || !cJSON_IsObject(descJson)) { + return ; } - if (!descJson.contains(KEY_OWNER_NAME) || !descJson.contains(KEY_PEER_DEVID)) { - return; + cJSON *nameObj = cJSON_GetObjectItemCaseSensitive(descJson, KEY_OWNER_NAME.c_str()); + if (sizeObj == nullptr || !cJSON_IsString(sizeObj)) { + return ; } - if (!IsString(descJson, KEY_OWNER_NAME) || !IsString(descJson, KEY_PEER_DEVID)) { - return; + cJSON *devObj = cJSON_GetObjectItemCaseSensitive(descJson, KEY_PEER_DEVID.c_str()); + if (devObj == nullptr || !cJSON_IsString(devObj)) { + return ; } - ownerName = descJson[KEY_OWNER_NAME].get(); - peerDevId = descJson[KEY_PEER_DEVID].get(); + ownerName = nameObj->valuestring; + peerDevId = devObj->valuestring; + cJSON_Delete(descJson); } std::shared_ptr TransBuffer2HiSBuffer(const std::shared_ptr& transBuffer) @@ -231,20 +260,69 @@ void DumpBufferToFile(std::string fileName, uint8_t *buffer, int32_t bufSize) ofs.close(); } -bool IsUInt32(const nlohmann::json &jsonObj, const std::string &key) +bool IsUInt32(cJSON *jsonObj, const std::string &key) { - return jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT32_MAX; + //return jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT32_MAX; + if (jsonObj == nullptr || !cJSON_IsObject(jsonObj)) { + AVTRANS_LOGE("JSON parameter is invalid."); + return false; + } + cJSON *paramValue = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (paramValue == nullptr) { + AVTRANS_LOGE("paramValue is null"); + return false; + } + + if (cJSON_IsNumber(paramValue)) { + int value = paramValue->valueint; + if (INT32_MIN <= value && value <= INT32_MAX) { + return true; + } + } + return false; } -bool IsInt64(const nlohmann::json &jsonObj, const std::string &key) +bool IsInt64(cJSON *jsonObj, const std::string &key) { - return jsonObj.contains(key) && jsonObj[key].is_number_integer() && INT64_MIN <= jsonObj[key] && - jsonObj[key] <= INT64_MAX; + // return jsonObj.contains(key) && jsonObj[key].is_number_integer() && INT64_MIN <= jsonObj[key] && + // jsonObj[key] <= INT64_MAX; + + if (jsonObj == nullptr || !cJSON_IsObject(jsonObj)) { + AVTRANS_LOGE("JSON parameter is invalid."); + return false; + } + cJSON *paramValue = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (paramValue == nullptr) { + AVTRANS_LOGE("paramValue is null"); + return false; + } + + if (cJSON_IsNumber(paramValue)) { + int value = paramValue->valueint; + if (INT64_MIN <= value && value <= INT64_MAX) { + return true; + } + } + return false; } -bool IsString(const nlohmann::json &jsonObj, const std::string &key) +bool IsString(cJSON *jsonObj, const std::string &key) { - return jsonObj.contains(key) && jsonObj[key].is_string() && jsonObj[key].size() <= MAX_MESSAGES_LEN; + //return jsonObj.contains(key) && jsonObj[key].is_string() && jsonObj[key].size() <= MAX_MESSAGES_LEN; + if (jsonObj == nullptr || !cJSON_IsObject(jsonObj)) { + AVTRANS_LOGE("JSON parameter is invalid."); + return false; + } + cJSON *paramValue = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (paramValue == nullptr) { + AVTRANS_LOGE("paramValue is null"); + return false; + } + + if (cJSON_IsString(paramValue)) { + return true; + } + return false; } int64_t GetCurrentTime() -- Gitee