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 #define CAMERA_ROTATION_TYPE_BASE 4
46 #define CAMERA_API_VERSION_BASE 14
47 class EXPORT_API HStreamRepeat : public HStreamRepeatStub, public HStreamCommon {
48 public:
49     HStreamRepeat(
50         sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height, RepeatStreamType type);
51     ~HStreamRepeat();
52 
53     int32_t LinkInput(sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,
54         std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility) override;
55     void SetStreamInfo(StreamInfo_V1_1& streamInfo) override;
56     void SetVideoStreamInfo(StreamInfo_V1_1& streamInfo);
57     int32_t ReleaseStream(bool isDelay) override;
58     int32_t Release() override;
59     int32_t Start() override;
60     int32_t Start(std::shared_ptr<OHOS::Camera::CameraMetadata> settings, bool isUpdateSeetings = false);
61     int32_t Stop() override;
62     int32_t SetCallback(sptr<IStreamRepeatCallback>& callback) override;
63     int32_t OnFrameStarted();
64     int32_t OnFrameEnded(int32_t frameCount);
65     int32_t OnFrameError(int32_t errorType);
66     int32_t OnSketchStatusChanged(SketchStatus status);
67     int32_t OnDeferredVideoEnhancementInfo(CaptureEndedInfoExt captureEndedInfo);
68     int32_t AddDeferredSurface(const sptr<OHOS::IBufferProducer>& producer) override;
69     int32_t ForkSketchStreamRepeat(
70         int32_t width, int32_t height, sptr<IStreamRepeat>& sketchStream, float sketchRatio) override;
71     int32_t RemoveSketchStreamRepeat() override;
72     int32_t EnableSecure(bool isEnable = false) override;
73     int32_t UpdateSketchRatio(float sketchRatio) override;
74     sptr<HStreamRepeat> GetSketchStream();
75     RepeatStreamType GetRepeatStreamType();
76     void SetMetaProducer(sptr<OHOS::IBufferProducer> metaProducer);
77     void SetMovingPhotoStartCallback(std::function<void()> callback);
78     void DumpStreamInfo(CameraInfoDumper& infoDumper) override;
79     int32_t OperatePermissionCheck(uint32_t interfaceCode) override;
80     int32_t SetFrameRate(int32_t minFrameRate, int32_t maxFrameRate) override;
81     int32_t SetMirror(bool isEnable) override;
82     int32_t SetPreviewRotation(std::string &deviceClass);
83     bool SetMirrorForLivePhoto(bool isEnable, int32_t mode);
84     void SetStreamTransform(int disPlayRotation = -1);
85     void SetUsedAsPosition(camera_position_enum_t cameraPosition);
86     int32_t AttachMetaSurface(const sptr<OHOS::IBufferProducer>& producer, int32_t videoMetaType) override;
87     int32_t SetCameraRotation(bool isEnable, int32_t rotation) override;
88     int32_t SetCameraApi(uint32_t apiCompatibleVersion) override;
89 private:
90     void OpenVideoDfxSwitch(std::shared_ptr<OHOS::Camera::CameraMetadata> settings);
91     void StartSketchStream(std::shared_ptr<OHOS::Camera::CameraMetadata> settings);
92     void UpdateSketchStatus(SketchStatus status);
93     void ProcessFixedTransform(int32_t& sensorOrientation, camera_position_enum_t& cameraPosition);
94     void ProcessCameraSetRotation(int32_t& sensorOrientation, camera_position_enum_t& cameraPosition);
95     void ProcessVerticalCameraPosition(int32_t& sensorOrientation, camera_position_enum_t& cameraPosition);
96     void ProcessFixedDiffDeviceTransform(camera_position_enum_t& cameraPosition);
97     void ProcessCameraPosition(int32_t& streamRotation, camera_position_enum_t& cameraPosition);
98     void UpdateVideoSettings(std::shared_ptr<OHOS::Camera::CameraMetadata> settings);
99     void UpdateFrameRateSettings(std::shared_ptr<OHOS::Camera::CameraMetadata> settings);
100     void UpdateFrameMuteSettings(std::shared_ptr<OHOS::Camera::CameraMetadata> &settings,
101                                  std::shared_ptr<OHOS::Camera::CameraMetadata> &dynamicSetting);
102 
103     RepeatStreamType repeatStreamType_;
104     sptr<IStreamRepeatCallback> streamRepeatCallback_;
105     std::mutex callbackLock_;
106     std::mutex sketchStreamLock_; // Guard sketchStreamRepeat_
107     std::mutex streamStartStopLock_;
108     sptr<HStreamRepeat> sketchStreamRepeat_;
109     wptr<HStreamRepeat> parentStreamRepeat_;
110     float sketchRatio_ = -1.0f;
111     SketchStatus sketchStatus_ = SketchStatus::STOPED;
112     RepeatStreamStatus repeatStreamStatus_ = RepeatStreamStatus::STOPED;
113     bool mEnableSecure = false;
114     bool enableMirror_ = false;
115     bool enableStreamRotate_ = false;
116     bool enableCameraRotation_ = false;
117     int32_t setCameraRotation_ = 0;
118     uint32_t apiCompatibleVersion_ = 0;
119     std::string deviceClass_ = "phone";
120     sptr<OHOS::IBufferProducer> metaProducer_;
121     std::mutex movingPhotoCallbackLock_;
122     std::function<void()> startMovingPhotoCallback_;
123     std::vector<int32_t> streamFrameRateRange_ = {};
124     sptr<BufferProducerSequenceable> metaSurfaceBufferQueue_;
125     camera_position_enum_t cameraUsedAsPosition_ = OHOS_CAMERA_POSITION_OTHER;
126 };
127 } // namespace CameraStandard
128 } // namespace OHOS
129 #endif // OHOS_CAMERA_H_STREAM_REPEAT_H
130