1 /* 2 * Copyright (c) 2021-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 16 #ifndef AUDIO_CAPTURER_H 17 #define AUDIO_CAPTURER_H 18 19 #include <memory> 20 21 #include "audio_info.h" 22 #include "timestamp.h" 23 24 namespace OHOS { 25 namespace AudioStandard { 26 /** 27 * @brief Defines information about audio capturer parameters 28 */ 29 struct AudioCapturerParams { 30 /** Audio source type */ 31 AudioSourceType inputSource = AUDIO_MIC; 32 /** Audio codec format */ 33 AudioEncodingType audioEncoding = ENCODING_PCM; 34 /** Sampling rate */ 35 AudioSamplingRate samplingRate = SAMPLE_RATE_44100; 36 /** Number of audio channels */ 37 AudioChannel audioChannel = MONO; 38 /** Audio stream type */ 39 AudioStreamType streamType = STREAM_MEDIA; 40 /** audioSampleFormat */ 41 AudioSampleFormat audioSampleFormat = SAMPLE_S16LE; 42 }; 43 44 /** 45 * @brief Enumerates the capturing states of the current device. 46 */ 47 enum CapturerState { 48 /** Capturer INVALID state */ 49 CAPTURER_INVALID = -1, 50 /** Create new capturer instance */ 51 CAPTURER_NEW, 52 /** Capturer Prepared state */ 53 CAPTURER_PREPARED, 54 /** Capturer Running state */ 55 CAPTURER_RUNNING, 56 /** Capturer Stopped state */ 57 CAPTURER_STOPPED, 58 /** Capturer Released state */ 59 CAPTURER_RELEASED 60 }; 61 62 class AudioCapturerCallback { 63 public: 64 virtual ~AudioCapturerCallback() = default; 65 /** 66 * Called when renderer state is updated. 67 * 68 * @param state Indicates updated state of the capturer. 69 * For details, refer enum CapturerState. 70 */ 71 virtual void OnStateChange(const CapturerState state) = 0; 72 }; 73 74 class CapturerPositionCallback { 75 public: 76 virtual ~CapturerPositionCallback() = default; 77 78 /** 79 * Called when the requested frame number is read. 80 * 81 * @param framePosition requested frame position. 82 */ 83 virtual void OnMarkReached(const int64_t &framePosition) = 0; 84 }; 85 86 class CapturerPeriodPositionCallback { 87 public: 88 virtual ~CapturerPeriodPositionCallback() = default; 89 90 /** 91 * Called when the requested frame count is read. 92 * 93 * @param frameCount requested frame frame count for callback. 94 */ 95 virtual void OnPeriodReached(const int64_t &frameNumber) = 0; 96 }; 97 98 /** 99 * @brief Provides functions for applications to implement audio capturing. 100 */ 101 class AudioCapturer { 102 public: 103 /** 104 * @brief creater capturer instance. 105 */ 106 static std::unique_ptr<AudioCapturer> Create(AudioStreamType audioStreamType); 107 108 /** 109 * @brief creater capturer instance. 110 */ 111 static std::unique_ptr<AudioCapturer> Create(const AudioCapturerOptions &capturerOptions); 112 113 /** 114 * @brief create capturer instance. 115 * 116 * @param cachePath Application cache path 117 * @param capturerOptions The audio capturer configuration to be used while creating capturer instance. 118 * refer AudioCapturerOptions in audio_info.h. 119 * @return Returns unique pointer to the AudioCapturer object 120 */ 121 static std::unique_ptr<AudioCapturer> Create(const std::string cachePath, 122 const AudioCapturerOptions &capturerOptions); 123 124 /** 125 * @brief Sets audio capture parameters. 126 * 127 * @param params Indicates information about audio capture parameters to set. For details, see 128 * {@link AudioCapturerParams}. 129 * @return Returns {@link SUCCESS} if the setting is successful; returns an error code defined 130 * in {@link audio_errors.h} otherwise. 131 */ 132 virtual int32_t SetParams(const AudioCapturerParams params) const = 0; 133 134 /** 135 * @brief Registers the capturer callback listener. 136 * (1)If old SetParams(const AudioCapturerParams params) API, 137 * this API must be called immediately after SetParams. 138 * (2) Else if using Create(const AudioCapturerOptions &capturerOptions), 139 * this API must be called immediately after Create. 140 * 141 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 142 * defined in {@link audio_errors.h} otherwise. 143 */ 144 virtual int32_t SetCapturerCallback(const std::shared_ptr<AudioCapturerCallback> &callback) = 0; 145 146 /** 147 * @brief Obtains audio capturer parameters. 148 * 149 * This function can be called after {@link SetParams} is successful. 150 * 151 * @param params Indicates information about audio capturer parameters.For details,see 152 * {@link AudioCapturerParams}. 153 * @return Returns {@link SUCCESS} if the parameter information is successfully obtained; returns an error code 154 * defined in {@link audio_errors.h} otherwise. 155 */ 156 virtual int32_t GetParams(AudioCapturerParams ¶ms) const = 0; 157 158 /** 159 * @brief Obtains audio capturer information. 160 * 161 * This function can be called after {@link SetParams} is successful. 162 * 163 * @param capturerInfo Indicates information about audio capturer information.For details,see 164 * {@link AudioCapturerInfo}. 165 * @return Returns {@link SUCCESS} if the parameter information is successfully obtained; returns an error code 166 * defined in {@link audio_errors.h} otherwise. 167 */ 168 virtual int32_t GetCapturerInfo(AudioCapturerInfo &capturerInfo) const = 0; 169 170 /** 171 * @brief Obtains audio stream information. 172 * 173 * This function can be called after {@link Create} is successful. 174 * 175 * @param streamInfo Indicates information about audio stream information.For details,see 176 * {@link AudioStreamInfo}. 177 * @return Returns {@link SUCCESS} if the parameter information is successfully obtained; returns an error code 178 * defined in {@link audio_errors.h} otherwise. 179 */ 180 virtual int32_t GetStreamInfo(AudioStreamInfo &streamInfo) const = 0; 181 182 /** 183 * @brief Starts audio capturing. 184 * 185 * @return Returns <b>true</b> if the capturing is successfully started; returns <b>false</b> otherwise. 186 */ 187 virtual bool Start() const = 0; 188 189 /** 190 * @brief capture audio data. 191 * 192 * @param buffer Indicates the pointer to the buffer into which the audio data is to be written. 193 * @param userSize Indicates the size of the buffer into which the audio data is to be written, in bytes. 194 * <b>userSize >= frameCount * channelCount * BytesPerSample</b> must evaluate to <b>true</b>. You can call 195 * {@link GetFrameCount} to obtain the <b>frameCount</b> value. 196 * @param isBlockingRead Specifies whether data reading will be blocked. 197 * @return Returns the size of the audio data read from the device. The value ranges from <b>0</b> to 198 * <b>userSize</b>. If the reading fails, one of the following error codes is returned. 199 * <b>ERR_INVALID_PARAM</b>: The input parameter is incorrect. 200 * <b>ERR_ILLEGAL_STATE</b>: The <b>AudioCapturer</b> instance is not initialized. 201 * <b>ERR_INVALID_READ</b>: The read size < 0. 202 */ 203 virtual int32_t Read(uint8_t &buffer, size_t userSize, bool isBlockingRead) const = 0; 204 205 /** 206 * @brief Obtains the audio capture state. 207 * 208 * @return Returns the audio capture state defined in {@link CapturerState}. 209 */ 210 virtual CapturerState GetStatus() const = 0; 211 212 /** 213 * @brief Obtains the Timestamp. 214 * 215 * @param timestamp Indicates a {@link Timestamp} instance reference provided by the caller. 216 * @param base Indicates the time base, which can be {@link Timestamp.Timestampbase#BOOTTIME} or 217 * {@link Timestamp.Timestampbase#MONOTONIC}. 218 * @return Returns <b>true</b> if the timestamp is successfully obtained; returns <b>false</b> otherwise. 219 */ 220 virtual bool GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbase base) const = 0; 221 222 /** 223 * @brief Stops audio capturing. 224 * 225 * @return Returns <b>true</b> if the capturing is successfully stopped; returns <b>false</b> otherwise. 226 */ 227 virtual bool Stop() const = 0; 228 /** 229 * @brief flush capture stream. 230 * 231 * @return Returns <b>true</b> if the object is successfully flushed; returns <b>false</b> otherwise. 232 */ 233 virtual bool Flush() const = 0; 234 235 /** 236 * @brief Releases a local <b>AudioCapturer</b> object. 237 * 238 * @return Returns <b>true</b> if the object is successfully released; returns <b>false</b> otherwise. 239 */ 240 virtual bool Release() const = 0; 241 242 /** 243 * @brief Obtains a reasonable minimum buffer size for capturer, however, the capturer can 244 * accept other read sizes as well. 245 * 246 * @param bufferSize Indicates a buffersize pointer value that wil be written. 247 * @return Returns {@link SUCCESS} if bufferSize is successfully obtained; returns an error code 248 * defined in {@link audio_errors.h} otherwise. 249 */ 250 virtual int32_t GetBufferSize(size_t &bufferSize) const = 0; 251 252 /* @brief Obtains the number of frames required in the current condition, in bytes per sample. 253 * 254 * @param frameCount Indicates the pointer in which framecount will be written 255 * @return Returns {@link SUCCESS} if frameCount is successfully obtained; returns an error code 256 * defined in {@link audio_errors.h} otherwise. 257 */ 258 259 virtual int32_t GetFrameCount(uint32_t &frameCount) const = 0; 260 /** 261 * @brief Registers the capturer position callback listener 262 * 263 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 264 * defined in {@link audio_errors.h} otherwise. 265 */ 266 267 virtual int32_t SetCapturerPositionCallback(int64_t markPosition, 268 const std::shared_ptr<CapturerPositionCallback> &callback) = 0; 269 270 /** 271 * @brief Unregisters the capturer position callback listener 272 * 273 */ 274 virtual void UnsetCapturerPositionCallback() = 0; 275 276 /** 277 * @brief Registers the capturer period position callback listener 278 * 279 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 280 * defined in {@link audio_errors.h} otherwise. 281 */ 282 virtual int32_t SetCapturerPeriodPositionCallback(int64_t frameNumber, 283 const std::shared_ptr<CapturerPeriodPositionCallback> &callback) = 0; 284 285 /** 286 * @brief Unregisters the capturer period position callback listener 287 * 288 */ 289 virtual void UnsetCapturerPeriodPositionCallback() = 0; 290 291 /** 292 * @brief set the buffer duration for capturer, minimum buffer duration is 5msec 293 * maximum is 20msec 294 * 295 * @param bufferDuration Indicates a buffer duration to be set for capturer 296 * @return Returns {@link SUCCESS} if bufferDuration is successfully set; returns an error code 297 * defined in {@link audio_errors.h} otherwise. 298 */ 299 virtual int32_t SetBufferDuration(uint64_t bufferDuration) const = 0; 300 301 /** 302 * @brief Set the application cache path to access the application resources 303 * 304 * @param cachePath Indicates application cache path. 305 * @return none 306 */ 307 virtual void SetApplicationCachePath(const std::string cachePath) = 0; 308 309 /** 310 * @brief Obtains the capturer supported formats. 311 * 312 * @return vector with capturer supported formats. 313 */ 314 static std::vector<AudioSampleFormat> GetSupportedFormats(); 315 316 /** 317 * @brief Obtains the capturer supported channels. 318 * 319 * @return vector with capturer supported channels. 320 */ 321 static std::vector<AudioChannel> GetSupportedChannels(); 322 323 /** 324 * @brief Obtains the capturer supported encoding types. 325 * 326 * @return vector with capturer supported encoding types. 327 */ 328 static std::vector<AudioEncodingType> GetSupportedEncodingTypes(); 329 330 /** 331 * @brief Obtains the capturer supported SupportedSamplingRates. 332 * 333 * @return vector with capturer supported SupportedSamplingRates. 334 */ 335 static std::vector<AudioSamplingRate> GetSupportedSamplingRates(); 336 337 virtual ~AudioCapturer(); 338 }; 339 } // namespace AudioStandard 340 } // namespace OHOS 341 #endif // AUDIO_CAPTURER_H 342