• 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 #ifndef NAPI_AUDIO_CAPTURER_H
16 #define NAPI_AUDIO_CAPTURER_H
17 
18 #include <iostream>
19 #include <functional>
20 #include <map>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "audio_capturer.h"
25 #include "napi_async_work.h"
26 #include "napi_audio_capturer_callback_inner.h"
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 using namespace std;
31 
32 const std::string NAPI_AUDIO_CAPTURER_CLASS_NAME = "AudioCapturer";
33 
34 class NapiAudioCapturer {
35 public:
36     NapiAudioCapturer();
37     ~NapiAudioCapturer();
38 
39     static napi_value Init(napi_env env, napi_value exports);
40 #if !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
41     std::shared_ptr<AudioCapturer> audioCapturer_;
42 #else
43     std::unique_ptr<AudioCapturer> audioCapturer_;
44 #endif
45     std::mutex readCallbackMutex_;
46     std::condition_variable readCallbackCv_;
47     std::list<std::shared_ptr<NapiAudioCapturerCallbackInner>> audioCapturerCallbacks_;
48     std::atomic<bool> isFrameCallbackDone_;
49 
50 private:
51     struct AudioCapturerAsyncContext : public ContextBase {
~AudioCapturerAsyncContextAudioCapturerAsyncContext52         virtual ~AudioCapturerAsyncContext()
53         {
54             AUDIO_DEBUG_LOG("~AudioCapturerAsyncContext enter");
55         }
56         uint64_t time;
57         int32_t intValue;
58         uint32_t userSize;
59         uint8_t *buffer = nullptr;
60         size_t bytesRead;
61         size_t bufferSize;
62         uint32_t audioStreamId;
63         bool isBlocking;
64         bool isTrue;
65         SourceType sourceType;
66         AudioCapturerOptions capturerOptions;
67         AudioCapturerInfo capturerInfo;
68         AudioStreamInfo streamInfo;
69         Timestamp timeStamp;
70         uint32_t overflowCount;
71         bool muteWhenInterrupted;
72     };
73 
74     static napi_status InitAudioCapturer(napi_env env, napi_value &constructor);
75     static void Destructor(napi_env env, void *nativeObject, void *finalizeHint);
76     static napi_value Construct(napi_env env, napi_callback_info info);
77     static napi_value CreateAudioCapturerWrapper(napi_env env, const AudioCapturerOptions capturerOptions);
78     static napi_value CreateAudioCapturer(napi_env env, napi_callback_info info);
79     static napi_value CreateAudioCapturerSync(napi_env env, napi_callback_info info);
80     static napi_value GetCapturerInfo(napi_env env, napi_callback_info info);
81     static napi_value GetCapturerInfoSync(napi_env env, napi_callback_info info);
82     static napi_value GetStreamInfo(napi_env env, napi_callback_info info);
83     static napi_value GetStreamInfoSync(napi_env env, napi_callback_info info);
84     static napi_value Start(napi_env env, napi_callback_info info);
85     static napi_value Read(napi_env env, napi_callback_info info);
86     static napi_value GetAudioTime(napi_env env, napi_callback_info info);
87     static napi_value GetAudioTimeSync(napi_env env, napi_callback_info info);
88     static napi_value Stop(napi_env env, napi_callback_info info);
89     static napi_value Release(napi_env env, napi_callback_info info);
90     static napi_value GetBufferSize(napi_env env, napi_callback_info info);
91     static napi_value GetBufferSizeSync(napi_env env, napi_callback_info info);
92     static napi_value GetAudioStreamId(napi_env env, napi_callback_info info);
93     static napi_value GetAudioStreamIdSync(napi_env env, napi_callback_info info);
94     static napi_value GetAudioTimestampInfo(napi_env env, napi_callback_info info);
95     static napi_value GetAudioTimestampInfoSync(napi_env env, napi_callback_info info);
96     static napi_value On(napi_env env, napi_callback_info info);
97     static napi_value Off(napi_env env, napi_callback_info info);
98     static napi_value GetCurrentInputDevices(napi_env env, napi_callback_info info);
99     static napi_value GetCurrentAudioCapturerChangeInfo(napi_env env, napi_callback_info info);
100     static napi_value GetCurrentMicrophones(napi_env env, napi_callback_info info);
101     static napi_value GetState(napi_env env, napi_callback_info info);
102     static napi_value GetCallback(size_t argc, napi_value *argv);
103     static napi_value GetOverflowCount(napi_env env, napi_callback_info info);
104     static napi_value SetInputDeviceToAccessory(napi_env env, napi_callback_info info);
105     static napi_value GetOverflowCountSync(napi_env env, napi_callback_info info);
106     static napi_value SetWillMuteWhenInterrupted(napi_env env, napi_callback_info info);
107     static napi_value RegisterCallback(napi_env env, napi_value jsThis,
108         napi_value *argv, const std::string &cbName);
109     static napi_value RegisterCapturerCallback(napi_env env, napi_value *argv,
110         const std::string &cbName, NapiAudioCapturer *napiCapturer);
111     static napi_value RegisterPositionCallback(napi_env env, napi_value *argv,
112         const std::string &cbName, NapiAudioCapturer *napiCapturer);
113     static napi_value RegisterPeriodPositionCallback(napi_env env, napi_value *argv, const std::string &cbName,
114         NapiAudioCapturer *napiCapturer);
115     static void RegisterAudioCapturerDeviceChangeCallback(napi_env env, napi_value *argv,
116         const std::string &cbName, NapiAudioCapturer *napiCapturer);
117     static void RegisterAudioCapturerInfoChangeCallback(napi_env env, napi_value *argv,
118         const std::string &cbName, NapiAudioCapturer *napiCapturer);
119     static void RegisterCapturerReadDataCallback(napi_env env, napi_value *argv,
120         const std::string &cbName, NapiAudioCapturer *napiCapturer);
121     static napi_value UnregisterCallback(napi_env env, napi_value jsThis, size_t argc, napi_value *argv,
122         const std::string &cbName);
123     static void UnregisterCapturerCallback(napi_env env, size_t argc, const std::string &cbName,
124         napi_value *argv, NapiAudioCapturer *napiCapturer);
125     static void UnregisterAudioCapturerDeviceChangeCallback(napi_env env, size_t argc,
126         const std::string &cbName, napi_value *argv, NapiAudioCapturer *napiCapturer);
127     static void UnregisterAudioCapturerInfoChangeCallback(napi_env env, size_t argc,
128         const std::string &cbName, napi_value *argv, NapiAudioCapturer *napiCapturer);
129     static void UnregisterCapturerReadDataCallback(napi_env env, size_t argc, napi_value *argv,
130         NapiAudioCapturer *napiCapturer);
131     static void UnregisterCapturerPeriodPositionCallback(napi_env env, size_t argc,
132         const std::string &cbName, napi_value *argv, NapiAudioCapturer *napiCapturer);
133     static void UnregisterCapturerPositionCallback(napi_env env, size_t argc, const std::string &cbName,
134         napi_value *argv, NapiAudioCapturer *napiCapturer);
135 
136     /* common interface in NapiAudioCapturer */
137     static bool CheckContextStatus(std::shared_ptr<AudioCapturerAsyncContext> context);
138     static bool CheckAudioCapturerStatus(NapiAudioCapturer *napi, std::shared_ptr<AudioCapturerAsyncContext> context);
139     static NapiAudioCapturer* GetParamWithSync(const napi_env &env, napi_callback_info info,
140         size_t &argc, napi_value *args);
141     static unique_ptr<NapiAudioCapturer> CreateAudioCapturerNativeObject(napi_env env);
142     static napi_status ReadFromNative(shared_ptr<AudioCapturerAsyncContext> context);
143 
144     static std::unique_ptr<AudioCapturerOptions> sCapturerOptions_;
145     static std::mutex createMutex_;
146     static int32_t isConstructSuccess_;
147 
148     std::shared_ptr<AudioCapturerCallback> callbackNapi_ = nullptr;
149     std::shared_ptr<CapturerPositionCallback> positionCbNapi_ = nullptr;
150     std::shared_ptr<CapturerPeriodPositionCallback> periodPositionCbNapi_ = nullptr;
151     std::shared_ptr<AudioCapturerReadCallback> capturerReadDataCallbackNapi_ = nullptr;
152 
153     SourceType sourceType_;
154     napi_env env_;
155 };
156 } // namespace AudioStandard
157 } // namespace OHOS
158 #endif /* NAPI_AUDIO_CAPTURER_H */
159