1 /* 2 * Copyright (C) 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 */ 15 16 #ifndef AUDIO_HAPTIC_PLAYER_CALLBACK_NAPI_H 17 #define AUDIO_HAPTIC_PLAYER_CALLBACK_NAPI_H 18 19 #include "audio_info.h" 20 #include "napi/native_api.h" 21 #include "napi/native_node_api.h" 22 23 #include "audio_haptic_log.h" 24 #include "audio_haptic_player.h" 25 #include "audio_haptic_common_napi.h" 26 27 namespace OHOS { 28 namespace Media { 29 class AudioHapticPlayerCallbackNapi : public AudioHapticPlayerCallback { 30 public: 31 explicit AudioHapticPlayerCallbackNapi(napi_env env); 32 ~AudioHapticPlayerCallbackNapi(); 33 void SaveCallbackReference(const std::string &callbackName, napi_value callback); 34 void RemoveCallbackReference(const std::string &callbackName); 35 void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override; 36 void OnEndOfStream(void) override; 37 void OnError(int32_t errorCode) override; 38 private: 39 struct AudioHapticPlayerJsCallback { 40 std::shared_ptr<AutoRef> callback = nullptr; 41 std::string callbackName = "unknown"; 42 AudioStandard::InterruptEvent interruptEvent; 43 }; 44 45 void OnInterruptJsCallback(std::unique_ptr<AudioHapticPlayerJsCallback> &jsCb); 46 void OnEndOfStreamJsCallback(std::unique_ptr<AudioHapticPlayerJsCallback> &jsCb); 47 48 std::mutex cbMutex_; 49 napi_env env_ = nullptr; 50 std::shared_ptr<AutoRef> audioInterruptCb_ = nullptr; 51 std::shared_ptr<AutoRef> endOfStreamCb_ = nullptr; 52 }; 53 } // namespace Media 54 } // namespace OHOS 55 #endif // AUDIO_HAPTIC_PLAYER_CALLBACK_NAPI_H 56