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 "video_session_info.h"
17 
18 #include "deferred_video_processing_session.h"
19 #include "dp_catch.h"
20 #include "dp_log.h"
21 #include "dps.h"
22 #include "session_command.h"
23 
24 namespace OHOS {
25 namespace CameraStandard {
26 namespace DeferredProcessing {
27 class VideoSessionInfo::CallbackDeathRecipient : public IRemoteObject::DeathRecipient {
28 public:
CallbackDeathRecipient(wptr<VideoSessionInfo> sessionInfo)29     explicit CallbackDeathRecipient(wptr<VideoSessionInfo> sessionInfo)
30         : sessionInfo_(sessionInfo)
31     {
32     }
33 
34     virtual ~CallbackDeathRecipient() = default;
35 
Initialize(const sptr<IDeferredVideoProcessingSessionCallback> & callback)36     int32_t Initialize(const sptr<IDeferredVideoProcessingSessionCallback>& callback)
37     {
38         DP_DEBUG_LOG("entered.");
39         sptr<IRemoteObject> object = callback->AsObject();
40         auto result = object->AddDeathRecipient(this);
41         DP_CHECK_ERROR_RETURN_RET_LOG(!result, DP_INIT_FAIL, "add DeathRecipient for Callback failed.");
42         return DP_OK;
43     }
44 
Destroy(const sptr<IDeferredVideoProcessingSessionCallback> & callback)45     int32_t Destroy(const sptr<IDeferredVideoProcessingSessionCallback>& callback)
46     {
47         DP_DEBUG_LOG("entered.");
48         sptr<IRemoteObject> object = callback->AsObject();
49         auto result = object->RemoveDeathRecipient(this);
50         DP_CHECK_ERROR_RETURN_RET_LOG(!result, DP_INIT_FAIL, "remove DeathRecipient for Callback failed.");
51         return DP_OK;
52     }
53 
OnRemoteDied(const wptr<IRemoteObject> & remote)54     void OnRemoteDied(const wptr<IRemoteObject> &remote) override
55     {
56         DP_ERR_LOG("Remote died, do clean works.");
57         auto info = sessionInfo_.promote();
58         DP_CHECK_RETURN_LOG(info == nullptr, "VideoSessionInfo is nullptr.");
59         info->OnCallbackDied();
60     }
61 
62 private:
63     wptr<VideoSessionInfo> sessionInfo_;
64 };
65 
VideoSessionInfo(const int32_t userId,const sptr<IDeferredVideoProcessingSessionCallback> & callback)66 VideoSessionInfo::VideoSessionInfo(const int32_t userId, const sptr<IDeferredVideoProcessingSessionCallback>& callback)
67     : userId_(userId), callback_(callback), deathRecipient_(nullptr)
68 {
69     DP_DEBUG_LOG("entered. userId: %{public}d.", userId_);
70     Initialize();
71 }
72 
~VideoSessionInfo()73 VideoSessionInfo::~VideoSessionInfo()
74 {
75     DP_DEBUG_LOG("entered.");
76     DP_CHECK_EXECUTE(callback_ != nullptr && deathRecipient_ != nullptr, deathRecipient_->Destroy(callback_));
77     session_ = nullptr;
78     callback_ = nullptr;
79     deathRecipient_ =nullptr;
80 }
81 
Initialize()82 int32_t VideoSessionInfo::Initialize()
83 {
84     DP_CHECK_ERROR_RETURN_RET_LOG(callback_ == nullptr, DP_NULL_POINTER,
85         "VideoSessionInfo init failed, callback is nullptr.");
86 
87     session_ = sptr<DeferredVideoProcessingSession>::MakeSptr(userId_);
88     deathRecipient_ = sptr<CallbackDeathRecipient>::MakeSptr(this);
89 
90     auto ret = deathRecipient_->Initialize(callback_);
91     DP_CHECK_ERROR_RETURN_RET_LOG(ret != DP_OK, ret, "set DeathRecipient failed.");
92     return DP_OK;
93 }
94 
GetDeferredVideoProcessingSession()95 sptr<IDeferredVideoProcessingSession> VideoSessionInfo::GetDeferredVideoProcessingSession()
96 {
97     return session_;
98 }
99 
GetRemoteCallback()100 sptr<IDeferredVideoProcessingSessionCallback> VideoSessionInfo::GetRemoteCallback()
101 {
102     return callback_;
103 }
104 
GetUserId() const105 int32_t VideoSessionInfo::GetUserId() const
106 {
107     return userId_;
108 }
109 
OnCallbackDied()110 void VideoSessionInfo::OnCallbackDied()
111 {
112     auto ret = DPS_SendUrgentCommand<DeleteVideoSessionCommand>(this);
113     DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK, "DeleteVideoSession failed.");
114 }
115 
SetCallback(const sptr<IDeferredVideoProcessingSessionCallback> & callback)116 void VideoSessionInfo::SetCallback(const sptr<IDeferredVideoProcessingSessionCallback>& callback)
117 {
118     DP_INFO_LOG("reset callback");
119     callback_ = callback;
120     auto ret = deathRecipient_->Initialize(callback_);
121     DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK, "set DeathRecipient failed.");
122 }
123 } // namespace DeferredProcessing
124 } // namespace CameraStandard
125 } // namespace OHOS
126