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 #ifndef OHOS_CAMERA_PREVIEW_OUTPUT_H 17 #define OHOS_CAMERA_PREVIEW_OUTPUT_H 18 19 #include <cstdint> 20 #include "camera_output_capability.h" 21 #include "capture_output.h" 22 #include "hstream_repeat_callback_stub.h" 23 #include "icamera_service.h" 24 #include "istream_repeat.h" 25 #include "istream_repeat_callback.h" 26 27 namespace OHOS { 28 namespace CameraStandard { 29 static const std::string CONST_PREVIEW_FRAME_START = "frameStart"; 30 static const std::string CONST_PREVIEW_FRAME_END = "frameEnd"; 31 static const std::string CONST_PREVIEW_FRAME_ERROR = "error"; 32 static const std::string CONST_SKETCH_STATUS_CHANGED = "sketchStatusChanged"; 33 class SketchWrapper; 34 class PreviewStateCallback { 35 public: 36 PreviewStateCallback() = default; 37 virtual ~PreviewStateCallback() = default; 38 39 /** 40 * @brief Called when preview frame is started rendering. 41 */ 42 virtual void OnFrameStarted() const = 0; 43 44 /** 45 * @brief Called when preview frame is ended. 46 * 47 * @param frameCount Indicates number of frames captured. 48 */ 49 virtual void OnFrameEnded(const int32_t frameCount) const = 0; 50 51 /** 52 * @brief Called when error occured during preview rendering. 53 * 54 * @param errorCode Indicates a {@link ErrorCode} which will give information for preview callback error. 55 */ 56 virtual void OnError(const int32_t errorCode) const = 0; 57 58 /** 59 * @brief Called when sketch status changed. 60 * 61 * @param statusData Indicates a {@link SketchStatusData}. 62 */ 63 virtual void OnSketchStatusDataChanged(const SketchStatusData& statusData) const = 0; 64 }; 65 66 class PreviewOutput : public CaptureOutput { 67 public: 68 explicit PreviewOutput(sptr<IBufferProducer> bufferProducer); 69 explicit PreviewOutput(); 70 virtual ~PreviewOutput(); 71 72 /** 73 * @brief Set the preview callback for the preview output. 74 * 75 * @param PreviewStateCallback to be triggered. 76 */ 77 void SetCallback(std::shared_ptr<PreviewStateCallback> callback); 78 79 int32_t CreateStream() override; 80 81 /** 82 * @brief Releases a instance of the preview output. 83 */ 84 int32_t Release() override; 85 86 /** 87 * @brief Add delayed preview surface. 88 * 89 * @param surface to add. 90 */ 91 void AddDeferredSurface(sptr<Surface> surface); 92 93 /** 94 * @brief Start preview stream. 95 */ 96 int32_t Start(); 97 98 /** 99 * @brief stop preview stream. 100 */ 101 int32_t Stop(); 102 103 /** 104 * @brief Check whether the current preview mode supports sketch. 105 * 106 * @return Return the supported result. 107 */ 108 bool IsSketchSupported(); 109 110 /** 111 * @brief Get the scaling ratio threshold for sketch callback data. 112 * 113 * @return Return the threshold value. 114 */ 115 float GetSketchRatio(); 116 117 /** 118 * @brief get the preview rotation angle. 119 * 120 * @return result of the photo rotation angle. 121 */ 122 int32_t GetPreviewRotation(int32_t imageRotation); 123 124 /** 125 * @brief set the preview rotation angle. 126 */ 127 int32_t SetPreviewRotation(int32_t imageRotation, bool isDisplayLocked); 128 129 /** 130 * @brief Enable sketch 131 * 132 * @param isEnable True for enable, false otherwise. 133 * 134 */ 135 int32_t EnableSketch(bool isEnable); 136 137 /** 138 * @brief Attach sketch surface 139 * 140 * @param sketchSurface Sketch surface 141 * 142 */ 143 int32_t AttachSketchSurface(sptr<Surface> sketchSurface); 144 145 /** 146 * @brief Set the preview fps. 147 * 148 * @param frameRate value of frame rate. 149 */ 150 int32_t SetFrameRate(int32_t minFrameRate, int32_t maxFrameRate); 151 152 /** 153 * @brief Get the active preview frame rate range. 154 * 155 * @return Returns vector<int32_t> of active exposure compensation range. 156 */ 157 const std::vector<int32_t>& GetFrameRateRange(); 158 159 /** 160 * @brief Set the preview fps range. If fixed frame rate 161 * to be set the both min and max framerate should be same. 162 * 163 * @param minFrameRate min frame rate value of range. 164 * @param maxFrameRate max frame rate value of range. 165 */ 166 void SetFrameRateRange(int32_t minFrameRate, int32_t maxFrameRate); 167 168 /** 169 * @brief Set the format 170 * 171 * @param format format of the previewOutput. 172 */ 173 void SetOutputFormat(int32_t format); 174 175 /** 176 * @brief Set the size 177 * 178 * @param size size of the previewOutput. 179 */ 180 void SetSize(Size size); 181 182 /** 183 * @brief Get the supported preview frame rate range. 184 * 185 * @return Returns vector<int32_t> of supported exposure compensation range. 186 */ 187 std::vector<std::vector<int32_t>> GetSupportedFrameRates(); 188 189 /** 190 * @brief Get the application callback information. 191 * 192 * @return Returns the pointer application callback. 193 */ 194 std::shared_ptr<PreviewStateCallback> GetApplicationCallback(); 195 196 /** 197 * @brief Get Observed matadata tags 198 * Register tags into capture session. If the tags data changes,{@link OnControlMetadataChanged} will be 199 * called. 200 * @return Observed tags 201 */ 202 virtual const std::set<camera_device_metadata_tag_t>& GetObserverControlTags() override; 203 204 /** 205 * @brief Callback of request metadata change. 206 * @return Operate result 207 */ 208 int32_t OnControlMetadataChanged( 209 const camera_device_metadata_tag_t tag, const camera_metadata_item_t& metadataItem) override; 210 211 /** 212 * @brief Callback of result metadata change. 213 * @return Operate result 214 */ 215 int32_t OnResultMetadataChanged( 216 const camera_device_metadata_tag_t tag, const camera_metadata_item_t& metadataItem) override; 217 218 int32_t OnSketchStatusChanged(SketchStatus status); 219 220 void OnNativeRegisterCallback(const std::string& eventString); 221 void OnNativeUnregisterCallback(const std::string& eventString); 222 private: 223 int32_t PreviewFormat_; 224 Size PreviewSize_; 225 std::shared_ptr<PreviewStateCallback> appCallback_; 226 sptr<IStreamRepeatCallback> svcCallback_; 227 std::shared_ptr<SketchWrapper> sketchWrapper_; 228 std::shared_ptr<OHOS::Camera::CameraMetadata> GetDeviceMetadata(); 229 std::shared_ptr<Size> FindSketchSize(); 230 std::vector<int32_t> previewFrameRateRange_{0, 0}; 231 int32_t CreateSketchWrapper(Size sketchSize); 232 int32_t StartSketch(); 233 int32_t StopSketch(); 234 void CameraServerDied(pid_t pid) override; 235 int32_t canSetFrameRateRange(int32_t minFrameRate, int32_t maxFrameRate); 236 }; 237 238 class PreviewOutputCallbackImpl : public HStreamRepeatCallbackStub { 239 public: 240 wptr<PreviewOutput> previewOutput_ = nullptr; PreviewOutputCallbackImpl()241 PreviewOutputCallbackImpl() : previewOutput_(nullptr) {} 242 PreviewOutputCallbackImpl(PreviewOutput * previewOutput)243 explicit PreviewOutputCallbackImpl(PreviewOutput* previewOutput) : previewOutput_(previewOutput) {} 244 245 /** 246 * @brief Called when preview frame is started rendering. 247 */ 248 int32_t OnFrameStarted() override; 249 250 /** 251 * @brief Called when preview frame is ended. 252 * 253 * @param frameCount Indicates number of frames captured. 254 */ 255 int32_t OnFrameEnded(int32_t frameCount) override; 256 257 /** 258 * @brief Called when error occured during preview rendering. 259 * 260 * @param errorCode Indicates a {@link ErrorCode} which will give information for preview callback error. 261 */ 262 int32_t OnFrameError(int32_t errorCode) override; 263 264 /** 265 * @brief Called when sketch status changed. 266 * 267 * @param status Indicates a {@link SketchStatus} which will give information for preview callback error. 268 */ 269 int32_t OnSketchStatusChanged(SketchStatus status) override; 270 271 int32_t OnDeferredVideoEnhancementInfo(CaptureEndedInfoExt captureEndedInfo) override; 272 }; 273 } // namespace CameraStandard 274 } // namespace OHOS 275 #endif // OHOS_CAMERA_PREVIEW_OUTPUT_H 276