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
21using json = nlohmann::json;
22
23namespace OHOS {
24namespace DistributedHardware {
25void VideoParam::SetScreenWidth(uint32_t screenWidth)
26{
27    screenWidth_ = screenWidth;
28}
29
30uint32_t VideoParam::GetScreenWidth() const
31{
32    return screenWidth_;
33}
34
35void VideoParam::SetScreenHeight(uint32_t screenHeight)
36{
37    screenHeight_ = screenHeight;
38}
39
40uint32_t VideoParam::GetScreenHeight() const
41{
42    return screenHeight_;
43}
44
45void VideoParam::SetVideoWidth(uint32_t videoWidth)
46{
47    videoWidth_ = videoWidth;
48}
49
50uint32_t VideoParam::GetVideoWidth() const
51{
52    return videoWidth_;
53}
54
55void VideoParam::SetPartialRefreshFlag(bool flag)
56{
57    isPartialRefresh_ = flag;
58}
59
60bool VideoParam::GetPartialRefreshFlag() const
61{
62    return isPartialRefresh_;
63}
64
65void VideoParam::SetVideoHeight(uint32_t videoHeight)
66{
67    videoHeight_ = videoHeight;
68}
69
70uint32_t VideoParam::GetVideoHeight() const
71{
72    return videoHeight_;
73}
74
75void VideoParam::SetFps(double fps)
76{
77    fps_ = fps;
78}
79
80double VideoParam::GetFps() const
81{
82    return fps_;
83}
84
85void VideoParam::SetCodecType(uint8_t codecType)
86{
87    codecType_ = codecType;
88}
89
90uint8_t VideoParam::GetCodecType() const
91{
92    return codecType_;
93}
94
95void VideoParam::SetVideoFormat(uint8_t videoFormat)
96{
97    videoFormat_ = videoFormat;
98}
99
100uint8_t VideoParam::GetVideoFormat() const
101{
102    return videoFormat_;
103}
104
105void 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
119void 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