1 /*
2 * Copyright (c) 2023-2025 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 #include "session_coordinator.h"
17
18 #include "dp_log.h"
19 #include "dps_event_report.h"
20
21 namespace OHOS {
22 namespace CameraStandard {
23 namespace DeferredProcessing {
24 class SessionCoordinator::VideoProcCallbacks : public IVideoProcessCallbacks {
25 public:
VideoProcCallbacks(const std::weak_ptr<SessionCoordinator> & coordinator)26 explicit VideoProcCallbacks(const std::weak_ptr<SessionCoordinator>& coordinator) : coordinator_(coordinator)
27 {
28 }
29
30 ~VideoProcCallbacks() = default;
31
OnProcessDone(const int32_t userId,const std::string & videoId,const sptr<IPCFileDescriptor> & ipcFd)32 void OnProcessDone(const int32_t userId, const std::string& videoId,
33 const sptr<IPCFileDescriptor>& ipcFd) override
34 {
35 auto video = coordinator_.lock();
36 DP_CHECK_ERROR_RETURN_LOG(video == nullptr, "SessionCoordinator is nullptr.");
37 video->OnVideoProcessDone(userId, videoId, ipcFd);
38 }
39
OnError(const int32_t userId,const std::string & videoId,DpsError errorCode)40 void OnError(const int32_t userId, const std::string& videoId, DpsError errorCode) override
41 {
42 auto video = coordinator_.lock();
43 DP_CHECK_ERROR_RETURN_LOG(video == nullptr, "SessionCoordinator is nullptr.");
44 video->OnVideoError(userId, videoId, errorCode);
45 }
46
OnStateChanged(const int32_t userId,DpsStatus statusCode)47 void OnStateChanged(const int32_t userId, DpsStatus statusCode) override
48 {
49 auto video = coordinator_.lock();
50 DP_CHECK_ERROR_RETURN_LOG(video == nullptr, "SessionCoordinator is nullptr.");
51 video->OnStateChanged(userId, statusCode);
52 }
53
54 private:
55 std::weak_ptr<SessionCoordinator> coordinator_;
56 };
57
SessionCoordinator()58 SessionCoordinator::SessionCoordinator()
59 {
60 DP_DEBUG_LOG("entered.");
61 }
62
~SessionCoordinator()63 SessionCoordinator::~SessionCoordinator()
64 {
65 DP_DEBUG_LOG("entered.");
66 videoCallbackMap_.clear();
67 pendingRequestResults_.clear();
68 }
69
Initialize()70 int32_t SessionCoordinator::Initialize()
71 {
72 videoProcCallbacks_ = std::make_shared<VideoProcCallbacks>(weak_from_this());
73 return DP_OK;
74 }
75
Start()76 void SessionCoordinator::Start()
77 {
78 //dps_log
79 }
80
Stop()81 void SessionCoordinator::Stop()
82 {
83 //dps_log
84 }
85
GetVideoProcCallbacks()86 std::shared_ptr<IVideoProcessCallbacks> SessionCoordinator::GetVideoProcCallbacks()
87 {
88 return videoProcCallbacks_;
89 }
90
OnStateChanged(const int32_t userId,DpsStatus statusCode)91 void SessionCoordinator::OnStateChanged(const int32_t userId, DpsStatus statusCode)
92 {
93 DP_INFO_LOG("DPS_OHOTO: userId: %{public}d, statusCode: %{public}d", userId, statusCode);
94 }
95
AddVideoSession(const sptr<VideoSessionInfo> & sessionInfo)96 void SessionCoordinator::AddVideoSession(const sptr<VideoSessionInfo>& sessionInfo)
97 {
98 int32_t userId = sessionInfo->GetUserId();
99 DP_INFO_LOG("Add vidoe session userId: %{public}d", userId);
100 auto callback = sessionInfo->GetRemoteCallback();
101 if (callback != nullptr) {
102 videoCallbackMap_[userId] = callback;
103 }
104 }
105
DeleteVideoSession(const int32_t userId)106 void SessionCoordinator::DeleteVideoSession(const int32_t userId)
107 {
108 if (videoCallbackMap_.erase(userId) > 0) {
109 DP_INFO_LOG("Delete VideoSession userId: %{public}d", userId);
110 }
111 }
112
OnVideoProcessDone(const int32_t userId,const std::string & videoId,const sptr<IPCFileDescriptor> & ipcFd)113 void SessionCoordinator::OnVideoProcessDone(const int32_t userId, const std::string& videoId,
114 const sptr<IPCFileDescriptor> &ipcFd)
115 {
116 DP_INFO_LOG("DPS_VIDEO: userId: %{public}d, userMap size: %{public}zu", userId, videoCallbackMap_.size());
117 auto callback = GetRemoteVideoCallback(userId);
118 if (callback != nullptr) {
119 callback->OnProcessVideoDone(videoId, ipcFd);
120 } else {
121 DP_INFO_LOG("callback is null, videoId: %{public}s.", videoId.c_str());
122 }
123 }
124
OnVideoError(const int32_t userId,const std::string & videoId,DpsError errorCode)125 void SessionCoordinator::OnVideoError(const int32_t userId, const std::string& videoId, DpsError errorCode)
126 {
127 DP_INFO_LOG("DPS_VIDEO: userId: %{public}d, userMap: %{public}zu, videoId: %{public}s, error: %{public}d",
128 userId, videoCallbackMap_.size(), videoId.c_str(), errorCode);
129 auto callback = GetRemoteVideoCallback(userId);
130 if (callback != nullptr) {
131 callback->OnError(videoId, static_cast<int32_t>(MapDpsErrorCode(errorCode)));
132 } else {
133 DP_INFO_LOG("callback is null, videoId: %{public}s, errorCode: %{public}d", videoId.c_str(), errorCode);
134 }
135 }
136
OnVideoStateChanged(const int32_t userId,DpsStatus statusCode)137 void SessionCoordinator::OnVideoStateChanged(const int32_t userId, DpsStatus statusCode)
138 {
139 DP_DEBUG_LOG("entered.");
140 }
141
ProcessVideoResults(sptr<IDeferredVideoProcessingSessionCallback> callback)142 void SessionCoordinator::ProcessVideoResults(sptr<IDeferredVideoProcessingSessionCallback> callback)
143 {
144 DP_DEBUG_LOG("entered.");
145 while (!pendingRequestResults_.empty()) {
146 auto result = pendingRequestResults_.front();
147 if (result.callbackType == CallbackType::ON_PROCESS_DONE) {
148 callback->OnProcessVideoDone(result.requestId, result.ipcFd);
149 }
150 if (result.callbackType == CallbackType::ON_ERROR) {
151 callback->OnError(result.requestId, static_cast<int32_t>(MapDpsErrorCode(result.errorCode)));
152 }
153 if (result.callbackType == CallbackType::ON_STATE_CHANGED) {
154 callback->OnStateChanged(static_cast<int32_t>(MapDpsStatus(result.statusCode)));
155 }
156 pendingRequestResults_.pop_front();
157 }
158 }
159 } // namespace DeferredProcessing
160 } // namespace CameraStandard
161 } // namespace OHOS