1 /* 2 * Copyright (c) 2021-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 AUDIO_VOLUME_KEY_EVENT_NAPI_CALLBACK_H_ 17 #define AUDIO_VOLUME_KEY_EVENT_NAPI_CALLBACK_H_ 18 19 #include "audio_manager_napi.h" 20 #include "audio_system_manager.h" 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 24 namespace OHOS { 25 namespace AudioStandard { 26 struct VolumeKeyEventAutoRef { VolumeKeyEventAutoRefVolumeKeyEventAutoRef27 VolumeKeyEventAutoRef(napi_env env, napi_ref cb) 28 : env_(env), cb_(cb) 29 { 30 } ~VolumeKeyEventAutoRefVolumeKeyEventAutoRef31 ~VolumeKeyEventAutoRef() 32 { 33 if (env_ != nullptr && cb_ != nullptr) { 34 (void)napi_delete_reference(env_, cb_); 35 } 36 } 37 napi_env env_; 38 napi_ref cb_; 39 }; 40 41 class AudioVolumeKeyEventNapi : public VolumeKeyEventCallback { 42 public: 43 explicit AudioVolumeKeyEventNapi(napi_env env); 44 virtual ~AudioVolumeKeyEventNapi(); 45 46 /** 47 * @brief OnVolumeKeyEvent will be executed when hard volume key is pressed up/down 48 * 49 * @param streamType the stream type for which volume must be updated. 50 * @param volumeLevel the volume level to be updated. 51 * @param isUpdateUi whether to update volume level in UI. 52 **/ 53 void OnVolumeKeyEvent(VolumeEvent volumeEvent) override; 54 void SaveCallbackReference(const std::string &callbackName, napi_value callback); 55 56 private: 57 struct AudioVolumeKeyEventJsCallback { 58 std::shared_ptr<VolumeKeyEventAutoRef> callback = nullptr; 59 std::string callbackName = "unknown"; 60 VolumeEvent volumeEvent; 61 }; 62 63 void OnJsCallbackVolumeEvent(std::unique_ptr<AudioVolumeKeyEventJsCallback> &jsCb); 64 65 std::shared_ptr<VolumeKeyEventAutoRef> audioVolumeKeyEventJsCallback_ = nullptr; 66 std::mutex mutex_; 67 napi_env env_; 68 69 static napi_ref sConstructor_; 70 }; 71 } // namespace AudioStandard 72 } // namespace OHOS 73 #endif // AUDIO_VOLUME_KEY_EVENT_NAPI_CALLBACK_H_ 74