1 /*
2  * Copyright (C) 2022 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 #ifndef AVRECORDER_CALLBACK_H
16 #define AVRECORDER_CALLBACK_H
17 
18 #include "avrecorder_napi.h"
19 #include "recorder.h"
20 #include "av_common.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 class AVRecorderCallback : public RecorderCallback {
28 public:
29     explicit AVRecorderCallback(napi_env env);
30     virtual ~AVRecorderCallback();
31 
32     void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref);
33     void CancelCallbackReference(const std::string &name);
34     void ClearCallbackReference();
35     void SendErrorCallback(int32_t errCode, const std::string &msg);
36     void SendStateCallback(const std::string &state, const StateChangeReason &reason);
37     void SendAudioCaptureChangeCallback(const AudioRecorderChangeInfo &audioRecorderChangeInfo);
38     void SendPhotoAssertAvailableCallback(const std::string &uri);
39     std::string GetState();
40 
41 protected:
42     void OnError(RecorderErrorType errorType, int32_t errCode) override;
43     void OnInfo(int32_t type, int32_t extra) override;
44     void OnAudioCaptureChange(const AudioRecorderChangeInfo &audioRecorderChangeInfo) override;
45     void OnPhotoAssertAvailable(const std::string &uri) override;
46 
47 private:
48     struct AVRecordJsCallback {
49         std::weak_ptr<AutoRef> autoRef;
50         std::string callbackName = "unknown";
51         std::string errorMsg = "unknown";
52         int32_t errorCode = MSERR_EXT_UNKNOWN;
53         int32_t reason = 1;
54         std::string state = "unknown";
55         AudioRecorderChangeInfo audioRecorderChangeInfo;
56         std::string uri = "unknown";
57     };
58     void OnJsErrorCallBack(AVRecordJsCallback *jsCb) const;
59     void OnJsStateCallBack(AVRecordJsCallback *jsCb) const;
60     void OnJsAudioCaptureChangeCallback(AVRecordJsCallback *jsCb) const;
61 #ifdef SUPPORT_RECORDER_CREATE_FILE
62     void OnJsPhotoAssertAvailableCallback(AVRecordJsCallback *jsCb) const;
63 #endif
64     napi_env env_ = nullptr;
65     std::mutex mutex_;
66     std::string currentState_ = AVRecorderState::STATE_IDLE;
67     std::map<std::string, std::weak_ptr<AutoRef>> refMap_;
68 };
69 
70 class AudioCaptureChangeInfoJsCallback : public MediaJsResult {
71 public:
AudioCaptureChangeInfoJsCallback(AudioRecorderChangeInfo value)72     explicit AudioCaptureChangeInfoJsCallback(AudioRecorderChangeInfo value)
73         : value_(value)
74     {
75     }
76     ~AudioCaptureChangeInfoJsCallback() = default;
77     napi_status GetJsResult(napi_env env, napi_value &result);
78     napi_status SetAudioCapturerInfo(napi_env env, napi_value &captureInfo, napi_value &result);
79     napi_status SetDeviceInfo(napi_env env, napi_value &deviceDescriptors, napi_value &result);
80 
81 private:
82     AudioRecorderChangeInfo value_;
83     napi_status SetDeviceProperty(napi_env env, napi_value &element);
84 };
85 } // namespace Media
86 } // namespace OHOS
87 #endif // AVRECORDER_CALLBACK_H