1 /*
2  * Copyright (c) 2023-2023 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_DEFERRED_PHOTO_PROCESSOR_H
17 #define OHOS_CAMERA_DEFERRED_PHOTO_PROCESSOR_H
18 
19 #include <refbase.h>
20 #include <iostream>
21 #include <mutex>
22 #include "camera_death_recipient.h"
23 #include "ideferred_photo_processing_session.h"
24 #include "ideferred_photo_processing_session_callback.h"
25 #include "deferred_photo_processing_session_callback_stub.h"
26 #include "dps_metadata_info.h"
27 #include "hcamera_service_proxy.h"
28 #include "deferred_type.h"
29 
30 namespace OHOS::Media {
31     class Picture;
32 }
33 namespace OHOS {
34 namespace CameraStandard {
35 class IDeferredPhotoProcSessionCallback : public RefBase {
36 public:
37     IDeferredPhotoProcSessionCallback() = default;
38     virtual ~IDeferredPhotoProcSessionCallback() = default;
39     virtual void OnProcessImageDone(const std::string &imageId, std::shared_ptr<Media::Picture> picture,
40         bool isCloudImageEnhanceSupported) = 0;
41     virtual void OnDeliveryLowQualityImage(const std::string &imageId, std::shared_ptr<Media::Picture> picture) = 0;
42     virtual void OnProcessImageDone(const std::string& imageId, const uint8_t* addr, const long bytes,
43         bool isCloudImageEnhanceSupported) = 0;
44     virtual void OnError(const std::string& imageId, const DpsErrorCode errorCode) = 0;
45     virtual void OnStateChanged(const DpsStatusCode status) = 0;
46 };
47 
48 class DeferredPhotoProcSession : public RefBase {
49 public:
50     DeferredPhotoProcSession(int userId, std::shared_ptr<IDeferredPhotoProcSessionCallback> callback);
51     virtual ~DeferredPhotoProcSession();
52     void BeginSynchronize();
53     void EndSynchronize();
54     void AddImage(const std::string& imageId, DpsMetadata& metadata, const bool discardable = false);
55     void RemoveImage(const std::string& imageId, const bool restorable = true);
56     void RestoreImage(const std::string& imageId);
57     void ProcessImage(const std::string& appName, const std::string& imageId);
58     bool CancelProcessImage(const std::string& imageId);
59     std::shared_ptr<IDeferredPhotoProcSessionCallback> GetCallback();
60 private:
61     friend class CameraManager;
62     int32_t SetDeferredPhotoSession(sptr<DeferredProcessing::IDeferredPhotoProcessingSession>& session);
63     void CameraServerDied(pid_t pid);
64     void ReconnectDeferredProcessingSession();
65     void ConnectDeferredProcessingSession();
66     int userId_;
67     std::shared_ptr<IDeferredPhotoProcSessionCallback> callback_;
68     sptr<DeferredProcessing::IDeferredPhotoProcessingSession> remoteSession_;
69     sptr<CameraDeathRecipient> deathRecipient_ = nullptr;
70     sptr<ICameraService> serviceProxy_;
71 };
72 
73 class DeferredPhotoProcessingSessionCallback : public DeferredProcessing::DeferredPhotoProcessingSessionCallbackStub {
74 public:
DeferredPhotoProcessingSessionCallback()75     DeferredPhotoProcessingSessionCallback() : deferredPhotoProcSession_(nullptr) {
76     }
77 
DeferredPhotoProcessingSessionCallback(sptr<DeferredPhotoProcSession> deferredPhotoProcSession)78     explicit DeferredPhotoProcessingSessionCallback(sptr<DeferredPhotoProcSession> deferredPhotoProcSession)
79         : deferredPhotoProcSession_(deferredPhotoProcSession)
80     {
81     }
82 
~DeferredPhotoProcessingSessionCallback()83     ~DeferredPhotoProcessingSessionCallback()
84     {
85         deferredPhotoProcSession_ = nullptr;
86     }
87 
88     int32_t OnProcessImageDone(const std::string &imageId, const sptr<IPCFileDescriptor> ipcFileDescriptor,
89         const long bytes, const bool isCloudImageEnhanceSupported) override;
90     int32_t OnProcessImageDone(const std::string &imageId, std::shared_ptr<Media::Picture> picture,
91         bool isCloudImageEnhanceSupported) override;
92     int32_t OnDeliveryLowQualityImage(const std::string &imageId, std::shared_ptr<Media::Picture> picture) override;
93     int32_t OnError(const std::string &imageId, const DeferredProcessing::ErrorCode errorCode) override;
94     int32_t OnStateChanged(const DeferredProcessing::StatusCode status) override;
95 
96 private:
97     sptr<DeferredPhotoProcSession> deferredPhotoProcSession_;
98 };
99 
100 } // namespace CameraStandard
101 } // namespace OHOS
102 #endif // OHOS_CAMERA_DEFERRED_PHOTO_PROCESSOR_H