• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         std::weak_ptr<AutoRef> autoRef;
53         std::string callbackName = "unknown";
54         std::string errorMsg = "unknown";
55         int32_t errorCode = MSERR_EXT_UNKNOWN;
56         int32_t reason = 1;
57         int32_t loadSoundId = 0;
58     };
59     void OnJsErrorCallBack(SoundPoolJsCallBack *jsCb) const;
60     void OnJsloadCompletedCallBack(SoundPoolJsCallBack *jsCb) const;
61     void OnJsplayCompletedCallBack(SoundPoolJsCallBack *jsCb) const;
62     napi_env env_ = nullptr;
63     std::mutex mutex_;
64     std::map<std::string, std::weak_ptr<AutoRef>> refMap_;
65 };
66 } // namespace Media
67 } // namespace OHOS
68 #endif // SOUNDPOOL_CALLBACK_NAPI_H
69