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 AUDIO_STREAM_MGR_NAPI_H_ 17 #define AUDIO_STREAM_MGR_NAPI_H_ 18 19 #include "audio_errors.h" 20 #include "audio_renderer.h" 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 #include "audio_stream_manager.h" 24 #include "audio_system_manager.h" 25 26 namespace OHOS { 27 namespace AudioStandard { 28 static const std::string AUDIO_STREAM_MGR_NAPI_CLASS_NAME = "AudioStreamManager"; 29 30 class AudioStreamMgrNapi { 31 public: 32 AudioStreamMgrNapi(); 33 ~AudioStreamMgrNapi(); 34 35 static napi_value Init(napi_env env, napi_value exports); 36 static napi_value CreateStreamManagerWrapper(napi_env env); 37 static napi_value GetStreamManager(napi_env env, napi_callback_info info); 38 39 private: 40 struct AudioStreamMgrAsyncContext { 41 napi_env env; 42 napi_async_work work; 43 napi_deferred deferred; 44 napi_ref callbackRef = nullptr; 45 int32_t status = SUCCESS; 46 int32_t volType; 47 int32_t contentType; 48 int32_t streamUsage; 49 bool isTrue; 50 bool isLowLatencySupported; 51 bool isActive; 52 AudioStreamInfo audioStreamInfo; 53 AudioStreamMgrNapi *objectInfo; 54 std::vector<std::unique_ptr<AudioRendererChangeInfo>> audioRendererChangeInfos; 55 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> audioCapturerChangeInfos; 56 AudioSceneEffectInfo audioSceneEffectInfo; 57 }; 58 59 static napi_value GetCurrentAudioRendererInfos(napi_env env, napi_callback_info info); 60 static napi_value GetCurrentAudioRendererInfosSync(napi_env env, napi_callback_info info); 61 static napi_value GetCurrentAudioCapturerInfos(napi_env env, napi_callback_info info); 62 static napi_value GetCurrentAudioCapturerInfosSync(napi_env env, napi_callback_info info); 63 static napi_value On(napi_env env, napi_callback_info info); 64 static napi_value Off(napi_env env, napi_callback_info info); 65 static napi_value IsAudioRendererLowLatencySupported(napi_env env, napi_callback_info info); 66 static napi_value IsStreamActive(napi_env env, napi_callback_info info); 67 static napi_value IsStreamActiveSync(napi_env env, napi_callback_info info); 68 static void RegisterCallback(napi_env env, napi_value jsThis, 69 napi_value* args, const std::string& cbName); 70 static void RegisterCapturerStateChangeCallback(napi_env env, napi_value* args, 71 const std::string& cbName, AudioStreamMgrNapi *streamMgrNapi); 72 static void RegisterRendererStateChangeCallback(napi_env env, napi_value* args, 73 const std::string& cbName, AudioStreamMgrNapi *streamMgrNapi); 74 static void UnregisterCallback(napi_env env, napi_value jsThis, const std::string& cbName); 75 static napi_value Construct(napi_env env, napi_callback_info info); 76 static void Destructor(napi_env env, void *nativeObject, void *finalize_hint); 77 static bool ParseAudioStreamInfo(napi_env env, napi_value root, AudioStreamInfo &audioStreamInfo); 78 static void IsLowLatencySupportedCallback(napi_env env, napi_status status, void *data); 79 static napi_value GetEffectInfoArray(napi_env env, napi_callback_info info); 80 static napi_value GetEffectInfoArraySync(napi_env env, napi_callback_info info); 81 static void GetCurrentCapturerChangeInfosCallbackComplete(napi_env env, napi_status status, void *data); 82 static void IsTrueAsyncCallbackComplete(napi_env env, napi_status status, void *data); 83 static void GetEffectInfoArrayCallbackComplete(napi_env env, napi_status status, void *data); 84 static void GetCurrentRendererChangeInfosCallbackComplete(napi_env env, napi_status status, void *data); 85 static void CommonCallbackRoutine(napi_env env, AudioStreamMgrAsyncContext* &asyncContext, 86 const napi_value &valueParam); 87 static void AsyncGetEffectInfoArray(napi_env env, void *data); 88 static void GetEffectInfoArrayResult(napi_env env, std::unique_ptr<AudioStreamMgrAsyncContext> &asyncContext, 89 napi_status status, napi_value &result); 90 napi_env env_; 91 AudioStreamManager *audioStreamMngr_; 92 AudioSystemManager *audioMngr_; 93 int32_t cachedClientId_ = -1; 94 std::shared_ptr<AudioRendererStateChangeCallback> rendererStateChangeCallbackNapi_ = nullptr; 95 std::shared_ptr<AudioCapturerStateChangeCallback> capturerStateChangeCallbackNapi_ = nullptr; 96 }; 97 } // namespace AudioStandard 98 } // namespace OHOS 99 #endif /* AUDIO_STREAM_MGR_NAPI_H_ */ 100