1 /*
2  * Copyright (C) 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 RECORDER_CALLBACK_NAPI_H_
17 #define RECORDER_CALLBACK_NAPI_H_
18 
19 #include "audio_recorder_napi.h"
20 #include "recorder.h"
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 #include "common_napi.h"
24 
25 namespace OHOS {
26 namespace Media {
27 const std::string ERROR_CALLBACK_NAME = "error";
28 const std::string PREPARE_CALLBACK_NAME = "prepare";
29 const std::string START_CALLBACK_NAME = "start";
30 const std::string PAUSE_CALLBACK_NAME = "pause";
31 const std::string RESUME_CALLBACK_NAME = "resume";
32 const std::string STOP_CALLBACK_NAME = "stop";
33 const std::string RESET_CALLBACK_NAME = "reset";
34 const std::string RELEASE_CALLBACK_NAME = "release";
35 class RecorderCallbackNapi : public RecorderCallback {
36 public:
37     explicit RecorderCallbackNapi(napi_env env, bool isVideo);
38     virtual ~RecorderCallbackNapi();
39 
40     void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref);
41     void ClearCallbackReference();
42     void SendErrorCallback(int32_t errCode);
43     void SendStateCallback(const std::string &callbackName);
44 
45 protected:
46     void OnError(RecorderErrorType errorType, int32_t errCode) override;
47     void OnInfo(int32_t type, int32_t extra) override;
48     void OnAudioCaptureChange(const AudioRecorderChangeInfo &audioRecorderChangeInfo) override;
49 
50 private:
51     struct RecordJsCallback {
52         std::weak_ptr<AutoRef> autoRef;
53         std::string callbackName = "unknown";
54         std::string errorMsg = "unknown";
55         int32_t errorCode = MSERR_EXT_UNKNOWN;
56     };
57     void OnJsErrorCallBack(RecordJsCallback *jsCb) const;
58     void OnJsStateCallBack(RecordJsCallback *jsCb) const;
59     napi_env env_ = nullptr;
60     std::mutex mutex_;
61     std::map<std::string, std::weak_ptr<AutoRef>> refMap_;
62     bool isVideo_ = false;
63 };
64 } // namespace Media
65 } // namespace OHOS
66 #endif // RECORDER_CALLBACK_NAPI_H_
67