1 /* 2 * Copyright (c) 2021 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CAMERA_CAMERA_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CAMERA_CAMERA_H 18 19 #include <list> 20 #include "core/components/camera/camera_component.h" 21 #include "core/components/common/layout/constants.h" 22 23 #include "recorder.h" 24 #include "input/camera_manager.h" 25 namespace OHOS::Ace { 26 27 using TakePhotoListener = std::function<void(const std::map<std::string, std::string>&)>; 28 using ErrorListener = std::function<void(const std::string&, const std::string&)>; 29 using RecordListener = std::function<void(const std::map<std::string, std::string>&)>; 30 31 constexpr int32_t TEMPORARY_WINDOW_SIZE = 480; 32 33 enum State : int32_t { 34 STATE_IDLE, 35 STATE_RUNNING, 36 STATE_BUTT 37 }; 38 39 enum ReadyMode : int32_t { 40 NONE, 41 PHOTO, 42 VIDEO 43 }; 44 45 class CaptureListener; 46 47 class CameraCallback { 48 public: 49 ACE_DISALLOW_COPY_AND_MOVE(CameraCallback); 50 using PrepareEventListener = std::function<void()>; 51 CameraCallback()52 CameraCallback() 53 {} 54 ~CameraCallback()55 ~CameraCallback() 56 { 57 photoListener_ = nullptr; 58 if (captureConsumerSurface_ != nullptr) { 59 captureConsumerSurface_->UnregisterConsumerListener(); 60 } 61 Stop(true); 62 } 63 SetPipelineContext(const WeakPtr<PipelineContext> & context)64 void SetPipelineContext(const WeakPtr<PipelineContext>& context) 65 { 66 context_ = context; 67 } 68 SetRenderNode(const WeakPtr<RenderNode> & renderNode)69 void SetRenderNode(const WeakPtr<RenderNode> &renderNode) 70 { 71 renderNode_ = renderNode; 72 } 73 void Stop(bool isClosePreView); 74 void Release(); 75 void Capture(Size photoSize); 76 void StartPreview(); 77 void StartRecord(); 78 void PrepareCameraInput(sptr<OHOS::CameraStandard::CaptureInput> InCamInput_); 79 sptr<Surface> createSubWindowSurface(); 80 int32_t PreparePhoto(sptr<OHOS::CameraStandard::CameraManager> camManagerObj); 81 int32_t PrepareVideo(sptr<OHOS::CameraStandard::CameraManager> camManagerObj); 82 int32_t PrepareCamera(bool bIsRecorder); 83 int32_t SaveData(char *buffer, int32_t size, std::string& path); 84 void OnTakePhoto(bool isSucces, std::string info); 85 void AddTakePhotoListener(TakePhotoListener&& listener); 86 void AddErrorListener(ErrorListener&& listener); 87 void AddRecordListener(RecordListener&& listener); 88 void SetCatchFilePath(std::string cacheFilePath); 89 void OnCameraSizeChange(double width, double height); 90 void OnCameraOffsetChange(double x, double y); 91 void MarkWhole(); 92 void OnFirstBufferAvailable(); 93 void SetLayoutOffset(double x, double y); SetLayoutSize(double width,double height)94 void SetLayoutSize(double width, double height) 95 { 96 layoutSize_.SetWidth(width); 97 layoutSize_.SetHeight(height); 98 } AddPrepareEventListener(PrepareEventListener && listener)99 void AddPrepareEventListener(PrepareEventListener&& listener) 100 { 101 prepareEventListener_ = std::move(listener); 102 } 103 friend class CaptureListener; 104 105 bool hasMarkWhole_ = false; 106 107 protected: 108 void CloseRecorder(); 109 110 private: 111 int PrepareRecorder(); 112 std::shared_ptr<Media::Recorder> CreateRecorder(); 113 void onError(); 114 void onRecord(bool isSucces, std::string info); 115 void MakeDir(const std::string& path); 116 117 std::shared_ptr<Media::Recorder> recorder_ = nullptr; 118 int32_t videoSourceId_ = -1; 119 int32_t PreviousReadyMode_ = ReadyMode::NONE; 120 sptr<Surface> previewSurface_; 121 Size layoutSize_; 122 Offset layoutOffset_; 123 124 ErrorListener onErrorListener_; 125 RecordListener onRecordListener_; 126 int32_t recordFileId_ = -1; 127 std::string recordPath_; 128 129 WeakPtr<RenderNode> renderNode_; 130 WeakPtr<PipelineContext> context_; 131 sptr<Surface> captureSurface_; 132 Size windowSize_ = Size(TEMPORARY_WINDOW_SIZE, TEMPORARY_WINDOW_SIZE); 133 Offset windowOffset_ = Offset(0, 0); 134 bool sizeInitSucceeded_ = false; 135 bool offsetInitSucceeded_ = false; 136 137 bool isReady_ = false; 138 bool hasCallPreView_ = false; 139 bool hasCallPhoto_ = false; 140 State previewState_ = State::STATE_IDLE; 141 State recordState_ = State::STATE_IDLE; 142 sptr<OHOS::CameraStandard::CaptureSession> capSession_; 143 sptr<OHOS::CameraStandard::CaptureInput> camInput_; 144 sptr<IBufferConsumerListener> photoListener_; 145 sptr<OHOS::CameraStandard::CaptureOutput> photoOutput_; 146 sptr<OHOS::CameraStandard::CaptureOutput> previewOutput_; 147 sptr<OHOS::CameraStandard::CaptureOutput> videoOutput_; 148 std::string cacheFilePath_; 149 TakePhotoListener takePhotoListener_; 150 sptr<IConsumerSurface> captureConsumerSurface_; 151 PrepareEventListener prepareEventListener_; 152 }; 153 154 class CaptureListener : public IBufferConsumerListener { 155 public: CaptureListener(CameraCallback * cameraCallback)156 explicit CaptureListener(CameraCallback *cameraCallback) : cameraCallback_(cameraCallback) {} ~CaptureListener()157 ~CaptureListener() 158 { 159 cameraCallback_->photoListener_ = nullptr; 160 cameraCallback_ = nullptr; 161 } 162 void OnBufferAvailable() override; 163 164 private: 165 CameraCallback *cameraCallback_; 166 }; 167 168 class Camera : public virtual AceType { 169 DECLARE_ACE_TYPE(Camera, AceType); 170 171 public: 172 using PrepareEventListener = std::function<void()>; 173 Camera(const WeakPtr<PipelineContext> & context)174 explicit Camera(const WeakPtr<PipelineContext> &context) 175 : context_(context) {} 176 ~Camera() override = default; 177 178 void Release(); 179 void Create(const std::function<void()>& onCreate); 180 void SetRenderNode(const WeakPtr<RenderNode> &renderNode); 181 182 void Stop(bool isClosePreView); 183 void TakePhoto(Size photoSize); 184 void StartPreview(); 185 void StartRecord(); 186 187 void OnCameraSizeChange(double width, double height); 188 void OnCameraOffsetChange(double x, double y); AddPrepareEventListener(PrepareEventListener && listener)189 void AddPrepareEventListener(PrepareEventListener&& listener) 190 { 191 cameraCallback_.AddPrepareEventListener(std::move(listener)); 192 } 193 194 void AddTakePhotoListener(TakePhotoListener&& listener); 195 void AddErrorListener(ErrorListener&& listener); 196 void AddRecordListener(RecordListener&& listener); 197 198 private: 199 void CreateCamera(); 200 WeakPtr<PipelineContext> context_; 201 WeakPtr<RenderNode> renderNode_; 202 203 TakePhotoListener takePhotoListener_; 204 ErrorListener onErrorListener_; 205 RecordListener onRecordListener_; 206 207 CameraCallback cameraCallback_; 208 sptr<OHOS::CameraStandard::CaptureInput> camInput_; 209 }; 210 211 } // namespace OHOS::Ace 212 213 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CAMERA_CAMERA_H 214