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