• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 
16 #include "hstream_common.h"
17 
18 #include <atomic>
19 #include <cstdint>
20 #include <mutex>
21 #include <set>
22 #include <string>
23 
24 #include "camera_log.h"
25 #include "camera_util.h"
26 #include "display/graphic/common/v2_1/cm_color_space.h"
27 #include "display/composer/v1_1/display_composer_type.h"
28 #include "ipc_skeleton.h"
29 #include "camera_report_uitls.h"
30 #include "rotate_plugin/camera_rotate_plugin.h"
31 #include "camera_util.h"
32 #include "image_receiver.h"
33 
34 namespace OHOS {
35 namespace CameraStandard {
36 using namespace OHOS::HDI::Camera::V1_0;
37 using namespace OHOS::HDI::Display::Graphic::Common::V2_1;
38 using namespace OHOS::HDI::Display::Composer::V1_1;
39 using CM_ColorSpaceType_V2_1 = OHOS::HDI::Display::Graphic::Common::V2_1::CM_ColorSpaceType;
40 static const std::map<ColorSpace, CM_ColorSpaceType_V2_1> g_fwkToMetaColorSpaceMap_ = {
41     {COLOR_SPACE_UNKNOWN, CM_ColorSpaceType_V2_1::CM_COLORSPACE_NONE},
42     {DISPLAY_P3, CM_ColorSpaceType_V2_1::CM_P3_FULL},
43     {SRGB, CM_ColorSpaceType_V2_1::CM_SRGB_FULL},
44     {BT709, CM_ColorSpaceType_V2_1::CM_BT709_FULL},
45     {BT2020_HLG, CM_ColorSpaceType_V2_1::CM_BT2020_HLG_FULL},
46     {BT2020_PQ, CM_ColorSpaceType_V2_1::CM_BT2020_PQ_FULL},
47     {P3_HLG, CM_ColorSpaceType_V2_1::CM_P3_HLG_FULL},
48     {P3_PQ, CM_ColorSpaceType_V2_1::CM_P3_PQ_FULL},
49     {DISPLAY_P3_LIMIT, CM_ColorSpaceType_V2_1::CM_P3_LIMIT},
50     {SRGB_LIMIT, CM_ColorSpaceType_V2_1::CM_SRGB_LIMIT},
51     {BT709_LIMIT, CM_ColorSpaceType_V2_1::CM_BT709_LIMIT},
52     {BT2020_HLG_LIMIT, CM_ColorSpaceType_V2_1::CM_BT2020_HLG_LIMIT},
53     {BT2020_PQ_LIMIT, CM_ColorSpaceType_V2_1::CM_BT2020_PQ_LIMIT},
54     {P3_HLG_LIMIT, CM_ColorSpaceType_V2_1::CM_P3_HLG_LIMIT},
55     {P3_PQ_LIMIT, CM_ColorSpaceType_V2_1::CM_P3_PQ_LIMIT},
56 };
57 namespace {
58 static const int32_t STREAMID_BEGIN = 1;
59 static const int32_t CAPTUREID_BEGIN = 1;
60 static const int32_t STREAMID_MAX = INT32_MAX - 1000;
61 static const int32_t CAPTUREID_MAX = INT32_MAX - 1000;
62 static std::atomic<int32_t> g_currentStreamId = STREAMID_BEGIN;
63 
64 static std::atomic_int32_t g_currentCaptureId = CAPTUREID_BEGIN;
65 
GenerateStreamId()66 static int32_t GenerateStreamId()
67 {
68     int newId = g_currentStreamId++;
69     if (newId == STREAMID_MAX) {
70         g_currentStreamId = STREAMID_BEGIN;
71     }
72     return newId;
73 }
74 
GenerateCaptureId()75 static int32_t GenerateCaptureId()
76 {
77     int32_t newId = g_currentCaptureId++;
78     if (newId == CAPTUREID_MAX) {
79         g_currentCaptureId = CAPTUREID_BEGIN;
80     }
81     return newId;
82 }
83 } // namespace
84 
HStreamCommon(StreamType streamType,sptr<OHOS::IBufferProducer> producer,int32_t format,int32_t width,int32_t height)85 HStreamCommon::HStreamCommon(
86     StreamType streamType, sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height)
87     : format_(format), width_(width), height_(height), producer_(producer), streamType_(streamType)
88 {
89     MEDIA_DEBUG_LOG("Enter Into HStreamCommon::HStreamCommon");
90     callerToken_ = IPCSkeleton::GetCallingTokenID();
91     const int32_t metaStreamId = -1;
92     fwkStreamId_ = streamType == StreamType::METADATA ? metaStreamId : GenerateStreamId();
93     MEDIA_DEBUG_LOG("HStreamCommon Create streamId:%{public}d type:%{public}d width:%{public}d height:%{public}d"
94                     " format:%{public}d ",
95         fwkStreamId_, streamType_,  width_, height_, format_);
96 }
97 
HStreamCommon(StreamType streamType,int32_t format,int32_t width,int32_t height)98 HStreamCommon::HStreamCommon(
99     StreamType streamType, int32_t format, int32_t width, int32_t height)
100     : format_(format), width_(width), height_(height), streamType_(streamType)
101 {
102     MEDIA_DEBUG_LOG("Enter Into HStreamCommon::HStreamCommon");
103     callerToken_ = IPCSkeleton::GetCallingTokenID();
104     const int32_t metaStreamId = -1;
105     fwkStreamId_ = streamType == StreamType::METADATA ? metaStreamId : GenerateStreamId();
106     MEDIA_DEBUG_LOG("HStreamCommon Create streamId:%{public}d type:%{public}d width:%{public}d height:%{public}d"
107                     " format:%{public}d surfaceId:%{public}s",
108         fwkStreamId_, streamType_,  width_, height_, format_, surfaceId_.c_str());
109 }
110 
~HStreamCommon()111 HStreamCommon::~HStreamCommon()
112 {
113     MEDIA_DEBUG_LOG("Enter Into HStreamCommon::~HStreamCommon streamId is:%{public}d, streamType is:%{public}d",
114         fwkStreamId_, streamType_);
115     streamOperator_ = nullptr;
116 }
117 
SetColorSpace(ColorSpace colorSpace)118 void HStreamCommon::SetColorSpace(ColorSpace colorSpace)
119 {
120     auto itr = g_fwkToMetaColorSpaceMap_.find(colorSpace);
121     if (itr != g_fwkToMetaColorSpaceMap_.end()) {
122         dataSpace_ = itr->second;
123         MEDIA_INFO_LOG("HStreamCommon::SetColorSpace fwk colorSpace:%{public}d, HDI colorSpace: %{public}d", colorSpace,
124                        dataSpace_);
125     } else {
126         MEDIA_ERR_LOG("HStreamCommon::SetColorSpace, %{public}d failed", static_cast<int32_t>(colorSpace));
127     }
128 }
129 
GetColorSpace()130 ColorSpace HStreamCommon::GetColorSpace()
131 {
132     ColorSpace colorSpace = COLOR_SPACE_UNKNOWN;
133     for (const auto& iter: g_fwkToMetaColorSpaceMap_) {
134         if (iter.second == dataSpace_) {
135             colorSpace = iter.first;
136             break;
137         }
138     }
139     MEDIA_INFO_LOG("HStreamCommon::GetColorSpace fwk colorSpace:%{public}d", colorSpace);
140     return colorSpace;
141 }
142 
LinkInput(wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)143 int32_t HStreamCommon::LinkInput(wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,
144     std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)
145 {
146     CHECK_RETURN_RET_ELOG(streamOperator == nullptr || cameraAbility == nullptr, CAMERA_INVALID_ARG,
147         "HStreamCommon::LinkInput streamOperator is null");
148     SetStreamOperator(streamOperator);
149     std::lock_guard<std::mutex> lock(cameraAbilityLock_);
150     cameraAbility_ = cameraAbility;
151     return CAMERA_OK;
152 }
153 
UnlinkInput()154 int32_t HStreamCommon::UnlinkInput()
155 {
156     MEDIA_INFO_LOG("HStreamCommon::UnlinkInput streamType:%{public}d, streamId:%{public}d, hidStreamId:%{public}d",
157         streamType_, fwkStreamId_, hdiStreamId_);
158     StopStream();
159     SetStreamOperator(nullptr);
160     hdiStreamId_ = STREAM_ID_UNSET;
161     return CAMERA_OK;
162 }
163 
StopStream()164 int32_t HStreamCommon::StopStream()
165 {
166     CAMERA_SYNC_TRACE;
167     MEDIA_INFO_LOG("HStreamCommon::StopStream streamType:%{public}d, streamId:%{public}d, hdiStreamId:%{public}d, "
168         "captureId:%{public}d", streamType_, fwkStreamId_, hdiStreamId_, curCaptureID_);
169     auto streamOperator = GetStreamOperator();
170     if (streamOperator == nullptr) {
171         MEDIA_DEBUG_LOG("HStreamCommon::StopStream streamOperator is nullptr");
172         return CAMERA_OK;
173     }
174 
175     if (curCaptureID_ != CAPTURE_ID_UNSET) {
176         CamRetCode rc = (CamRetCode)(streamOperator->CancelCapture(curCaptureID_));
177         CHECK_PRINT_ELOG(rc != CamRetCode::NO_ERROR,
178             "HStreamCommon::StopStream streamOperator->CancelCapture get error code:%{public}d", rc);
179         ResetCaptureId();
180         return HdiToServiceError(rc);
181     }
182     return CAMERA_OK;
183 }
184 
PrepareCaptureId()185 int32_t HStreamCommon::PrepareCaptureId()
186 {
187     curCaptureID_ = GenerateCaptureId();
188     captureIdForConfirmCapture_ = curCaptureID_;
189     return CAMERA_OK;
190 }
191 
ResetCaptureId()192 void HStreamCommon::ResetCaptureId()
193 {
194     curCaptureID_ = CAPTURE_ID_UNSET;
195 }
196 
GetPreparedCaptureId()197 int32_t HStreamCommon::GetPreparedCaptureId()
198 {
199     return curCaptureID_;
200 }
201 
SetStreamInfo(StreamInfo_V1_1 & streamInfo)202 void HStreamCommon::SetStreamInfo(StreamInfo_V1_1 &streamInfo)
203 {
204     int32_t pixelFormat = OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_YCRCB_420_SP;
205     auto it = g_cameraToPixelFormat.find(format_);
206     if (it != g_cameraToPixelFormat.end()) {
207         pixelFormat = it->second;
208     } else {
209         MEDIA_ERR_LOG("HStreamCommon::SetStreamInfo find format error, pixelFormat use default format");
210     }
211     MEDIA_DEBUG_LOG("HStreamCommon::SetStreamInfo pixelFormat:%{public}d type:%{public}d colorSpace:%{public}d",
212         pixelFormat, streamType_, dataSpace_);
213     streamInfo.v1_0.streamId_ = hdiStreamId_;
214     streamInfo.v1_0.width_ = width_;
215     streamInfo.v1_0.height_ = height_;
216     streamInfo.v1_0.format_ = pixelFormat;
217     streamInfo.v1_0.dataspace_ = dataSpace_;
218     streamInfo.v1_0.minFrameDuration_ = 0;
219     streamInfo.v1_0.tunneledMode_ = true;
220     {
221         std::lock_guard<std::mutex> lock(producerLock_);
222         if (producer_ != nullptr) {
223             MEDIA_DEBUG_LOG("HStreamCommon:producer is not null");
224             streamInfo.v1_0.bufferQueue_ = new BufferProducerSequenceable(producer_);
225         } else if (surface_!= nullptr && surface_->GetProducer() != nullptr) {
226             MEDIA_DEBUG_LOG("HStreamCommon:surface & producer is not null");
227             streamInfo.v1_0.bufferQueue_ = new BufferProducerSequenceable(surface_->GetProducer());
228         } else {
229             streamInfo.v1_0.bufferQueue_ = nullptr;
230         }
231     }
232     streamInfo.extendedStreamInfos = {};
233 }
234 
ReleaseStream(bool isDelay)235 int32_t HStreamCommon::ReleaseStream(bool isDelay)
236 {
237     MEDIA_INFO_LOG("Enter Into HStreamCommon::Release streamId is:%{public}d, hdiStreamId is:%{public}d, streamType "
238         "is:%{public}d, isDelay:%{public}d",
239         fwkStreamId_, hdiStreamId_, streamType_, isDelay);
240     StopStream();
241     if (!isDelay && hdiStreamId_ != STREAM_ID_UNSET) {
242         auto streamOperator = GetStreamOperator();
243         if (streamOperator != nullptr) {
244             streamOperator->ReleaseStreams({ hdiStreamId_ });
245         }
246     }
247     fwkStreamId_ = STREAM_ID_UNSET;
248     hdiStreamId_ = STREAM_ID_UNSET;
249     SetStreamOperator(nullptr);
250     {
251         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
252         cameraAbility_ = nullptr;
253     }
254     {
255         std::lock_guard<std::mutex> lock(producerLock_);
256         producer_ = nullptr;
257     }
258     return CAMERA_OK;
259 }
260 
DumpStreamInfo(CameraInfoDumper & infoDumper)261 void HStreamCommon::DumpStreamInfo(CameraInfoDumper& infoDumper)
262 {
263     StreamInfo_V1_1 curStreamInfo;
264     SetStreamInfo(curStreamInfo);
265     std::string streamInfo = "Buffer producer Id:";
266     {
267         std::lock_guard<std::mutex> lock(producerLock_);
268         if (curStreamInfo.v1_0.bufferQueue_ && curStreamInfo.v1_0.bufferQueue_->producer_) {
269             streamInfo.append("[" + std::to_string(curStreamInfo.v1_0.bufferQueue_->producer_->GetUniqueId()) + "]");
270         } else {
271             streamInfo.append("[empty]");
272         }
273     }
274     streamInfo.append("    stream Id:[" + std::to_string(curStreamInfo.v1_0.streamId_) + "]");
275     std::map<int, std::string>::const_iterator iter = g_cameraFormat.find(format_);
276     CHECK_EXECUTE(iter != g_cameraFormat.end(), streamInfo.append("    format:[" + iter->second + "]"));
277     streamInfo.append("  width:[" + std::to_string(curStreamInfo.v1_0.width_) + "]");
278     streamInfo.append("  height:[" + std::to_string(curStreamInfo.v1_0.height_) + "]");
279     streamInfo.append("  dataspace:[" + std::to_string(curStreamInfo.v1_0.dataspace_) + "]");
280     streamInfo.append("  StreamType:[" + std::to_string(curStreamInfo.v1_0.intent_) + "]");
281     streamInfo.append("  TunnelMode:[" + std::to_string(curStreamInfo.v1_0.tunneledMode_) + "]");
282     streamInfo.append("  Encoding Type:[" + std::to_string(curStreamInfo.v1_0.encodeType_) + "]");
283 
284     infoDumper.Msg(streamInfo);
285     infoDumper.Push();
286     infoDumper.Title("Stream Extened Info");
287     for (auto& extInfo : curStreamInfo.extendedStreamInfos) {
288         auto bufferQueue = extInfo.bufferQueue;
289         infoDumper.Msg("type:" + std::to_string(static_cast<int32_t>(extInfo.type)) +
290                        "  width:" + std::to_string(static_cast<int32_t>(extInfo.width)) +
291                        "  height:" + std::to_string(static_cast<int32_t>(extInfo.height)) +
292                        "  format:" + std::to_string(static_cast<int32_t>(extInfo.format)) +
293                        "  dataspace:" + std::to_string(static_cast<int32_t>(extInfo.dataspace)) +
294                        "  producer:" + ((bufferQueue == nullptr || bufferQueue->producer_ == nullptr)
295                                ? "empty"
296                                : std::to_string(bufferQueue->producer_->GetUniqueId())));
297     }
298     infoDumper.Pop();
299 }
300 
PrintCaptureDebugLog(const std::shared_ptr<OHOS::Camera::CameraMetadata> & captureMetadataSetting_)301 void HStreamCommon::PrintCaptureDebugLog(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting_)
302 {
303     CHECK_RETURN_ELOG(captureMetadataSetting_ == nullptr,
304         "HStreamCapture::PrintCaptureDebugLog captureMetadataSetting_ is nullptr");
305     camera_metadata_item_t item;
306     int result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_QUALITY, &item);
307     if (result != CAM_META_SUCCESS) {
308         MEDIA_DEBUG_LOG("HStreamCapture::Failed to find OHOS_JPEG_QUALITY tag");
309     } else {
310         MEDIA_DEBUG_LOG("HStreamCapture::find OHOS_JPEG_QUALITY value = %{public}d", item.data.u8[0]);
311         CameraReportUtils::GetInstance().UpdateImagingInfo(DFX_PHOTO_SETTING_QUALITY, std::to_string(item.data.u8[0]));
312     }
313 
314     // debug log for capture mirror
315     result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_CONTROL_CAPTURE_MIRROR, &item);
316     if (result != CAM_META_SUCCESS) {
317         MEDIA_DEBUG_LOG("HStreamCapture::Failed to find OHOS_CONTROL_CAPTURE_MIRROR tag");
318     } else {
319         MEDIA_DEBUG_LOG("HStreamCapture::find OHOS_CONTROL_CAPTURE_MIRROR value = %{public}d", item.data.u8[0]);
320         CameraReportUtils::GetInstance().UpdateImagingInfo(DFX_PHOTO_SETTING_MIRROR, std::to_string(item.data.u8[0]));
321     }
322 
323     // debug log for image rotation
324     result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_JPEG_ORIENTATION, &item);
325     if (result != CAM_META_SUCCESS) {
326         MEDIA_DEBUG_LOG("HStreamCapture::Failed to find OHOS_JPEG_ORIENTATION tag");
327     } else {
328         MEDIA_DEBUG_LOG("HStreamCapture::find OHOS_JPEG_ORIENTATION value = %{public}d", item.data.i32[0]);
329         CameraReportUtils::GetInstance().UpdateImagingInfo(DFX_PHOTO_SETTING_ROTATION,
330             std::to_string(item.data.i32[0]));
331     }
332 
333     // debug log for video frame rate range
334     CallerInfo caller = CameraReportUtils::GetCallerInfo();
335     result = OHOS::Camera::FindCameraMetadataItem(captureMetadataSetting_->get(), OHOS_CONTROL_FPS_RANGES, &item);
336     if (result != CAM_META_SUCCESS) {
337         MEDIA_DEBUG_LOG("HStreamCapture::Failed to find OHOS_CONTROL_FPS_RANGES tag");
338     } else {
339         MEDIA_DEBUG_LOG("HStreamCapture::find OHOS_CONTROL_FPS_RANGES value = %{public}d",
340             item.data.i32[0]);
341         CameraReportUtils::GetInstance().ReportUserBehavior(DFX_UB_SET_FRAMERATERANGE,
342             std::to_string(item.data.i32[0]), caller);
343     }
344 }
345 
SetBasicInfo(std::map<int32_t,std::string> basicParam)346 void HStreamCommon::SetBasicInfo(std::map<int32_t, std::string> basicParam)
347 {
348     std::lock_guard<std::mutex> lock(basicInfoLock_);
349     param = basicParam;
350 }
351 } // namespace CameraStandard
352 } // namespace OHOS
353