1 /*
2 * Copyright (c) 2020-2021 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 "frame_config.h"
16 #include "media_log.h"
17 #include "securec.h"
18
19 using namespace std;
20
21 namespace OHOS {
22 namespace Media {
23 const int32_t DEFAULT_FPS = 30;
GetFrameConfigType()24 int32_t FrameConfig::GetFrameConfigType()
25 {
26 return type_;
27 }
28
GetSurfaces()29 list<Surface *> FrameConfig::GetSurfaces()
30 {
31 return surfaceList_;
32 }
33
AddSurface(Surface & surface)34 void FrameConfig::AddSurface(Surface &surface)
35 {
36 surfaceList_.remove(&surface);
37 surfaceList_.emplace_back(&surface);
38 }
39
RemoveSurface(Surface & surface)40 void FrameConfig::RemoveSurface(Surface &surface)
41 {
42 surfaceList_.remove(&surface);
43 }
44
GetValue(uint32_t key)45 void *FrameConfig::GetValue(uint32_t key)
46 {
47 if (key == CAM_IMAGE_CROP_RECT) {
48 return &crop;
49 }
50 auto p = keyMap_.find(key);
51 return (p == keyMap_.end()) ? nullptr : (&p->second);
52 }
53
SetVendorParameter(uint8_t * value,uint32_t len)54 void FrameConfig::SetVendorParameter(uint8_t *value, uint32_t len)
55 {
56 if (value == nullptr) {
57 MEDIA_ERR_LOG("value is a nullptr");
58 return;
59 }
60 errno_t ret = memset_s(privateTag_, sizeof(privateTag_), 0, sizeof(privateTag_));
61 if (ret != EOK) {
62 MEDIA_ERR_LOG("memset failed when set private tag, ret(%d)", ret);
63 return;
64 }
65 ret = memcpy_s(privateTag_, sizeof(privateTag_), value, len);
66 if (ret != EOK) {
67 MEDIA_ERR_LOG("memcpy failed when set private tag, ret(%d)", ret);
68 }
69 }
70
GetVendorParameter(uint8_t * value,uint32_t len)71 void FrameConfig::GetVendorParameter(uint8_t *value, uint32_t len)
72 {
73 if (value == nullptr) {
74 MEDIA_ERR_LOG("value is a nullptr");
75 return;
76 }
77 uint32_t realLength = (len > PRIVATE_TAG_LEN) ? PRIVATE_TAG_LEN : len;
78 errno_t ret = memcpy_s(value, realLength, privateTag_, realLength);
79 if (ret != EOK) {
80 MEDIA_ERR_LOG("memcpy failed when get private tag, ret(%d)", ret);
81 }
82 }
83
SetValue(uint32_t key,const void * value)84 void FrameConfig::SetValue(uint32_t key, const void *value)
85 {
86 if (value == nullptr) {
87 MEDIA_ERR_LOG("value is a nullptr");
88 return;
89 }
90 switch (key) {
91 case PARAM_KEY_IMAGE_ENCODE_QFACTOR:
92 case CAM_FRAME_FPS:
93 case CAM_IMAGE_INVERT_MODE:
94 case CAM_IMAGE_FORMAT:
95 keyMap_[key] = *(static_cast<const int32_t *>(value));
96 break;
97 case CAM_IMAGE_CROP_RECT:
98 crop.x = (static_cast<const CameraRect *>(value))->x;
99 crop.y = (static_cast<const CameraRect *>(value))->y;
100 crop.w = (static_cast<const CameraRect *>(value))->w;
101 crop.h = (static_cast<const CameraRect *>(value))->h;
102 break;
103 default:
104 break;
105 }
106 }
107
FrameConfig(int32_t type)108 FrameConfig::FrameConfig(int32_t type) : type_(type)
109 {
110 crop.x = 0;
111 crop.y = 0;
112 crop.w = 0;
113 crop.h = 0;
114 SetParameter(CAM_IMAGE_CROP_RECT, crop);
115 SetParameter(CAM_IMAGE_INVERT_MODE, CAM_CENTER_MIRROR);
116 SetParameter(CAM_FRAME_FPS, DEFAULT_FPS);
117 if (type == FRAME_CONFIG_RECORD) {
118 SetParameter(CAM_IMAGE_FORMAT, CAM_FORMAT_H265);
119 } else if (type == FRAME_CONFIG_CAPTURE) {
120 SetParameter(CAM_IMAGE_FORMAT, CAM_FORMAT_JPEG);
121 } else {
122 SetParameter(CAM_IMAGE_FORMAT, CAM_FORMAT_YVU420);
123 }
124 }
125 } // namespace Media
126 } // namespace OHOS