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 * Description: supply cast session listener for napi interface. 15 * Author: zhangjingnan 16 * Create: 2022-7-11 17 */ 18 19 #ifndef NAPI_CAST_SESSION_LISTENER_H 20 #define NAPI_CAST_SESSION_LISTENER_H 21 22 #include <memory> 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 #include <list> 26 #include "i_cast_session.h" 27 #include "napi_callback.h" 28 #include "cast_engine_common.h" 29 #include "napi_castengine_utils.h" 30 31 namespace OHOS { 32 namespace CastEngine { 33 namespace CastEngineClient { 34 class NapiCastSessionListener : public ICastSessionListener { 35 public: 36 using NapiArgsGetter = std::function<void(napi_env env, int &argc, napi_value *argv)>; 37 enum { 38 EVENT_ON_EVENT, 39 EVENT_DEVICE_STATE, 40 EVENT_REMOTE_CTRL, 41 EVENT_TYPE_MAX 42 }; NapiCastSessionListener()43 NapiCastSessionListener() {}; 44 ~NapiCastSessionListener() override; 45 46 void OnDeviceState(const DeviceStateInfo &stateEvent) override; 47 void OnEvent(const EventId &eventId, const std::string &jsonParam) override; 48 void OnRemoteCtrlEvent(int eventType, const uint8_t *data, uint32_t len) override; 49 50 napi_status AddCallback(napi_env env, int32_t event, napi_value callback); 51 napi_status RemoveCallback(napi_env env, int32_t event, napi_value callback); 52 53 private: 54 void HandleEvent(int32_t event, NapiArgsGetter &getter); 55 56 std::mutex lock_; 57 std::shared_ptr<NapiCallback> callback_; 58 }; 59 } // namespace CastEngineClient 60 } // namespace CastEngine 61 } // namespace OHOS 62 #endif