• 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 #ifndef OHOS_NAPI_AVCONTROLLER_CALLBACK_H
17 #define OHOS_NAPI_AVCONTROLLER_CALLBACK_H
18 
19 #include <list>
20 #include "avsession_info.h"
21 #include "avsession_log.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "napi_async_callback.h"
25 
26 namespace OHOS::AVSession {
27 class NapiAVControllerCallback : public AVControllerCallback {
28 public:
29     enum {
30         EVENT_SESSION_DESTROY,
31         EVENT_PLAYBACK_STATE_CHANGE,
32         EVENT_META_DATA_CHANGE,
33         EVENT_ACTIVE_STATE_CHANGE,
34         EVENT_VALID_COMMAND_CHANGE,
35         EVENT_OUTPUT_DEVICE_CHANGE,
36         EVENT_TYPE_MAX
37     };
38 
39     NapiAVControllerCallback();
40     ~NapiAVControllerCallback() override;
41 
42     void OnSessionDestroy() override;
43     void OnPlaybackStateChange(const AVPlaybackState& state) override;
44     void OnMetaDataChange(const AVMetaData& data) override;
45     void OnActiveStateChange(bool isActive) override;
46     void OnValidCommandChange(const std::vector<int32_t>& cmds) override;
47     void OnOutputDeviceChange(const OutputDeviceInfo& info) override;
48 
49     napi_status AddCallback(napi_env env, int32_t event, napi_value callback);
50     napi_status RemoveCallback(napi_env env, int32_t event, napi_value callback);
51 
52 private:
53     void HandleEvent(int32_t event);
54 
55     template<typename T>
56     void HandleEvent(int32_t event, const T& param);
57 
58     std::mutex lock_;
59     std::shared_ptr<NapiAsyncCallback> asyncCallback_;
60     std::list<napi_ref> callbacks_[EVENT_TYPE_MAX] {};
61 };
62 } // namespace OHOS::AVSession
63 #endif // OHOS_NAPI_AVCONTROLLER_CALLBACK_H