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 #ifndef NAPI_AUDIO_RENDERER_CALLBACK_H 16 #define NAPI_AUDIO_RENDERER_CALLBACK_H 17 18 #include <uv.h> 19 20 #include "napi/native_api.h" 21 #include "napi/native_node_api.h" 22 #include "napi_async_work.h" 23 #include "audio_renderer.h" 24 25 namespace OHOS { 26 namespace AudioStandard { 27 const std::string INTERRUPT_CALLBACK_NAME = "interrupt"; 28 const std::string AUDIO_INTERRUPT_CALLBACK_NAME = "audioInterrupt"; 29 const std::string STATE_CHANGE_CALLBACK_NAME = "stateChange"; 30 const std::string MARK_REACH_CALLBACK_NAME = "markReach"; 31 const std::string PERIOD_REACH_CALLBACK_NAME = "periodReach"; 32 const std::string DATA_REQUEST_CALLBACK_NAME = "dataRequest"; 33 const std::string DEVICECHANGE_CALLBACK_NAME = "outputDeviceChange"; 34 const std::string OUTPUT_DEVICECHANGE_WITH_INFO = "outputDeviceChangeWithInfo"; 35 const std::string WRITE_DATA_CALLBACK_NAME = "writeData"; 36 37 class NapiAudioRendererCallback : public AudioRendererCallback { 38 public: 39 explicit NapiAudioRendererCallback(napi_env env); 40 virtual ~NapiAudioRendererCallback(); 41 void OnInterrupt(const InterruptEvent &interruptEvent) override; 42 void OnStateChange(const RendererState state, const StateChangeCmdType __attribute__((unused)) cmdType) override; 43 void SaveCallbackReference(const std::string &callbackName, napi_value args); 44 void RemoveCallbackReference(const std::string &callbackName); 45 46 private: 47 struct AudioRendererJsCallback { 48 std::shared_ptr<AutoRef> callback = nullptr; 49 std::string callbackName = "unknown"; 50 InterruptEvent interruptEvent; 51 RendererState state; 52 }; 53 54 static void WorkCallbackStateChangeDone(uv_work_t *work, int status); 55 static void WorkCallbackInterruptDone(uv_work_t *work, int status); 56 57 void OnJsCallbackInterrupt(std::unique_ptr<AudioRendererJsCallback> &jsCb); 58 void OnJsCallbackStateChange(std::unique_ptr<AudioRendererJsCallback> &jsCb); 59 60 std::mutex mutex_; 61 napi_env env_ = nullptr; 62 std::shared_ptr<AutoRef> interruptCallback_ = nullptr; 63 std::shared_ptr<AutoRef> stateChangeCallback_ = nullptr; 64 }; 65 } // namespace AudioStandard 66 } // namespace OHOS 67 #endif /* NAPI_AUDIO_RENDERER_CALLBACK_H */ 68