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 SOUNDPOOL_NAPI_H 17 #define SOUNDPOOL_NAPI_H 18 19 #include "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 #include "common_napi.h" 22 #include "media_dfx.h" 23 #include "soundpool_callback_napi.h" 24 25 namespace OHOS { 26 namespace Media { 27 using RetInfo = std::pair<int32_t, std::string>; 28 const int PARAM0 = 0; 29 const int PARAM1 = 1; 30 const int PARAM2 = 2; 31 const int PARAM3 = 3; 32 const int PARAM4 = 4; 33 const int MAX_PARAM = 5; 34 35 struct SoundPoolAsyncContext; 36 37 class SoundPoolNapi { 38 public: 39 __attribute__((visibility("default"))) static napi_value Init(napi_env env, napi_value exports); 40 41 private: SoundPoolNapi()42 SoundPoolNapi() {} 43 ~SoundPoolNapi(); 44 static napi_value Constructor(napi_env env, napi_callback_info info); 45 static napi_value ConstructorParallel(napi_env env, napi_callback_info info); 46 static void Destructor(napi_env env, void *nativeObject, void *finalize); 47 48 /** 49 * createSoundPool(maxStreams: number, audioRenderInfo: audio.AudioRendererInfo, 50 * callback: AsyncCallback<SoundPool>): void 51 * 52 * createSoundPool(maxStreams: number, audioRenderInfo: audio.AudioRendererInfo): Promise<SoundPool> 53 */ 54 static napi_value JsCreateSoundPool(napi_env env, napi_callback_info info); 55 56 /** 57 * createParallelSoundPool(maxStreams: number, audioRenderInfo: audio.AudioRendererInfo): Promise<SoundPool> 58 */ 59 static napi_value JsCreateParallelSoundPool(napi_env env, napi_callback_info info); 60 61 /** 62 * load(uri: string, callback: AsyncCallback<number>): void 63 * load(uri: string): Promise<number> 64 * 65 * or 66 * 67 * load(fd: number, offset: number, length: number, callback: AsyncCallback<number>): void 68 * load(fd: number, offset: number, length: number): Promise<number> 69 */ 70 static napi_value JsLoad(napi_env env, napi_callback_info info); 71 72 /** 73 * play(soundID: number, params?: PlayParameters): Promise<number> 74 * play(soundID: number, callback: AsyncCallback<number>): void 75 * play(soundID: number, params: PlayParameters, callback: AsyncCallback<number>): void 76 */ 77 static napi_value JsPlay(napi_env env, napi_callback_info info); 78 79 /** 80 * stop(streamID: number, callback: AsyncCallback<void>): void 81 * stop(streamID: number): Promise<void> 82 */ 83 static napi_value JsStop(napi_env env, napi_callback_info info); 84 85 /** 86 * setLoop(streamID: number, loop: number, callback: AsyncCallback<void>): void 87 * setLoop(streamID: number, loop: number): Promise<void> 88 */ 89 static napi_value JsSetLoop(napi_env env, napi_callback_info info); 90 91 /** 92 * setPriority(streamID: number, priority: number, callback: AsyncCallback<void>): void 93 * setPriority(streamID: number, priority: number): Promise<void> 94 */ 95 static napi_value JsSetPriority(napi_env env, napi_callback_info info); 96 97 /** 98 * setRate(streamID: number, rate: audio.AudioRendererRate, callback: AsyncCallback<void>): void 99 * setRate(streamID: number, rate: audio.AudioRendererRate): Promise<void> 100 */ 101 static napi_value JsSetRate(napi_env env, napi_callback_info info); 102 103 /** 104 * setVolume(streamID: number, leftVolume: number, rightVolume: number, callback: AsyncCallback<void>): void 105 * setVolume(streamID: number, leftVolume: number, rightVolume: number): Promise<void> 106 */ 107 static napi_value JsSetVolume(napi_env env, napi_callback_info info); 108 109 /** 110 * unload(soundID: number, callback: AsyncCallback<void>): void 111 * unload(soundID: number): Promise<void> 112 */ 113 static napi_value JsUnload(napi_env env, napi_callback_info info); 114 115 /** 116 * release(callback: AsyncCallback<void>): void 117 * release(): Promise<void> 118 */ 119 static napi_value JsRelease(napi_env env, napi_callback_info info); 120 121 /** 122 * 123 * on(type: 'loadCompleted', callback: Callback<number>): void 124 * off(type: 'loadCompleted'): void 125 * 126 * on(type: 'playFinished', callback: Callback<void>): void 127 * off(type: 'playFinished'): void 128 * 129 * on(type: 'error', callback: ErrorCallback): void 130 * off(type: 'error'): void 131 * 132 */ 133 static napi_value JsSetOnCallback(napi_env env, napi_callback_info info); 134 static napi_value JsClearOnCallback(napi_env env, napi_callback_info info); 135 // Use to get soundpool instance. 136 static SoundPoolNapi* GetJsInstanceAndArgs(napi_env env, napi_callback_info info, 137 size_t &argCount, napi_value *args); 138 static napi_status GetJsInstanceWithParameter(napi_env env, napi_value *argv, int32_t argvLength); 139 static void SendCompleteEvent(napi_env env, std::unique_ptr<SoundPoolAsyncContext> asyncCtx); 140 static bool IsSystemApp(); 141 int32_t ParserLoadOptionFromJs(std::unique_ptr<SoundPoolAsyncContext> &asyncCtx, 142 napi_env env, napi_value *argv, size_t argCount); 143 int32_t ParserPlayOptionFromJs(std::unique_ptr<SoundPoolAsyncContext> &asyncCtx, 144 napi_env env, napi_value *argv, size_t argCount); 145 int32_t ParserRateOptionFromJs(std::unique_ptr<SoundPoolAsyncContext> &asyncCtx, 146 napi_env env, napi_value *argv); 147 int32_t ParserVolumeOptionFromJs(std::unique_ptr<SoundPoolAsyncContext> &asyncCtx, 148 napi_env env, napi_value *argv); 149 bool GetPropertyBool(napi_env env, napi_value configObj, const std::string &type, bool &result); 150 151 void ErrorCallback(int32_t errCode, const std::string &operate, const std::string &add = ""); 152 void SetCallbackReference(const std::string &callbackName, std::shared_ptr<AutoRef> ref); 153 void CancelCallbackReference(const std::string &callbackName); 154 void CancelCallback(std::shared_ptr<ISoundPoolCallback> callback); 155 156 static thread_local napi_ref constructor_; 157 static thread_local napi_ref constructorParallel_; 158 static int32_t maxStreams; 159 static AudioStandard::AudioRendererInfo rendererInfo; 160 161 napi_env env_ = nullptr; 162 std::shared_ptr<ISoundPool> soundPool_; 163 std::shared_ptr<ISoundPoolCallback> callbackNapi_; 164 std::map<std::string, std::shared_ptr<AutoRef>> eventCbMap_; 165 }; 166 167 struct SoundPoolAsyncContext : public MediaAsyncContext { SoundPoolAsyncContextSoundPoolAsyncContext168 explicit SoundPoolAsyncContext(napi_env env) : MediaAsyncContext(env) {} 169 ~SoundPoolAsyncContext() = default; 170 void SoundPoolAsyncSignError(int32_t errCode, const std::string &operate, 171 const std::string ¶m, const std::string &add = ""); 172 SoundPoolNapi *napi = nullptr; 173 std::shared_ptr<ISoundPool> soundPool_; 174 std::shared_ptr<ISoundPoolCallback> callbackNapi_; 175 std::string url_ = ""; 176 int32_t fd_ = 0; 177 int64_t offset_ = 0; 178 int64_t length_ = 0; 179 int32_t soundId_ = 0; 180 PlayParams playParameters_; 181 int32_t streamId_ = 0; 182 int32_t loop_ = 0; 183 int32_t priority_ = 0; 184 float leftVolume_ = 0.0f; 185 float rightVolume_ = 0.0f; 186 AudioStandard::AudioRendererRate renderRate_ = AudioStandard::AudioRendererRate::RENDER_RATE_NORMAL; 187 }; 188 } // namespace Media 189 } // namespace OHOS 190 #endif // SOUNDPOOL_NAPI_H