From 4c65e3a888885d4a02aa7b205a3c1e8f64e3e4cc Mon Sep 17 00:00:00 2001 From: yangli Date: Wed, 17 May 2023 15:20:03 +0800 Subject: [PATCH 1/3] =?UTF-8?q?json=E6=A0=A1=E9=AA=8C=E5=92=8C=E8=B6=85?= =?UTF-8?q?=E5=A4=A7=E5=BE=AA=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangli --- common/include/dscreen_constants.h | 6 ++ .../common/utils/src/dscreen_maprelation.cpp | 6 +- .../screenregionmgr/src/screenregionmgr.cpp | 2 +- .../include/screen_data_channel_impl.h | 6 +- .../src/screen_data_channel_impl.cpp | 62 +++++++++++++------ 5 files changed, 58 insertions(+), 24 deletions(-) diff --git a/common/include/dscreen_constants.h b/common/include/dscreen_constants.h index 083d0b44..7b016d56 100644 --- a/common/include/dscreen_constants.h +++ b/common/include/dscreen_constants.h @@ -135,6 +135,11 @@ const std::string KEY_DISPLAY_RECT = "displayRect"; const std::string KEY_SCREEN_RECT = "screenRect"; const std::string KEY_POINT_START_X = "startX"; const std::string KEY_POINT_START_Y = "startY"; +const std::string KEY_POINT_DIRTY_X = "dirtyX"; +const std::string KEY_POINT_DIRTY_Y = "dirtyY"; +const std::string KEY_POINT_DIRTY_W = "dirtyW"; +const std::string KEY_POINT_DIRTY_H = "dirtyH"; +const std::string KEY_POINT_DIRTY_SIZE = "dirtySize"; const std::string KEY_WIDTH = "width"; const std::string KEY_HEIGHT = "height"; const std::string KEY_SCREEN_WIDTH = "screenWidth"; @@ -169,6 +174,7 @@ constexpr uint32_t MAX_MESSAGES_LEN = 40 * 1024 * 1024; constexpr float DEFAULT_DENSITY = 2.0; constexpr int32_t DEFAULT_SCREEN_FLAGS = 0; constexpr uint32_t DEFAULT_FPS = 30; +constexpr int32_t DIRTY_MAX_SIZE = 10; constexpr uint8_t DEFAULT_CODECTYPE = VIDEO_CODEC_TYPE_VIDEO_H264; constexpr uint8_t DEFAULT_VIDEO_FORMAT = VIDEO_DATA_FORMAT_NV12; constexpr int32_t DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID = 4807; diff --git a/services/common/utils/src/dscreen_maprelation.cpp b/services/common/utils/src/dscreen_maprelation.cpp index 57d3db3e..0572b157 100644 --- a/services/common/utils/src/dscreen_maprelation.cpp +++ b/services/common/utils/src/dscreen_maprelation.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -84,7 +84,9 @@ void from_json(const json &j, DScreenMapRelation &dScreenMapRelation) } dScreenMapRelation.displayId_ = j[KEY_DISPLAY_ID].get(); dScreenMapRelation.screenId_ = j[KEY_SCREEN_ID].get(); - + if (!j.contains(KEY_DISPLAY_RECT) || !j.contains(KEY_SCREEN_RECT)) { + return; + } from_json(j.at(KEY_DISPLAY_RECT), dScreenMapRelation.displayRect_); from_json(j.at(KEY_SCREEN_RECT), dScreenMapRelation.screenRect_); } diff --git a/services/screenservice/sinkservice/screenregionmgr/src/screenregionmgr.cpp b/services/screenservice/sinkservice/screenregionmgr/src/screenregionmgr.cpp index bfc0368d..85f1d512 100644 --- a/services/screenservice/sinkservice/screenregionmgr/src/screenregionmgr.cpp +++ b/services/screenservice/sinkservice/screenregionmgr/src/screenregionmgr.cpp @@ -136,7 +136,7 @@ void ScreenRegionManager::HandleNotifySetUp(const std::string &remoteDevId, cons NotifyRemoteSourceSetUpResult(remoteDevId, "", ERR_DH_SCREEN_SA_SCREENREGION_SETUP_FAIL, ""); return; } - if (eventContentJson.contains(KEY_VERSION)) { + if (IsString(eventContentJson, KEY_VERSION)) { version_ = eventContentJson[KEY_VERSION].get(); } uint64_t screenId = eventContentJson[KEY_SCREEN_ID].get(); diff --git a/services/screentransport/screendatachannel/include/screen_data_channel_impl.h b/services/screentransport/screendatachannel/include/screen_data_channel_impl.h index 0b43f3b4..5a715838 100644 --- a/services/screentransport/screendatachannel/include/screen_data_channel_impl.h +++ b/services/screentransport/screendatachannel/include/screen_data_channel_impl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -23,6 +23,7 @@ #include "iscreen_channel.h" #include "softbus_adapter.h" +#include "nlohmann/json.hpp" namespace OHOS { namespace DistributedHardware { class ScreenDataChannelImpl : public IScreenChannel, @@ -46,7 +47,8 @@ public: void OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen) override; void OnStreamReceived(int32_t sessionId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param) override; - + void DirtyVecToJson(nlohmann::json &rectJson, const std::shared_ptr &screenData); + void JsonToDirtyJson(nlohmann::json &rectJson, std::shared_ptr &screenData); private: static const constexpr char *LOG_TAG = "ScreenDataChannel"; bool jpegSessionFlag_ = false; diff --git a/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp b/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp index dcf23a66..72412fcf 100644 --- a/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp +++ b/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -22,7 +22,6 @@ #include "dscreen_hisysevent.h" #include "dscreen_log.h" #include "dscreen_util.h" -#include "nlohmann/json.hpp" #include "dscreen_json_util.h" namespace OHOS { @@ -202,15 +201,7 @@ int32_t ScreenDataChannelImpl::SendDirtyData(const std::shared_ptr & return ERR_DH_SCREEN_TRANS_NULL_VALUE; } nlohmann::json rectJson; - std::vector dirtyRectVec = screenData->GetDirtyRectVec(); - rectJson["dataType"] = screenData->DataType(); - rectJson["dirtySize"] = static_cast(dirtyRectVec.size()); - int32_t rectIndex = 0; - for (auto item : dirtyRectVec) { - std::string tempRectIndex = std::to_string(rectIndex); - rectJson[tempRectIndex] = {item.xPos, item.yPos, item.width, item.height, item.dirtySize}; - rectIndex++; - } + DirtyVecToJson(rectJson, screenData); StreamData data = {reinterpret_cast(screenData->Data()), screenData->Capacity()}; std::string rectInfo = rectJson.dump(); char *dirtyInfo = new char[rectInfo.length() + 1] {0}; @@ -341,28 +332,61 @@ void ScreenDataChannelImpl::ProcessDirtyData(const StreamData *data, DHLOGE("%s: OnStreamReceived rectJson invalid", LOG_TAG); return; } + JsonToDirtyJson(rectJson, dataBuffer); + listener->OnDataReceived(dataBuffer); +} + +void ScreenDataChannelImpl::DirtyVecToJson(nlohmann::json &rectJson, const std::shared_ptr &screenData) +{ + std::vector dirtyRectVec = screenData->GetDirtyRectVec(); + rectJson["dataType"] = screenData->DataType(); + rectJson["dirtySize"] = dirtyRectVec.size(); + int32_t rectIndex = 0; + for (auto item : dirtyRectVec) { + std::string tempRectIndex = std::to_string(rectIndex); + rectJson[tempRectIndex] = nlohmann::json { + {KEY_POINT_DIRTY_X, item.xPos}, + {KEY_POINT_DIRTY_Y, item.yPos}, + {KEY_POINT_DIRTY_W, item.width}, + {KEY_POINT_DIRTY_H, item.height}, + {KEY_POINT_DIRTY_SIZE, item.dirtySize} + }; + rectIndex++; + } +} + +void ScreenDataChannelImpl::JsonToDirtyJson(nlohmann::json &rectJson, std::shared_ptr &screenData) +{ if (!IsInt32(rectJson, "dirtySize") || !IsInt32(rectJson, "dataType")) { return; } int32_t dirtySize = rectJson["dirtySize"].get(); int32_t dataType = rectJson["dataType"].get(); - uint8_t num = 0; + int32_t num = 0; + if (dirtySize >= DIRTY_MAX_SIZE) { + return; + } while (num < dirtySize) { auto item = std::to_string(num); if (!rectJson.contains(item)) { return; } - int32_t X = rectJson[item][0].get(); - int32_t Y = rectJson[item][1].get(); - int32_t W = rectJson[item][2].get(); - int32_t H = rectJson[item][3].get(); - int32_t Size = rectJson[item][4].get(); + if (!IsInt32(rectJson[item], KEY_POINT_DIRTY_X) || !IsInt32(rectJson[item], KEY_POINT_DIRTY_Y) || + !IsInt32(rectJson[item], KEY_POINT_DIRTY_W) || !IsInt32(rectJson[item], KEY_POINT_DIRTY_H) || + !IsInt32(rectJson[item], KEY_POINT_DIRTY_SIZE)) { + DHLOGE("%s: JsonToDirtyJson rectJson not contain int32."); + return; + } + int32_t X = rectJson[item][KEY_POINT_DIRTY_X].get(); + int32_t Y = rectJson[item][KEY_POINT_DIRTY_Y].get(); + int32_t W = rectJson[item][KEY_POINT_DIRTY_W].get(); + int32_t H = rectJson[item][KEY_POINT_DIRTY_H].get(); + int32_t Size = rectJson[item][KEY_POINT_DIRTY_SIZE].get(); DirtyRect rect = {X, Y, W, H, Size}; dataBuffer->AddDirtyRect(rect); num++; } - dataBuffer->SetDataType(dataType); - listener->OnDataReceived(dataBuffer); + screenData->AddDirtyRect(rect); } } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file -- Gitee From 5a3513b9b6accf4d0a8cd5b64219389ab3d94810 Mon Sep 17 00:00:00 2001 From: yangli Date: Wed, 17 May 2023 15:50:09 +0800 Subject: [PATCH 2/3] =?UTF-8?q?json=E6=A0=A1=E9=AA=8C=E5=92=8C=E8=B6=85?= =?UTF-8?q?=E5=A4=A7=E5=BE=AA=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangli --- .../screendatachannel/src/screen_data_channel_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp b/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp index 72412fcf..a8cb8ea8 100644 --- a/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp +++ b/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp @@ -383,7 +383,7 @@ void ScreenDataChannelImpl::JsonToDirtyJson(nlohmann::json &rectJson, std::share int32_t H = rectJson[item][KEY_POINT_DIRTY_H].get(); int32_t Size = rectJson[item][KEY_POINT_DIRTY_SIZE].get(); DirtyRect rect = {X, Y, W, H, Size}; - dataBuffer->AddDirtyRect(rect); + screenData->AddDirtyRect(rect); num++; } screenData->AddDirtyRect(rect); -- Gitee From 246b78a8067d428a633ec3fd448a1ed9b47c3a6f Mon Sep 17 00:00:00 2001 From: yangli Date: Wed, 17 May 2023 15:53:58 +0800 Subject: [PATCH 3/3] =?UTF-8?q?json=E6=A0=A1=E9=AA=8C=E5=92=8C=E8=B6=85?= =?UTF-8?q?=E5=A4=A7=E5=BE=AA=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangli --- .../screendatachannel/src/screen_data_channel_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp b/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp index a8cb8ea8..47a2811d 100644 --- a/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp +++ b/services/screentransport/screendatachannel/src/screen_data_channel_impl.cpp @@ -386,7 +386,7 @@ void ScreenDataChannelImpl::JsonToDirtyJson(nlohmann::json &rectJson, std::share screenData->AddDirtyRect(rect); num++; } - screenData->AddDirtyRect(rect); + screenData->SetDataType(dataType); } } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file -- Gitee