1 /*
2 * Copyright (c) 2024-2024 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_log.h"
20 #include "dps.h"
21 #include "session_command.h"
22
23 namespace OHOS {
24 namespace CameraStandard {
25 namespace DeferredProcessing {
26 class VideoSessionInfo::CallbackDeathRecipient : public IRemoteObject::DeathRecipient {
27 public:
CallbackDeathRecipient(const wptr<VideoSessionInfo> & sessionInfo)28 explicit CallbackDeathRecipient(const wptr<VideoSessionInfo>& sessionInfo)
29 : sessionInfo_(sessionInfo)
30 {
31 DP_DEBUG_LOG("entered.");
32 }
33 virtual ~CallbackDeathRecipient() = default;
34
Initialize(const sptr<IDeferredVideoProcessingSessionCallback> & callback)35 int32_t Initialize(const sptr<IDeferredVideoProcessingSessionCallback>& callback)
36 {
37 DP_DEBUG_LOG("entered.");
38 sptr<IRemoteObject> object = callback->AsObject();
39 auto result = object->AddDeathRecipient(this);
40 DP_CHECK_ERROR_RETURN_RET_LOG(!result, DP_INIT_FAIL,
41 "Add video 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,
51 "Remove video DeathRecipient for Callback failed.");
52 return DP_OK;
53 }
54
OnRemoteDied(const wptr<IRemoteObject> & remote)55 void OnRemoteDied(const wptr<IRemoteObject> &remote) override
56 {
57 DP_ERR_LOG("Video remote died.");
58 auto info = sessionInfo_.promote();
59 DP_CHECK_RETURN_LOG(info == nullptr, "VideoSessionInfo is nullptr.");
60 info->OnCallbackDied();
61 }
62
63 private:
64 wptr<VideoSessionInfo> sessionInfo_;
65 };
66
VideoSessionInfo(const int32_t userId,const sptr<IDeferredVideoProcessingSessionCallback> & callback)67 VideoSessionInfo::VideoSessionInfo(const int32_t userId, const sptr<IDeferredVideoProcessingSessionCallback>& callback)
68 : userId_(userId), callback_(callback), deathRecipient_(nullptr)
69 {
70 DP_DEBUG_LOG("entered. userId: %{public}d.", userId_);
71 Initialize();
72 }
73
~VideoSessionInfo()74 VideoSessionInfo::~VideoSessionInfo()
75 {
76 DP_INFO_LOG("entered. userId: %{public}d.", userId_);
77 DP_CHECK_EXECUTE(callback_ != nullptr && deathRecipient_ != nullptr, deathRecipient_->Destroy(callback_));
78 }
79
Initialize()80 int32_t VideoSessionInfo::Initialize()
81 {
82 DP_CHECK_ERROR_RETURN_RET_LOG(callback_ == nullptr, DP_NULL_POINTER,
83 "VideoSessionInfo init failed, callback is nullptr.");
84
85 session_ = sptr<DeferredVideoProcessingSession>::MakeSptr(userId_);
86 deathRecipient_ = sptr<CallbackDeathRecipient>::MakeSptr(this);
87
88 auto ret = deathRecipient_->Initialize(callback_);
89 DP_CHECK_ERROR_RETURN_RET_LOG(ret != DP_OK, ret, "set video DeathRecipient failed.");
90 return DP_OK;
91 }
92
GetDeferredVideoProcessingSession()93 sptr<IDeferredVideoProcessingSession> VideoSessionInfo::GetDeferredVideoProcessingSession()
94 {
95 return session_;
96 }
97
GetRemoteCallback()98 sptr<IDeferredVideoProcessingSessionCallback> VideoSessionInfo::GetRemoteCallback()
99 {
100 std::lock_guard lock(callbackMutex_);
101 return callback_;
102 }
103
OnCallbackDied()104 void VideoSessionInfo::OnCallbackDied()
105 {
106 auto ret = DPS_SendUrgentCommand<DeleteVideoSessionCommand>(this);
107 DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK, "DeleteVideoSession failed.");
108 }
109
SetCallback(const sptr<IDeferredVideoProcessingSessionCallback> & callback)110 void VideoSessionInfo::SetCallback(const sptr<IDeferredVideoProcessingSessionCallback>& callback)
111 {
112 DP_INFO_LOG("Reset video callback.");
113 std::lock_guard lock(callbackMutex_);
114 isCreate_ = false;
115 callback_ = callback;
116 auto ret = deathRecipient_->Initialize(callback_);
117 DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK, "Set video DeathRecipient failed.");
118 }
119 } // namespace DeferredProcessing
120 } // namespace CameraStandard
121 } // namespace OHOS
122