• 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 "hstream_repeat.h"
17 
18 #include "camera_util.h"
19 #include "metadata_utils.h"
20 #include "display.h"
21 #include "display_manager.h"
22 #include "camera_log.h"
23 
24 namespace OHOS {
25 namespace CameraStandard {
26 static const int32_t STREAM_ROTATE_90 = 90;
27 static const int32_t STREAM_ROTATE_180 = 180;
28 static const int32_t STREAM_ROTATE_270 = 270;
29 static const int32_t STREAM_ROTATE_360 = 360;
30 
HStreamRepeat(sptr<OHOS::IBufferProducer> producer,int32_t format,int32_t width,int32_t height,bool isVideo)31 HStreamRepeat::HStreamRepeat(
32     sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height, bool isVideo)
33     : HStreamCommon(StreamType::REPEAT, producer, format, width, height)
34 {
35     isVideo_ = isVideo;
36 }
37 
~HStreamRepeat()38 HStreamRepeat::~HStreamRepeat()
39 {}
40 
LinkInput(sptr<IStreamOperator> streamOperator,std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility,int32_t streamId)41 int32_t HStreamRepeat::LinkInput(sptr<IStreamOperator> streamOperator,
42                                  std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility, int32_t streamId)
43 {
44     int32_t ret = HStreamCommon::LinkInput(streamOperator, cameraAbility, streamId);
45     if (ret != CAMERA_OK) {
46         return ret;
47     }
48     if (!isVideo_) {
49         SetStreamTransform();
50     }
51     return CAMERA_OK;
52 }
53 
SetStreamInfo(StreamInfo & streamInfo)54 void HStreamRepeat::SetStreamInfo(StreamInfo &streamInfo)
55 {
56     HStreamCommon::SetStreamInfo(streamInfo);
57     if (isVideo_) {
58         streamInfo.intent_ = VIDEO;
59         streamInfo.encodeType_ = ENCODE_TYPE_H264;
60     } else {
61         streamInfo.intent_ = PREVIEW;
62         streamInfo.encodeType_ = ENCODE_TYPE_NULL;
63     }
64 }
65 
Start()66 int32_t HStreamRepeat::Start()
67 {
68     CAMERA_SYNC_TRACE;
69 
70     if (streamOperator_ == nullptr) {
71         return CAMERA_INVALID_STATE;
72     }
73     if (curCaptureID_ != 0) {
74         MEDIA_ERR_LOG("HStreamRepeat::Start, Already started with captureID: %{public}d", curCaptureID_);
75         return CAMERA_INVALID_STATE;
76     }
77     int32_t ret = AllocateCaptureId(curCaptureID_);
78     if (ret != CAMERA_OK) {
79         MEDIA_ERR_LOG("HStreamRepeat::Start Failed to allocate a captureId");
80         return ret;
81     }
82     std::vector<uint8_t> ability;
83     OHOS::Camera::MetadataUtils::ConvertMetadataToVec(cameraAbility_, ability);
84     CaptureInfo captureInfo;
85     captureInfo.streamIds_ = {streamId_};
86     captureInfo.captureSetting_ = ability;
87     captureInfo.enableShutterCallback_ = false;
88     MEDIA_INFO_LOG("HStreamRepeat::Start Starting with capture ID: %{public}d", curCaptureID_);
89     CamRetCode rc = (CamRetCode)(streamOperator_->Capture(curCaptureID_, captureInfo, true));
90     if (rc != HDI::Camera::V1_0::NO_ERROR) {
91         ReleaseCaptureId(curCaptureID_);
92         curCaptureID_ = 0;
93         MEDIA_ERR_LOG("HStreamRepeat::Start Failed with error Code:%{public}d", rc);
94         ret = HdiToServiceError(rc);
95     }
96     return ret;
97 }
98 
Stop()99 int32_t HStreamRepeat::Stop()
100 {
101     CAMERA_SYNC_TRACE;
102 
103     if (streamOperator_ == nullptr) {
104         return CAMERA_INVALID_STATE;
105     }
106     if (curCaptureID_ == 0) {
107         MEDIA_ERR_LOG("HStreamRepeat::Stop, Stream not started yet");
108         return CAMERA_INVALID_STATE;
109     }
110     int32_t ret = CAMERA_OK;
111     CamRetCode rc = (CamRetCode)(streamOperator_->CancelCapture(curCaptureID_));
112     if (rc != HDI::Camera::V1_0::NO_ERROR) {
113         MEDIA_ERR_LOG("HStreamRepeat::Stop Failed with errorCode:%{public}d, curCaptureID_: %{public}d",
114                       rc, curCaptureID_);
115         ret = HdiToServiceError(rc);
116     }
117     ReleaseCaptureId(curCaptureID_);
118     curCaptureID_ = 0;
119     return ret;
120 }
121 
Release()122 int32_t HStreamRepeat::Release()
123 {
124     if (curCaptureID_) {
125         ReleaseCaptureId(curCaptureID_);
126     }
127     std::lock_guard<std::mutex> lock(callbackLock_);
128     streamRepeatCallback_ = nullptr;
129     return HStreamCommon::Release();
130 }
131 
IsVideo()132 bool HStreamRepeat::IsVideo()
133 {
134     return isVideo_;
135 }
136 
SetCallback(sptr<IStreamRepeatCallback> & callback)137 int32_t HStreamRepeat::SetCallback(sptr<IStreamRepeatCallback> &callback)
138 {
139     if (callback == nullptr) {
140         MEDIA_ERR_LOG("HStreamRepeat::SetCallback callback is null");
141         return CAMERA_INVALID_ARG;
142     }
143     std::lock_guard<std::mutex> lock(callbackLock_);
144     streamRepeatCallback_ = callback;
145     return CAMERA_OK;
146 }
147 
OnFrameStarted()148 int32_t HStreamRepeat::OnFrameStarted()
149 {
150     CAMERA_SYNC_TRACE;
151     std::lock_guard<std::mutex> lock(callbackLock_);
152     if (streamRepeatCallback_ != nullptr) {
153         streamRepeatCallback_->OnFrameStarted();
154     }
155     return CAMERA_OK;
156 }
157 
OnFrameEnded(int32_t frameCount)158 int32_t HStreamRepeat::OnFrameEnded(int32_t frameCount)
159 {
160     CAMERA_SYNC_TRACE;
161     std::lock_guard<std::mutex> lock(callbackLock_);
162     if (streamRepeatCallback_ != nullptr) {
163         streamRepeatCallback_->OnFrameEnded(frameCount);
164     }
165     return CAMERA_OK;
166 }
167 
OnFrameError(int32_t errorType)168 int32_t HStreamRepeat::OnFrameError(int32_t errorType)
169 {
170     std::lock_guard<std::mutex> lock(callbackLock_);
171     if (streamRepeatCallback_ != nullptr) {
172         int32_t repeatErrorCode;
173         if (errorType == BUFFER_LOST) {
174             repeatErrorCode = CAMERA_STREAM_BUFFER_LOST;
175         } else {
176             repeatErrorCode = CAMERA_UNKNOWN_ERROR;
177         }
178         CAMERA_SYSEVENT_FAULT(CreateMsg("Preview OnFrameError! errorCode:%d", repeatErrorCode));
179         streamRepeatCallback_->OnFrameError(repeatErrorCode);
180     }
181     return CAMERA_OK;
182 }
183 
AddDeferredSurface(const sptr<OHOS::IBufferProducer> & producer)184 int32_t HStreamRepeat::AddDeferredSurface(const sptr<OHOS::IBufferProducer> &producer)
185 {
186     if (producer == nullptr) {
187         MEDIA_ERR_LOG("HStreamRepeat::AddDeferredSurface producer is null");
188         return CAMERA_INVALID_ARG;
189     }
190     producer_ = producer;
191     return CAMERA_OK;
192 }
193 
DumpStreamInfo(std::string & dumpString)194 void HStreamRepeat::DumpStreamInfo(std::string& dumpString)
195 {
196     dumpString += "repeat stream:\n";
197     HStreamCommon::DumpStreamInfo(dumpString);
198 }
199 
SetStreamTransform()200 void HStreamRepeat::SetStreamTransform()
201 {
202     camera_metadata_item_t item;
203     int ret = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_SENSOR_ORIENTATION, &item);
204     if (ret != CAM_META_SUCCESS) {
205         MEDIA_ERR_LOG("HStreamRepeat::SetStreamTransform get sensor orientation failed");
206         return;
207     }
208     int32_t sensorOrientation = item.data.i32[0];
209     MEDIA_INFO_LOG("HStreamRepeat::SetStreamTransform sensor orientation %{public}d", sensorOrientation);
210 
211     ret = OHOS::Camera::FindCameraMetadataItem(cameraAbility_->get(), OHOS_ABILITY_CAMERA_POSITION, &item);
212     if (ret != CAM_META_SUCCESS) {
213         MEDIA_ERR_LOG("HStreamRepeat::SetStreamTransform get camera position failed");
214         return;
215     }
216     camera_position_enum_t cameraPosition = static_cast<camera_position_enum_t>(item.data.u8[0]);
217     MEDIA_INFO_LOG("HStreamRepeat::SetStreamTransform camera position %{public}d", cameraPosition);
218 
219     auto display = OHOS::Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
220     if ((display->GetWidth() < display->GetHeight()) && (producer_ != nullptr)) {
221         ret = SurfaceError::SURFACE_ERROR_OK;
222         int32_t streamRotation = sensorOrientation;
223         if (cameraPosition == OHOS_CAMERA_POSITION_FRONT) {
224             switch (streamRotation) {
225                 case STREAM_ROTATE_90: {
226                     ret = producer_->SetTransform(GRAPHIC_FLIP_H_ROT90);
227                     break;
228                 }
229                 case STREAM_ROTATE_180: {
230                     ret = producer_->SetTransform(GRAPHIC_FLIP_H_ROT180);
231                     break;
232                 }
233                 case STREAM_ROTATE_270: {
234                     ret = producer_->SetTransform(GRAPHIC_FLIP_H_ROT270);
235                     break;
236                 }
237                 default: {
238                     break;
239                 }
240             }
241             MEDIA_INFO_LOG("HStreamRepeat::SetStreamTransform filp rotate %{public}d", streamRotation);
242         } else {
243             streamRotation = STREAM_ROTATE_360 - sensorOrientation;
244             switch (streamRotation) {
245                 case STREAM_ROTATE_90: {
246                     ret = producer_->SetTransform(GRAPHIC_ROTATE_90);
247                     break;
248                 }
249                 case STREAM_ROTATE_180: {
250                     ret = producer_->SetTransform(GRAPHIC_ROTATE_180);
251                     break;
252                 }
253                 case STREAM_ROTATE_270: {
254                     ret = producer_->SetTransform(GRAPHIC_ROTATE_270);
255                     break;
256                 }
257                 default: {
258                     break;
259                 }
260             }
261             MEDIA_INFO_LOG("HStreamRepeat::SetStreamTransform not flip rotate %{public}d", streamRotation);
262         }
263         if (ret != SurfaceError::SURFACE_ERROR_OK) {
264             MEDIA_ERR_LOG("HStreamRepeat::SetStreamTransform failed %{public}d", ret);
265         }
266     }
267 }
268 } // namespace Standard
269 } // namespace OHOS
270