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 16 #ifndef AUDIO_CONTAINER_BASE_H 17 #define AUDIO_CONTAINER_BASE_H 18 19 #include "audio_container_stream_callback.h" 20 #include "audio_stream_death_recipient.h" 21 22 #include "audio_log.h" 23 #include "unistd.h" 24 #include "securec.h" 25 26 namespace OHOS { 27 namespace AudioStandard { 28 class AudioContainerBase : public AudioTimer { 29 public: 30 /** 31 * Initializes audio service client for the required client type 32 * 33 * @param eClientType indicates the client type like playback, record or controller. 34 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 35 */ 36 int32_t Initialize(ASClientType eClientType, int serviceId); 37 38 // Stream handling APIs 39 40 /** 41 * Creates & initializes resources based on the audioParams and audioType 42 * 43 * @param audioParams indicate format, sampling rate and number of channels 44 * @param audioType indicate the stream type like music, system, ringtone etc 45 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 46 */ 47 int32_t CreateStream(AudioStreamParams audioParams, AudioStreamType audioType); 48 49 /** 50 * @brief Obtains session ID . 51 * 52 * @return Returns unique session ID for the created session 53 */ 54 int32_t GetSessionID(uint32_t &sessionID, const int32_t &trackId) const; 55 56 /** 57 * Starts the stream created using CreateStreamGa 58 * 59 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 60 */ 61 int32_t StartStream(const int32_t &trackId); 62 63 /** 64 * Stops the stream created using CreateStreamGa 65 * 66 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 67 */ 68 int32_t StopStream(const int32_t &trackId); 69 70 /** 71 * Flushes the stream created using CreateStreamGa. This is applicable for 72 * playback only 73 * 74 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 75 */ 76 int32_t FlushStream(const int32_t &trackId); 77 78 /** 79 * Drains the stream created using CreateStreamGa. This is applicable for 80 * playback only 81 * 82 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 83 */ 84 int32_t DrainStream(const int32_t &trackId); 85 86 /** 87 * Pauses the stream 88 * 89 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 90 */ 91 int32_t PauseStream(const int32_t &trackId); 92 93 /** 94 * Update the stream type 95 * 96 * @param audioStreamType Audio stream type 97 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 98 */ 99 int32_t SetStreamType(AudioStreamType audioStreamType, const int32_t &trackId); 100 101 /** 102 * Sets the volume of the stream associated with session ID 103 * 104 * @param sessionID indicates the ID for the active stream to be controlled 105 * @param volume indicates volume level between 0 to 65536 106 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 107 */ 108 int32_t SetStreamVolume(uint32_t sessionID, uint32_t volume); 109 110 /** 111 * Get the volume of the stream associated with session ID 112 * 113 * @param sessionID indicates the ID for the active stream to be controlled 114 * @return returns volume level between 0 to 65536 115 */ 116 uint32_t GetStreamVolume(uint32_t sessionID); 117 118 /** 119 * Writes audio data of the stream created using CreateStreamGa to active sink device 120 * 121 * @param buffer contains audio data to write 122 * @param bufferSize indicates the size of audio data in bytes to write from the buffer 123 * @param pError indicates pointer to error which will be filled in case of internal errors 124 * @return returns size of audio data written in bytes. 125 */ 126 size_t WriteStream(const StreamBuffer &stream, int32_t &pError, const int32_t &trackId); 127 128 /** 129 * Writes audio data of the stream created using CreateStreamGa to active sink device 130 Used when render mode is RENDER_MODE_CALLBACK 131 * 132 * @param buffer contains audio data to write 133 * @param bufferSize indicates the size of audio data in bytes to write from the buffer 134 * @param pError indicates pointer to error which will be filled in case of internal errors 135 * @return returns size of audio data written in bytes. 136 */ 137 size_t WriteStreamInCb(const StreamBuffer &stream, int32_t &pError, const int32_t &trackId); 138 139 /** 140 * Reads audio data of the stream created using CreateStreamGa from active source device 141 * 142 * @param StreamBuffer including buffer to be filled with audio data 143 * and bufferSize indicating the size of audio data to read into buffer 144 * @param isBlocking indicates if the read is blocking or not 145 * @return Returns size read if success; returns {@code -1} failure. 146 */ 147 virtual int32_t ReadStream(StreamBuffer &stream, bool isBlocking, const int32_t &trackId); 148 149 /** 150 * Release the resources allocated using CreateStreamGa 151 * 152 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 153 */ 154 int32_t ReleaseStream(const int32_t &trackId); 155 156 /** 157 * Provides the current timestamp for playback/record stream created using CreateStreamGa 158 * 159 * @param timeStamp will be filled up with current timestamp 160 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 161 */ 162 int32_t GetCurrentTimeStamp(uint64_t &timeStamp, const int32_t &trackId); 163 164 /** 165 * Provides the current latency for playback/record stream created using CreateStreamGa 166 * 167 * @param latency will be filled up with the current latency in microseconds 168 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 169 */ 170 int32_t GetAudioLatency(uint64_t &latency, const int32_t &trackId) const; 171 172 /** 173 * Provides the playback/record stream parameters created using CreateStreamGa 174 * 175 * @param audioParams will be filled up with stream audio parameters 176 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 177 */ 178 int32_t GetAudioStreamParams(AudioStreamParams &audioParams, const int32_t &trackId); 179 180 /** 181 * Provides the minimum buffer size required for this audio stream 182 * created using CreateStreamGa 183 * @param minBufferSize will be set to minimum buffer size in bytes 184 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 185 */ 186 int32_t GetMinimumBufferSize(size_t &minBufferSize, const int32_t &trackId) const; 187 188 /** 189 * Provides the minimum frame count required for this audio stream 190 * created using CreateStreamGa 191 * @param frameCount will be set to minimum number of frames 192 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 193 */ 194 int32_t GetMinimumFrameCount(uint32_t &frameCount, const int32_t &trackId) const; 195 196 /** 197 * @brief Set the track volume 198 * 199 * @param volume The volume to be set for the current track. 200 * @return Returns {@link SUCCESS} if volume is successfully set; returns an error code 201 * defined in {@link audio_errors.h} otherwise. 202 */ 203 int32_t SetStreamVolume(float volume, const int32_t &trackId); 204 205 /** 206 * @brief Obtains the current track volume 207 * 208 * @return Returns current track volume 209 */ 210 float GetStreamVolume(); 211 212 /** 213 * @brief Set the render rate 214 * 215 * @param renderRate The rate at which the stream needs to be rendered. 216 * @return Returns {@link SUCCESS} if render rate is successfully set; returns an error code 217 * defined in {@link audio_errors.h} otherwise. 218 */ 219 int32_t SetStreamRenderRate(AudioRendererRate renderRate, const int32_t &trackId); 220 221 /** 222 * @brief Obtains the current render rate 223 * 224 * @return Returns current render rate 225 */ 226 AudioRendererRate GetStreamRenderRate(); 227 228 /** 229 * @brief Saves StreamCallback 230 * 231 * @param callback callback reference to be saved. 232 * @return none. 233 */ 234 235 void SaveStreamCallback(const std::weak_ptr<AudioStreamCallback> &callback); 236 237 /** 238 * @brief Sets the render mode. By default the mode is RENDER_MODE_NORMAL. 239 * This API is needs to be used only if RENDER_MODE_CALLBACK is required. 240 * 241 * * @param renderMode The mode of render. 242 * @return Returns {@link SUCCESS} if render mode is successfully set; returns an error code 243 * defined in {@link audio_errors.h} otherwise. 244 */ 245 int32_t SetAudioRenderMode(AudioRenderMode renderMode, const int32_t &trackId); 246 247 /** 248 * @brief Obtains the render mode. 249 * 250 * @return Returns current render mode. 251 */ 252 AudioRenderMode GetAudioRenderMode(); 253 254 /** 255 * @brief Set the buffer duration in msec 256 * 257 * @param bufferSizeInMsec buffer size in duration. 258 * @return Returns {@link SUCCESS} defined in {@link audio_errors.h} otherwise. 259 */ 260 int32_t SetBufferSizeInMsec(int32_t bufferSizeInMsec); 261 262 /** 263 * Set the renderer frame position callback 264 * 265 * @param callback indicates pointer for registered callbacks 266 * @return none 267 */ 268 void SetRendererPositionCallback(int64_t markPosition, const std::shared_ptr<RendererPositionCallback> &callback); 269 270 /** 271 * Unset the renderer frame position callback 272 * 273 * @return none 274 */ 275 void UnsetRendererPositionCallback(); 276 277 /** 278 * Set the renderer frame period position callback 279 * 280 * @param callback indicates pointer for registered callbacks 281 * @return none 282 */ 283 void SetRendererPeriodPositionCallback(int64_t markPosition, 284 const std::shared_ptr<RendererPeriodPositionCallback> &callback); 285 286 /** 287 * Unset the renderer frame period position callback 288 * 289 * @return none 290 */ 291 void UnsetRendererPeriodPositionCallback(); 292 293 /** 294 * Set the capturer frame position callback 295 * 296 * @param callback indicates pointer for registered callbacks 297 * @return none 298 */ 299 void SetCapturerPositionCallback(int64_t markPosition, const std::shared_ptr<CapturerPositionCallback> &callback); 300 301 /** 302 * Unset the capturer frame position callback 303 * 304 * @return none 305 */ 306 void UnsetCapturerPositionCallback(); 307 308 /** 309 * Set the capturer frame period position callback 310 * 311 * @param callback indicates pointer for registered callbacks 312 * @return none 313 */ 314 void SetCapturerPeriodPositionCallback(int64_t markPosition, 315 const std::shared_ptr<CapturerPeriodPositionCallback> &callback); 316 317 /** 318 * Unset the capturer frame period position callback 319 * 320 * @return none 321 */ 322 void UnsetCapturerPeriodPositionCallback(); 323 324 // Audio timer callback 325 virtual void OnTimeOut(); 326 327 int32_t SetAudioCaptureMode(AudioCaptureMode captureMode); 328 329 int32_t SaveReadCallback(const std::weak_ptr<AudioCapturerReadCallback> &callback); 330 /** 331 * @brief Set the applicationcache path to access the application resources 332 * 333 * @return none 334 */ 335 void SetAppCachePath(const std::string cachePath); 336 337 AudioCaptureMode GetAudioCaptureMode(); 338 339 void InitAudioStreamManagerGa(int serviceId); 340 341 /** 342 * @brief Registers the renderer write callback listener. 343 * This API should only be used if RENDER_MODE_CALLBACK is needed. 344 * 345 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 346 * defined in {@link audio_errors.h} otherwise. 347 */ 348 virtual int32_t SaveWriteCallback(const std::weak_ptr<AudioRendererWriteCallback> &callback, 349 const int32_t &trackId = 0); 350 351 static void RegisterAudioStreamDeathRecipient(sptr<IRemoteObject> &object); 352 353 private: 354 static void AudioStreamDied(pid_t pid); 355 }; 356 } // namespace AudioStandard 357 } // namespace OHOS 358 #endif // AUDIO_CONTAINER_BASE_H