• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CAPTURE_SESSION_H
17 #define OHOS_CAMERA_H_CAPTURE_SESSION_H
18 #define EXPORT_API __attribute__((visibility("default")))
19 
20 #include <atomic>
21 #include <cstdint>
22 #include <functional>
23 #include <iostream>
24 #include <list>
25 #include <memory>
26 #include <mutex>
27 #include <refbase.h>
28 #include <unordered_map>
29 #include <unordered_set>
30 #include "camera_rotate_strategy_parser.h"
31 #include "hcamera_device.h"
32 #include "hcapture_session_stub.h"
33 #include "hstream_repeat.h"
34 #include "hstream_operator.h"
35 #include "icapture_session.h"
36 #include "istream_common.h"
37 #include "camera_photo_proxy.h"
38 #include "iconsumer_surface.h"
39 #include "blocking_queue.h"
40 #include "audio_capturer.h"
41 #include "audio_info.h"
42 #include "avcodec_task_manager.h"
43 #include "moving_photo_video_cache.h"
44 #include "drain_manager.h"
45 #include "audio_capturer_session.h"
46 #include "safe_map.h"
47 #ifdef CAMERA_USE_SENSOR
48 #include "sensor_agent.h"
49 #include "sensor_agent_type.h"
50 #endif
51 namespace OHOS {
52 namespace CameraStandard {
53 
54 
55 enum class CaptureSessionReleaseType : int32_t {
56     RELEASE_TYPE_CLIENT = 0,
57     RELEASE_TYPE_CLIENT_DIED,
58     RELEASE_TYPE_SECURE,
59     RELEASE_TYPE_OBJ_DIED,
60 };
61 
62 class StateMachine {
63 public:
64     explicit StateMachine();
65     virtual ~StateMachine() = default;
66     bool CheckTransfer(CaptureSessionState targetState);
67     bool Transfer(CaptureSessionState targetState);
68 
GetCurrentState()69     inline CaptureSessionState GetCurrentState()
70     {
71         std::lock_guard<std::recursive_mutex> lock(sessionStateLock_);
72         return currentState_;
73     }
74 
StateGuard(const std::function<void (const CaptureSessionState)> & fun)75     inline void StateGuard(const std::function<void(const CaptureSessionState)>& fun)
76     {
77         std::lock_guard<std::recursive_mutex> lock(sessionStateLock_);
78         fun(currentState_);
79     }
80 
IsStateNoLock(CaptureSessionState targetState)81     inline bool IsStateNoLock(CaptureSessionState targetState)
82     {
83         return currentState_ == targetState;
84     }
85 
86 private:
87     std::vector<CaptureSessionState> stateTransferMap_[static_cast<uint32_t>(CaptureSessionState::SESSION_STATE_MAX)];
88     std::recursive_mutex sessionStateLock_;
89     CaptureSessionState currentState_ = CaptureSessionState::SESSION_INIT;
90 };
91 using MetaElementType = std::pair<int64_t, sptr<SurfaceBuffer>>;
92 
93 class CameraInfoDumper;
94 
95 class EXPORT_API HCaptureSession : public HCaptureSessionStub, public IHCameraCloseListener {
96 public:
97     static CamServiceError NewInstance(const uint32_t callerToken, int32_t opMode, sptr<HCaptureSession>& outSession);
98     virtual ~HCaptureSession();
99 
100     int32_t BeginConfig() override;
101     int32_t CommitConfig() override;
102 
103     int32_t CanAddInput(sptr<ICameraDeviceService> cameraDevice, bool& result) override;
104     int32_t AddInput(sptr<ICameraDeviceService> cameraDevice) override;
105     int32_t AddOutput(StreamType streamType, sptr<IStreamCommon> stream) override;
106 
107     int32_t RemoveInput(sptr<ICameraDeviceService> cameraDevice) override;
108     int32_t RemoveOutput(StreamType streamType, sptr<IStreamCommon> stream) override;
109 
110     int32_t Start() override;
111     int32_t Stop() override;
112     int32_t Release() override;
113     int32_t Release(CaptureSessionReleaseType type);
114 
115     static void DestroyStubObjectForPid(pid_t pid);
116     int32_t SetCallback(sptr<ICaptureSessionCallback>& callback) override;
117     int32_t UnSetCallback() override;
118 
119     int32_t GetSessionState(CaptureSessionState& sessionState) override;
120     int32_t GetActiveColorSpace(ColorSpace& colorSpace) override;
121     int32_t SetColorSpace(ColorSpace colorSpace, bool isNeedUpdate) override;
122     bool QueryFpsAndZoomRatio(
123         float &currentFps, float &currentZoomRatio, std::vector<float> &crossZoomAndTime, int32_t operationMode);
124     bool QueryZoomPerformance(
125         std::vector<float> &crossZoomAndTime, int32_t operationMode, const camera_metadata_item_t &zoomItem);
126     int32_t GetRangeId(float& zoomRatio, std::vector<float>& crossZoom);
127     float GetCrossWaitTime(std::vector<std::vector<float>>& crossTime, int32_t targetRangeId, int32_t currentRangeId);
128     void GetCrossZoomAndTime(std::vector<float>& crossZoomAndTime,
129         std::vector<float>& crossZoom, std::vector<std::vector<float>>& crossTime);
130     bool QueryZoomBezierValue(std::vector<float>& zoomBezierValue);
131     int32_t SetSmoothZoom(
132         int32_t smoothZoomType, int32_t operationMode, float targetZoomRatio, float& duration) override;
133     void AddZoomAndTimeArray(
134         std::vector<uint32_t> &zoomAndTimeArray, std::vector<float> array, float frameIntervalMs, float waitTime);
135     int32_t EnableMovingPhoto(bool isEnable) override;
136     pid_t GetPid();
137     int32_t GetSessionId();
138     int32_t GetCurrentStreamInfos(std::vector<StreamInfo_V1_1>& streamInfos);
139     int32_t GetopMode();
140 
141     int32_t OperatePermissionCheck(uint32_t interfaceCode) override;
142     int32_t EnableMovingPhotoMirror(bool isMirror, bool isConfig) override;
143     std::shared_ptr<PhotoAssetIntf> ProcessPhotoProxy(int32_t captureId,
144         std::shared_ptr<PictureIntf> picturePtr, bool isBursting,
145         sptr<CameraServerPhotoProxy> cameraPhotoProxy, std::string &uri);
146     int32_t SetFeatureMode(int32_t featureMode) override;
147     void GetOutputStatus(int32_t &status);
148     int32_t SetPreviewRotation(std::string &deviceClass) override;
149     int32_t SetCommitConfigFlag(bool isNeedCommitting) override;
150 
151     void DumpSessionInfo(CameraInfoDumper& infoDumper);
152     static void DumpSessions(CameraInfoDumper& infoDumper);
153     static void DumpCameraSessionSummary(CameraInfoDumper& infoDumper);
154     void ReleaseStreams();
155     bool isEqual(float zoomPointA, float zoomPointB);
SetStreamOperator(wptr<HStreamOperator> hStreamOperator)156     inline void SetStreamOperator(wptr<HStreamOperator> hStreamOperator)
157     {
158         std::lock_guard<std::mutex> lock(streamOperatorLock_);
159         if (hStreamOperator == nullptr) {
160             return;
161         }
162         hStreamOperator_ = hStreamOperator;
163         auto hStreamOperatorSptr = hStreamOperator_.promote();
164         if (hStreamOperatorSptr != nullptr) {
165             hStreamOperatorSptr->SetCameraDevice(cameraDevice_);
166         }
167     }
168 
GetStreamOperator()169     inline sptr<HStreamOperator> GetStreamOperator()
170     {
171         std::lock_guard<std::mutex> lock(streamOperatorLock_);
172         if (hStreamOperator_ == nullptr) {
173             return nullptr;
174         }
175         return hStreamOperator_.promote();
176     }
177 
178     void ConfigPayload(uint32_t pid, uint32_t tid, const char *bundleName, int32_t qosLevel,
179         std::unordered_map<std::string, std::string> &mapPayload);
180     void SetCameraRotateStrategyInfos(std::vector<CameraRotateStrategyInfo> infos);
181     std::vector<CameraRotateStrategyInfo> GetCameraRotateStrategyInfos();
182     void UpdateCameraRotateAngleAndZoom(std::vector<CameraRotateStrategyInfo> &infos,
183         std::vector<int32_t> &frameRateRange);
184 
185     void BeforeDeviceClose() override;
186 
187     void OnSessionPreempt();
188 
189 private:
190     void InitDefaultColortSpace(SceneMode opMode);
191     explicit HCaptureSession(const uint32_t callingTokenId, int32_t opMode);
192     string lastDisplayName_ = "";
193     string lastBurstPrefix_ = "";
194     int32_t saveIndex = 0;
195     bool isNeedCommitting_ = false;
196     void SetCameraDevice(sptr<HCameraDevice> device);
GetCameraDevice()197     inline const sptr<HCameraDevice> GetCameraDevice()
198     {
199         std::lock_guard<std::mutex> lock(cameraDeviceLock_);
200         return cameraDevice_;
201     }
202     string CreateDisplayName();
203     string CreateBurstDisplayName(int32_t imageSeqId, int32_t seqId);
204     int32_t ValidateSessionInputs();
205     int32_t ValidateSessionOutputs();
206     int32_t ValidateSession();
207     int32_t RemoveOutputStream(sptr<HStreamCommon> stream);
208     int32_t LinkInputAndOutputs();
209     int32_t UnlinkInputAndOutputs();
210 
211     void ClearSketchRepeatStream();
212     void ExpandSketchRepeatStream();
213     void ExpandMovingPhotoRepeatStream();
214 
215     void ProcessMetaZoomArray(std::vector<uint32_t>& zoomAndTimeArray, sptr<HCameraDevice>& cameraDevice);
216     void UpdateMuteSetting(bool muteMode, std::shared_ptr<OHOS::Camera::CameraMetadata> &settings);
217     void StartMovingPhoto(sptr<HStreamRepeat>& curStreamRepeat);
218     int32_t GetSensorOritation();
219     std::string GetSessionState();
220 
221     void DynamicConfigStream();
222     bool IsNeedDynamicConfig();
223     void InitialHStreamOperator();
224     void ClearMovingPhotoRepeatStream();
225     StateMachine stateMachine_;
226 
227     #ifdef CAMERA_USE_SENSOR
228         std::mutex sensorLock_;
229         bool isRegisterSensorSuccess_ = false;
230         void RegisterSensorCallback();
231         void UnregisterSensorCallback();
232         static void GravityDataCallbackImpl(SensorEvent *event);
233         static int32_t CalcSensorRotation(int32_t sensorDegree);
234         static int32_t CalcRotationDegree(GravityData data);
235     #endif
236 
237     std::mutex cbMutex_;
238 
239     // Make sure device thread safe,set device by {SetCameraDevice}, get device by {GetCameraDevice}
240     std::mutex cameraDeviceLock_;
241     std::mutex streamOperatorLock_;
242     sptr<HCameraDevice> cameraDevice_;
243 
244     StreamContainer streamContainer_;
245     #ifdef CAMERA_USE_SENSOR
246         SensorUser user;
247     #endif
248     pid_t pid_ = 0;
249     uid_t uid_ = 0;
250     int32_t sessionId_ = 0;
251     uint32_t callerToken_ = 0;
252     int32_t opMode_ = 0;
253     int32_t featureMode_ = 0;
254     bool isSessionStarted_ = false;
255     bool isDynamicConfiged_ = false;
256     std::string deviceClass_ = "phone";
257     wptr<HStreamOperator> hStreamOperator_ = nullptr;
258     std::mutex cameraRotateStrategyInfosLock_;
259     std::vector<CameraRotateStrategyInfo> cameraRotateStrategyInfos_;
260 };
261 } // namespace CameraStandard
262 } // namespace OHOS
263 #endif // OHOS_CAMERA_H_CAPTURE_SESSION_H
264