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_PLAY_FINISHED_WITH_STREAM_ID = "playFinishedWithStreamId"; 32 const std::string EVENT_ERROR = "error"; 33 const std::string EVENT_ERROR_OCCURRED = "errorOccurred"; 34 } 35 36 class SoundPoolCallBackNapi : public ISoundPoolCallback { 37 public: 38 explicit SoundPoolCallBackNapi(napi_env env); 39 ~SoundPoolCallBackNapi() = default; 40 void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref); 41 void CancelCallbackReference(const std::string &name); 42 void ClearCallbackReference(); 43 void SendErrorCallback(int32_t errCode, const std::string &msg); 44 void SendErrorOccurredCallback(const Format &errorInfo); 45 void SendLoadCompletedCallback(int32_t soundId); 46 void SendPlayCompletedCallback(int32_t streamID); 47 48 protected: 49 void OnLoadCompleted(int32_t soundId) override; 50 void OnPlayFinished(int32_t streamID) override; 51 void OnError(int32_t errorCode) override; 52 void OnErrorOccurred(Format &errorInfo) override; 53 54 private: 55 struct SoundPoolJsCallBack { 56 void RunJsErrorCallBackTask(SoundPoolJsCallBack *event); 57 void RunJsErrorOccurredCallBackTask(SoundPoolJsCallBack *event); 58 void RunJsloadCompletedCallBackTask(SoundPoolJsCallBack *event); 59 void RunJsplayCompletedCallBackTask(SoundPoolJsCallBack *event); 60 61 std::weak_ptr<AutoRef> autoRef; 62 std::string callbackName = "unknown"; 63 std::string errorMsg = "unknown"; 64 int32_t errorCode = MSERR_EXT_UNKNOWN; 65 int32_t reason = 1; 66 int32_t loadSoundId = 0; 67 int32_t playFinishedStreamID = 0; 68 ERROR_TYPE errorType = ERROR_TYPE::LOAD_ERROR; 69 }; 70 void OnJsErrorCallBack(SoundPoolJsCallBack *jsCb) const; 71 void OnJsErrorOccurredCallBack(SoundPoolJsCallBack *jsCb) const; 72 void OnJsloadCompletedCallBack(SoundPoolJsCallBack *jsCb) const; 73 void OnJsplayCompletedCallBack(SoundPoolJsCallBack *jsCb) const; 74 int32_t GetExternalErrorCode(int32_t internalCode); 75 std::string GetErrorMsg(int32_t errorCode); 76 napi_env env_ = nullptr; 77 std::mutex mutex_; 78 std::map<std::string, std::weak_ptr<AutoRef>> refMap_; 79 }; 80 } // namespace Media 81 } // namespace OHOS 82 #endif // SOUNDPOOL_CALLBACK_NAPI_H 83