1 /* 2 * Copyright (c) 2022-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 #ifndef OHOS_NAPI_SESSIONLISTENER_H 17 #define OHOS_NAPI_SESSIONLISTENER_H 18 19 #include <list> 20 #include <memory> 21 #include "avsession_info.h" 22 #include "avsession_manager.h" 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 #include "napi_utils.h" 26 #include "napi_async_callback.h" 27 28 namespace OHOS::AVSession { 29 class NapiSessionListener : public SessionListener { 30 public: 31 enum { 32 EVENT_SESSION_CREATED, 33 EVENT_SESSION_DESTROYED, 34 EVENT_TOP_SESSION_CHANGED, 35 EVENT_AUDIO_SESSION_CHECKED, 36 EVENT_DEVICE_AVAILABLE, 37 EVENT_DEVICE_LOG_EVENT, 38 EVENT_DEVICE_OFFLINE, 39 EVENT_REMOTE_DISTRIBUTED_SESSION_CHANGED, 40 EVENT_DEVICE_STATE_CHANGED, 41 EVENT_TYPE_MAX 42 }; 43 44 NapiSessionListener(); 45 ~NapiSessionListener() override; 46 47 void OnSessionCreate(const AVSessionDescriptor& descriptor) override; 48 void OnSessionRelease(const AVSessionDescriptor& descriptor) override; 49 void OnTopSessionChange(const AVSessionDescriptor& descriptor) override; 50 void OnAudioSessionChecked(const int32_t uid) override; 51 void OnDeviceAvailable(const OutputDeviceInfo& castOutputDeviceInfo) override; 52 void OnDeviceLogEvent(const DeviceLogEventCode eventId, const int64_t param) override; 53 void OnDeviceOffline(const std::string& deviceId) override; 54 void OnRemoteDistributedSessionChange( 55 const std::vector<sptr<IRemoteObject>>& sessionControllers) override; 56 void OnDeviceStateChange(const DeviceState& deviceState) override; 57 58 napi_status AddCallback(napi_env env, int32_t event, napi_value callback); 59 napi_status RemoveCallback(napi_env env, int32_t event, napi_value callback); 60 int32_t GetCallbackNum(int32_t event); 61 62 private: 63 template<typename T> 64 void HandleEvent(int32_t event, const T& param); 65 66 template<typename T> 67 void HandleEvent(int32_t event, const T& param, bool checkValid); 68 69 template<typename T, typename N> 70 void HandleEvent(int32_t event, const T& firstParam, const N& secondParam); 71 72 std::shared_ptr<NapiAsyncCallback> asyncCallback_; 73 std::mutex lock_; 74 std::list<napi_ref> callbacks_[EVENT_TYPE_MAX] {}; 75 std::shared_ptr<bool> isValid_ = std::make_shared<bool>(false); 76 77 static constexpr size_t UNMASK_CHAR_NUM = 3; 78 }; 79 } // namespace OHOS::AVSession 80 #endif // OHOS_NAPI_SESSIONLISTENER_H