• 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_repeat.h"
21 
22 namespace OHOS {
23 namespace CameraStandard {
24 int32_t HStreamRepeat::videoCaptureId_ = VIDEO_CAPTURE_ID_START;
25 int32_t HStreamRepeat::previewCaptureId_ = PREVIEW_CAPTURE_ID_START;
26 
HStreamRepeat(sptr<OHOS::IBufferProducer> producer,int32_t format)27 HStreamRepeat::HStreamRepeat(sptr<OHOS::IBufferProducer> producer, int32_t format)
28 {
29     producer_ = producer;
30     format_ = format;
31     isVideo_ = false;
32     streamId_ = 0;
33     customPreviewWidth_ = 0;
34     customPreviewHeight_ = 0;
35     curCaptureID_ = 0;
36     isReleaseStream_ = false;
37 }
38 
HStreamRepeat(sptr<OHOS::IBufferProducer> producer,int32_t format,int32_t width,int32_t height)39 HStreamRepeat::HStreamRepeat(sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height)
40     : HStreamRepeat(producer, format)
41 {
42     customPreviewWidth_ = width;
43     customPreviewHeight_ = height;
44 }
45 
HStreamRepeat(sptr<OHOS::IBufferProducer> producer,int32_t format,bool isVideo)46 HStreamRepeat::HStreamRepeat(sptr<OHOS::IBufferProducer> producer, int32_t format, bool isVideo)
47     : HStreamRepeat(producer, format)
48 {
49     isVideo_ = isVideo;
50 }
51 
~HStreamRepeat()52 HStreamRepeat::~HStreamRepeat()
53 {}
54 
LinkInput(sptr<Camera::IStreamOperator> streamOperator,std::shared_ptr<Camera::CameraMetadata> cameraAbility,int32_t streamId)55 int32_t HStreamRepeat::LinkInput(sptr<Camera::IStreamOperator> streamOperator,
56                                  std::shared_ptr<Camera::CameraMetadata> cameraAbility, int32_t streamId)
57 {
58     int32_t previewWidth = 0;
59     int32_t previewHeight = 0;
60 
61     if (streamOperator == nullptr || cameraAbility == nullptr) {
62         MEDIA_ERR_LOG("HStreamRepeat::LinkInput streamOperator is null");
63         return CAMERA_INVALID_ARG;
64     }
65     if (isVideo_) {
66         if (!IsValidSize(cameraAbility, format_, producer_->GetDefaultWidth(), producer_->GetDefaultHeight())) {
67             return CAMERA_INVALID_SESSION_CFG;
68         }
69     } else {
70         previewWidth = (customPreviewWidth_ == 0) ? producer_->GetDefaultWidth() : customPreviewWidth_;
71         previewHeight =  (customPreviewHeight_ == 0) ? producer_->GetDefaultHeight() : customPreviewHeight_;
72         if (!IsValidSize(cameraAbility, format_, previewWidth, previewHeight)) {
73             return CAMERA_INVALID_SESSION_CFG;
74         }
75     }
76     streamId_ = streamId;
77     streamOperator_ = streamOperator;
78     cameraAbility_ = cameraAbility;
79     return CAMERA_OK;
80 }
81 
SetStreamInfo(std::shared_ptr<Camera::StreamInfo> streamInfo)82 void HStreamRepeat::SetStreamInfo(std::shared_ptr<Camera::StreamInfo> streamInfo)
83 {
84     int32_t pixelFormat;
85     auto it = g_cameraToPixelFormat.find(format_);
86     if (it != g_cameraToPixelFormat.end()) {
87         pixelFormat = it->second;
88     } else {
89 #ifdef RK_CAMERA
90         pixelFormat = PIXEL_FMT_RGBA_8888;
91 #else
92         pixelFormat = PIXEL_FMT_YCRCB_420_SP;
93 #endif
94     }
95     MEDIA_INFO_LOG("HStreamRepeat::SetStreamInfo pixelFormat is %{public}d", pixelFormat);
96     streamInfo->format_ = pixelFormat;
97     streamInfo->tunneledMode_ = true;
98     streamInfo->datasapce_ = CAMERA_PREVIEW_COLOR_SPACE;
99     streamInfo->bufferQueue_ = producer_;
100     streamInfo->width_ = (customPreviewWidth_ == 0) ? producer_->GetDefaultWidth() : customPreviewWidth_;
101     streamInfo->height_ = (customPreviewHeight_ == 0) ? producer_->GetDefaultHeight() : customPreviewHeight_;
102     streamInfo->streamId_ = streamId_;
103     if (isVideo_) {
104         streamInfo->intent_ = Camera::VIDEO;
105         streamInfo->encodeType_ = Camera::ENCODE_TYPE_H264;
106     } else {
107         streamInfo->intent_ = Camera::PREVIEW;
108     }
109 }
110 
SetReleaseStream(bool isReleaseStream)111 int32_t HStreamRepeat::SetReleaseStream(bool isReleaseStream)
112 {
113     isReleaseStream_ = isReleaseStream;
114     return CAMERA_OK;
115 }
116 
IsReleaseStream()117 bool HStreamRepeat::IsReleaseStream()
118 {
119     return isReleaseStream_;
120 }
121 
Start()122 int32_t HStreamRepeat::Start()
123 {
124     int32_t rc = CAMERA_OK;
125 
126     if (streamOperator_ == nullptr) {
127         return CAMERA_INVALID_STATE;
128     }
129     if (curCaptureID_ != 0) {
130         MEDIA_ERR_LOG("HStreamRepeat::Start, Already started with captureID: %{public}d", curCaptureID_);
131         return CAMERA_INVALID_STATE;
132     }
133     if (isVideo_) {
134         rc = StartVideo();
135     } else {
136         rc = StartPreview();
137     }
138     return rc;
139 }
140 
IsvalidCaptureID()141 bool HStreamRepeat::IsvalidCaptureID()
142 {
143     int32_t startValue = 0;
144     int32_t endValue = 0;
145     int32_t captureID = 0;
146 
147     if (isVideo_) {
148         captureID = videoCaptureId_;
149         startValue = VIDEO_CAPTURE_ID_START;
150         endValue = VIDEO_CAPTURE_ID_END;
151     } else {
152         captureID = previewCaptureId_;
153         startValue = PREVIEW_CAPTURE_ID_START;
154         endValue = PREVIEW_CAPTURE_ID_END;
155     }
156     return (captureID >= startValue && captureID <= endValue);
157 }
158 
StartVideo()159 int32_t HStreamRepeat::StartVideo()
160 {
161     Camera::CamRetCode rc = Camera::NO_ERROR;
162 
163     if (!IsvalidCaptureID()) {
164         MEDIA_ERR_LOG("HStreamRepeat::StartVideo Failed to Start Video videoCaptureId_:%{public}d", videoCaptureId_);
165         return CAMERA_CAPTURE_LIMIT_EXCEED;
166     }
167     curCaptureID_ = videoCaptureId_;
168     videoCaptureId_++;
169     std::shared_ptr<Camera::CaptureInfo> captureInfoVideo = std::make_shared<Camera::CaptureInfo>();
170     captureInfoVideo->streamIds_ = {streamId_};
171     captureInfoVideo->captureSetting_ = cameraAbility_;
172     captureInfoVideo->enableShutterCallback_ = false;
173     MEDIA_INFO_LOG("HStreamRepeat::StartVideo() Starting video with capture ID: %{public}d", curCaptureID_);
174     rc = streamOperator_->Capture(curCaptureID_, captureInfoVideo, true);
175     if (rc != Camera::NO_ERROR) {
176         curCaptureID_ = 0;
177         MEDIA_ERR_LOG("HStreamRepeat::Start CommitStreams Video failed with error Code:%{public}d", rc);
178         return HdiToServiceError(rc);
179     }
180     return HdiToServiceError(rc);
181 }
182 
StartPreview()183 int32_t HStreamRepeat::StartPreview()
184 {
185     Camera::CamRetCode rc = Camera::NO_ERROR;
186 
187     if (!IsvalidCaptureID()) {
188         MEDIA_ERR_LOG("HStreamRepeat::StartVideo Failed to Start Preview previewCaptureId_:%{public}d",
189                       previewCaptureId_);
190         return CAMERA_CAPTURE_LIMIT_EXCEED;
191     }
192     curCaptureID_ = previewCaptureId_;
193     previewCaptureId_++;
194     std::shared_ptr<Camera::CaptureInfo> captureInfoPreview = std::make_shared<Camera::CaptureInfo>();
195     captureInfoPreview->streamIds_ = {streamId_};
196     captureInfoPreview->captureSetting_ = cameraAbility_;
197     captureInfoPreview->enableShutterCallback_ = false;
198     MEDIA_INFO_LOG("HStreamRepeat::StartPreview() Starting preview with capture ID: %{public}d", curCaptureID_);
199     rc = streamOperator_->Capture(curCaptureID_, captureInfoPreview, true);
200     if (rc != Camera::NO_ERROR) {
201         MEDIA_ERR_LOG("HStreamRepeat::StartPreview failed with error Code:%{public}d", rc);
202         curCaptureID_ = 0;
203         return HdiToServiceError(rc);
204     }
205     return HdiToServiceError(rc);
206 }
207 
Stop()208 int32_t HStreamRepeat::Stop()
209 {
210     int32_t rc = NO_ERROR;
211     Camera::CamRetCode hdiCode = Camera::NO_ERROR;
212 
213     if (streamOperator_ == nullptr) {
214         return CAMERA_INVALID_STATE;
215     }
216     if (curCaptureID_ == 0) {
217         MEDIA_ERR_LOG("HStreamRepeat::Stop, Stream not started yet");
218         return CAMERA_INVALID_STATE;
219     }
220     hdiCode = streamOperator_->CancelCapture(curCaptureID_);
221     if (rc != NO_ERROR) {
222         MEDIA_ERR_LOG("HStreamRepeat::Stop failed  with errorCode:%{public}d, curCaptureID_: %{public}d",
223                       rc, curCaptureID_);
224         return HdiToServiceError(hdiCode);
225     }
226     curCaptureID_ = 0;
227     return rc;
228 }
229 
SetFps(float Fps)230 int32_t HStreamRepeat::SetFps(float Fps)
231 {
232     return CAMERA_OK;
233 }
234 
Release()235 int32_t HStreamRepeat::Release()
236 {
237     streamRepeatCallback_ = nullptr;
238     curCaptureID_ = 0;
239     streamOperator_ = nullptr;
240     streamId_ = 0;
241     cameraAbility_ = nullptr;
242     return CAMERA_OK;
243 }
244 
IsVideo()245 bool HStreamRepeat::IsVideo()
246 {
247     return isVideo_;
248 }
249 
GetBufferProducer()250 sptr<OHOS::IBufferProducer> HStreamRepeat::GetBufferProducer()
251 {
252     return producer_;
253 }
254 
SetCallback(sptr<IStreamRepeatCallback> & callback)255 int32_t HStreamRepeat::SetCallback(sptr<IStreamRepeatCallback> &callback)
256 {
257     if (callback == nullptr) {
258         MEDIA_ERR_LOG("HStreamRepeat::SetCallback callback is null");
259         return CAMERA_INVALID_ARG;
260     }
261     streamRepeatCallback_ = callback;
262     return CAMERA_OK;
263 }
264 
OnFrameStarted()265 int32_t HStreamRepeat::OnFrameStarted()
266 {
267     if (streamRepeatCallback_ != nullptr) {
268         streamRepeatCallback_->OnFrameStarted();
269     }
270     return CAMERA_OK;
271 }
272 
OnFrameEnded(int32_t frameCount)273 int32_t HStreamRepeat::OnFrameEnded(int32_t frameCount)
274 {
275     if (streamRepeatCallback_ != nullptr) {
276         streamRepeatCallback_->OnFrameEnded(frameCount);
277     }
278     return CAMERA_OK;
279 }
280 
OnFrameError(int32_t errorType)281 int32_t HStreamRepeat::OnFrameError(int32_t errorType)
282 {
283     if (streamRepeatCallback_ != nullptr) {
284         if (errorType == Camera::BUFFER_LOST) {
285             streamRepeatCallback_->OnFrameError(CAMERA_STREAM_BUFFER_LOST);
286         } else {
287             streamRepeatCallback_->OnFrameError(CAMERA_UNKNOWN_ERROR);
288         }
289     }
290     return CAMERA_OK;
291 }
292 
GetStreamId()293 int32_t HStreamRepeat::GetStreamId()
294 {
295     return streamId_;
296 }
297 
ResetCaptureIds()298 void HStreamRepeat::ResetCaptureIds()
299 {
300     videoCaptureId_ = VIDEO_CAPTURE_ID_START;
301     previewCaptureId_ = PREVIEW_CAPTURE_ID_START;
302 }
303 
dumpRepeatStreamInfo(std::string & dumpString)304 void HStreamRepeat::dumpRepeatStreamInfo(std::string& dumpString)
305 {
306     std::shared_ptr<Camera::StreamInfo> curStreamInfo;
307     curStreamInfo = std::make_shared<Camera::StreamInfo>();
308     SetStreamInfo(curStreamInfo);
309     dumpString += "repeat stream info: \n";
310     dumpString += "    Buffer Producer Id:[" + std::to_string(curStreamInfo->bufferQueue_->GetUniqueId());
311     dumpString += "]    stream Id:[" + std::to_string(curStreamInfo->streamId_);
312     std::map<int, std::string>::const_iterator iter =
313         g_cameraFormat.find(format_);
314     if (iter != g_cameraFormat.end()) {
315         dumpString += "]    format:[" + iter->second;
316     }
317     dumpString += "]    width:[" + std::to_string(curStreamInfo->width_);
318     dumpString += "]    height:[" + std::to_string(curStreamInfo->height_);
319     dumpString += "]    TunnelMode:[" + std::to_string(curStreamInfo->tunneledMode_);
320     dumpString += "]    dataspace:[" + std::to_string(curStreamInfo->datasapce_);
321     dumpString += "]    Is Video:[" + std::to_string(isVideo_);
322     if (isVideo_) {
323         dumpString += "]    StreamType:[" + std::to_string(curStreamInfo->intent_);
324         dumpString += "]    Encoding Type:[" + std::to_string(curStreamInfo->encodeType_) + "]:\n";
325     } else {
326         dumpString += "]    StreamType:[" + std::to_string(curStreamInfo->intent_) + "]:\n";
327     }
328 }
329 } // namespace Standard
330 } // namespace OHOS
331