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_STREAM_SERVICE_CLIENT_H 17 #define AUDIO_STREAM_SERVICE_CLIENT_H 18 19 #include "audio_container_stream_callback.h" 20 21 namespace OHOS { 22 namespace AudioStandard { 23 class AudioContainerClientBase : public IRemoteProxy<IAudioContainerService> { 24 public: 25 explicit AudioContainerClientBase(const sptr<IRemoteObject> &impl); 26 virtual ~AudioContainerClientBase(); 27 28 /** 29 * Initializes audio service client for the required client type 30 * 31 * @param eClientType indicates the client type like playback, record or controller. 32 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 33 */ 34 int32_t InitializeGa(ASClientType eClientType) override; 35 36 // Stream handling APIs 37 38 /** 39 * Creates & initializes resources based on the audioParams and audioType 40 * 41 * @param audioParams indicate format, sampling rate and number of channels 42 * @param audioType indicate the stream type like music, system, ringtone etc 43 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 44 */ 45 int32_t CreateStreamGa(AudioStreamParams audioParams, AudioStreamType audioType) override; 46 47 /** 48 * @brief Obtains session ID . 49 * 50 * @return Returns unique session ID for the created session 51 */ 52 int32_t GetSessionIDGa(uint32_t &sessionID, const int32_t &trackId) override; 53 54 /** 55 * Starts the stream created using CreateStreamGa 56 * 57 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 58 */ 59 int32_t StartStreamGa(const int32_t &trackId) override; 60 61 /** 62 * Stops the stream created using CreateStreamGa 63 * 64 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 65 */ 66 int32_t StopStreamGa(const int32_t &trackId) override; 67 68 /** 69 * Flushes the stream created using CreateStreamGa. This is applicable for 70 * playback only 71 * 72 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 73 */ 74 int32_t FlushStreamGa(const int32_t &trackId) override; 75 76 /** 77 * Drains the stream created using CreateStreamGa. This is applicable for 78 * playback only 79 * 80 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 81 */ 82 int32_t DrainStreamGa(const int32_t &trackId) override; 83 84 /** 85 * Pauses the stream 86 * 87 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 88 */ 89 int32_t PauseStreamGa(const int32_t &trackId) override; 90 91 /** 92 * Update the stream type 93 * 94 * @param audioStreamType Audio stream type 95 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 96 */ 97 int32_t SetStreamTypeGa(AudioStreamType audioStreamType, const int32_t &trackId) override; 98 99 /** 100 * Sets the volume of the stream associated with session ID 101 * 102 * @param sessionID indicates the ID for the active stream to be controlled 103 * @param volume indicates volume level between 0 to 65536 104 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 105 */ 106 int32_t SetStreamVolumeGa(uint32_t sessionID, uint32_t volume) override; 107 108 /** 109 * Get the volume of the stream associated with session ID 110 * 111 * @param sessionID indicates the ID for the active stream to be controlled 112 * @return returns volume level between 0 to 65536 113 */ 114 uint32_t GetStreamVolumeGa(uint32_t sessionID) override; 115 116 /** 117 * Writes audio data of the stream created using CreateStreamGa to active sink device 118 * 119 * @param buffer contains audio data to write 120 * @param bufferSize indicates the size of audio data in bytes to write from the buffer 121 * @param pError indicates pointer to error which will be filled in case of internal errors 122 * @return returns size of audio data written in bytes. 123 */ 124 size_t WriteStreamGa(const StreamBuffer &stream, int32_t &pError, const int32_t &trackId) override; 125 126 /** 127 * Writes audio data of the stream created using CreateStreamGa 128 * 129 * @param buffer contains audio data to write 130 * @param bufferSize indicates the size of audio data in bytes to write from the buffer 131 * @param pError indicates pointer to error which will be filled in case of internal errors 132 * @return returns size of audio data written in bytes. 133 */ 134 size_t WriteStreamInCbGa(const StreamBuffer &stream, int32_t &pError, const int32_t &trackId) override; 135 136 /** 137 * Reads audio data of the stream created using CreateStreamGa from active source device 138 * 139 * @param StreamBuffer including buffer to be filled with audio data 140 * and bufferSize indicating the size of audio data to read into buffer 141 * @param isBlocking indicates if the read is blocking or not 142 * @return Returns size read if success; returns {@code -1} failure. 143 */ 144 int32_t ReadStreamGa(StreamBuffer &stream, bool isBlocking, const int32_t &trackId) override; 145 146 /** 147 * Release the resources allocated using CreateStreamGa 148 * 149 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 150 */ 151 int32_t ReleaseStreamGa(const int32_t &trackId) override; 152 153 /** 154 * Provides the current timestamp for playback/record stream created using CreateStreamGa 155 * 156 * @param timeStamp will be filled up with current timestamp 157 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 158 */ 159 int32_t GetCurrentTimeStampGa(uint64_t &timeStamp, const int32_t &trackId) override; 160 161 /** 162 * Provides the current latency for playback/record stream created using CreateStreamGa 163 * 164 * @param latency will be filled up with the current latency in microseconds 165 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 166 */ 167 int32_t GetAudioLatencyGa(uint64_t &latency, const int32_t &trackId) override; 168 169 /** 170 * Provides the playback/record stream parameters created using CreateStreamGa 171 * 172 * @param audioParams will be filled up with stream audio parameters 173 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 174 */ 175 int32_t GetAudioStreamParamsGa(AudioStreamParams &audioParams, const int32_t &trackId) override; 176 177 /** 178 * Provides the minimum buffer size required for this audio stream 179 * created using CreateStreamGa 180 * @param minBufferSize will be set to minimum buffer size in bytes 181 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 182 */ 183 int32_t GetMinimumBufferSizeGa(size_t &minBufferSize, const int32_t &trackId) override; 184 185 /** 186 * Provides the minimum frame count required for this audio stream 187 * created using CreateStreamGa 188 * @param frameCount will be set to minimum number of frames 189 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 190 */ 191 int32_t GetMinimumFrameCountGa(uint32_t &frameCount, const int32_t &trackId) override; 192 193 /** 194 * @brief Set the buffer duration in msec 195 * 196 * @param bufferSizeInMsec buffer size in duration. 197 * @return Returns {@link SUCCESS} defined in {@link audio_errors.h} otherwise. 198 */ 199 int32_t SetBufferSizeInMsecGa(int32_t bufferSizeInMsec) override; 200 201 /** 202 * Provides the sampling rate for the active audio stream 203 * created using CreateStreamGa 204 * 205 * @return Returns sampling rate in Hz 206 */ 207 uint32_t GetSamplingRateGa() const override; 208 209 /** 210 * Provides the channel count for the active audio stream 211 * created using CreateStreamGa 212 * 213 * @return Returns number of channels 214 */ 215 uint8_t GetChannelCountGa() const override; 216 217 /** 218 * Provides the sample size for the active audio stream 219 * created using CreateStreamGa 220 * 221 * @return Returns sample size in number of bits 222 */ 223 uint8_t GetSampleSizeGa() const override; 224 225 // Device volume & route handling APIs 226 227 // Audio stream callbacks 228 229 /** 230 * Register for callbacks associated with the playback stream created using CreateStreamGa 231 * 232 * @param cb indicates pointer for registered callbacks 233 * @return none 234 */ 235 void RegisterAudioRendererCallbackGa(const AudioRendererCallback &cb) override; 236 237 /** 238 * Register for callbacks associated with the record stream created using CreateStreamGa 239 * 240 * @param cb indicates pointer for registered callbacks 241 * @return none 242 */ 243 void RegisterAudioCapturerCallbackGa(const AudioCapturerCallback &cb) override; 244 245 /** 246 * Set the renderer frame position callback 247 * 248 * @param callback indicates pointer for registered callbacks 249 * @return none 250 */ 251 void SetRendererPositionCallbackGa(int64_t markPosition, 252 const std::shared_ptr<RendererPositionCallback> &callback) override; 253 254 /** 255 * Unset the renderer frame position callback 256 * 257 * @return none 258 */ 259 void UnsetRendererPositionCallbackGa() override; 260 261 /** 262 * Set the renderer frame period position callback 263 * 264 * @param callback indicates pointer for registered callbacks 265 * @return none 266 */ 267 void SetRendererPeriodPositionCallbackGa(int64_t markPosition, 268 const std::shared_ptr<RendererPeriodPositionCallback> &callback) override; 269 270 /** 271 * Unset the renderer frame period position callback 272 * 273 * @return none 274 */ 275 void UnsetRendererPeriodPositionCallbackGa() override; 276 277 /** 278 * Set the capturer frame position callback 279 * 280 * @param callback indicates pointer for registered callbacks 281 * @return none 282 */ 283 void SetCapturerPositionCallbackGa(int64_t markPosition, 284 const std::shared_ptr<CapturerPositionCallback> &callback) override; 285 286 /** 287 * Unset the capturer frame position callback 288 * 289 * @return none 290 */ 291 void UnsetCapturerPositionCallbackGa() override; 292 293 /** 294 * Set the capturer frame period position callback 295 * 296 * @param callback indicates pointer for registered callbacks 297 * @return none 298 */ 299 void SetCapturerPeriodPositionCallbackGa(int64_t markPosition, 300 const std::shared_ptr<CapturerPeriodPositionCallback> &callback) override; 301 302 /** 303 * Unset the capturer frame period position callback 304 * 305 * @return none 306 */ 307 void UnsetCapturerPeriodPositionCallbackGa() override; 308 309 /** 310 * @brief Set the track volume 311 * 312 * @param volume The volume to be set for the current track. 313 * @return Returns {@link SUCCESS} if volume is successfully set; returns an error code 314 * defined in {@link audio_errors.h} otherwise. 315 */ 316 int32_t SetStreamVolumeGa(float volume, const int32_t &trackId) override; 317 318 /** 319 * @brief Obtains the current track volume 320 * 321 * @return Returns current track volume 322 */ 323 float GetStreamVolumeGa() override; 324 325 /** 326 * @brief Set the render rate 327 * 328 * @param renderRate The rate at which the stream needs to be rendered. 329 * @return Returns {@link SUCCESS} if render rate is successfully set; returns an error code 330 * defined in {@link audio_errors.h} otherwise. 331 */ 332 int32_t SetStreamRenderRateGa(AudioRendererRate renderRate, const int32_t &trackId) override; 333 334 /** 335 * @brief Obtains the current render rate 336 * 337 * @return Returns current render rate 338 */ 339 AudioRendererRate GetStreamRenderRateGa() override; 340 341 /** 342 * @brief Saves StreamCallback 343 * 344 * @param callback callback reference to be saved. 345 * @return none. 346 */ 347 348 void SaveStreamCallbackGa(const std::weak_ptr<AudioStreamCallback> &callback) override; 349 350 /** 351 * @brief Sets the render mode. By default the mode is RENDER_MODE_NORMAL. 352 * This API is needs to be used only if RENDER_MODE_CALLBACK is required. 353 * 354 * * @param renderMode The mode of render. 355 * @return Returns {@link SUCCESS} if render mode is successfully set; returns an error code 356 * defined in {@link audio_errors.h} otherwise. 357 */ 358 int32_t SetAudioRenderModeGa(AudioRenderMode renderMode, const int32_t &trackId) override; 359 360 /** 361 * @brief Obtains the render mode. 362 * 363 * @return Returns current render mode. 364 */ 365 AudioRenderMode GetAudioRenderModeGa() override; 366 367 /** 368 * @brief Registers the renderer write callback listener. 369 * This API should only be used if RENDER_MODE_CALLBACK is needed. 370 * 371 * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code 372 * defined in {@link audio_errors.h} otherwise. 373 */ 374 int32_t SaveWriteCallbackGa(const std::weak_ptr<AudioRendererWriteCallback> &callback, 375 const int32_t &trackId = 0) override; 376 377 int32_t SetAudioCaptureMode(AudioCaptureMode captureMode) override; 378 379 int32_t SaveReadCallback(const std::weak_ptr<AudioCapturerReadCallback> &callback) override; 380 /** 381 * @brief Set the applicationcache path to access the application resources 382 * 383 * @return none 384 */ 385 void SetAppCachePath(const std::string cachePath) override; 386 387 AudioCaptureMode GetAudioCaptureMode() override; 388 389 std::mutex dataMutex; 390 std::mutex ctrlMutex; 391 392 int32_t audioTrackId = 0; 393 394 const void *internalReadBuffer; 395 size_t internalRdBufLen; 396 size_t internalRdBufIndex; 397 398 float mVolumeFactor; 399 AudioStreamType mStreamType; 400 AudioSystemManager *mAudioSystemMgr; 401 402 AudioRendererRate renderRate; 403 AudioRenderMode renderMode_; 404 std::weak_ptr<AudioRendererWriteCallback> writeCallback_; 405 406 uint32_t mFrameSize = 0; 407 bool mMarkReached = false; 408 uint64_t mFrameMarkPosition = 0; 409 uint64_t mFramePeriodNumber = 0; 410 411 uint64_t mTotalBytesWritten = 0; 412 uint64_t mFramePeriodWritten = 0; 413 std::shared_ptr<RendererPositionCallback> mRenderPositionCb; 414 std::shared_ptr<RendererPeriodPositionCallback> mRenderPeriodPositionCb; 415 416 uint64_t mTotalBytesRead = 0; 417 uint64_t mFramePeriodRead = 0; 418 std::shared_ptr<CapturerPositionCallback> mCapturePositionCb; 419 std::shared_ptr<CapturerPeriodPositionCallback> mCapturePeriodPositionCb; 420 421 std::vector<std::unique_ptr<std::thread>> mPositionCBThreads; 422 std::vector<std::unique_ptr<std::thread>> mPeriodPositionCBThreads; 423 424 std::weak_ptr<AudioStreamCallback> streamCallback_; 425 State state_; 426 427 // To be set while using audio stream 428 // functionality for callbacks 429 AudioRendererCallback *mAudioRendererCallbacks; 430 AudioCapturerCallback *mAudioCapturerCallbacks; 431 432 ASClientType eAudioClientType; 433 434 // These APIs are applicable only for playback scenarios 435 void InitializeClientGa(); 436 int32_t InitializeAudioCacheGa(); 437 438 int32_t UpdateReadBufferGa(uint8_t *buffer, size_t &length, size_t &readSize); 439 int32_t WriteStreamInnerGa(const uint8_t *buffer, size_t &length, const int32_t &trackId); 440 void HandleRenderPositionCallbacksGa(size_t bytesWritten); 441 void HandleCapturePositionCallbacksGa(size_t bytesRead); 442 443 // Default values 444 static const uint32_t MINIMUM_BUFFER_SIZE = 1024; 445 static const uint32_t DEFAULT_SAMPLING_RATE = 44100; 446 static const uint8_t DEFAULT_CHANNEL_COUNT = 2; 447 static const uint8_t DEFAULT_SAMPLE_SIZE = 2; 448 static const uint32_t DEFAULT_STREAM_VOLUME = 65536; 449 static const std::string GetStreamNameGa(AudioStreamType audioType); 450 451 static constexpr float MAX_STREAM_VOLUME_LEVEL = 1.0f; 452 static constexpr float MIN_STREAM_VOLUME_LEVEL = 0.0f; 453 454 // Resets PA audio client and free up resources if any with this API 455 void ResetPAAudioClientGa(); 456 457 static inline BrokerDelegator<AudioContainerClientBase> delegator_; 458 }; 459 } // namespace AudioStandard 460 } // namespace OHOS 461 #endif // AUDIO_STREAM_SERVICE_CLIENT_H