1 /*
2  * Copyright (c) 2022-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 #include "video_param.h"
16 
17 #include "dscreen_constants.h"
18 #include "dscreen_json_util.h"
19 #include "dscreen_log.h"
20 
21 using json = nlohmann::json;
22 
23 namespace OHOS {
24 namespace DistributedHardware {
SetScreenWidth(uint32_t screenWidth)25 void VideoParam::SetScreenWidth(uint32_t screenWidth)
26 {
27     screenWidth_ = screenWidth;
28 }
29 
GetScreenWidth() const30 uint32_t VideoParam::GetScreenWidth() const
31 {
32     return screenWidth_;
33 }
34 
SetScreenHeight(uint32_t screenHeight)35 void VideoParam::SetScreenHeight(uint32_t screenHeight)
36 {
37     screenHeight_ = screenHeight;
38 }
39 
GetScreenHeight() const40 uint32_t VideoParam::GetScreenHeight() const
41 {
42     return screenHeight_;
43 }
44 
SetVideoWidth(uint32_t videoWidth)45 void VideoParam::SetVideoWidth(uint32_t videoWidth)
46 {
47     videoWidth_ = videoWidth;
48 }
49 
GetVideoWidth() const50 uint32_t VideoParam::GetVideoWidth() const
51 {
52     return videoWidth_;
53 }
54 
SetPartialRefreshFlag(bool flag)55 void VideoParam::SetPartialRefreshFlag(bool flag)
56 {
57     isPartialRefresh_ = flag;
58 }
59 
GetPartialRefreshFlag() const60 bool VideoParam::GetPartialRefreshFlag() const
61 {
62     return isPartialRefresh_;
63 }
64 
SetVideoHeight(uint32_t videoHeight)65 void VideoParam::SetVideoHeight(uint32_t videoHeight)
66 {
67     videoHeight_ = videoHeight;
68 }
69 
GetVideoHeight() const70 uint32_t VideoParam::GetVideoHeight() const
71 {
72     return videoHeight_;
73 }
74 
SetFps(double fps)75 void VideoParam::SetFps(double fps)
76 {
77     fps_ = fps;
78 }
79 
GetFps() const80 double VideoParam::GetFps() const
81 {
82     return fps_;
83 }
84 
SetCodecType(uint8_t codecType)85 void VideoParam::SetCodecType(uint8_t codecType)
86 {
87     codecType_ = codecType;
88 }
89 
GetCodecType() const90 uint8_t VideoParam::GetCodecType() const
91 {
92     return codecType_;
93 }
94 
SetVideoFormat(uint8_t videoFormat)95 void VideoParam::SetVideoFormat(uint8_t videoFormat)
96 {
97     videoFormat_ = videoFormat;
98 }
99 
GetVideoFormat() const100 uint8_t VideoParam::GetVideoFormat() const
101 {
102     return videoFormat_;
103 }
104 
to_json(json &j, const DistributedHardware::VideoParam &videoParam)105 void to_json(json &j, const DistributedHardware::VideoParam &videoParam)
106 {
107     j = json {
108         {KEY_SCREEN_WIDTH, videoParam.screenWidth_},
109         {KEY_SCREEN_HEIGHT, videoParam.screenHeight_},
110         {KEY_VIDEO_WIDTH, videoParam.videoWidth_},
111         {KEY_VIDEO_HEIGHT, videoParam.videoHeight_},
112         {KEY_FPS, videoParam.fps_},
113         {KEY_CODECTYPE, videoParam.codecType_},
114         {KEY_COLOR_FORMAT, videoParam.videoFormat_},
115         {KEY_PARTIALREFRESH, videoParam.isPartialRefresh_}
116     };
117 }
118 
from_json(const json &j, DistributedHardware::VideoParam &videoParam)119 void from_json(const json &j, DistributedHardware::VideoParam &videoParam)
120 {
121     if (!IsUInt32(j, KEY_SCREEN_WIDTH) || !IsUInt32(j, KEY_SCREEN_HEIGHT) ||
122         !IsUInt32(j, KEY_VIDEO_WIDTH) || !IsUInt32(j, KEY_VIDEO_HEIGHT) ||
123         !IsFloat(j, KEY_FPS) || !IsUInt8(j, KEY_CODECTYPE) ||
124         !IsUInt8(j, KEY_COLOR_FORMAT)) {
125         DHLOGE("Invalid JSON value for one or more keys.");
126         return;
127     }
128 
129     const uint32_t screenWidth = j[KEY_SCREEN_WIDTH].get<uint32_t>();
130     const uint32_t screenHeight = j[KEY_SCREEN_HEIGHT].get<uint32_t>();
131     const uint32_t videoWidth = j[KEY_VIDEO_WIDTH].get<uint32_t>();
132     const uint32_t videoHeight = j[KEY_VIDEO_HEIGHT].get<uint32_t>();
133 
134     if ((screenWidth > DSCREEN_MAX_SCREEN_DATA_WIDTH) || (screenHeight > DSCREEN_MAX_SCREEN_DATA_HEIGHT)) {
135         DHLOGE("Screen width or height exceeds the maximum limit.");
136         return;
137     }
138 
139     if ((videoWidth > DSCREEN_MAX_VIDEO_DATA_WIDTH) || (videoHeight > DSCREEN_MAX_VIDEO_DATA_HEIGHT)) {
140         DHLOGE("Video width or height exceeds the maximum limit.");
141         return;
142     }
143 
144     videoParam.screenWidth_ = screenWidth;
145     videoParam.screenHeight_ = screenHeight;
146     videoParam.videoWidth_ = videoWidth;
147     videoParam.videoHeight_ = videoHeight;
148     videoParam.fps_ = j[KEY_FPS].get<double>();
149     videoParam.codecType_ = j[KEY_CODECTYPE].get<uint8_t>();
150     videoParam.videoFormat_ = j[KEY_COLOR_FORMAT].get<uint8_t>();
151     videoParam.isPartialRefresh_ = false;
152     if (IsBool(j, KEY_PARTIALREFRESH)) {
153         videoParam.isPartialRefresh_ = j[KEY_PARTIALREFRESH].get<bool>();
154     }
155 }
156 } // namespace DistributedHardware
157 } // namespace OHOS
158