• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 NAPI_APPVOLUME_CHANGE_CALLBACK_H
17 #define NAPI_APPVOLUME_CHANGE_CALLBACK_H
18 
19 #include <uv.h>
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 #include "napi_async_work.h"
23 #include "napi_audio_manager.h"
24 #include "audio_system_manager.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 const std::string APP_VOLUME_CHANGE_CALLBACK_NAME_FOR_UID = "appVolumeChangeForUid";
29 const std::string APP_VOLUME_CHANGE_CALLBACK_NAME = "appVolumeChange";
30 class NapiAudioManagerAppVolumeChangeCallback : public AudioManagerAppVolumeChangeCallback {
31 public:
32     explicit NapiAudioManagerAppVolumeChangeCallback(napi_env env);
33     virtual ~NapiAudioManagerAppVolumeChangeCallback();
34     void SaveVolumeChangeCallbackForUidReference(const std::string &callbackName, napi_value args, int32_t appUid);
35     void SaveSelfVolumdChangeCallbackReference(const std::string &callbackName, napi_value args);
36     bool IsSameCallback(napi_env env, napi_value callback, napi_ref refCallback);
37     void RemoveCallbackReference(const napi_value args);
38     void OnAppVolumeChangedForUid(int32_t appUid, const VolumeEvent &event) override;
39     void OnSelfAppVolumeChanged(const VolumeEvent &event) override;
40     void CreateManagerAppVolumeChangeTsfn(napi_env env);
41     bool GetManagerAppVolumeChangeTsfnFlag();
42     void RemoveAudioVolumeChangeForUidCbRef(napi_env env, napi_value callback);
43     void RemoveAllAudioVolumeChangeForUidCbRef();
44     void RemoveSelfAudioVolumeChangeCbRef(napi_env env, napi_value callback);
45     void RemoveAllSelfAudioVolumeChangeCbRef();
46     int32_t GetAppVolumeChangeForUidListSize();
47     int32_t GetSelfAppVolumeChangeListSize();
48 private:
49     struct AudioManagerAppVolumeChangeJsCallback {
50         std::shared_ptr<AutoRef> callback = nullptr;
51         std::string callbackName = "unknown";
52         VolumeEvent appVolumeChangeEvent;
53     };
54 
55     void OnJsCallbackAppVolumeChange(std::unique_ptr<AudioManagerAppVolumeChangeJsCallback> &jsCb);
56     static void AppVolumeChangeTsfnFinalize(napi_env env, void *data, void *hint);
57     static void SafeJsCallbackAppVolumeChangeWork(napi_env env, napi_value js_cb, void *context, void *data);
58 
59     std::mutex mutex_;
60     napi_env env_ = nullptr;
61     std::shared_ptr<AutoRef> appVolumeChangeCallback_ = nullptr;
62     std::list<std::pair<std::shared_ptr<AutoRef>, int32_t>> appVolumeChangeForUidList_;
63     std::list<std::shared_ptr<AutoRef>> selfAppVolumeChangeList_;
64     bool regAmAppVolumeChgTsfn_ = false;
65     napi_threadsafe_function amAppVolumeChgTsfn_ = nullptr;
66 };
67 }
68 }
69 
70 #endif