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