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_CALLBACK_NAPI_H 17 #define SOUNDPOOL_CALLBACK_NAPI_H 18 19 #include "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 #include "common_napi.h" 22 #include "media_errors.h" 23 #include "media_log.h" 24 #include "isoundpool.h" 25 26 namespace OHOS { 27 namespace Media { 28 namespace SoundPoolEvent { 29 const std::string EVENT_LOAD_COMPLETED = "loadComplete"; 30 const std::string EVENT_PLAY_FINISHED = "playFinished"; 31 const std::string EVENT_ERROR = "error"; 32 } 33 34 class SoundPoolCallBackNapi : public ISoundPoolCallback { 35 public: 36 explicit SoundPoolCallBackNapi(napi_env env); 37 ~SoundPoolCallBackNapi() = default; 38 void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref); 39 void CancelCallbackReference(const std::string &name); 40 void ClearCallbackReference(); 41 void SendErrorCallback(int32_t errCode, const std::string &msg); 42 void SendLoadCompletedCallback(int32_t soundId); 43 void SendPlayCompletedCallback(); 44 45 protected: 46 void OnLoadCompleted(int32_t soundId) override; 47 void OnPlayFinished() override; 48 void OnError(int32_t errorCode) override; 49 50 private: 51 struct SoundPoolJsCallBack { 52 void RunJsErrorCallBackTask(int status, SoundPoolJsCallBack *event); 53 void RunJsloadCompletedCallBackTask(int status, SoundPoolJsCallBack *event); 54 void RunJsplayCompletedCallBackTask(int status, SoundPoolJsCallBack *event); 55 56 std::weak_ptr<AutoRef> autoRef; 57 std::string callbackName = "unknown"; 58 std::string errorMsg = "unknown"; 59 int32_t errorCode = MSERR_EXT_UNKNOWN; 60 int32_t reason = 1; 61 int32_t loadSoundId = 0; 62 }; 63 void OnJsErrorCallBack(SoundPoolJsCallBack *jsCb) const; 64 void OnJsloadCompletedCallBack(SoundPoolJsCallBack *jsCb) const; 65 void OnJsplayCompletedCallBack(SoundPoolJsCallBack *jsCb) const; 66 napi_env env_ = nullptr; 67 std::mutex mutex_; 68 std::map<std::string, std::weak_ptr<AutoRef>> refMap_; 69 }; 70 } // namespace Media 71 } // namespace OHOS 72 #endif // SOUNDPOOL_CALLBACK_NAPI_H 73