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
7 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 * @addtogroup MultiMedia_AudioCapturer
\r
20 * @brief Declares APIs in the <b>AudioCapturer</b> class for audio capture.
\r
28 * @file audio_capturer.h
\r
30 * @brief Provides the <b>AudioCapturer</b> class to implement operations related to audio capture.
\r
37 #ifndef AUDIO_CAPTURER_H
\r
38 #define AUDIO_CAPTURER_H
\r
45 #include "media_errors.h"
\r
46 #include "audio_base_type.h"
\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
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
67 int32_t bitRate = 0;
\r
68 /** Audio stream type */
\r
69 AudioStreamType streamType = TYPE_MEDIA;
\r
71 AudioBitWidth bitWidth = BIT_WIDTH_16;
\r
73 AudioSession sessionID = AUDIO_SESSION_ID_NONE;
\r
77 * @brief Represents timestamp information, including the frame position information and high-resolution time source.
\r
84 Timestamp() : framePosition(0)
\r
89 ~Timestamp() = default;
\r
90 uint32_t framePosition;
\r
91 struct timespec time;
\r
94 * @brief Enumerates the time base of this <b>Timestamp</b>. Different timing methods are supported.
\r
97 enum class Timebase : int32_t {
\r
98 /** Monotonically increasing time, excluding the system sleep time */
\r
100 /** Monotonically increasing time, including the system sleep time */
\r
106 * @brief Enumerates the recording states of the current device.
\r
111 enum State : uint32_t {
\r
125 * @brief Provides functions for applications to implement audio capturing.
\r
130 class AudioCapturer {
\r
133 virtual ~AudioCapturer();
\r
136 * @brief Obtains the minimum number of frames required in a specified condition, in bytes per sample.
\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
147 static bool GetMinFrameCount(int32_t sampleRate, int32_t channelCount, AudioCodecFormat audioFormat,
\r
148 size_t &frameCount);
\r
151 * @brief Obtains the number of frames required in the current condition, in bytes per sample.
\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
158 uint64_t GetFrameCount();
\r
161 * @brief Sets audio capture parameters.
\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
170 int32_t SetCapturerInfo(const AudioCapturerInfo info);
\r
173 * @brief Obtains audio capture parameters.
\r
175 * This function can be called after {@link SetCapturerInfo} is successful.
\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
183 int32_t GetCapturerInfo(AudioCapturerInfo &info);
\r
186 * @brief Starts audio recording.
\r
188 * @return Returns <b>true</b> if the recording is successfully started; returns <b>false</b> otherwise.
\r
195 * @brief Reads audio data.
\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
210 int32_t Read(uint8_t *buffer, size_t userSize, bool isBlockingRead);
\r
213 * @brief Obtains the audio capture state.
\r
215 * @return Returns the audio capture state defined in {@link State}.
\r
222 * @brief Obtains the timestamp.
\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
231 bool GetAudioTime(Timestamp ×tamp, Timestamp::Timebase base);
\r
234 * @brief Stops audio recording.
\r
236 * @return Returns <b>true</b> if the recording is successfully stopped; returns <b>false</b> otherwise.
\r
243 * @brief Releases a local <b>AudioCapturer</b> object.
\r
245 * @return Returns <b>true</b> if the object is successfully released; returns <b>false</b> otherwise.
\r
252 class AudioCapturerImpl;
\r
253 std::unique_ptr<AudioCapturerImpl> impl_;
\r
255 } // namespace Audio
\r
256 } // namespace OHOS
\r
257 #endif // AUDIO_CAPTURER_H
\r