• 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 "camera_util.h"
19 #include "camera_log.h"
20 
21 namespace OHOS {
22 namespace CameraStandard {
HStreamCommon(StreamType streamType,sptr<OHOS::IBufferProducer> producer,int32_t format,int32_t width,int32_t height)23 HStreamCommon::HStreamCommon(StreamType streamType, sptr<OHOS::IBufferProducer> producer,
24                              int32_t format, int32_t width, int32_t height)
25 {
26     MEDIA_DEBUG_LOG("Enter Into HStreamCommon::HStreamCommon");
27     streamId_ = 0;
28     curCaptureID_ = 0;
29     isReleaseStream_ = false;
30     streamOperator_ = nullptr;
31     cameraAbility_ = nullptr;
32     producer_ = producer;
33     width_ = width;
34     height_ = height;
35     format_ = format;
36     streamType_ = streamType;
37 }
38 
~HStreamCommon()39 HStreamCommon::~HStreamCommon()
40 {}
41 
SetReleaseStream(bool isReleaseStream)42 int32_t HStreamCommon::SetReleaseStream(bool isReleaseStream)
43 {
44     isReleaseStream_ = isReleaseStream;
45     return CAMERA_OK;
46 }
47 
IsReleaseStream()48 bool HStreamCommon::IsReleaseStream()
49 {
50     return isReleaseStream_;
51 }
52 
GetStreamId()53 int32_t HStreamCommon::GetStreamId()
54 {
55     return streamId_;
56 }
57 
GetStreamType()58 StreamType HStreamCommon::GetStreamType()
59 {
60     return streamType_;
61 }
62 
LinkInput(sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator,std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility,int32_t streamId)63 int32_t HStreamCommon::LinkInput(sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator,
64                                  std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility, int32_t streamId)
65 {
66     if (streamOperator == nullptr || cameraAbility == nullptr) {
67         MEDIA_ERR_LOG("HStreamCommon::LinkInput streamOperator is null");
68         return CAMERA_INVALID_ARG;
69     }
70     if (!IsValidSize(cameraAbility, format_, width_, height_)) {
71         return CAMERA_INVALID_SESSION_CFG;
72     }
73     streamId_ = streamId;
74     MEDIA_DEBUG_LOG("HStreamCommon::LinkInput streamId_ is %{public}d", streamId_);
75     {
76         std::lock_guard<std::mutex> lock(streamOperatorLock_);
77         streamOperator_ = streamOperator;
78     }
79     std::lock_guard<std::mutex> lock(cameraAbilityLock_);
80     cameraAbility_ = cameraAbility;
81     return CAMERA_OK;
82 }
83 
SetStreamInfo(StreamInfo_V1_1 & streamInfo)84 void HStreamCommon::SetStreamInfo(StreamInfo_V1_1 &streamInfo)
85 {
86     int32_t pixelFormat = PIXEL_FMT_YCRCB_420_SP;
87     auto it = g_cameraToPixelFormat.find(format_);
88     if (it != g_cameraToPixelFormat.end()) {
89         pixelFormat = it->second;
90     } else {
91         MEDIA_ERR_LOG("HStreamCommon::SetStreamInfo find format error, pixelFormat use default format");
92     }
93     MEDIA_INFO_LOG("HStreamCommon::SetStreamInfo pixelFormat is %{public}d", pixelFormat);
94     streamInfo.v1_0.streamId_ = streamId_;
95     streamInfo.v1_0.width_ = width_;
96     streamInfo.v1_0.height_ = height_;
97     streamInfo.v1_0.format_ = pixelFormat;
98     streamInfo.v1_0.minFrameDuration_ = 0;
99     streamInfo.v1_0.tunneledMode_ = true;
100     if (producer_ != nullptr) {
101         MEDIA_INFO_LOG("HStreamCommon:producer is not null");
102         streamInfo.v1_0.bufferQueue_ = new BufferProducerSequenceable(producer_);
103     } else {
104         streamInfo.v1_0.bufferQueue_ = nullptr;
105     }
106     streamInfo.v1_0.dataspace_ = CAMERA_COLOR_SPACE;
107     streamInfo.extendedStreamInfos = {};
108 }
109 
Release()110 int32_t HStreamCommon::Release()
111 {
112     MEDIA_DEBUG_LOG("Enter Into HStreamCommon::Release");
113     streamId_ = 0;
114     curCaptureID_ = 0;
115     {
116         std::lock_guard<std::mutex> lock(streamOperatorLock_);
117         streamOperator_ = nullptr;
118     }
119     {
120         std::lock_guard<std::mutex> lock(cameraAbilityLock_);
121         cameraAbility_ = nullptr;
122     }
123     {
124         std::lock_guard<std::mutex> lock(producerLock_);
125         producer_ = nullptr;
126     }
127     return CAMERA_OK;
128 }
129 
DumpStreamInfo(std::string & dumpString)130 void HStreamCommon::DumpStreamInfo(std::string& dumpString)
131 {
132     StreamInfo_V1_1 curStreamInfo;
133     SetStreamInfo(curStreamInfo);
134     dumpString += "release status:[" + std::to_string(isReleaseStream_) + "]:\n";
135     dumpString += "stream info: \n";
136     std::string bufferProducerId = "    Buffer producer Id:[";
137     if (curStreamInfo.v1_0.bufferQueue_ && curStreamInfo.v1_0.bufferQueue_->producer_) {
138         bufferProducerId += std::to_string(curStreamInfo.v1_0.bufferQueue_->producer_->GetUniqueId());
139     } else {
140         bufferProducerId += "empty";
141     }
142     dumpString += bufferProducerId;
143     dumpString += "]    stream Id:[" + std::to_string(curStreamInfo.v1_0.streamId_);
144     std::map<int, std::string>::const_iterator iter =
145         g_cameraFormat.find(format_);
146     if (iter != g_cameraFormat.end()) {
147         dumpString += "]    format:[" + iter->second;
148     }
149     dumpString += "]    width:[" + std::to_string(curStreamInfo.v1_0.width_);
150     dumpString += "]    height:[" + std::to_string(curStreamInfo.v1_0.height_);
151     dumpString += "]    dataspace:[" + std::to_string(curStreamInfo.v1_0.dataspace_);
152     dumpString += "]    StreamType:[" + std::to_string(curStreamInfo.v1_0.intent_);
153     dumpString += "]    TunnelMode:[" + std::to_string(curStreamInfo.v1_0.tunneledMode_);
154     dumpString += "]    Encoding Type:[" + std::to_string(curStreamInfo.v1_0.encodeType_) + "]:\n";
155     if (curStreamInfo.v1_0.bufferQueue_) {
156         delete curStreamInfo.v1_0.bufferQueue_;
157     }
158 }
159 } // namespace CameraStandard
160 } // namespace OHOS
161