• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 <iostream>
17 
18 #include "camera_util.h"
19 #include "media_log.h"
20 #include "hstream_capture.h"
21 
22 namespace OHOS {
23 namespace CameraStandard {
24 int32_t HStreamCapture::photoCaptureId_ = PHOTO_CAPTURE_ID_START;
25 
HStreamCapture(sptr<OHOS::IBufferProducer> producer,int32_t format)26 HStreamCapture::HStreamCapture(sptr<OHOS::IBufferProducer> producer, int32_t format)
27 {
28     producer_ = producer;
29     format_ = format;
30     photoStreamId_ = 0;
31     isReleaseStream_ = false;
32 }
33 
~HStreamCapture()34 HStreamCapture::~HStreamCapture()
35 {}
36 
LinkInput(sptr<Camera::IStreamOperator> streamOperator,std::shared_ptr<Camera::CameraMetadata> cameraAbility,int32_t streamId)37 int32_t HStreamCapture::LinkInput(sptr<Camera::IStreamOperator> streamOperator,
38     std::shared_ptr<Camera::CameraMetadata> cameraAbility, int32_t streamId)
39 {
40     if (streamOperator == nullptr || cameraAbility == nullptr) {
41         MEDIA_ERR_LOG("HStreamCapture::LinkInput streamOperator is null");
42         return CAMERA_INVALID_ARG;
43     }
44     if (!IsValidSize(cameraAbility, format_, producer_->GetDefaultWidth(), producer_->GetDefaultHeight())) {
45         return CAMERA_INVALID_SESSION_CFG;
46     }
47     streamOperator_ = streamOperator;
48     photoStreamId_ = streamId;
49     cameraAbility_ = cameraAbility;
50     return CAMERA_OK;
51 }
52 
SetStreamInfo(std::shared_ptr<Camera::StreamInfo> streamInfoPhoto)53 void HStreamCapture::SetStreamInfo(std::shared_ptr<Camera::StreamInfo> streamInfoPhoto)
54 {
55     int32_t pixelFormat;
56     auto it = g_cameraToPixelFormat.find(format_);
57     if (it != g_cameraToPixelFormat.end()) {
58         pixelFormat = it->second;
59     } else {
60 #ifdef RK_CAMERA
61         pixelFormat = PIXEL_FMT_RGBA_8888;
62 #else
63         pixelFormat = PIXEL_FMT_YCRCB_420_SP;
64 #endif
65     }
66     MEDIA_INFO_LOG("HStreamCapture::SetStreamInfo pixelFormat is %{public}d", pixelFormat);
67     streamInfoPhoto->streamId_ = photoStreamId_;
68     streamInfoPhoto->width_ = producer_->GetDefaultWidth();
69     streamInfoPhoto->height_ = producer_->GetDefaultHeight();
70     streamInfoPhoto->format_ = pixelFormat;
71     streamInfoPhoto->datasapce_ = CAMERA_PHOTO_COLOR_SPACE;
72     streamInfoPhoto->intent_ = Camera::STILL_CAPTURE;
73     streamInfoPhoto->tunneledMode_ = true;
74     streamInfoPhoto->bufferQueue_ = producer_;
75     streamInfoPhoto->encodeType_ = Camera::ENCODE_TYPE_JPEG;
76 }
77 
SetReleaseStream(bool isReleaseStream)78 int32_t HStreamCapture::SetReleaseStream(bool isReleaseStream)
79 {
80     isReleaseStream_ = isReleaseStream;
81     return CAMERA_OK;
82 }
83 
IsReleaseStream()84 bool HStreamCapture::IsReleaseStream()
85 {
86     return isReleaseStream_;
87 }
88 
IsValidCaptureID()89 bool HStreamCapture::IsValidCaptureID()
90 {
91     return (photoCaptureId_ >= PHOTO_CAPTURE_ID_START && photoCaptureId_ <= PHOTO_CAPTURE_ID_END);
92 }
93 
Capture(const std::shared_ptr<Camera::CameraMetadata> & captureSettings)94 int32_t HStreamCapture::Capture(const std::shared_ptr<Camera::CameraMetadata> &captureSettings)
95 {
96     Camera::CamRetCode rc = Camera::NO_ERROR;
97     int32_t CurCaptureId = 0;
98 
99     if (streamOperator_ == nullptr) {
100         return CAMERA_INVALID_STATE;
101     }
102     if (!IsValidCaptureID()) {
103         MEDIA_ERR_LOG("HStreamCapture::Capture crossed the allowed limit, CurCaptureId: %{public}d", photoCaptureId_);
104         return CAMERA_CAPTURE_LIMIT_EXCEED;
105     }
106     CurCaptureId = photoCaptureId_;
107     photoCaptureId_++;
108     std::shared_ptr<Camera::CaptureInfo> captureInfoPhoto = std::make_shared<Camera::CaptureInfo>();
109     captureInfoPhoto->streamIds_ = {photoStreamId_};
110     if (!Camera::GetCameraMetadataItemCount(captureSettings->get())) {
111         captureInfoPhoto->captureSetting_ = cameraAbility_;
112     } else {
113         captureInfoPhoto->captureSetting_ = captureSettings;
114     }
115     captureInfoPhoto->enableShutterCallback_ = true;
116 
117     MEDIA_INFO_LOG("HStreamCapture::Capture() Starting photo capture with capture ID: %{public}d", CurCaptureId);
118     rc = streamOperator_->Capture(CurCaptureId, captureInfoPhoto, false);
119     if (rc != Camera::NO_ERROR) {
120         MEDIA_ERR_LOG("HStreamCapture::Capture failed with error Code: %{public}d", rc);
121         return HdiToServiceError(rc);
122     }
123     return CAMERA_OK;
124 }
125 
CancelCapture()126 int32_t HStreamCapture::CancelCapture()
127 {
128     // Cancel cature is dummy till continuous/burst mode is supported
129     return CAMERA_OK;
130 }
131 
Release()132 int32_t HStreamCapture::Release()
133 {
134     streamCaptureCallback_ = nullptr;
135     streamOperator_ = nullptr;
136     photoStreamId_ = 0;
137     cameraAbility_ = nullptr;
138     return CAMERA_OK;
139 }
140 
SetCallback(sptr<IStreamCaptureCallback> & callback)141 int32_t HStreamCapture::SetCallback(sptr<IStreamCaptureCallback> &callback)
142 {
143     if (callback == nullptr) {
144         MEDIA_ERR_LOG("HStreamCapture::SetCallback callback is null");
145         return CAMERA_INVALID_ARG;
146     }
147     streamCaptureCallback_ = callback;
148     return CAMERA_OK;
149 }
150 
OnCaptureStarted(int32_t captureId)151 int32_t HStreamCapture::OnCaptureStarted(int32_t captureId)
152 {
153     if (streamCaptureCallback_ != nullptr) {
154         streamCaptureCallback_->OnCaptureStarted(captureId);
155     }
156     return CAMERA_OK;
157 }
158 
OnCaptureEnded(int32_t captureId,int32_t frameCount)159 int32_t HStreamCapture::OnCaptureEnded(int32_t captureId, int32_t frameCount)
160 {
161     if (streamCaptureCallback_ != nullptr) {
162         streamCaptureCallback_->OnCaptureEnded(captureId, frameCount);
163     }
164     return CAMERA_OK;
165 }
166 
OnCaptureError(int32_t captureId,int32_t errorCode)167 int32_t HStreamCapture::OnCaptureError(int32_t captureId, int32_t errorCode)
168 {
169     if (streamCaptureCallback_ != nullptr) {
170         if (errorCode == Camera::BUFFER_LOST) {
171             streamCaptureCallback_->OnCaptureError(captureId, CAMERA_STREAM_BUFFER_LOST);
172         } else {
173             streamCaptureCallback_->OnCaptureError(captureId, CAMERA_UNKNOWN_ERROR);
174         }
175     }
176     return CAMERA_OK;
177 }
178 
OnFrameShutter(int32_t captureId,uint64_t timestamp)179 int32_t HStreamCapture::OnFrameShutter(int32_t captureId, uint64_t timestamp)
180 {
181     if (streamCaptureCallback_ != nullptr) {
182         streamCaptureCallback_->OnFrameShutter(captureId, timestamp);
183     }
184     return CAMERA_OK;
185 }
186 
GetStreamId()187 int32_t HStreamCapture::GetStreamId()
188 {
189     return photoStreamId_;
190 }
191 
ResetCaptureId()192 void HStreamCapture::ResetCaptureId()
193 {
194     photoCaptureId_ = PHOTO_CAPTURE_ID_START;
195 }
196 
dumpCaptureStreamInfo(std::string & dumpString)197 void HStreamCapture::dumpCaptureStreamInfo(std::string& dumpString)
198 {
199     std::shared_ptr<Camera::StreamInfo> curStreamInfo;
200     curStreamInfo = std::make_shared<Camera::StreamInfo>();
201     SetStreamInfo(curStreamInfo);
202     dumpString += "photo stream info: \n";
203     dumpString += "    Buffer producer Id:[" + std::to_string(curStreamInfo->bufferQueue_->GetUniqueId());
204     dumpString += "]    stream Id:[" + std::to_string(curStreamInfo->streamId_);
205     std::map<int, std::string>::const_iterator iter =
206         g_cameraFormat.find(format_);
207     if (iter != g_cameraFormat.end()) {
208         dumpString += "]    format:[" + iter->second;
209     }
210     dumpString += "]    width:[" + std::to_string(curStreamInfo->width_);
211     dumpString += "]    height:[" + std::to_string(curStreamInfo->height_);
212     dumpString += "]    dataspace:[" + std::to_string(curStreamInfo->datasapce_);
213     dumpString += "]    StreamType:[" + std::to_string(curStreamInfo->intent_);
214     dumpString += "]    TunnelMode:[" + std::to_string(curStreamInfo->tunneledMode_);
215     dumpString += "]    Encoding Type:[" + std::to_string(curStreamInfo->encodeType_) + "]:\n";
216 }
217 } // namespace CameraStandard
218 } // namespace OHOS
219