1 /*
2  * Copyright (c) 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 "image_receiver_impl.h"
16 #include "image_log.h"
17 #include "securec.h"
18 #include "cj_color_mgr_utils.h"
19 
20 namespace OHOS {
21 namespace Media {
22 
23 const int32_t CAMERA_APP_INNER_ENCODING_FORMAT = 4;
24 const int32_t JPEG_ENCODING_FORMAT = 2000;
25 
26 struct ImageEnum {
27     std::string name;
28     int32_t numVal;
29     std::string strVal;
30 };
31 
32 static std::vector<struct ImageEnum> sImageFormatMap = {
33     {"CAMERA_APP_INNER", CAMERA_APP_INNER_ENCODING_FORMAT, ""},
34     {"JPEG", JPEG_ENCODING_FORMAT, ""},
35 };
36 
CheckFormat(int32_t format)37 static bool CheckFormat(int32_t format)
38 {
39     for (auto imgEnum : sImageFormatMap) {
40         if (imgEnum.numVal == format) {
41             return true;
42         }
43     }
44     return false;
45 }
46 
CreateImageReceiver(int32_t width,int32_t height,int32_t format,int32_t capacity)47 int64_t ImageReceiverImpl::CreateImageReceiver(int32_t width, int32_t height, int32_t format, int32_t capacity)
48 {
49     IMAGE_LOGD("[ImageReceiver] Create.");
50     if (!CheckFormat(format)) {
51         IMAGE_LOGE("[ImageReceiverImpl] Invailed param.");
52         return INIT_FAILED;
53     }
54     std::shared_ptr imageReceiver = ImageReceiver::CreateImageReceiver(width, height, format, capacity);
55     if (imageReceiver == nullptr) {
56         IMAGE_LOGE("[ImageReceiverImpl] Failed to create native ImageReceiver.");
57         return INIT_FAILED;
58     }
59     auto receiverImpl = FFIData::Create<ImageReceiverImpl>(imageReceiver);
60     return receiverImpl->GetID();
61 }
62 
ImageReceiverImpl(std::shared_ptr<ImageReceiver> imageReceiver)63 ImageReceiverImpl::ImageReceiverImpl(std::shared_ptr<ImageReceiver> imageReceiver)
64 {
65     imageReceiver_ = imageReceiver;
66 }
67 
GetSize(CSize * ret)68 uint32_t ImageReceiverImpl::GetSize(CSize *ret)
69 {
70     if (imageReceiver_ == nullptr || imageReceiver_->iraContext_ == nullptr) {
71         IMAGE_LOGE("[ImageReceiverImpl] GetSize : Image receiver context is nullptr");
72         return ERR_IMAGE_INIT_ABNORMAL;
73     }
74 
75     ret->width = imageReceiver_->iraContext_->GetWidth();
76     ret->height = imageReceiver_->iraContext_->GetHeight();
77     return SUCCESS;
78 }
79 
GetCapacity(int32_t * ret)80 uint32_t ImageReceiverImpl::GetCapacity(int32_t *ret)
81 {
82     if (imageReceiver_ == nullptr || imageReceiver_->iraContext_ == nullptr) {
83         IMAGE_LOGE("[ImageReceiverImpl] GetCapacity : Image receiver context is nullptr");
84         return ERR_IMAGE_INIT_ABNORMAL;
85     }
86 
87     *ret = imageReceiver_->iraContext_->GetCapicity();
88     return SUCCESS;
89 }
90 
GetFormat(int32_t * ret)91 uint32_t ImageReceiverImpl::GetFormat(int32_t *ret)
92 {
93     if (imageReceiver_ == nullptr || imageReceiver_->iraContext_ == nullptr) {
94         IMAGE_LOGE("[ImageReceiverImpl] GetFormat : Image receiver context is nullptr");
95         return ERR_IMAGE_INIT_ABNORMAL;
96     }
97 
98     *ret = imageReceiver_->iraContext_->GetFormat();
99     return SUCCESS;
100 }
101 
GetReceivingSurfaceId()102 char *ImageReceiverImpl::GetReceivingSurfaceId()
103 {
104     if (imageReceiver_ == nullptr) {
105         return nullptr;
106     }
107     std::shared_ptr<ImageReceiverContext> iraContext = imageReceiver_->iraContext_;
108     if (iraContext == nullptr) {
109         return nullptr;
110     }
111 
112     auto str = iraContext->GetReceiverKey().c_str();
113     char *newStr = Utils::MallocCString(str);
114     return newStr;
115 }
116 
ReadNextImage()117 sptr<ImageImpl> ImageReceiverImpl::ReadNextImage()
118 {
119     if (imageReceiver_ == nullptr) {
120         return nullptr;
121     }
122     auto image = imageReceiver_->NextNativeImage();
123     if (image == nullptr) {
124         IMAGE_LOGE("NextNativeImage is nullptr");
125         return nullptr;
126     }
127     auto imageImpl = FFIData::Create<ImageImpl>(image);
128     if (imageImpl == nullptr) {
129         IMAGE_LOGE("ImageImpl Create is nullptr");
130         return nullptr;
131     }
132     return imageImpl;
133 }
134 
ReadLatestImage()135 sptr<ImageImpl> ImageReceiverImpl::ReadLatestImage()
136 {
137     if (imageReceiver_ == nullptr) {
138         return nullptr;
139     }
140     auto image = imageReceiver_->LastNativeImage();
141     if (image == nullptr) {
142         IMAGE_LOGE("LastNativeImage is nullptr.");
143         return nullptr;
144     }
145     return FFIData::Create<ImageImpl>(image);
146 }
147 
Release()148 void ImageReceiverImpl::Release()
149 {
150     imageReceiver_ = nullptr;
151 }
152 }
153 }