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_DPS_SESSION_COORDINATOR_H
17 #define OHOS_CAMERA_DPS_SESSION_COORDINATOR_H
18 
19 #include <memory>
20 #include <stdint.h>
21 #include "iimage_process_callbacks.h"
22 #include "ivideo_process_callbacks.h"
23 #include "ipc_file_descriptor.h"
24 #include "ideferred_photo_processing_session_callback.h"
25 #include "ideferred_video_processing_session_callback.h"
26 #include "video_session_info.h"
27 #include "task_manager.h"
28 
29 namespace OHOS::Media {
30     class Picture;
31 }
32 namespace OHOS {
33 namespace CameraStandard {
34 namespace DeferredProcessing {
35 class SessionCoordinator : public std::enable_shared_from_this<SessionCoordinator> {
36 public:
37     SessionCoordinator();
38     ~SessionCoordinator();
39     void Initialize();
40     void Start();
41     void Stop();
42 
43     void OnProcessDone(const int32_t userId, const std::string& imageId,
44         const sptr<IPCFileDescriptor>& ipcFd, const int32_t dataSize, bool isCloudImageEnhanceSupported);
45     void OnProcessDoneExt(int userId, const std::string& imageId, std::shared_ptr<Media::Picture> picture,
46         bool isCloudImageEnhanceSupported);
47     void OnError(const int32_t userId, const std::string& imageId, DpsError errorCode);
48     void OnStateChanged(const int32_t userId, DpsStatus statusCode);
49     std::shared_ptr<IImageProcessCallbacks> GetImageProcCallbacks();
50     void NotifySessionCreated(const int32_t userId, sptr<IDeferredPhotoProcessingSessionCallback> callback,
51         TaskManager* taskManager);
52     void NotifyCallbackDestroyed(const int32_t userId);
53 
54     void AddSession(const sptr<VideoSessionInfo>& sessionInfo);
55     void DeleteSession(const int32_t userId);
56     void OnVideoProcessDone(const int32_t userId, const std::string& videoId, const sptr<IPCFileDescriptor>& ipcFd);
57     void OnVideoError(const int32_t userId, const std::string& videoId, DpsError errorCode);
58     void OnVideoStateChanged(const int32_t userId, DpsStatus statusCode);
59     std::shared_ptr<IVideoProcessCallbacks> GetVideoProcCallbacks();
60 
61 private:
62     class ImageProcCallbacks;
63     class VideoProcCallbacks;
64 
65     enum struct CallbackType {
66         ON_PROCESS_DONE,
67         ON_ERROR,
68         ON_STATE_CHANGED
69     };
70 
71     struct ImageResult {
72         CallbackType callbackType;
73         const int32_t userId;
74         std::string imageId;
75         sptr<IPCFileDescriptor> ipcFd;
76         long dataSize;
77         DpsError errorCode;
78         DpsStatus statusCode;
79         bool isCloudImageEnhanceSupported;
80     };
81 
82     struct ImageResultExt {
83         CallbackType callbackType;
84         int userId;
85         std::string imageId;
86         std::shared_ptr<Media::Picture> picture;
87         long dataSize;
88         DpsError errorCode;
89         DpsStatus statusCode;
90     };
91 
92     struct RequestResult {
93         CallbackType callbackType;
94         const int32_t userId;
95         const std::string requestId;
96         sptr<IPCFileDescriptor> ipcFd;
97         long dataSize;
98         DpsError errorCode;
99         DpsStatus statusCode;
100     };
101 
102     void ProcessPendingResults(sptr<IDeferredPhotoProcessingSessionCallback> callback);
103     void ProcessVideoResults(sptr<IDeferredVideoProcessingSessionCallback> callback);
104 
GetRemoteImageCallback(int32_t userId)105     inline sptr<IDeferredPhotoProcessingSessionCallback> GetRemoteImageCallback(int32_t userId)
106     {
107         std::lock_guard<std::mutex> lock(remoteImageCallbacksMapMutex_);
108         auto iter = remoteImageCallbacksMap_.find(userId);
109         if (iter != remoteImageCallbacksMap_.end()) {
110             return iter->second.promote();
111         }
112         return nullptr;
113     }
114 
115     std::mutex mutex_;
116     std::shared_ptr<IImageProcessCallbacks> imageProcCallbacks_;
117 
118     std::mutex remoteImageCallbacksMapMutex_;
119     std::map<int32_t, wptr<IDeferredPhotoProcessingSessionCallback>> remoteImageCallbacksMap_;
120 
121     std::mutex pendingImageResultsMutex_;
122     std::deque<ImageResult> pendingImageResults_;
123 
124     std::shared_ptr<IVideoProcessCallbacks> videoProcCallbacks_;
125     std::map<int32_t, wptr<IDeferredVideoProcessingSessionCallback>> remoteVideoCallbacksMap_;
126     std::deque<RequestResult> pendingRequestResults_;
127 };
128 } // namespace DeferredProcessing
129 } // namespace CameraStandard
130 } // namespace OHOS
131 #endif // OHOS_CAMERA_DPS_SESSION_COORDINATOR_H