• 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_CAMERA_SERVICE_H
17 #define OHOS_CAMERA_H_CAMERA_SERVICE_H
18 #include <iostream>
19 #include <memory>
20 #include <set>
21 #include <shared_mutex>
22 
23 #include "camera_util.h"
24 #include "hcamera_device.h"
25 #include "hcamera_host_manager.h"
26 #include "hcamera_service_stub.h"
27 #include "hcapture_session.h"
28 #include "hstream_capture.h"
29 #include "hstream_metadata.h"
30 #include "hstream_repeat.h"
31 #include "iremote_stub.h"
32 #include "privacy_kit.h"
33 #include "refbase.h"
34 #include "system_ability.h"
35 #ifdef CAMERA_USE_SENSOR
36 #include "sensor_agent.h"
37 #include "sensor_agent_type.h"
38 #endif
39 #include "ideferred_photo_processing_session_callback.h"
40 #include "ideferred_photo_processing_session.h"
41 
42 namespace OHOS {
43 namespace CameraStandard {
44 using namespace std;
45 using namespace OHOS::HDI::Camera::V1_0;
46 using namespace DeferredProcessing;
47 struct CameraMetaInfo {
48     string cameraId;
49     uint8_t position;
50     uint8_t connectionType;
51     shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility;
CameraMetaInfoCameraMetaInfo52     CameraMetaInfo(string cameraId, uint8_t position, uint8_t connectionType,
53         shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility)
54         : cameraId(cameraId), position(position), connectionType (connectionType), cameraAbility(cameraAbility) {}
55 };
56 class HCameraService : public SystemAbility, public HCameraServiceStub, public HCameraHostManager::StatusCallback {
57     DECLARE_SYSTEM_ABILITY(HCameraService);
58 
59 public:
60     DISALLOW_COPY_AND_MOVE(HCameraService);
61 
62     explicit HCameraService(int32_t systemAbilityId, bool runOnCreate = true);
63     ~HCameraService() override;
64     int32_t GetCameras(vector<string>& cameraIds,
65         vector<shared_ptr<OHOS::Camera::CameraMetadata>>& cameraAbilityList) override;
66     int32_t CreateCameraDevice(string cameraId, sptr<ICameraDeviceService>& device) override;
67     int32_t CreateCaptureSession(sptr<ICaptureSession>& session, int32_t opMode) override;
68     int32_t CreateDeferredPhotoProcessingSession(int32_t userId,
69         sptr<DeferredProcessing::IDeferredPhotoProcessingSessionCallback>& callback,
70         sptr<DeferredProcessing::IDeferredPhotoProcessingSession>& session) override;
71     int32_t CreatePhotoOutput(const sptr<OHOS::IBufferProducer>& producer, int32_t format, int32_t width,
72         int32_t height, sptr<IStreamCapture>& photoOutput) override;
73     int32_t CreateDeferredPreviewOutput(
74         int32_t format, int32_t width, int32_t height, sptr<IStreamRepeat>& previewOutput) override;
75     int32_t CreatePreviewOutput(const sptr<OHOS::IBufferProducer>& producer, int32_t format, int32_t width,
76         int32_t height, sptr<IStreamRepeat>& previewOutput) override;
77     int32_t CreateMetadataOutput(
78         const sptr<OHOS::IBufferProducer>& producer, int32_t format, sptr<IStreamMetadata>& metadataOutput) override;
79     int32_t CreateVideoOutput(const sptr<OHOS::IBufferProducer>& producer, int32_t format, int32_t width,
80         int32_t height, sptr<IStreamRepeat>& videoOutput) override;
81     int32_t SetCallback(sptr<ICameraServiceCallback>& callback) override;
82     int32_t UnSetCallback(pid_t pid) override;
83     int32_t CloseCameraForDestory(pid_t pid) override;
84     int32_t SetMuteCallback(sptr<ICameraMuteServiceCallback>& callback) override;
85     int32_t SetTorchCallback(sptr<ITorchServiceCallback>& callback) override;
86     int32_t MuteCamera(bool muteMode) override;
87     int32_t PrelaunchCamera() override;
88     int32_t PreSwitchCamera(const std::string cameraId) override;
89     int32_t SetPrelaunchConfig(string cameraId, RestoreParamTypeOhos restoreParamType, int activeTime,
90         EffectParam effectParam) override;
91 //    std::string GetClientBundle(int uid);
92     int32_t IsCameraMuted(bool& muteMode) override;
93     int32_t SetTorchLevel(float level) override;
94     int32_t AllowOpenByOHSide(std::string cameraId, int32_t state, bool &canOpenCamera) override;
95     int32_t NotifyCameraState(std::string cameraId, int32_t state) override;
96     void OnDump() override;
97     void OnStart() override;
98     void OnStop() override;
99     int32_t Dump(int fd, const vector<u16string>& args) override;
100 
101     // HCameraHostManager::StatusCallback
102     void OnCameraStatus(const string& cameraId, CameraStatus status) override;
103     void OnFlashlightStatus(const string& cameraId, FlashStatus status) override;
104     void OnTorchStatus(TorchStatus status) override;
105 
106 protected:
107     explicit HCameraService(sptr<HCameraHostManager> cameraHostManager);
108 
109 private:
110     class ServiceHostStatus : public StatusCallback {
111     public:
ServiceHostStatus(wptr<HCameraService> cameraService)112         explicit ServiceHostStatus(wptr<HCameraService> cameraService) : cameraService_(cameraService) {};
113         virtual ~ServiceHostStatus() = default;
OnCameraStatus(const std::string & cameraId,CameraStatus status)114         void OnCameraStatus(const std::string& cameraId, CameraStatus status) override
115         {
116             auto cameraService = cameraService_.promote();
117             if (cameraService != nullptr) {
118                 cameraService->OnCameraStatus(cameraId, status);
119             }
120         }
OnFlashlightStatus(const std::string & cameraId,FlashStatus status)121         void OnFlashlightStatus(const std::string& cameraId, FlashStatus status) override
122         {
123             auto cameraService = cameraService_.promote();
124             if (cameraService != nullptr) {
125                 cameraService->OnFlashlightStatus(cameraId, status);
126             }
127         }
OnTorchStatus(TorchStatus status)128         void OnTorchStatus(TorchStatus status) override
129         {
130             auto cameraService = cameraService_.promote();
131             if (cameraService != nullptr) {
132                 cameraService->OnTorchStatus(status);
133             }
134         }
135 
136     private:
137         wptr<HCameraService> cameraService_;
138     };
139 
140     void CameraSummary(vector<string> cameraIds, string& dumpString);
141     void CameraDumpCameraInfo(std::string& dumpString, std::vector<std::string>& cameraIds,
142         std::vector<std::shared_ptr<OHOS::Camera::CameraMetadata>>& cameraAbilityList);
143     void CameraDumpAbility(common_metadata_header_t* metadataEntry, string& dumpString);
144     void CameraDumpStreaminfo(common_metadata_header_t* metadataEntry, string& dumpString);
145     void CameraDumpZoom(common_metadata_header_t* metadataEntry, string& dumpString);
146     void CameraDumpFlash(common_metadata_header_t* metadataEntry, string& dumpString);
147     void CameraDumpAF(common_metadata_header_t* metadataEntry, string& dumpString);
148     void CameraDumpAE(common_metadata_header_t* metadataEntry, string& dumpString);
149     void CameraDumpSensorInfo(common_metadata_header_t* metadataEntry, string& dumpString);
150     void CameraDumpVideoStabilization(common_metadata_header_t* metadataEntry, string& dumpString);
151     void CameraDumpVideoFrameRateRange(common_metadata_header_t* metadataEntry, string& dumpString);
152     void CameraDumpPrelaunch(common_metadata_header_t* metadataEntry, string& dumpString);
153     void CameraDumpThumbnail(common_metadata_header_t* metadataEntry, string& dumpString);
154     vector<shared_ptr<CameraMetaInfo>> ChooseDeFaultCameras(vector<shared_ptr<CameraMetaInfo>> cameraInfos);
155     bool IsCameraMuteSupported(string cameraId);
156     bool IsPrelaunchSupported(string cameraId);
157     int32_t UpdateMuteSetting(sptr<HCameraDevice> cameraDevice, bool muteMode);
158     std::shared_ptr<OHOS::Camera::CameraMetadata> CreateDefaultSettingForRestore(sptr<HCameraDevice> activeDevice);
159     int32_t UpdateSkinSmoothSetting(shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata, int skinSmoothValue);
160     int32_t UpdateFaceSlenderSetting(shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata,
161         int faceSlenderValue);
162     int32_t UpdateSkinToneSetting(shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata, int skinToneValue);
163     int32_t UnSetMuteCallback(pid_t pid);
164     int32_t UnSetTorchCallback(pid_t pid);
165     bool IsDeviceAlreadyOpen(pid_t& tempPid, string& tempCameraId, sptr<HCameraDevice>& tempDevice);
166     int32_t DeviceClose(sptr<HCameraDevice> cameraDevice);
167 #ifdef CAMERA_USE_SENSOR
168     void RegisterSensorCallback();
169     void UnRegisterSensorCallback();
170     static void DropDetectionDataCallbackImpl(SensorEvent *event);
171 #endif
172     int32_t SaveCurrentParamForRestore(string cameraId, RestoreParamTypeOhos restoreParamType, int activeTime,
173         EffectParam effectParam, sptr<HCaptureSession> captureSession);
174     mutex mutex_;
175     mutex cbMutex_;
176     mutex muteCbMutex_;
177     recursive_mutex torchCbMutex_;
178     TorchStatus torchStatus_ = TorchStatus::TORCH_STATUS_UNAVAILABLE;
179     sptr<HCameraHostManager> cameraHostManager_;
180     std::shared_ptr<StatusCallback> statusCallback_;
181     map<uint32_t, sptr<ITorchServiceCallback>> torchServiceCallbacks_;
182     map<uint32_t, sptr<ICameraMuteServiceCallback>> cameraMuteServiceCallbacks_;
183     map<uint32_t, sptr<ICameraServiceCallback>> cameraServiceCallbacks_;
184     bool muteMode_;
185     bool isFoldable = false;
186     bool isFoldableInit = false;
187     mutex mapOperatorsLock_;
188     string preCameraId_;
189     string preCameraClient_;
190     bool isRegisterSensorSuccess;
191 #ifdef CAMERA_USE_SENSOR
192     SensorUser user;
193 #endif
194     SafeMap<uint32_t, sptr<HCaptureSession>> captureSessionsManager_;
195 };
196 } // namespace CameraStandard
197 } // namespace OHOS
198 #endif // OHOS_CAMERA_H_CAMERA_SERVICE_H
199