1 /* 2 * Copyright (c) 2020-2021 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_MODULE_H 17 #define AUDIO_MODULE_H 18 19 #include "audio_player.h" 20 21 namespace OHOS { 22 namespace ACELite { 23 24 struct AudioCallbackInfo { 25 JSIValue thisVal_ = JSI::CreateUndefined(); 26 JSIValue callback = JSI::CreateUndefined(); 27 }; 28 29 struct FuncParams { 30 JSIValue handlerRef = JSI::CreateUndefined(); 31 JSIValue thisVarRef_ = JSI::CreateUndefined(); 32 const JSIValue *args = nullptr; 33 uint8_t argsSize = 0; 34 }; 35 36 class AudioModule final : public MemoryHeap { 37 public: 38 /** 39 * constructor 40 */ 41 AudioModule() = default; 42 43 /** 44 * desconstructor 45 */ 46 ~AudioModule() = default; 47 48 static JSIValue Oncallback(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 49 static JSIValue PlayRemote(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 50 51 static void OnPlayRemote(int32_t data); 52 53 static void OnEventAsyncWorkFunc(void *data); 54 55 /** 56 * Set volume, 0.0 means the no silcence, 1.0 means the original volume. 57 * 58 * @param volume Indicates the target volume to set, which ranges from 0.0 to 1.0 59 * @return Returns {@code true} if setting is successful; returns {@code false} otherwise. 60 */ 61 static bool SetVolume(float volume); 62 63 /** 64 * Call when the app is terminated 65 */ 66 static void OnTerminate(); 67 68 /** 69 * define property 70 */ 71 static void 72 DefineProperty(JSIValue target, const char *propName, JSIFunctionHandler getter, JSIFunctionHandler setter); 73 74 /** 75 * the play method of audio 76 * 77 * @param thisVal Indicates function execute context 78 * @param args Indicates the function arguments 79 * @param argsSize Indicates the size of function arguments 80 */ 81 static JSIValue Play(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 82 83 /** 84 * the pause method of audio 85 * 86 * @param thisVal Indicates function execute context 87 * @param args Indicates the function arguments 88 * @param argsSize Indicates the size of function arguments 89 */ 90 static JSIValue Pause(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 91 92 /** 93 * the stop method of audio 94 * 95 * @param thisVal Indicates function execute context 96 * @param args Indicates the function arguments 97 * @param argsSize Indicates the size of function arguments 98 */ 99 static JSIValue Stop(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 100 101 /** 102 * the getPlayState method of audio 103 * 104 * @param thisVal Indicates function execute context 105 * @param args Indicates the function arguments 106 * @param argsSize Indicates the size of function arguments 107 */ 108 static JSIValue GetPlayState(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 109 110 /** 111 * the getter of audio src property 112 * 113 * @param thisVal Indicates function execute context 114 * @param args Indicates the function arguments 115 * @param argsSize Indicates the size of function arguments 116 */ 117 static JSIValue SrcGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 118 119 /** 120 * the setter of audio src property 121 * 122 * @param thisVal Indicates function execute context 123 * @param args Indicates the function arguments 124 * @param argsSize Indicates the size of function arguments 125 */ 126 static JSIValue SrcSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 127 128 /** 129 * the getter of audio currentTime property 130 * 131 * @param thisVal Indicates function execute context 132 * @param args Indicates the function arguments 133 * @param argsSize Indicates the size of function arguments 134 */ 135 static JSIValue CurrentTimeGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 136 137 /** 138 * the setter of audio currentTime property 139 * 140 * @param thisVal Indicates function execute context 141 * @param args Indicates the function arguments 142 * @param argsSize Indicates the size of function arguments 143 */ 144 static JSIValue CurrentTimeSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 145 146 /** 147 * the getter of audio duration property 148 * 149 * @param thisVal Indicates function execute context 150 * @param args Indicates the function arguments 151 * @param argsSize Indicates the size of function arguments 152 */ 153 static JSIValue DurationGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 154 155 /** 156 * the getter of audio duration property 157 * 158 * @param thisVal Indicates function execute context 159 * @param args Indicates the function arguments 160 * @param argsSize Indicates the size of function arguments 161 */ 162 static JSIValue DurationSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 163 164 /** 165 * the getter of audio autoplay property 166 * 167 * @param thisVal Indicates function execute context 168 * @param args Indicates the function arguments 169 * @param argsSize Indicates the size of function arguments 170 */ 171 static JSIValue AutoPlayGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 172 173 /** 174 * the setter of audio autoplay property 175 * 176 * @param thisVal Indicates function execute context 177 * @param args Indicates the function arguments 178 * @param argsSize Indicates the size of function arguments 179 */ 180 static JSIValue AutoPlaySetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 181 182 /** 183 * the getter of audio loop property 184 * 185 * @param thisVal Indicates function execute context 186 * @param args Indicates the function arguments 187 * @param argsSize Indicates the size of function arguments 188 */ 189 static JSIValue LoopGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 190 191 /** 192 * the setter of audio loop property 193 * 194 * @param thisVal Indicates function execute context 195 * @param args Indicates the function arguments 196 * @param argsSize Indicates the size of function arguments 197 */ 198 static JSIValue LoopSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 199 200 /** 201 * the getter of audio volume property 202 * 203 * @param thisVal Indicates function execute context 204 * @param args Indicates the function arguments 205 * @param argsSize Indicates the size of function arguments 206 */ 207 static JSIValue VolumeGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 208 209 /** 210 * the setter of audio volume property 211 * 212 * @param thisVal Indicates function execute context 213 * @param args Indicates the function arguments 214 * @param argsSize Indicates the size of function arguments 215 */ 216 static JSIValue VolumeSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 217 218 /** 219 * the getter of audio muted property 220 * 221 * @param thisVal Indicates function execute context 222 * @param args Indicates the function arguments 223 * @param argsSize Indicates the size of function arguments 224 */ 225 static JSIValue MutedGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 226 227 /** 228 * the setter of audio muted property 229 * 230 * @param thisVal Indicates function execute context 231 * @param args Indicates the function arguments 232 * @param argsSize Indicates the size of function arguments 233 */ 234 static JSIValue MutedSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 235 236 /** 237 * the getter of audio onplay event 238 * 239 * @param thisVal Indicates function execute context 240 * @param args Indicates the function arguments 241 * @param argsSize Indicates the size of function arguments 242 */ 243 static JSIValue OnPlayGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 244 245 /** 246 * the setter of audio onplay event 247 * 248 * @param thisVal Indicates function execute context 249 * @param args Indicates the function arguments 250 * @param argsSize Indicates the size of function arguments 251 */ 252 static JSIValue OnPlaySetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 253 254 /** 255 * the getter of audio onpause event 256 * 257 * @param thisVal Indicates function execute context 258 * @param args Indicates the function arguments 259 * @param argsSize Indicates the size of function arguments 260 */ 261 static JSIValue OnPauseGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 262 263 /** 264 * the setter of audio onpause event 265 * 266 * @param thisVal Indicates function execute context 267 * @param args Indicates the function arguments 268 * @param argsSize Indicates the size of function arguments 269 */ 270 static JSIValue OnPauseSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 271 272 /** 273 * the getter of audio onstop event 274 * 275 * @param thisVal Indicates function execute context 276 * @param args Indicates the function arguments 277 * @param argsSize Indicates the size of function arguments 278 */ 279 static JSIValue OnStopGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 280 281 /** 282 * the setter of audio onstop event 283 * 284 * @param thisVal Indicates function execute context 285 * @param args Indicates the function arguments 286 * @param argsSize Indicates the size of function arguments 287 */ 288 static JSIValue OnStopSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 289 290 /** 291 * the getter of audio onloadeddata event 292 * 293 * @param thisVal Indicates function execute context 294 * @param args Indicates the function arguments 295 * @param argsSize Indicates the size of function arguments 296 */ 297 static JSIValue OnLoadedDataGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 298 299 /** 300 * the setter of audio onloadeddata event 301 * 302 * @param thisVal Indicates function execute context 303 * @param args Indicates the function arguments 304 * @param argsSize Indicates the size of function arguments 305 */ 306 static JSIValue OnLoadedDataSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 307 308 /** 309 * the getter of audio onerror event 310 * 311 * @param thisVal Indicates function execute context 312 * @param args Indicates the function arguments 313 * @param argsSize Indicates the size of function arguments 314 */ 315 static JSIValue OnEndedGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 316 317 /** 318 * the setter of audio onerror event 319 * 320 * @param thisVal Indicates function execute context 321 * @param args Indicates the function arguments 322 * @param argsSize Indicates the size of function arguments 323 */ 324 static JSIValue OnEndedSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 325 326 /** 327 * the getter of audio onerror event 328 * 329 * @param thisVal Indicates function execute context 330 * @param args Indicates the function arguments 331 * @param argsSize Indicates the size of function arguments 332 */ 333 static JSIValue OnErrorGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 334 335 /** 336 * the setter of audio onerror event 337 * 338 * @param thisVal Indicates function execute context 339 * @param args Indicates the function arguments 340 * @param argsSize Indicates the size of function arguments 341 */ 342 static JSIValue OnErrorSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 343 344 /** 345 * the getter of audio ontimeupdate event 346 * 347 * @param thisVal Indicates function execute context 348 * @param args Indicates the function arguments 349 * @param argsSize Indicates the size of function arguments 350 */ 351 static JSIValue OnTimeUpdateGetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 352 353 /** 354 * the setter of audio ontimeupdate event 355 * 356 * @param thisVal Indicates function execute context 357 * @param args Indicates the function arguments 358 * @param argsSize Indicates the size of function arguments 359 */ 360 static JSIValue OnTimeUpdateSetter(const JSIValue thisVal, const JSIValue *args, uint8_t argsSize); 361 362 private: 363 static JSIValue GetEventCallback(const AudioEventListener *listener); 364 365 static AudioEventListener *CreateAudioEventListener(const JSIValue *args, uint8_t argsSize); 366 367 static AudioCallbackInfo audiocallbackinfo_; 368 }; 369 370 void InitAudioModule(JSIValue exports); 371 } // namespace ACELite 372 } // namespace OHOS 373 #endif // AUDIO_MODULE_H