• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <memory>
17 
18 #include "avsession_log.h"
19 #include "avsession_info.h"
20 #include "avsession_trace.h"
21 #include "napi_session_listener.h"
22 
23 namespace OHOS::AVSession {
NapiSessionListener()24 NapiSessionListener::NapiSessionListener()
25 {
26     SLOGI("construct");
27 }
28 
~NapiSessionListener()29 NapiSessionListener::~NapiSessionListener()
30 {
31     SLOGI("destroy");
32 }
33 
34 template<typename T>
HandleEvent(int32_t event,const T & param)35 void NapiSessionListener::HandleEvent(int32_t event, const T& param)
36 {
37     std::lock_guard<std::mutex> lockGuard(lock_);
38     if (callbacks_[event].empty()) {
39         SLOGE("not register callback event=%{public}d", event);
40         return;
41     }
42     for (auto& ref : callbacks_[event]) {
43         asyncCallback_->Call(ref, [param](napi_env env, int& argc, napi_value* argv) {
44             argc = 1;
45             NapiUtils::SetValue(env, param, *argv);
46         });
47     }
48 }
49 
OnSessionCreate(const AVSessionDescriptor & descriptor)50 void NapiSessionListener::OnSessionCreate(const AVSessionDescriptor& descriptor)
51 {
52     AVSESSION_TRACE_SYNC_START("NapiSessionListener::OnSessionCreate");
53     SLOGI("sessionId=%{public}s", descriptor.sessionId_.c_str());
54     HandleEvent(EVENT_SESSION_CREATED, descriptor);
55 }
56 
OnSessionRelease(const AVSessionDescriptor & descriptor)57 void NapiSessionListener::OnSessionRelease(const AVSessionDescriptor& descriptor)
58 {
59     SLOGI("sessionId=%{public}s", descriptor.sessionId_.c_str());
60     HandleEvent(EVENT_SESSION_DESTROYED, descriptor);
61 }
62 
OnTopSessionChange(const AVSessionDescriptor & descriptor)63 void NapiSessionListener::OnTopSessionChange(const AVSessionDescriptor& descriptor)
64 {
65     AVSESSION_TRACE_SYNC_START("NapiSessionListener::OnTopSessionChange");
66     SLOGI("sessionId=%{public}s", descriptor.sessionId_.c_str());
67     HandleEvent(EVENT_TOP_SESSION_CHANGED, descriptor);
68 }
69 
AddCallback(napi_env env,int32_t event,napi_value callback)70 napi_status NapiSessionListener::AddCallback(napi_env env, int32_t event, napi_value callback)
71 {
72     std::lock_guard<std::mutex> lockGuard(lock_);
73     napi_ref ref = nullptr;
74     CHECK_AND_RETURN_RET_LOG(napi_ok == NapiUtils::GetRefByCallback(env, callbacks_[event], callback, ref),
75                              napi_generic_failure, "get callback reference failed");
76     CHECK_AND_RETURN_RET_LOG(ref == nullptr, napi_ok, "callback has been registered");
77     napi_status status = napi_create_reference(env, callback, 1, &ref);
78     if (status != napi_ok) {
79         SLOGE("napi_create_reference failed");
80         return status;
81     }
82     if (asyncCallback_ == nullptr) {
83         asyncCallback_ = std::make_shared<NapiAsyncCallback>(env);
84     }
85     callbacks_[event].push_back(ref);
86     return napi_ok;
87 }
88 
RemoveCallback(napi_env env,int32_t event,napi_value callback)89 napi_status NapiSessionListener::RemoveCallback(napi_env env, int32_t event, napi_value callback)
90 {
91     std::lock_guard<std::mutex> lockGuard(lock_);
92     if (callback == nullptr) {
93         for (auto& callbackRef : callbacks_[event]) {
94             napi_status ret = napi_delete_reference(env, callbackRef);
95             CHECK_AND_RETURN_RET_LOG(napi_ok == ret, ret, "delete callback reference failed");
96         }
97         callbacks_[event].clear();
98         return napi_ok;
99     }
100     napi_ref ref = nullptr;
101     CHECK_AND_RETURN_RET_LOG(napi_ok == NapiUtils::GetRefByCallback(env, callbacks_[event], callback, ref),
102                              napi_generic_failure, "get callback reference failed");
103     CHECK_AND_RETURN_RET_LOG(ref != nullptr, napi_ok, "callback has been remove");
104     callbacks_[event].remove(ref);
105     return napi_delete_reference(env, ref);
106 }
107 }