• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 napi interface for cast session.
15  * Author: zhangjingnan
16  * Create: 2022-7-11
17  */
18 
19 #ifndef NAPI_CAST_SESSION_H_
20 #define NAPI_CAST_SESSION_H_
21 
22 #include <map>
23 #include <memory>
24 #include <mutex>
25 
26 #include "napi/native_api.h"
27 #include "napi/native_node_api.h"
28 #include "cast_engine_log.h"
29 #include "cast_engine_common.h"
30 #include "napi_errors.h"
31 #include "cast_engine_errors.h"
32 #include "i_cast_session.h"
33 #include "napi_cast_session_listener.h"
34 
35 namespace OHOS {
36 namespace CastEngine {
37 namespace CastEngineClient {
38 class EXPORT NapiCastSession {
39 public:
40     using OnEventHandlerType = std::function<napi_status(napi_env, napi_value, NapiCastSession *)>;
41     using OffEventHandlerType = std::function<napi_status(napi_env, napi_value, NapiCastSession *)>;
42 
43     static void DefineCastSessionJSClass(napi_env env);
44     static napi_status CreateNapiCastSession(napi_env env, std::shared_ptr<ICastSession> session, napi_value &out);
45     std::shared_ptr<NapiCastSessionListener> NapiListenerGetter();
46     void NapiListenerSetter(std::shared_ptr<NapiCastSessionListener> listener);
47 
NapiCastSession(std::shared_ptr<ICastSession> session)48     NapiCastSession(std::shared_ptr<ICastSession> session) : session_(session) {}
49     NapiCastSession() = default;
50     ~NapiCastSession() = default;
GetCastSession()51     std::shared_ptr<ICastSession> GetCastSession()
52     {
53         std::lock_guard<std::mutex> lock(mutex_);
54         return session_;
55     }
Reset()56     void Reset()
57     {
58         std::lock_guard<std::mutex> lock(mutex_);
59         session_.reset();
60         listener_.reset();
61     }
62 
63 private:
64     static napi_value NapiCastSessionConstructor(napi_env env, napi_callback_info info);
65     static napi_value AddDevice(napi_env env, napi_callback_info info);
66     static napi_value RemoveDevice(napi_env env, napi_callback_info info);
67     static napi_value GetSessionId(napi_env env, napi_callback_info info);
68     static napi_value SetSessionProperty(napi_env env, napi_callback_info info);
69     static napi_value CreateMirrorPlayer(napi_env env, napi_callback_info info);
70     static napi_value CreateStreamPlayer(napi_env env, napi_callback_info info);
71     static napi_value SetCastMode(napi_env env, napi_callback_info info);
72     static napi_value Release(napi_env env, napi_callback_info info);
73     static napi_value GetRemoteDeviceInfo(napi_env env, napi_callback_info info);
74 
75     static napi_value OnEvent(napi_env env, napi_callback_info info);
76     static napi_value OffEvent(napi_env env, napi_callback_info info);
77 
78     static napi_status OnInnerEvent(napi_env env, napi_value callback, NapiCastSession *napiSession);
79     static napi_status OnDeviceState(napi_env env, napi_value callback, NapiCastSession *napiSession);
80 
81     static napi_status OffInnerEvent(napi_env env, napi_value callback, NapiCastSession *napiSession);
82     static napi_status OffDeviceState(napi_env env, napi_value callback, NapiCastSession *napiSession);
83 
84     static NapiCastSession *GetNapiCastSession(napi_env env, napi_callback_info info);
85     static napi_status RegisterNativeSessionListener(NapiCastSession *napiSession);
86 
87     static std::map<std::string, std::pair<OnEventHandlerType, OffEventHandlerType>> eventHandlers_;
88     std::mutex mutex_;
89     std::shared_ptr<ICastSession> session_;
90     std::shared_ptr<NapiCastSessionListener> listener_;
91     static thread_local napi_ref consRef_;
92 };
93 } // namespace CastEngineClient
94 } // namespace CastEngine
95 } // namespace OHOS
96 #endif