1cab88e2fbca5254cc8b79dedd89da255a952f4e
[mirrors/FD230.git] /
1 /*\r
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.\r
3  * Licensed under the Apache License, Version 2.0 (the "License");\r
4  * you may not use this file except in compliance with the License.\r
5  * You may obtain a copy of the License at\r
6  *\r
7  *     http://www.apache.org/licenses/LICENSE-2.0\r
8  *\r
9  * Unless required by applicable law or agreed to in writing, software\r
10  * distributed under the License is distributed on an "AS IS" BASIS,\r
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12  * See the License for the specific language governing permissions and\r
13  * limitations under the License.\r
14  */\r
15  \r
16 /**\r
17  * @addtogroup MultiMedia_AudioCapturer\r
18  * @{\r
19  *\r
20  * @brief Declares APIs in the <b>AudioCapturer</b> class for audio capture.\r
21  *\r
22  *\r
23  * @since 1.0\r
24  * @version 1.0\r
25  */\r
26 \r
27 /**\r
28  * @file audio_capturer.h\r
29  *\r
30  * @brief Provides the <b>AudioCapturer</b> class to implement operations related to audio capture.\r
31  *\r
32  *\r
33  * @since 1.0\r
34  * @version 1.0\r
35  */\r
36 \r
37 #ifndef AUDIO_CAPTURER_H\r
38 #define AUDIO_CAPTURER_H\r
39 \r
40 #include <cstddef>\r
41 #include <cstdint>\r
42 #include <time.h>\r
43 #include <memory>\r
44 \r
45 #include "media_errors.h"\r
46 #include "audio_base_type.h"\r
47 \r
48 namespace OHOS {\r
49 namespace Audio {\r
50 /**\r
51  * @brief Defines information about audio capture parameters, including the input source, audio codec format,sampling\r
52  * rate (Hz), number of audio channels, bit rate, and bit width.\r
53  *\r
54  * @since 1.0\r
55  * @version 1.0\r
56  */\r
57 struct AudioCapturerInfo {\r
58     /** Audio source type */\r
59     AudioSourceType inputSource = AUDIO_MIC;\r
60     /** Audio codec format */\r
61     AudioCodecFormat audioFormat = AUDIO_DEFAULT;\r
62     /** Sampling rate */\r
63     int32_t sampleRate = 0;\r
64     /** Number of audio channels */\r
65     int32_t channelCount = 0;\r
66     /** Bit rate */\r
67     int32_t bitRate = 0;\r
68     /** Audio stream type */\r
69     AudioStreamType streamType = TYPE_MEDIA;\r
70     /** Bit width */\r
71     AudioBitWidth bitWidth = BIT_WIDTH_16;\r
72     /** Session id */\r
73     AudioSession sessionID = AUDIO_SESSION_ID_NONE;\r
74 };\r
75 \r
76 /**\r
77  * @brief Represents timestamp information, including the frame position information and high-resolution time source.\r
78  *\r
79  * @since 1.0\r
80  * @version 1.0\r
81  */\r
82 class Timestamp {\r
83 public:\r
84     Timestamp() : framePosition(0)\r
85     {\r
86         time.tv_sec = 0;\r
87         time.tv_nsec = 0;\r
88     }\r
89     ~Timestamp() = default;\r
90     uint32_t framePosition;\r
91     struct timespec time;\r
92 \r
93     /**\r
94      * @brief Enumerates the time base of this <b>Timestamp</b>. Different timing methods are supported.\r
95      *\r
96      */\r
97     enum class Timebase : int32_t {\r
98         /** Monotonically increasing time, excluding the system sleep time */\r
99         MONOTONIC = 0,\r
100         /** Monotonically increasing time, including the system sleep time */\r
101         BOOTTIME = 1\r
102         };\r
103 };\r
104 \r
105 /**\r
106  * @brief Enumerates the recording states of the current device.\r
107  *\r
108  * @since 1.0\r
109  * @version 1.0\r
110  */\r
111 enum State : uint32_t {\r
112     /** Initialized */\r
113     INITIALIZED,\r
114     /** Prepared */\r
115     PREPARED,\r
116     /** Recording */\r
117     RECORDING,\r
118     /** Stopped */\r
119     STOPPED,\r
120     /** Released */\r
121     RELEASED\r
122 };\r
123 \r
124 /**\r
125  * @brief Provides functions for applications to implement audio capturing.\r
126  *\r
127  * @since 1.0\r
128  * @version 1.0\r
129  */\r
130 class AudioCapturer {\r
131 public:\r
132     AudioCapturer();\r
133     virtual ~AudioCapturer();\r
134 \r
135     /**\r
136      * @brief Obtains the minimum number of frames required in a specified condition, in bytes per sample.\r
137      *\r
138      * @param sampleRate Indicates the audio sampling rate, in Hz.\r
139      * @param channelCount Indicates the number of audio recording channels.\r
140      * @param audioFormat Indicates the audio data format.\r
141      * @param frameCount Indicates the minimum number of frames, in bytes per sample.\r
142      * @return Returns <b>true</b> if the minimum number of frames is successfully obtained; returns <b>false</b>\r
143      * otherwise.\r
144      * @since 1.0\r
145      * @version 1.0\r
146      */\r
147     static bool GetMinFrameCount(int32_t sampleRate, int32_t channelCount, AudioCodecFormat audioFormat,\r
148                                 size_t &frameCount);\r
149 \r
150     /**\r
151      * @brief Obtains the number of frames required in the current condition, in bytes per sample.\r
152      *\r
153      * @return Returns the number of frames (in bytes per sample) if the operation is successful; returns <b>-1</b>\r
154      * if an exception occurs.\r
155      * @since 1.0\r
156      * @version 1.0\r
157      */\r
158     uint64_t GetFrameCount();\r
159 \r
160     /**\r
161      * @brief Sets audio capture parameters.\r
162      *\r
163      * @param info Indicates information about audio capture parameters to set. For details, see\r
164      * {@link AudioCapturerInfo}.\r
165      * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined\r
166      * in {@link media_errors.h} otherwise.\r
167      * @since 1.0\r
168      * @version 1.0\r
169      */\r
170     int32_t SetCapturerInfo(const AudioCapturerInfo info);\r
171 \r
172     /**\r
173      * @brief Obtains audio capture parameters.\r
174      *\r
175      * This function can be called after {@link SetCapturerInfo} is successful.\r
176      *\r
177      * @param info Indicates information about audio capture parameters. For details, see {@link AudioCapturerInfo}.\r
178      * @return Returns {@link SUCCESS} if the parameter information is successfully obtained; returns an error code\r
179      * defined in {@link media_errors.h} otherwise.\r
180      * @since 1.0\r
181      * @version 1.0\r
182      */\r
183     int32_t GetCapturerInfo(AudioCapturerInfo &info);\r
184 \r
185     /**\r
186      * @brief Starts audio recording.\r
187      *\r
188      * @return Returns <b>true</b> if the recording is successfully started; returns <b>false</b> otherwise.\r
189      * @since 1.0\r
190      * @version 1.0\r
191      */\r
192     bool Start();\r
193 \r
194     /**\r
195      * @brief Reads audio data.\r
196      *\r
197      * @param buffer Indicates the pointer to the buffer into which the audio data is to be written.\r
198      * @param userSize Indicates the size of the buffer into which the audio data is to be written, in bytes.\r
199      * <b>userSize >= frameCount * channelCount * BytesPerSample</b> must evaluate to <b>true</b>. You can call\r
200      * {@link GetFrameCount} to obtain the <b>frameCount</b> value.\r
201      * @param isBlockingRead Specifies whether data reading will be blocked.\r
202      * @return Returns the size of the audio data read from the device. The value ranges from <b>0</b> to\r
203      * <b>userSize</b>. If the reading fails, one of the following error codes is returned:\r
204      * <b>ERR_INVALID_PARAM</b>: The input parameter is incorrect.\r
205      * <b>ERR_ILLEGAL_STATE</b>: The <b>AudioCapturer</b> instance is not initialized.\r
206      * <b>ERR_SOURCE_NOT_SET</b>: The state of hardware device instance is abnormal.\r
207      * @since 1.0\r
208      * @version 1.0\r
209      */\r
210     int32_t Read(uint8_t *buffer, size_t userSize, bool isBlockingRead);\r
211 \r
212     /**\r
213      * @brief Obtains the audio capture state.\r
214      *\r
215      * @return Returns the audio capture state defined in {@link State}.\r
216      * @since 1.0\r
217      * @version 1.0\r
218      */\r
219     State GetStatus();\r
220 \r
221     /**\r
222      * @brief Obtains the timestamp.\r
223      *\r
224      * @param timestamp Indicates a {@link Timestamp} instance reference provided by the caller.\r
225      * @param base Indicates the time base, which can be {@link Timestamp.Timebase#BOOTTIME Timestamp.Timebase.BOOTTIME}\r
226      * or {@link Timestamp.Timebase#MONOTONIC Timestamp.Timebase.MONOTONIC}.\r
227      * @return Returns <b>true</b> if the timestamp is successfully obtained; returns <b>false</b> otherwise.\r
228      * @since 1.0\r
229      * @version 1.0\r
230      */\r
231     bool GetAudioTime(Timestamp &timestamp, Timestamp::Timebase base);\r
232 \r
233     /**\r
234      * @brief Stops audio recording.\r
235      *\r
236      * @return Returns <b>true</b> if the recording is successfully stopped; returns <b>false</b> otherwise.\r
237      * @since 1.0\r
238      * @version 1.0\r
239      */\r
240     bool Stop();\r
241 \r
242     /**\r
243      * @brief Releases a local <b>AudioCapturer</b> object.\r
244      *\r
245      * @return Returns <b>true</b> if the object is successfully released; returns <b>false</b> otherwise.\r
246      * @since 1.0\r
247      * @version 1.0\r
248      */\r
249     bool Release();\r
250 \r
251 private:\r
252     class AudioCapturerImpl;\r
253     std::unique_ptr<AudioCapturerImpl> impl_;\r
254 };\r
255 }  // namespace Audio\r
256 }  // namespace OHOS\r
257 #endif  // AUDIO_CAPTURER_H\r