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_SERVICE_CLIENT_H 17 #define AUDIO_SERVICE_CLIENT_H 18 19 #include <algorithm> 20 #include <array> 21 #include <cstring> 22 #include <iostream> 23 #include <map> 24 #include <memory> 25 #include <mutex> 26 #include <queue> 27 #include <stdlib.h> 28 #include <thread> 29 #include <unistd.h> 30 #include <audio_error.h> 31 #include <audio_info.h> 32 #include <audio_timer.h> 33 #include "audio_capturer.h" 34 #include "audio_renderer.h" 35 #include "audio_system_manager.h" 36 37 #include "ipc_types.h" 38 #include "iremote_broker.h" 39 #include "iremote_proxy.h" 40 #include "iremote_stub.h" 41 #include "iservice_registry.h" 42 43 namespace OHOS { 44 namespace AudioStandard { 45 enum ASClientType { 46 AUDIO_SERVICE_CLIENT_PLAYBACK, 47 AUDIO_SERVICE_CLIENT_RECORD, 48 AUDIO_SERVICE_CLIENT_CONTROLLER 49 }; 50 51 struct StreamBuffer { 52 uint8_t *buffer; // the virtual address of stream 53 uint32_t bufferLen; // stream length in bytes 54 }; 55 56 struct AudioCache { 57 std::unique_ptr<uint8_t[]> buffer; 58 uint32_t readIndex; 59 uint32_t writeIndex; 60 uint32_t totalCacheSize; 61 bool isFull; 62 }; 63 64 /** 65 * @brief Enumerates the stream states of the current device. 66 * 67 * @since 1.0 68 * @version 1.0 69 */ 70 enum State { 71 /** INVALID */ 72 INVALID = -1, 73 /** New */ 74 NEW, 75 /** Prepared */ 76 PREPARED, 77 /** Running */ 78 RUNNING, 79 /** Stopped */ 80 STOPPED, 81 /** Released */ 82 RELEASED, 83 /** Paused */ 84 PAUSED 85 }; 86 87 /** 88 * @brief Enumerates the stream states of the current device. 89 * 90 * @since 1.0 91 * @version 1.0 92 */ 93 enum CMD_SEND { 94 CMD_CREATE_AUDIOSTREAM = 1, 95 CMD_START_AUDIOSTREAM = 2, 96 CMD_PAUSE_AUDIOSTREAM = 3, 97 CMD_STOP_AUDIOSTREAM = 4, 98 CMD_FLUSH_AUDIOSTREAM = 5, 99 CMD_DRAIN_AUDIOSTREAM = 6, 100 CMD_GET_AUDIO_SESSIONID = 7, 101 CMD_SET_AUDIORENDERER_MODE = 8, 102 CMD_WRITE_AUDIOSTREAM = 10, 103 CMD_RELEASE_AUDIOSTREAM = 11, 104 CMD_GET_MINIMUM_BUFFERSIZE = 12, 105 CMD_GET_MINIMUM_FRAMECOUNT = 13, 106 CMD_SET_STREAM_VOLUME = 15, 107 CMD_GET_AUDIO_LATENCY = 16, 108 CMD_SET_STREAM_TYPE = 17, 109 CMD_SET_STREAM_RENDER_RATE = 19, 110 CMD_GET_CURRENT_TIMESTAMP = 21, 111 CMD_GET_AUDIOSTREAM_PARAMS = 29, 112 CMD_WRITE_RENDERER_CALLBACK = 30, 113 CMD_READ_STREAM = 39, 114 }; 115 116 enum AUDIO_STREAM_ERR { 117 // Error code used 118 AUDIO_CLIENT_SUCCESS = 0, 119 AUDIO_CLIENT_ERR = -1, 120 AUDIO_CLIENT_INVALID_PARAMS_ERR = -2, 121 AUDIO_CLIENT_INIT_ERR = -3, 122 AUDIO_CLIENT_CREATE_STREAM_ERR = -4, 123 AUDIO_CLIENT_START_STREAM_ERR = -5, 124 AUDIO_CLIENT_READ_STREAM_ERR = -6, 125 AUDIO_CLIENT_WRITE_STREAM_ERR = -7, 126 AUDIO_CLIENT_PA_ERR = -8, 127 }; 128 129 130 class AudioStreamCallback { 131 public: 132 virtual ~AudioStreamCallback() = default; 133 /** 134 * Called when stream state is updated. 135 * 136 * @param state Indicates the InterruptEvent information needed by client. 137 * For details, refer InterruptEvent struct in audio_info.h 138 */ 139 virtual void OnStateChange(const State state, StateChangeCmdType cmdType = CMD_FROM_CLIENT) = 0; 140 }; 141 142 class AudioContainerStreamCallback { 143 // Need to check required state changes to update applications 144 virtual void OnStreamStateChangeCb() const = 0; 145 virtual void OnStreamBufferUnderFlowCb() const = 0; 146 virtual void OnStreamBufferOverFlowCb() const = 0; 147 virtual void OnErrorCb(AudioServiceErrorCodes error) const = 0; 148 virtual void OnEventCb(AudioServiceEventTypes error) const = 0; 149 }; 150 151 class AudioRendererCallbacks : public AudioContainerStreamCallback { 152 public: 153 virtual ~AudioRendererCallbacks(); 154 virtual void OnSinkDeviceUpdatedCb() const = 0; 155 }; 156 157 class AudioCapturerCallbacks : public AudioContainerStreamCallback { 158 public: 159 virtual ~AudioCapturerCallbacks(); 160 virtual void OnSourceDeviceUpdatedCb() const = 0; 161 }; 162 163 class IAudioContainerService : public IRemoteBroker { 164 public: 165 /** 166 * Initializes audio service client for the required client type 167 * 168 * @param eClientType indicates the client type like playback, record or controller. 169 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 170 */ 171 virtual int32_t InitializeGa(ASClientType eClientType) = 0; 172 173 // Stream handling APIs 174 175 /** 176 * Creates & initializes resources based on the audioParams and audioType 177 * 178 * @param audioParams indicate format, sampling rate and number of channels 179 * @param audioType indicate the stream type like music, system, ringtone etc 180 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 181 */ 182 virtual int32_t CreateStreamGa(AudioStreamParams audioParams, AudioStreamType audioType) = 0; 183 184 /** 185 * @brief Obtains session ID 186 * 187 * @return Returns unique session ID for the created session. 188 */ 189 virtual int32_t GetSessionIDGa(uint32_t &sessionID, const int32_t &trackId) = 0; 190 191 /** 192 * Starts the stream created using CreateStreamGa 193 * 194 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 195 */ 196 virtual int32_t StartStreamGa(const int32_t &trackId) = 0; 197 198 /** 199 * Stops the stream created using CreateStreamGa 200 * 201 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 202 */ 203 virtual int32_t StopStreamGa(const int32_t &trackId) = 0; 204 205 /** 206 * Flushes the stream created using CreateStreamGa. This is applicable for 207 * playback only 208 * 209 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 210 */ 211 virtual int32_t FlushStreamGa(const int32_t &trackId) = 0; 212 213 /** 214 * Drains the stream created using CreateStreamGa. This is applicable for 215 * playback only 216 * 217 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 218 */ 219 virtual int32_t DrainStreamGa(const int32_t &trackId) = 0; 220 221 /** 222 * Pauses the stream 223 * 224 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 225 */ 226 virtual int32_t PauseStreamGa(const int32_t &trackId) = 0; 227 228 /** 229 * Update the stream type 230 * 231 * @param audioStreamType Audio stream type 232 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 233 */ 234 virtual int32_t SetStreamTypeGa(AudioStreamType audioStreamType, const int32_t &trackId) = 0; 235 236 /** 237 * Sets the volume of the stream associated with session ID 238 * 239 * @param sessionID indicates the ID for the active stream to be controlled 240 * @param volume indicates volume level between 0 to 65536 241 * @return returns {@code 0} if success; returns {@code -1} otherwise. 242 */ 243 virtual int32_t SetStreamVolumeGa(uint32_t sessionID, uint32_t volume) = 0; 244 245 /** 246 * Get the volume of the stream associated with session ID 247 * 248 * @param sessionID indicates the ID for the active stream to be controlled 249 * @return Returns volume level between 0 to 65536 250 */ 251 virtual uint32_t GetStreamVolumeGa(uint32_t sessionID) = 0; 252 253 /** 254 * Writes audio data of the stream created using CreateStreamGa to active sink device 255 * 256 * @param buffer contains audio data to write 257 * @param bufferSize indicates the size of audio data in bytes to write from the buffer 258 * @param pError indicates pointer to error which will be filled in case of internal errors 259 * @return Returns size of audio data written in bytes. 260 */ 261 virtual size_t WriteStreamGa(const StreamBuffer &stream, int32_t &pError, const int32_t &trackId) = 0; 262 263 /** 264 * Writes audio data of the stream created using CreateStreamGa 265 * 266 * @param buffer contains audio data to write 267 * @param bufferSize indicates the size of audio data in bytes to write from the buffer 268 * @param pError indicates pointer to error which will be filled in case of internal errors 269 * @return Returns size of audio data written in bytes. 270 */ 271 virtual size_t WriteStreamInCbGa(const StreamBuffer &stream, int32_t &pError, const int32_t &trackId) = 0; 272 273 /** 274 * Reads audio data of the stream created using CreateStreamGa from active source device 275 * 276 * @param StreamBuffer including buffer to be filled with audio data 277 * andbufferSize indicating the size of audio data to read into buffer 278 * @param isBlocking indicates if the read is blocking or not 279 * @return Returns size read if success; returns {@code -1} failure. 280 */ 281 virtual int32_t ReadStreamGa(StreamBuffer &stream, bool isBlocking, const int32_t &trackId) = 0; 282 283 /** 284 * Release the resources allocated using CreateStreamGa 285 * 286 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 287 */ 288 virtual int32_t ReleaseStreamGa(const int32_t &trackId) = 0; 289 290 /** 291 * Provides the current timestamp for playback/record stream created using CreateStreamGa 292 * 293 * @param timeStamp will be filled up with the current timestamp 294 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 295 */ 296 virtual int32_t GetCurrentTimeStampGa(uint64_t &timeStamp, const int32_t &trackId) = 0; 297 298 /** 299 * Provides the current latency for playback/record stream created using CreateStreamGa 300 * 301 * @param latency will be filled up with current latency in microseconds 302 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 303 */ 304 virtual int32_t GetAudioLatencyGa(uint64_t &latency, const int32_t &trackId) = 0; 305 306 /** 307 * Provides the playback/record stream parameters created using CreateStreamGa 308 * 309 * @param audioParams will be filled up with stream audio parameters 310 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 311 */ 312 virtual int32_t GetAudioStreamParamsGa(AudioStreamParams &audioParams, const int32_t &trackId) = 0; 313 314 /** 315 * Provides the minimum buffer size required for this audio stream 316 * created using CreateStreamGa 317 * @param minBufferSize will be set to minimum buffer size in bytes 318 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 319 */ 320 virtual int32_t GetMinimumBufferSizeGa(size_t &minBufferSize, const int32_t &trackId) = 0; 321 322 /** 323 * Provides the minimum frame count required for this audio stream 324 * created using CreateStreamGa 325 * @param frameCount will be set to minimum number of frames 326 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 327 */ 328 virtual int32_t GetMinimumFrameCountGa(uint32_t &frameCount, const int32_t &trackId) = 0; 329 330 /** 331 * @brief Set the buffer duration in msec 332 * 333 * @param bufferSizeInMsec buffer size in duration. 334 * @return Returns {@link SUCCESS} defined in {@link audio_errors.h} otherwise. 335 */ 336 virtual int32_t SetBufferSizeInMsecGa(int32_t bufferSizeInMsec) = 0; 337 338 /** 339 * Provides the sampling rate for the active audio stream 340 * created using CreateStreamGa 341 * 342 * @return Returns sampling rate in Hz 343 */ 344 virtual uint32_t GetSamplingRateGa() const = 0; 345 346 /** 347 * Provides the channel count for the active audio stream 348 * created using CreateStreamGa 349 * 350 * @return Returns number of channels 351 */ 352 virtual uint8_t GetChannelCountGa() const = 0; 353 354 /** 355 * Provides the sample size for the active audio stream 356 * created using CreateStreamGa 357 * 358 * @return Returns sample size in number of bits 359 */ 360 virtual uint8_t GetSampleSizeGa() const = 0; 361 362 // Device volume & route handling APIS 363 364 // Audio stream callbacks 365 366 /** 367 * Register for callbacks associated with the playback stream created using CreateStreamGa 368 * 369 * @param cb indicates pointer for registered callbacks 370 * @return none 371 */ 372 virtual void RegisterAudioRendererCallbackGa(const AudioRendererCallback &cb) = 0; 373 374 /** 375 * Register for callbacks associated with the record stream created using CreateStream 376 * 377 * @param cb indicates pointer for registered callbacks 378 * @return none 379 */ 380 virtual void RegisterAudioCapturerCallbackGa(const AudioCapturerCallback &cb) = 0; 381 382 /** 383 * Set the renderer frame position callback 384 * 385 * @param callback indicates pointer for registered callbacks 386 * @return none 387 */ 388 virtual void SetRendererPositionCallbackGa(int64_t markPosition, 389 const std::shared_ptr<RendererPositionCallback> &callback) = 0; 390 391 /** 392 * Unset the renderer frame position callback 393 * 394 * @return none 395 */ 396 virtual void UnsetRendererPositionCallbackGa() = 0; 397 398 /** 399 * Set the renderer frame period position callback 400 * 401 * @param callback indicates pointer for registered callbacks 402 * @return none 403 */ 404 virtual void SetRendererPeriodPositionCallbackGa(int64_t markPosition, 405 const std::shared_ptr<RendererPeriodPositionCallback> &callback) = 0; 406 407 /** 408 * Unset the renderer frame period position callback 409 * 410 * @return none 411 */ 412 virtual void UnsetRendererPeriodPositionCallbackGa() = 0; 413 414 /** 415 * Set the capturer frame position callback 416 * 417 * @param callback indicates pointer for registered callbacks 418 * @return none 419 */ 420 virtual void SetCapturerPositionCallbackGa(int64_t markPosition, 421 const std::shared_ptr<CapturerPositionCallback> &callback) = 0; 422 423 /** 424 * Unset the capturer frame position callback 425 * 426 * @return none 427 */ 428 virtual void UnsetCapturerPositionCallbackGa() = 0; 429 430 /** 431 * Set the capturer frame period position callback 432 * 433 * @param callback indicates pointer for registered callbacks 434 * @return none 435 */ 436 virtual void SetCapturerPeriodPositionCallbackGa(int64_t markPosition, 437 const std::shared_ptr<CapturerPeriodPositionCallback> &callback) = 0; 438 439 /** 440 * Unset the capturer frame period position callback 441 * 442 * @return none 443 */ 444 virtual void UnsetCapturerPeriodPositionCallbackGa() = 0; 445 446 /** 447 * @brief Set the track volume 448 * 449 * @param volume The volume to be set for the current track. 450 * @return Returns {@link SUCCESS} if volume is successfully set; returns an error code 451 * defined in {@link audio_errors.h} otherwise. 452 */ 453 virtual int32_t SetStreamVolumeGa(float volume, const int32_t &trackId) = 0; 454 455 /** 456 * @brief Obtains the current track volume 457 * 458 * @return Returns current track volume 459 */ 460 virtual float GetStreamVolumeGa() = 0; 461 462 /** 463 * @brief Set the render rate 464 * 465 * @param renderRate The rate at which the stream needs to be rendered. 466 * @return Returns {@link SUCCESS} if render rate is successfully set; returns an error code 467 * defined in {@link audio_errors.h} otherwise. 468 */ 469 virtual int32_t SetStreamRenderRateGa(AudioRendererRate renderRate, const int32_t &trackId) = 0; 470 471 /** 472 * @brief Obtains the current render rate 473 * 474 * @return Returns current render rate 475 */ 476 virtual AudioRendererRate GetStreamRenderRateGa() = 0; 477 478 /** 479 * @brief Saves StreamCallback 480 * 481 * @param callback callback reference to be saved. 482 * @return none. 483 */ 484 virtual void SaveStreamCallbackGa(const std::weak_ptr<AudioStreamCallback> &callback) = 0; 485 486 /** 487 * @brief Sets the render mode. By default the mode is RENDER_MODE_NORMAL. 488 * This API is needs to be used only if RENDER_MODE_CALLBACK is required. 489 * 490 * @param renderMode The mode of render. 491 * @return Returns {@link SUCCESS} if render mode is successfully set; returns an error code 492 * defined in {@link audio_errors.h} otherwise. 493 */ 494 virtual int32_t SetAudioRenderModeGa(AudioRenderMode renderMode, const int32_t &trackId) = 0; 495 496 /** 497 * @brief Obtains the render mode. 498 * 499 * @return Returns current render mode. 500 */ 501 virtual AudioRenderMode GetAudioRenderModeGa() = 0; 502 503 /** 504 * @brief Registers the renderer write callback listener. 505 * This API should only be used if RENDER_MODE_CALLBACK is needed. 506 * 507 * @return Returns {@link SUCCESS} if callback registreation is successful; returns an error code 508 * defined in {@link audio_errors.h} otherwise. 509 */ 510 virtual int32_t SaveWriteCallbackGa(const std::weak_ptr<AudioRendererWriteCallback> &callback, 511 const int32_t &trackId = 0) = 0; 512 513 virtual int32_t SetAudioCaptureMode(AudioCaptureMode captureMode) = 0; 514 515 virtual int32_t SaveReadCallback(const std::weak_ptr<AudioCapturerReadCallback> &callback) = 0; 516 /** 517 * @brief Set the applicationcache path to access the application resources 518 * 519 * @return none 520 */ 521 virtual void SetAppCachePath(const std::string cachePath) = 0; 522 523 virtual AudioCaptureMode GetAudioCaptureMode() = 0; 524 public: 525 DECLARE_INTERFACE_DESCRIPTOR(u"ohos.sysability.samgr.IAudioStreamServiceGateway"); 526 }; 527 } // namespace AudioStandard 528 } // namespace OHOS 529 #endif // AUDIO_SERVICE_CLIENT_H 530