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 "capture_output.h" 20 #include "istream_repeat.h" 21 #include "camera_output_capability.h" 22 #include "istream_repeat_callback.h" 23 #include "hstream_repeat_callback_stub.h" 24 25 namespace OHOS { 26 namespace CameraStandard { 27 class PreviewStateCallback { 28 public: 29 PreviewStateCallback() = default; 30 virtual ~PreviewStateCallback() = default; 31 32 /** 33 * @brief Called when preview frame is started rendering. 34 */ 35 virtual void OnFrameStarted() const = 0; 36 37 /** 38 * @brief Called when preview frame is ended. 39 * 40 * @param frameCount Indicates number of frames captured. 41 */ 42 virtual void OnFrameEnded(const int32_t frameCount) const = 0; 43 44 /** 45 * @brief Called when error occured during preview rendering. 46 * 47 * @param errorCode Indicates a {@link ErrorCode} which will give information for preview callback error. 48 */ 49 virtual void OnError(const int32_t errorCode) const = 0; 50 }; 51 52 class PreviewOutput : public CaptureOutput { 53 public: 54 explicit PreviewOutput(sptr<IStreamRepeat> &streamRepeat); 55 virtual ~PreviewOutput(); 56 57 /** 58 * @brief Set the preview callback for the preview output. 59 * 60 * @param PreviewStateCallback to be triggered. 61 */ 62 void SetCallback(std::shared_ptr<PreviewStateCallback> callback); 63 64 /** 65 * @brief Releases a instance of the preview output. 66 */ 67 int32_t Release() override; 68 69 /** 70 * @brief Add delayed preview surface. 71 * 72 * @param surface to add. 73 */ 74 void AddDeferredSurface(sptr<Surface> surface); 75 76 /** 77 * @brief Start preview stream. 78 */ 79 int32_t Start(); 80 81 /** 82 * @brief stop preview stream. 83 */ 84 int32_t Stop(); 85 86 /** 87 * @brief Get the application callback information. 88 * 89 * @return Returns the pointer application callback. 90 */ 91 std::shared_ptr<PreviewStateCallback> GetApplicationCallback(); 92 93 /** 94 * @brief Get the application callback information. 95 */ 96 CameraFormat format; 97 private: 98 std::shared_ptr<PreviewStateCallback> appCallback_; 99 sptr<IStreamRepeatCallback> svcCallback_; 100 }; 101 102 class PreviewOutputCallbackImpl : public HStreamRepeatCallbackStub { 103 public: 104 PreviewOutput* previewOutput_ = nullptr; PreviewOutputCallbackImpl()105 PreviewOutputCallbackImpl() : previewOutput_(nullptr) { 106 } 107 PreviewOutputCallbackImpl(PreviewOutput * previewOutput)108 explicit PreviewOutputCallbackImpl(PreviewOutput* previewOutput) : previewOutput_(previewOutput) { 109 } 110 ~PreviewOutputCallbackImpl()111 ~PreviewOutputCallbackImpl() 112 { 113 previewOutput_ = nullptr; 114 } 115 116 /** 117 * @brief Called when preview frame is started rendering. 118 */ 119 int32_t OnFrameStarted() override; 120 121 /** 122 * @brief Called when preview frame is ended. 123 * 124 * @param frameCount Indicates number of frames captured. 125 */ 126 int32_t OnFrameEnded(int32_t frameCount) override; 127 128 /** 129 * @brief Called when error occured during preview rendering. 130 * 131 * @param errorCode Indicates a {@link ErrorCode} which will give information for preview callback error. 132 */ 133 int32_t OnFrameError(int32_t errorCode) override; 134 }; 135 } // namespace CameraStandard 136 } // namespace OHOS 137 #endif // OHOS_CAMERA_PREVIEW_OUTPUT_H 138 139