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_H_STREAM_REPEAT_H
17 #define OHOS_CAMERA_H_STREAM_REPEAT_H
18 #define EXPORT_API __attribute__((visibility("default")))
19 
20 #include <cstdint>
21 #include <functional>
22 #include <iostream>
23 #include <refbase.h>
24 
25 #include "camera_metadata_info.h"
26 #include "hstream_common.h"
27 #include "hstream_repeat_stub.h"
28 #include "istream_repeat_callback.h"
29 #include "v1_0/istream_operator.h"
30 
31 namespace OHOS {
32 namespace CameraStandard {
33 using OHOS::HDI::Camera::V1_0::BufferProducerSequenceable;
34 enum class RepeatStreamType {
35     PREVIEW,
36     VIDEO,
37     SKETCH,
38     LIVEPHOTO
39 };
40 
41 enum class RepeatStreamStatus {
42     STOPED,
43     STARTED
44 };
45 
46 class EXPORT_API HStreamRepeat : public HStreamRepeatStub, public HStreamCommon {
47 public:
48     HStreamRepeat(
49         sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height, RepeatStreamType type);
50     ~HStreamRepeat();
51 
52     int32_t LinkInput(sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,
53         std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility) override;
54     void SetStreamInfo(StreamInfo_V1_1& streamInfo) override;
55     void SetVideoStreamInfo(StreamInfo_V1_1& streamInfo);
56     int32_t ReleaseStream(bool isDelay) override;
57     int32_t Release() override;
58     int32_t Start() override;
59     int32_t Start(std::shared_ptr<OHOS::Camera::CameraMetadata> settings, bool isUpdateSeetings = false);
60     int32_t Stop() override;
61     int32_t SetCallback(sptr<IStreamRepeatCallback>& callback) override;
62     int32_t OnFrameStarted();
63     int32_t OnFrameEnded(int32_t frameCount);
64     int32_t OnFrameError(int32_t errorType);
65     int32_t OnSketchStatusChanged(SketchStatus status);
66     int32_t OnDeferredVideoEnhancementInfo(CaptureEndedInfoExt captureEndedInfo);
67     int32_t AddDeferredSurface(const sptr<OHOS::IBufferProducer>& producer) override;
68     int32_t ForkSketchStreamRepeat(
69         int32_t width, int32_t height, sptr<IStreamRepeat>& sketchStream, float sketchRatio) override;
70     int32_t RemoveSketchStreamRepeat() override;
71     int32_t EnableSecure(bool isEnable = false) override;
72     int32_t UpdateSketchRatio(float sketchRatio) override;
73     sptr<HStreamRepeat> GetSketchStream();
74     RepeatStreamType GetRepeatStreamType();
75     void SetMetaProducer(sptr<OHOS::IBufferProducer> metaProducer);
76     void SetMovingPhotoStartCallback(std::function<void()> callback);
77     void DumpStreamInfo(CameraInfoDumper& infoDumper) override;
78     int32_t OperatePermissionCheck(uint32_t interfaceCode) override;
79     int32_t SetFrameRate(int32_t minFrameRate, int32_t maxFrameRate) override;
80     int32_t SetMirror(bool isEnable) override;
81     void SetMirrorForLivePhoto(bool isEnable, int32_t mode);
82     int32_t SetPreviewRotation(std::string &deviceClass);
83     void SetStreamTransform(int disPlayRotation = -1);
84     void SetUsedAsPosition(camera_position_enum_t cameraPosition);
85     int32_t AttachMetaSurface(const sptr<OHOS::IBufferProducer>& producer, int32_t videoMetaType) override;
86     int32_t SetCameraRotation(bool isEnable, int32_t rotation) override;
87 
88 private:
89     void OpenVideoDfxSwitch(std::shared_ptr<OHOS::Camera::CameraMetadata> settings);
90     void StartSketchStream(std::shared_ptr<OHOS::Camera::CameraMetadata> settings);
91     void UpdateSketchStatus(SketchStatus status);
92     void ProcessVerticalCameraPosition(int32_t& sensorOrientation, camera_position_enum_t& cameraPosition);
93     void ProcessCameraPosition(int32_t& streamRotation, camera_position_enum_t& cameraPosition);
94     void ProcessCameraSetRotation(int32_t& streamRotation, camera_position_enum_t& cameraPosition);
95     void UpdateVideoSettings(std::shared_ptr<OHOS::Camera::CameraMetadata> settings);
96     void UpdateFrameRateSettings(std::shared_ptr<OHOS::Camera::CameraMetadata> settings);
97     void UpdateFrameMuteSettings(std::shared_ptr<OHOS::Camera::CameraMetadata> &settings,
98                                  std::shared_ptr<OHOS::Camera::CameraMetadata> &dynamicSetting);
99 
100     RepeatStreamType repeatStreamType_;
101     sptr<IStreamRepeatCallback> streamRepeatCallback_;
102     std::mutex callbackLock_;
103     std::mutex sketchStreamLock_; // Guard sketchStreamRepeat_
104     std::mutex streamStartStopLock_;
105     sptr<HStreamRepeat> sketchStreamRepeat_;
106     wptr<HStreamRepeat> parentStreamRepeat_;
107     float sketchRatio_ = -1.0f;
108     SketchStatus sketchStatus_ = SketchStatus::STOPED;
109     RepeatStreamStatus repeatStreamStatus_ = RepeatStreamStatus::STOPED;
110     bool mEnableSecure = false;
111     bool enableMirror_ = false;
112     bool enableStreamRotate_ = false;
113     bool enableCameraRotation_ = false;
114     int32_t setCameraRotation_ = 0;
115     std::string deviceClass_ = "phone";
116     sptr<OHOS::IBufferProducer> metaProducer_;
117     std::mutex movingPhotoCallbackLock_;
118     std::function<void()> startMovingPhotoCallback_;
119     std::vector<int32_t> streamFrameRateRange_ = {};
120     sptr<BufferProducerSequenceable> metaSurfaceBufferQueue_;
121     camera_position_enum_t cameraUsedAsPosition_ = OHOS_CAMERA_POSITION_OTHER;
122 };
123 } // namespace CameraStandard
124 } // namespace OHOS
125 #endif // OHOS_CAMERA_H_STREAM_REPEAT_H
126