1 /*
2  * Copyright (c) 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_COMMON_H
17 #define OHOS_CAMERA_H_STREAM_COMMON_H
18 
19 #include <atomic>
20 #include <cstdint>
21 #include <iostream>
22 #include <mutex>
23 #include <refbase.h>
24 
25 #include "camera/v1_2/types.h"
26 #include "camera/v1_3/types.h"
27 #include "camera_info_dumper.h"
28 #include "camera_metadata_info.h"
29 #include "icapture_session.h"
30 #include "istream_common.h"
31 #include "v1_0/istream_operator.h"
32 #include "v1_1/istream_operator.h"
33 #include "v1_2/istream_operator.h"
34 #include "v1_3/istream_operator.h"
35 
36 namespace OHOS {
37 namespace CameraStandard {
38 using OHOS::HDI::Camera::V1_0::IStreamOperator;
39 using OHOS::HDI::Camera::V1_1::StreamInfo_V1_1;
40 constexpr int32_t CAPTURE_ID_UNSET = 0;
41 constexpr int32_t STREAM_ID_UNSET = 0;
42 
43 class HStreamCommon : virtual public RefBase {
44 public:
45     explicit HStreamCommon(
46         StreamType streamType, sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height);
47     virtual ~HStreamCommon();
48     virtual int32_t LinkInput(sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator,
49         std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility);
50     virtual int32_t UnlinkInput();
51     virtual void SetStreamInfo(StreamInfo_V1_1& streamInfo) = 0;
52     virtual int32_t ReleaseStream(bool isDelay) = 0;
53     virtual void DumpStreamInfo(CameraInfoDumper& infoDumper) = 0;
54 
55     virtual void SetColorSpace(ColorSpace colorSpace) final;
56     virtual int32_t StopStream() final;
57 
58     virtual int32_t GetPreparedCaptureId() final;
59 
60     void PrintCaptureDebugLog(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting_);
61 
GetFwkStreamId()62     inline int32_t GetFwkStreamId()
63     {
64         return fwkStreamId_;
65     }
66 
GetStreamType()67     inline StreamType GetStreamType()
68     {
69         return streamType_;
70     }
71 
SetHdiStreamId(int32_t hdiStreamId)72     inline void SetHdiStreamId(int32_t hdiStreamId)
73     {
74         hdiStreamId_ = hdiStreamId;
75     }
76 
GetHdiStreamId()77     inline int32_t GetHdiStreamId()
78     {
79         return hdiStreamId_;
80     }
81 
82     int32_t format_;
83     int32_t width_;
84     int32_t height_;
85     int32_t dataSpace_ = 0;
86     sptr<OHOS::IBufferProducer> producer_;
87 
88     std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility_ = nullptr;
89 
90 protected:
91     /*
92      * Prepare a capture id, return CAMERA_OK or CAMERA_CAPTURE_LIMIT_EXCEED
93      */
94     virtual int32_t PrepareCaptureId() final;
95     virtual void ResetCaptureId() final;
96 
GetStreamOperator()97     inline sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> GetStreamOperator()
98     {
99         std::lock_guard<std::mutex> lock(streamOperatorLock_);
100         return streamOperator_;
101     }
102 
SetStreamOperator(sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator)103     inline void SetStreamOperator(sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator)
104     {
105         std::lock_guard<std::mutex> lock(streamOperatorLock_);
106         streamOperator_ = streamOperator;
107     }
108 
109     std::mutex producerLock_;
110     std::mutex cameraAbilityLock_;
111     uint32_t callerToken_;
112 
113     std::mutex streamOperatorLock_;
114     sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator_ = nullptr;
115 
116     int32_t captureIdForConfirmCapture_ = CAPTURE_ID_UNSET;
117 
118 private:
119     StreamType streamType_;
120     int32_t fwkStreamId_ = STREAM_ID_UNSET;
121     int32_t hdiStreamId_ = STREAM_ID_UNSET;
122     int32_t curCaptureID_ = CAPTURE_ID_UNSET;
123 };
124 } // namespace CameraStandard
125 } // namespace OHOS
126 #endif // OHOS_CAMERA_H_STREAM_COMMON_H
127