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 #include "deferred_proc_session/deferred_video_proc_session.h"
17 #include "camera_log.h"
18 #include "camera_util.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS {
23 namespace CameraStandard {
24
OnProcessVideoDone(const std::string & videoId,const sptr<IPCFileDescriptor> & ipcFileDescriptor)25 int32_t DeferredVideoProcessingSessionCallback::OnProcessVideoDone(const std::string& videoId,
26 const sptr<IPCFileDescriptor>& ipcFileDescriptor)
27 {
28 MEDIA_INFO_LOG("DeferredVideoProcessingSessionCallback::OnProcessVideoDone() is called!");
29 if (deferredVideoProcSession_ != nullptr && deferredVideoProcSession_->GetCallback() != nullptr) {
30 deferredVideoProcSession_->GetCallback()->OnProcessVideoDone(videoId, ipcFileDescriptor);
31 } else {
32 MEDIA_INFO_LOG("DeferredVideoProcessingSessionCallback::OnProcessVideoDone not set!, Discarding callback");
33 }
34 return ERR_OK;
35 }
36
OnError(const std::string & videoId,int32_t errorCode)37 int32_t DeferredVideoProcessingSessionCallback::OnError(const std::string& videoId,
38 int32_t errorCode)
39 {
40 MEDIA_INFO_LOG("DeferredVideoProcessingSessionCallback::OnError() is called, errorCode: %{public}d", errorCode);
41 if (deferredVideoProcSession_ != nullptr && deferredVideoProcSession_->GetCallback() != nullptr) {
42 deferredVideoProcSession_->GetCallback()->OnError(videoId, DpsErrorCode(errorCode));
43 } else {
44 MEDIA_INFO_LOG("DeferredVideoProcessingSessionCallback::OnError not set!, Discarding callback");
45 }
46 return ERR_OK;
47 }
48
OnStateChanged(int32_t status)49 int32_t DeferredVideoProcessingSessionCallback::OnStateChanged(int32_t status)
50 {
51 MEDIA_INFO_LOG("DeferredVideoProcessingSessionCallback::OnStateChanged() is called, status:%{public}d", status);
52 if (deferredVideoProcSession_ != nullptr && deferredVideoProcSession_->GetCallback() != nullptr) {
53 deferredVideoProcSession_->GetCallback()->OnStateChanged(DpsStatusCode(status));
54 } else {
55 MEDIA_INFO_LOG("DeferredVideoProcessingSessionCallback::OnStateChanged not set!, Discarding callback");
56 }
57 return ERR_OK;
58 }
59
DeferredVideoProcSession(int userId,std::shared_ptr<IDeferredVideoProcSessionCallback> callback)60 DeferredVideoProcSession::DeferredVideoProcSession(int userId,
61 std::shared_ptr<IDeferredVideoProcSessionCallback> callback)
62 {
63 MEDIA_INFO_LOG("enter.");
64 userId_ = userId;
65 callback_ = callback;
66 }
67
~DeferredVideoProcSession()68 DeferredVideoProcSession::~DeferredVideoProcSession()
69 {
70 MEDIA_INFO_LOG("DeferredVideoProcSession::DeferredVideoProcSession Destructor!");
71 if (remoteSession_ != nullptr) {
72 (void)remoteSession_->AsObject()->RemoveDeathRecipient(deathRecipient_);
73 remoteSession_ = nullptr;
74 }
75 }
76
BeginSynchronize()77 void DeferredVideoProcSession::BeginSynchronize()
78 {
79 if (remoteSession_ == nullptr) {
80 MEDIA_ERR_LOG("DeferredVideoProcSession::BeginSynchronize failed due to binder died.");
81 } else {
82 MEDIA_INFO_LOG("DeferredVideoProcSession:BeginSynchronize() enter.");
83 auto ret = remoteSession_->BeginSynchronize();
84 CHECK_ERROR_PRINT_LOG(ret != ERR_OK, "EndSynchronize failed errorCode: %{public}d", ret);
85 }
86 }
87
EndSynchronize()88 void DeferredVideoProcSession::EndSynchronize()
89 {
90 if (remoteSession_ == nullptr) {
91 MEDIA_ERR_LOG("DeferredVideoProcSession::EndSynchronize failed due to binder died.");
92 } else {
93 MEDIA_INFO_LOG("DeferredVideoProcSession::EndSynchronize() enter.");
94 auto ret = remoteSession_->EndSynchronize();
95 CHECK_ERROR_PRINT_LOG(ret != ERR_OK, "EndSynchronize failed errorCode: %{public}d", ret);
96 }
97 }
98
AddVideo(const std::string & videoId,const sptr<IPCFileDescriptor> srcFd,const sptr<IPCFileDescriptor> dstFd)99 void DeferredVideoProcSession::AddVideo(const std::string& videoId, const sptr<IPCFileDescriptor> srcFd,
100 const sptr<IPCFileDescriptor> dstFd)
101 {
102 if (remoteSession_ == nullptr) {
103 MEDIA_ERR_LOG("DeferredVideoProcSession::AddVideo failed due to binder died.");
104 } else {
105 MEDIA_INFO_LOG("DeferredVideoProcSession::AddVideo() enter.");
106 auto ret = remoteSession_->AddVideo(videoId, srcFd, dstFd);
107 CHECK_ERROR_PRINT_LOG(ret != ERR_OK, "AddVideo failed errorCode: %{public}d", ret);
108 }
109 }
110
RemoveVideo(const std::string & videoId,const bool restorable)111 void DeferredVideoProcSession::RemoveVideo(const std::string& videoId, const bool restorable)
112 {
113 if (remoteSession_ == nullptr) {
114 MEDIA_ERR_LOG("DeferredVideoProcSession::RemoveVideo failed due to binder died.");
115 } else {
116 MEDIA_INFO_LOG("DeferredVideoProcSession RemoveVideo() enter.");
117 auto ret = remoteSession_->RemoveVideo(videoId, restorable);
118 CHECK_ERROR_PRINT_LOG(ret != ERR_OK, "RemoveVideo failed errorCode: %{public}d", ret);
119 }
120 }
121
RestoreVideo(const std::string & videoId)122 void DeferredVideoProcSession::RestoreVideo(const std::string& videoId)
123 {
124 if (remoteSession_ == nullptr) {
125 MEDIA_ERR_LOG("DeferredVideoProcSession::RestoreVideo failed due to binder died.");
126 } else {
127 MEDIA_INFO_LOG("DeferredVideoProcSession RestoreVideo() enter.");
128 auto ret = remoteSession_->RestoreVideo(videoId);
129 CHECK_ERROR_PRINT_LOG(ret != ERR_OK, "RestoreVideo failed errorCode: %{public}d", ret);
130 }
131 }
132
SetDeferredVideoSession(sptr<DeferredProcessing::IDeferredVideoProcessingSession> & session)133 int32_t DeferredVideoProcSession::SetDeferredVideoSession(
134 sptr<DeferredProcessing::IDeferredVideoProcessingSession>& session)
135 {
136 remoteSession_ = session;
137 sptr<IRemoteObject> object = remoteSession_->AsObject();
138 pid_t pid = 0;
139 deathRecipient_ = new(std::nothrow) CameraDeathRecipient(pid);
140 CHECK_AND_RETURN_RET_LOG(deathRecipient_ != nullptr, CAMERA_ALLOC_ERROR, "failed to new CameraDeathRecipient.");
141
142 deathRecipient_->SetNotifyCb(std::bind(&DeferredVideoProcSession::CameraServerDied, this, std::placeholders::_1));
143 bool result = object->AddDeathRecipient(deathRecipient_);
144 if (!result) {
145 MEDIA_ERR_LOG("failed to add deathRecipient");
146 return -1; // return error
147 }
148 return ERR_OK;
149 }
150
CameraServerDied(pid_t pid)151 void DeferredVideoProcSession::CameraServerDied(pid_t pid)
152 {
153 MEDIA_ERR_LOG("camera server has died, pid:%{public}d!", pid);
154 if (remoteSession_ != nullptr) {
155 (void)remoteSession_->AsObject()->RemoveDeathRecipient(deathRecipient_);
156 remoteSession_ = nullptr;
157 }
158 deathRecipient_ = nullptr;
159 ReconnectDeferredProcessingSession();
160 if (callback_ != nullptr) {
161 MEDIA_INFO_LOG("Reconnect session successful, send sync requestion.");
162 callback_->OnError("", DpsErrorCode::ERROR_SESSION_SYNC_NEEDED);
163 }
164 }
165
ReconnectDeferredProcessingSession()166 void DeferredVideoProcSession::ReconnectDeferredProcessingSession()
167 {
168 MEDIA_INFO_LOG("DeferredVideoProcSession::ReconnectDeferredProcessingSession, enter.");
169 ConnectDeferredProcessingSession();
170 if (remoteSession_ == nullptr) {
171 MEDIA_INFO_LOG("Reconnecting deferred processing session failed.");
172 ReconnectDeferredProcessingSession();
173 }
174 }
175
ConnectDeferredProcessingSession()176 void DeferredVideoProcSession::ConnectDeferredProcessingSession()
177 {
178 MEDIA_INFO_LOG("DeferredVideoProcSession::ConnectDeferredProcessingSession, enter.");
179 CHECK_ERROR_RETURN_LOG(remoteSession_ != nullptr, "remoteSession_ is not null");
180 sptr<IRemoteObject> object = nullptr;
181 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
182 CHECK_ERROR_RETURN_LOG(samgr == nullptr, "Failed to get System ability manager");
183 object = samgr->GetSystemAbility(CAMERA_SERVICE_ID);
184 CHECK_ERROR_RETURN_LOG(object == nullptr, "object is null");
185 serviceProxy_ = iface_cast<ICameraService>(object);
186 CHECK_ERROR_RETURN_LOG(serviceProxy_ == nullptr, "serviceProxy_ is null");
187 sptr<DeferredProcessing::IDeferredVideoProcessingSession> session = nullptr;
188 sptr<DeferredProcessing::IDeferredVideoProcessingSessionCallback> remoteCallback = nullptr;
189 sptr<DeferredVideoProcSession> deferredVideoProcSession = nullptr;
190 deferredVideoProcSession = new(std::nothrow) DeferredVideoProcSession(userId_, callback_);
191 CHECK_ERROR_RETURN(deferredVideoProcSession == nullptr);
192 remoteCallback = new(std::nothrow) DeferredVideoProcessingSessionCallback(deferredVideoProcSession);
193 CHECK_ERROR_RETURN(remoteCallback == nullptr);
194 serviceProxy_->CreateDeferredVideoProcessingSession(userId_, remoteCallback, session);
195 if (session) {
196 SetDeferredVideoSession(session);
197 }
198 }
199
GetCallback()200 std::shared_ptr<IDeferredVideoProcSessionCallback> DeferredVideoProcSession::GetCallback()
201 {
202 return callback_;
203 }
204
205 } // namespace CameraStandard
206 } // namespace OHOS