• 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 
23 #include "camera_log.h"
24 #include "camera_util.h"
25 #include "display/graphic/common/v1_0/cm_color_space.h"
26 #include "display/composer/v1_1/display_composer_type.h"
27 #include "ipc_skeleton.h"
28 
29 namespace OHOS {
30 namespace CameraStandard {
31 using namespace OHOS::HDI::Camera::V1_0;
32 using namespace OHOS::HDI::Display::Graphic::Common::V1_0;
33 using namespace OHOS::HDI::Display::Composer::V1_1;
34 static const std::map<ColorSpace, CM_ColorSpaceType> g_fwkToMetaColorSpaceMap_ = {
35     {COLOR_SPACE_UNKNOWN, CM_COLORSPACE_NONE},
36     {DISPLAY_P3, CM_P3_FULL},
37     {SRGB, CM_SRGB_FULL},
38     {BT709, CM_BT709_FULL},
39     {BT2020_HLG, CM_BT2020_HLG_FULL},
40     {BT2020_PQ, CM_BT2020_PQ_FULL},
41     {P3_HLG, CM_P3_HLG_FULL},
42     {P3_PQ, CM_P3_PQ_FULL},
43     {DISPLAY_P3_LIMIT, CM_P3_LIMIT},
44     {SRGB_LIMIT, CM_SRGB_LIMIT},
45     {BT709_LIMIT, CM_BT709_LIMIT},
46     {BT2020_HLG_LIMIT, CM_BT2020_HLG_LIMIT},
47     {BT2020_PQ_LIMIT, CM_BT2020_PQ_LIMIT},
48     {P3_HLG_LIMIT, CM_P3_HLG_LIMIT},
49     {P3_PQ_LIMIT, CM_P3_PQ_LIMIT}
50 };
51 namespace {
52 static const int32_t STREAMID_BEGIN = 1;
53 static const int32_t CAPTUREID_BEGIN = 1;
54 static int32_t g_currentStreamId = STREAMID_BEGIN;
55 static std::set<int32_t> g_freeStreamIdQueue;
56 static std::mutex g_freeStreamQueueMutex;
57 
58 static std::atomic_int32_t g_currentCaptureId = CAPTUREID_BEGIN;
59 
GenerateStreamId()60 static int32_t GenerateStreamId()
61 {
62     std::lock_guard<std::mutex> lock(g_freeStreamQueueMutex);
63     int newId;
64     if (g_freeStreamIdQueue.empty()) {
65         newId = g_currentStreamId++;
66         if (newId == INT32_MAX) {
67             g_currentStreamId = STREAMID_BEGIN;
68         }
69         return newId;
70     }
71     auto firstIt = g_freeStreamIdQueue.begin();
72     newId = *firstIt;
73     g_freeStreamIdQueue.erase(firstIt);
74     return newId;
75 }
76 
FreeStreamId(int32_t streamId)77 static void FreeStreamId(int32_t streamId)
78 {
79     if (streamId == STREAM_ID_UNSET) {
80         return;
81     }
82     std::lock_guard<std::mutex> lock(g_freeStreamQueueMutex);
83     g_freeStreamIdQueue.emplace(streamId);
84 }
85 
GenerateCaptureId()86 static int32_t GenerateCaptureId()
87 {
88     int32_t newId = g_currentCaptureId++;
89     if (newId == INT32_MAX) {
90         g_currentCaptureId = CAPTUREID_BEGIN;
91     }
92     return newId;
93 }
94 } // namespace
95 
HStreamCommon(StreamType streamType,sptr<OHOS::IBufferProducer> producer,int32_t format,int32_t width,int32_t height)96 HStreamCommon::HStreamCommon(
97     StreamType streamType, sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height)
98 {
99     MEDIA_DEBUG_LOG("Enter Into HStreamCommon::HStreamCommon");
100     callerToken_ = IPCSkeleton::GetCallingTokenID();
101     streamId_ = streamType == StreamType::METADATA ? STREAM_ID_UNSET : GenerateStreamId();
102     curCaptureID_ = CAPTURE_ID_UNSET;
103     streamOperator_ = nullptr;
104     cameraAbility_ = nullptr;
105     producer_ = producer;
106     width_ = width;
107     height_ = height;
108     format_ = format;
109     streamType_ = streamType;
110     MEDIA_DEBUG_LOG("HStreamCommon Create streamId_ is %{public}d, streamType is:%{public}d", streamId_, streamType_);
111 }
112 
~HStreamCommon()113 HStreamCommon::~HStreamCommon()
114 {
115     MEDIA_DEBUG_LOG("Enter Into HStreamCommon::~HStreamCommon streamId is:%{public}d, streamType is:%{public}d",
116         streamId_, streamType_);
117     FreeStreamId(streamId_);
118     streamId_ = STREAM_ID_UNSET;
119 }
120 
SetColorSpace(ColorSpace colorSpace)121 void HStreamCommon::SetColorSpace(ColorSpace colorSpace)
122 {
123     auto itr = g_fwkToMetaColorSpaceMap_.find(colorSpace);
124     if (itr != g_fwkToMetaColorSpaceMap_.end()) {
125         dataSpace_ = itr->second;
126     } else {
127         MEDIA_ERR_LOG("HStreamCommon::SetColorSpace, %{public}d failed", static_cast<int32_t>(colorSpace));
128     }
129 }
130 
LinkInput(sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)131 int32_t HStreamCommon::LinkInput(sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,
132     std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)
133 {
134     if (streamOperator == nullptr || cameraAbility == nullptr) {
135         MEDIA_ERR_LOG("HStreamCommon::LinkInput streamOperator is null");
136         return CAMERA_INVALID_ARG;
137     }
138     if (!IsValidMode(cameraAbility)) {
139         return CAMERA_INVALID_SESSION_CFG;
140     }
141     if (!IsValidSize(cameraAbility, format_, width_, height_)) {
142         return CAMERA_INVALID_SESSION_CFG;
143     }
144     SetStreamOperator(streamOperator);
145     std::lock_guard<std::mutex> lock(cameraAbilityLock_);
146     cameraAbility_ = cameraAbility;
147     return CAMERA_OK;
148 }
149 
UnlinkInput()150 int32_t HStreamCommon::UnlinkInput()
151 {
152     MEDIA_INFO_LOG("HStreamCommon::UnlinkInput streamType:%{public}d, streamId:%{public}d", streamType_, streamId_);
153     StopStream();
154     SetStreamOperator(nullptr);
155     return CAMERA_OK;
156 }
157 
StopStream()158 int32_t HStreamCommon::StopStream()
159 {
160     CAMERA_SYNC_TRACE;
161     MEDIA_INFO_LOG("HStreamCommon::StopStream streamType:%{public}d, streamId:%{public}d, captureId:%{public}d",
162         streamType_, streamId_, curCaptureID_);
163     auto streamOperator = GetStreamOperator();
164     if (streamOperator == nullptr) {
165         MEDIA_DEBUG_LOG("HStreamCommon::StopStream streamOperator is nullptr");
166         return CAMERA_OK;
167     }
168 
169     if (curCaptureID_ != CAPTURE_ID_UNSET) {
170         CamRetCode rc = (CamRetCode)(streamOperator->CancelCapture(curCaptureID_));
171         if (rc != CamRetCode::NO_ERROR) {
172             MEDIA_ERR_LOG("HStreamCommon::StopStream streamOperator->CancelCapture get error code:%{public}d", rc);
173         }
174         ResetCaptureId();
175         return HdiToServiceError(rc);
176     }
177     return CAMERA_OK;
178 }
179 
PrepareCaptureId()180 int32_t HStreamCommon::PrepareCaptureId()
181 {
182     curCaptureID_ = GenerateCaptureId();
183     return CAMERA_OK;
184 }
185 
ResetCaptureId()186 void HStreamCommon::ResetCaptureId()
187 {
188     curCaptureID_ = CAPTURE_ID_UNSET;
189 }
190 
GetPreparedCaptureId()191 int32_t HStreamCommon::GetPreparedCaptureId()
192 {
193     return curCaptureID_;
194 }
195 
SetStreamInfo(StreamInfo_V1_1 & streamInfo)196 void HStreamCommon::SetStreamInfo(StreamInfo_V1_1 &streamInfo)
197 {
198     int32_t pixelFormat = OHOS::HDI::Display::Composer::V1_1::PIXEL_FMT_YCRCB_420_SP;
199     auto it = g_cameraToPixelFormat.find(format_);
200     if (it != g_cameraToPixelFormat.end()) {
201         pixelFormat = it->second;
202     } else {
203         MEDIA_ERR_LOG("HStreamCommon::SetStreamInfo find format error, pixelFormat use default format");
204     }
205     MEDIA_INFO_LOG("HStreamCommon::SetStreamInfo pixelFormat is %{public}d", pixelFormat);
206     streamInfo.v1_0.streamId_ = streamId_;
207     streamInfo.v1_0.width_ = width_;
208     streamInfo.v1_0.height_ = height_;
209     streamInfo.v1_0.format_ = pixelFormat;
210     streamInfo.v1_0.minFrameDuration_ = 0;
211     streamInfo.v1_0.tunneledMode_ = true;
212     {
213         std::lock_guard<std::mutex> lock(producerLock_);
214         if (producer_ != nullptr) {
215             MEDIA_INFO_LOG("HStreamCommon:producer is not null");
216             streamInfo.v1_0.bufferQueue_ = new BufferProducerSequenceable(producer_);
217         } else {
218             streamInfo.v1_0.bufferQueue_ = nullptr;
219         }
220     }
221     MEDIA_DEBUG_LOG("HStreamCommon::SetStreamInfo type %{public}d, dataSpace %{public}d", streamType_, dataSpace_);
222     streamInfo.v1_0.dataspace_ = dataSpace_;
223     streamInfo.extendedStreamInfos = {};
224 }
225 
ReleaseStream(bool isDelay)226 int32_t HStreamCommon::ReleaseStream(bool isDelay)
227 {
228     MEDIA_DEBUG_LOG(
229         "Enter Into HStreamCommon::Release streamId is:%{public}d, streamType is:%{public}d, isDelay:%{public}d",
230         streamId_, streamType_, isDelay);
231     StopStream();
232     if (!isDelay && streamId_ != STREAM_ID_UNSET) {
233         auto streamOperator = GetStreamOperator();
234         if (streamOperator != nullptr) {
235             streamOperator->ReleaseStreams({ streamId_ });
236         }
237     }
238     FreeStreamId(streamId_);
239     streamId_ = STREAM_ID_UNSET;
240     SetStreamOperator(nullptr);
241     {
242         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
243         cameraAbility_ = nullptr;
244     }
245     {
246         std::lock_guard<std::mutex> lock(producerLock_);
247         producer_ = nullptr;
248     }
249     return CAMERA_OK;
250 }
251 
DumpStreamInfo(std::string & dumpString)252 void HStreamCommon::DumpStreamInfo(std::string& dumpString)
253 {
254     StreamInfo_V1_1 curStreamInfo;
255     SetStreamInfo(curStreamInfo);
256     dumpString += "stream info: \n";
257     std::string bufferProducerId = "    Buffer producer Id:[";
258     {
259         std::lock_guard<std::mutex> lock(producerLock_);
260         if (curStreamInfo.v1_0.bufferQueue_ && curStreamInfo.v1_0.bufferQueue_->producer_) {
261             bufferProducerId += std::to_string(curStreamInfo.v1_0.bufferQueue_->producer_->GetUniqueId());
262         } else {
263             bufferProducerId += "empty";
264         }
265     }
266     dumpString += bufferProducerId;
267     dumpString += "]    stream Id:[" + std::to_string(curStreamInfo.v1_0.streamId_);
268     std::map<int, std::string>::const_iterator iter =
269         g_cameraFormat.find(format_);
270     if (iter != g_cameraFormat.end()) {
271         dumpString += "]    format:[" + iter->second;
272     }
273     dumpString += "]    width:[" + std::to_string(curStreamInfo.v1_0.width_);
274     dumpString += "]    height:[" + std::to_string(curStreamInfo.v1_0.height_);
275     dumpString += "]    dataspace:[" + std::to_string(curStreamInfo.v1_0.dataspace_);
276     dumpString += "]    StreamType:[" + std::to_string(curStreamInfo.v1_0.intent_);
277     dumpString += "]    TunnelMode:[" + std::to_string(curStreamInfo.v1_0.tunneledMode_);
278     dumpString += "]    Encoding Type:[" + std::to_string(curStreamInfo.v1_0.encodeType_) + "]:\n";
279     if (curStreamInfo.v1_0.bufferQueue_) {
280         delete curStreamInfo.v1_0.bufferQueue_;
281     }
282 }
283 } // namespace CameraStandard
284 } // namespace OHOS
285