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_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 <pulse/pulseaudio.h> 31 #include <pulse/thread-mainloop.h> 32 #include <audio_error.h> 33 #include <audio_info.h> 34 #include <audio_timer.h> 35 36 #include "event_handler.h" 37 #include "event_runner.h" 38 39 #include "audio_capturer.h" 40 #include "audio_policy_manager.h" 41 #include "audio_renderer.h" 42 #include "audio_system_manager.h" 43 #include "i_audio_stream.h" 44 45 namespace OHOS { 46 namespace AudioStandard { 47 enum ASClientType { 48 AUDIO_SERVICE_CLIENT_PLAYBACK, 49 AUDIO_SERVICE_CLIENT_RECORD, 50 AUDIO_SERVICE_CLIENT_CONTROLLER 51 }; 52 53 typedef pa_sink_input_info SinkInputInfo; 54 typedef pa_source_output_info SourceOutputInfo; 55 typedef pa_sink_info SinkDeviceInfo; 56 typedef pa_source_info SourceDeviceInfo; 57 typedef pa_client_info ClientInfo; 58 59 struct StreamBuffer { 60 uint8_t *buffer; // the virtual address of stream 61 uint32_t bufferLen; // stream length in bytes 62 }; 63 64 struct AudioCache { 65 std::unique_ptr<uint8_t[]> buffer; 66 uint32_t readIndex; 67 uint32_t writeIndex; 68 uint32_t totalCacheSize; 69 bool isFull; 70 }; 71 72 class AudioRendererCallbacks { 73 public: 74 virtual ~AudioRendererCallbacks(); 75 virtual void OnSinkDeviceUpdatedCb() const = 0; 76 // Need to check required state changes to update applications 77 virtual void OnStreamStateChangeCb() const = 0; 78 virtual void OnStreamBufferUnderFlowCb() const = 0; 79 virtual void OnStreamBufferOverFlowCb() const = 0; 80 virtual void OnErrorCb(AudioServiceErrorCodes error) const = 0; 81 virtual void OnEventCb(AudioServiceEventTypes error) const = 0; 82 }; 83 84 class AudioCapturerCallbacks { 85 public: 86 virtual ~AudioCapturerCallbacks(); 87 virtual void OnSourceDeviceUpdatedCb() const = 0; 88 // Need to check required state changes to update applications 89 virtual void OnStreamStateChangeCb() const = 0; 90 virtual void OnStreamBufferUnderFlowCb() const = 0; 91 virtual void OnStreamBufferOverFlowCb() const = 0; 92 virtual void OnErrorCb(AudioServiceErrorCodes error) const = 0; 93 virtual void OnEventCb(AudioServiceEventTypes error) const = 0; 94 }; 95 96 class AudioServiceClient : public IAudioStream, public AudioTimer, public AppExecFwk::EventHandler { 97 public: 98 static constexpr char PA_RUNTIME_DIR[] = "/data/data/.pulse_dir/runtime"; 99 static constexpr char PA_STATE_DIR[] = "/data/data/.pulse_dir/state"; 100 static constexpr char PA_HOME_DIR[] = "/data/data/.pulse_dir/state"; 101 102 AudioServiceClient(); 103 virtual ~AudioServiceClient(); 104 105 /** 106 * Initializes audio service client for the required client type 107 * 108 * @param eClientType indicates the client type like playback, record or controller. 109 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 110 */ 111 int32_t Initialize(ASClientType eClientType); 112 113 // Stream handling APIs 114 115 /** 116 * Creates & initializes resources based on the audioParams and audioType 117 * 118 * @param audioParams indicate format, sampling rate and number of channels 119 * @param audioType indicate the stream type like music, system, ringtone etc 120 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 121 */ 122 int32_t CreateStream(AudioStreamParams audioParams, AudioStreamType audioType); 123 124 /** 125 * @brief Obtains session ID . 126 * 127 * @return Returns unique session ID for the created session 128 */ 129 int32_t GetSessionID(uint32_t &sessionID) const; 130 131 /** 132 * Starts the stream created using CreateStream 133 * 134 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 135 */ 136 int32_t StartStream(StateChangeCmdType cmdType = CMD_FROM_CLIENT); 137 138 /** 139 * Stops the stream created using CreateStream 140 * 141 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 142 */ 143 int32_t StopStream(); 144 145 /** 146 * Flushes the stream created using CreateStream. This is applicable for 147 * playback only 148 * 149 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 150 */ 151 int32_t FlushStream(); 152 153 /** 154 * Drains the stream created using CreateStream. This is applicable for 155 * playback only 156 * 157 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 158 */ 159 int32_t DrainStream(); 160 161 /** 162 * Pauses the stream 163 * 164 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 165 */ 166 int32_t PauseStream(StateChangeCmdType cmdType = CMD_FROM_CLIENT); 167 168 /** 169 * Update the stream type 170 * 171 * @param audioStreamType Audio stream type 172 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 173 */ 174 int32_t SetStreamType(AudioStreamType audioStreamType); 175 176 /** 177 * Writes audio data of the stream created using CreateStream to active sink device 178 * 179 * @param buffer contains audio data to write 180 * @param bufferSize indicates the size of audio data in bytes to write from the buffer 181 * @param pError indicates pointer to error which will be filled in case of internal errors 182 * @return returns size of audio data written in bytes. 183 */ 184 size_t WriteStream(const StreamBuffer &stream, int32_t &pError); 185 int32_t RenderPrebuf(uint32_t writeLen); 186 187 /** 188 * Writes audio data of the stream created using CreateStream to active sink device 189 Used when render mode is RENDER_MODE_CALLBACK 190 * 191 * @param buffer contains audio data to write 192 * @param bufferSize indicates the size of audio data in bytes to write from the buffer 193 * @param pError indicates pointer to error which will be filled in case of internal errors 194 * @return returns size of audio data written in bytes. 195 */ 196 size_t WriteStreamInCb(const StreamBuffer &stream, int32_t &pError); 197 198 /** 199 * Reads audio data of the stream created using CreateStream from active source device 200 * 201 * @param StreamBuffer including buffer to be filled with audio data 202 * and bufferSize indicating the size of audio data to read into buffer 203 * @param isBlocking indicates if the read is blocking or not 204 * @return Returns size read if success; returns {@code -1} failure. 205 */ 206 int32_t ReadStream(StreamBuffer &stream, bool isBlocking); 207 208 /** 209 * Release the resources allocated using CreateStream 210 * 211 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 212 */ 213 int32_t ReleaseStream(bool releaseRunner = true); 214 215 /** 216 * Provides the current timestamp for playback/record stream created using CreateStream 217 * 218 * @param timeStamp will be filled up with current timestamp 219 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 220 */ 221 int32_t GetCurrentTimeStamp(uint64_t &timeStamp); 222 223 /** 224 * Provides the current latency for playback/record stream created using CreateStream 225 * 226 * @param latency will be filled up with the current latency in microseconds 227 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 228 */ 229 int32_t GetAudioLatency(uint64_t &latency); 230 231 /** 232 * Provides the playback/record stream parameters created using CreateStream 233 * 234 * @param audioParams will be filled up with stream audio parameters 235 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 236 */ 237 int32_t GetAudioStreamParams(AudioStreamParams &audioParams) const; 238 239 /** 240 * Provides the minimum buffer size required for this audio stream 241 * created using CreateStream 242 * @param minBufferSize will be set to minimum buffer size in bytes 243 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 244 */ 245 int32_t GetMinimumBufferSize(size_t &minBufferSize) const; 246 247 /** 248 * Provides the minimum frame count required for this audio stream 249 * created using CreateStream 250 * @param frameCount will be set to minimum number of frames 251 * @return Returns {@code 0} if success; returns {@code -1} otherwise. 252 */ 253 int32_t GetMinimumFrameCount(uint32_t &frameCount) const; 254 255 /** 256 * Provides the sampling rate for the active audio stream 257 * created using CreateStream 258 * 259 * @return Returns sampling rate in Hz 260 */ 261 uint32_t GetSamplingRate() const; 262 263 /** 264 * Provides the channel count for the active audio stream 265 * created using CreateStream 266 * 267 * @return Returns number of channels 268 */ 269 uint8_t GetChannelCount() const; 270 271 /** 272 * Provides the sample size for the active audio stream 273 * created using CreateStream 274 * 275 * @return Returns sample size in number of bits 276 */ 277 uint8_t GetSampleSize() const; 278 279 void SetStreamInnerCapturerState(bool isInnerCapturer); 280 281 void SetStreamPrivacyType(AudioPrivacyType privacyType); 282 283 /** 284 * Provides the underflow count required for this audio stream 285 * created using CreateStream 286 * @param underFlowCount will be get to number of frames 287 * @return Returns number of underflow 288 */ 289 uint32_t GetUnderflowCount() override; 290 291 // Device volume & route handling APIs 292 293 // Audio stream callbacks 294 295 /** 296 * Register for callbacks associated with the playback stream created using CreateStream 297 * 298 * @param cb indicates pointer for registered callbacks 299 * @return none 300 */ 301 void RegisterAudioRendererCallbacks(const AudioRendererCallbacks &cb); 302 303 /** 304 * Register for callbacks associated with the record stream created using CreateStream 305 * 306 * @param cb indicates pointer for registered callbacks 307 * @return none 308 */ 309 void RegisterAudioCapturerCallbacks(const AudioCapturerCallbacks &cb); 310 311 /** 312 * Set the renderer frame position callback 313 * 314 * @param callback indicates pointer for registered callbacks 315 * @return none 316 */ 317 void SetRendererPositionCallback(int64_t markPosition, const std::shared_ptr<RendererPositionCallback> &callback) 318 override; 319 320 /** 321 * Unset the renderer frame position callback 322 * 323 * @return none 324 */ 325 void UnsetRendererPositionCallback() override; 326 327 /** 328 * Set the renderer frame period position callback 329 * 330 * @param callback indicates pointer for registered callbacks 331 * @return none 332 */ 333 void SetRendererPeriodPositionCallback(int64_t markPosition, 334 const std::shared_ptr<RendererPeriodPositionCallback> &callback) override; 335 336 /** 337 * Unset the renderer frame period position callback 338 * 339 * @return none 340 */ 341 void UnsetRendererPeriodPositionCallback() override; 342 343 /** 344 * Set the capturer frame position callback 345 * 346 * @param callback indicates pointer for registered callbacks 347 * @return none 348 */ 349 void SetCapturerPositionCallback(int64_t markPosition, const std::shared_ptr<CapturerPositionCallback> &callback) 350 override; 351 352 /** 353 * Unset the capturer frame position callback 354 * 355 * @return none 356 */ 357 void UnsetCapturerPositionCallback() override; 358 359 /** 360 * Set the capturer frame period position callback 361 * 362 * @param callback indicates pointer for registered callbacks 363 * @return none 364 */ 365 void SetCapturerPeriodPositionCallback(int64_t markPosition, 366 const std::shared_ptr<CapturerPeriodPositionCallback> &callback) override; 367 368 /** 369 * Unset the capturer frame period position callback 370 * 371 * @return none 372 */ 373 void UnsetCapturerPeriodPositionCallback() override; 374 375 /** 376 * @brief Set the track volume 377 * 378 * @param volume The volume to be set for the current track. 379 * @return Returns {@link SUCCESS} if volume is successfully set; returns an error code 380 * defined in {@link audio_errors.h} otherwise. 381 */ 382 int32_t SetStreamVolume(float volume); 383 384 /** 385 * @brief Obtains the current track volume 386 * 387 * @return Returns current track volume 388 */ 389 float GetStreamVolume(); 390 391 /** 392 * @brief Set the render rate 393 * 394 * @param renderRate The rate at which the stream needs to be rendered. 395 * @return Returns {@link SUCCESS} if render rate is successfully set; returns an error code 396 * defined in {@link audio_errors.h} otherwise. 397 */ 398 int32_t SetStreamRenderRate(AudioRendererRate renderRate); 399 400 /** 401 * @brief Obtains the current render rate 402 * 403 * @return Returns current render rate 404 */ 405 AudioRendererRate GetStreamRenderRate(); 406 407 /** 408 * @brief Set stream render sampling rate 409 * 410 * @param sampleRate The sample rate at which the stream needs to be rendered. 411 * @return Returns {@link SUCCESS} if render rate is successfully set; returns an error code 412 * defined in {@link audio_errors.h} otherwise. 413 */ 414 int32_t SetRendererSamplingRate(uint32_t sampleRate) override; 415 416 /** 417 * @brief Obtains render sampling rate 418 * 419 * @return Returns current render sampling rate 420 */ 421 uint32_t GetRendererSamplingRate() override; 422 423 /** 424 * @brief Set the buffer duration in msec 425 * 426 * @param bufferSizeInMsec buffer size in duration. 427 * @return Returns {@link SUCCESS} defined in {@link audio_errors.h} otherwise. 428 */ 429 int32_t SetBufferSizeInMsec(int32_t bufferSizeInMsec) override; 430 431 /** 432 * @brief Saves StreamCallback 433 * 434 * @param callback callback reference to be saved. 435 * @return none. 436 */ 437 438 void SaveStreamCallback(const std::weak_ptr<AudioStreamCallback> &callback); 439 440 /** 441 * @brief Sets the render mode. By default the mode is RENDER_MODE_NORMAL. 442 * This API is needs to be used only if RENDER_MODE_CALLBACK is required. 443 * 444 * * @param renderMode The mode of render. 445 * @return Returns {@link SUCCESS} if render mode is successfully set; returns an error code 446 * defined in {@link audio_errors.h} otherwise. 447 */ 448 int32_t SetAudioRenderMode(AudioRenderMode renderMode); 449 450 /** 451 * @brief Obtains the render mode. 452 * 453 * @return Returns current render mode. 454 */ 455 AudioRenderMode GetAudioRenderMode(); 456 457 /** 458 * @brief Gets the audio effect mode. 459 * 460 * @return Returns current audio effect mode. 461 */ 462 AudioEffectMode GetStreamAudioEffectMode(); 463 464 /** 465 * @brief Gets the audio frame size that has been written. 466 * 467 * @return Returns the audio frame size that has been written. 468 */ 469 int64_t GetStreamFramesWritten(); 470 471 /** 472 * @brief Gets the audio frame size that has been read. 473 * 474 * @return Returns the audio frame size that has been read. 475 */ 476 int64_t GetStreamFramesRead(); 477 478 /** 479 * @brief Sets the audio effect mode. 480 * 481 * @param effectMode The audio effect mode at which the stream needs to be rendered. 482 * @return Returns {@link SUCCESS} if audio effect mode is successfully set; returns an error code 483 * defined in {@link audio_errors.h} otherwise. 484 */ 485 int32_t SetStreamAudioEffectMode(AudioEffectMode effectMode); 486 487 void SetStreamUsage(StreamUsage usage); 488 489 int32_t SetAudioCaptureMode(AudioCaptureMode captureMode); 490 AudioCaptureMode GetAudioCaptureMode(); 491 /** 492 * @brief Set the applicationcache path to access the application resources 493 * 494 * @return none 495 */ 496 void SetApplicationCachePath(const std::string cachePath) override; 497 498 /** 499 * @brief Verifies the clients permsiion based on appTokenId 500 * 501 * @return Returns whether the authentication was success or not 502 */ 503 bool CheckRecordingCreate(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid) override; 504 505 bool CheckRecordingStateChange(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid, 506 AudioPermissionState state) override; 507 int32_t SetStreamLowPowerVolume(float powerVolumeFactor); 508 float GetStreamLowPowerVolume(); 509 float GetSingleStreamVol(); 510 511 // Audio timer callback 512 virtual void OnTimeOut() override; 513 514 void SetClientID(int32_t clientPid, int32_t clientUid) override; 515 516 IAudioStream::StreamClass GetStreamClass() override; 517 void GetStreamSwitchInfo(SwitchInfo& info); 518 519 void SetWakeupCapturerState(bool isWakeupCapturer) override; 520 521 protected: 522 virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 523 void SendWriteBufferRequestEvent(); 524 void SendReadBufferRequestEvent(); 525 void HandleWriteRequestEvent(); 526 void HandleReadRequestEvent(); 527 int32_t SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> &callback) override; 528 int32_t SetCapturerReadCallback(const std::shared_ptr<AudioCapturerReadCallback> &callback) override; 529 530 private: 531 pa_threaded_mainloop *mainLoop; 532 pa_mainloop_api *api; 533 pa_context *context; 534 pa_stream *paStream; 535 pa_sample_spec sampleSpec; 536 537 std::mutex dataMutex_; 538 std::condition_variable dataCv_; 539 std::mutex ctrlMutex_; 540 std::mutex capturerMarkReachedMutex_; 541 std::mutex capturerPeriodReachedMutex_; 542 std::mutex rendererMarkReachedMutex_; 543 std::mutex rendererPeriodReachedMutex_; 544 std::mutex runnerMutex_; 545 std::mutex writeCallbackMutex_; 546 std::mutex stoppingMutex_; 547 bool runnerReleased_ = false; 548 AudioCache acache_; 549 const void *internalReadBuffer_; 550 size_t internalRdBufLen_; 551 size_t internalRdBufIndex_; 552 size_t setBufferSize_; 553 int32_t streamCmdStatus_; 554 int32_t streamDrainStatus_; 555 int32_t streamFlushStatus_; 556 bool isMainLoopStarted_; 557 bool isContextConnected_; 558 bool isStreamConnected_; 559 bool isInnerCapturerStream_; 560 bool isWakeupCapturerStream_ = false; 561 AudioPrivacyType mPrivacyType; 562 StreamUsage mStreamUsage; 563 564 std::unique_ptr<uint8_t[]> preBuf_ {nullptr}; 565 uint32_t sinkLatencyInMsec_ {0}; 566 567 int32_t clientPid_ = 0; 568 int32_t clientUid_ = 0; 569 570 std::string appCookiePath = ""; 571 std::string cachePath_ = ""; 572 573 float mVolumeFactor; 574 float mPowerVolumeFactor; 575 AudioStreamType mStreamType; 576 AudioSystemManager *mAudioSystemMgr; 577 578 pa_cvolume cvolume; 579 uint32_t streamIndex; 580 uint32_t sessionID_; 581 uint32_t volumeChannels; 582 bool streamInfoUpdated; 583 bool firstFrame_; 584 585 AudioRendererRate renderRate; 586 uint32_t rendererSampleRate; 587 AudioRenderMode renderMode_; 588 AudioCaptureMode captureMode_; 589 std::string effectSceneName = ""; 590 AudioEffectMode effectMode; 591 std::shared_ptr<AudioCapturerReadCallback> readCallback_; 592 std::shared_ptr<AudioRendererWriteCallback> writeCallback_; 593 int64_t mWriteCbStamp = 0; // used to measure callback duration 594 uint32_t mFrameSize = 0; 595 bool mMarkReached = false; 596 uint64_t mFrameMarkPosition = 0; 597 uint64_t mFramePeriodNumber = 0; 598 599 uint64_t mTotalBytesWritten = 0; 600 uint64_t mFramePeriodWritten = 0; 601 std::shared_ptr<RendererPositionCallback> mRenderPositionCb; 602 std::shared_ptr<RendererPeriodPositionCallback> mRenderPeriodPositionCb; 603 604 uint64_t mTotalBytesRead = 0; 605 uint64_t mFramePeriodRead = 0; 606 std::shared_ptr<CapturerPositionCallback> mCapturePositionCb; 607 std::shared_ptr<CapturerPeriodPositionCallback> mCapturePeriodPositionCb; 608 609 std::vector<std::unique_ptr<std::thread>> mPositionCBThreads; 610 std::vector<std::unique_ptr<std::thread>> mPeriodPositionCBThreads; 611 612 std::weak_ptr<AudioStreamCallback> streamCallback_; 613 State state_; 614 StateChangeCmdType stateChangeCmdType_ = CMD_FROM_CLIENT; 615 pa_stream_success_cb_t PAStreamCorkSuccessCb; 616 617 // To be set while using audio stream 618 // functionality for callbacks 619 AudioRendererCallbacks *mAudioRendererCallbacks; 620 AudioCapturerCallbacks *mAudioCapturerCallbacks; 621 622 std::map<uint32_t, SinkDeviceInfo*> sinkDevices; 623 std::map<uint32_t, SourceDeviceInfo*> sourceDevices; 624 std::map<uint32_t, SinkInputInfo*> sinkInputs; 625 std::map<uint32_t, SourceOutputInfo*> sourceOutputs; 626 std::map<uint32_t, ClientInfo*> clientInfo; 627 628 IAudioStream::StreamClass streamClass_; 629 630 ASClientType eAudioClientType; 631 632 uint32_t underFlowCount; 633 int32_t ConnectStreamToPA(); 634 std::pair<const int32_t, const std::string> GetDeviceNameForConnect(); 635 636 // Audio cache related functions. These APIs are applicable only for playback scenarios 637 int32_t InitializeAudioCache(); 638 size_t WriteToAudioCache(const StreamBuffer &stream); 639 int32_t DrainAudioCache(); 640 641 int32_t UpdateReadBuffer(uint8_t *buffer, size_t &length, size_t &readSize); 642 int32_t PaWriteStream(const uint8_t *buffer, size_t &length); 643 void HandleRenderPositionCallbacks(size_t bytesWritten); 644 void HandleCapturePositionCallbacks(size_t bytesRead); 645 646 void WriteStateChangedSysEvents(); 647 648 // Error code used 649 static const int32_t AUDIO_CLIENT_SUCCESS = 0; 650 static const int32_t AUDIO_CLIENT_ERR = -1; 651 static const int32_t AUDIO_CLIENT_INVALID_PARAMS_ERR = -2; 652 static const int32_t AUDIO_CLIENT_INIT_ERR = -3; 653 static const int32_t AUDIO_CLIENT_CREATE_STREAM_ERR = -4; 654 static const int32_t AUDIO_CLIENT_START_STREAM_ERR = -5; 655 static const int32_t AUDIO_CLIENT_READ_STREAM_ERR = -6; 656 static const int32_t AUDIO_CLIENT_WRITE_STREAM_ERR = -7; 657 static const int32_t AUDIO_CLIENT_PA_ERR = -8; 658 static const int32_t AUDIO_CLIENT_PERMISSION_ERR = -9; 659 660 // Default values 661 static const uint32_t MINIMUM_BUFFER_SIZE = 1024; 662 static const uint32_t DEFAULT_SAMPLING_RATE = 44100; 663 static const uint8_t DEFAULT_CHANNEL_COUNT = 2; 664 static const uint8_t DEFAULT_SAMPLE_SIZE = 2; 665 static const uint32_t DEFAULT_STREAM_VOLUME = 65536; 666 static const std::string GetStreamName(AudioStreamType audioType); 667 static pa_sample_spec ConvertToPAAudioParams(AudioStreamParams audioParams); 668 static AudioStreamParams ConvertFromPAAudioParams(pa_sample_spec paSampleSpec); 669 static const std::string GetEffectModeName(AudioEffectMode effectMode); 670 671 static constexpr float MAX_STREAM_VOLUME_LEVEL = 1.0f; 672 static constexpr float MIN_STREAM_VOLUME_LEVEL = 0.0f; 673 674 // audio channel index 675 static const uint8_t CHANNEL1_IDX = 0; 676 static const uint8_t CHANNEL2_IDX = 1; 677 static const uint8_t CHANNEL3_IDX = 2; 678 static const uint8_t CHANNEL4_IDX = 3; 679 static const uint8_t CHANNEL5_IDX = 4; 680 static const uint8_t CHANNEL6_IDX = 5; 681 static const uint8_t CHANNEL7_IDX = 6; 682 static const uint8_t CHANNEL8_IDX = 7; 683 684 // Resets PA audio client and free up resources if any with this API 685 void ResetPAAudioClient(); 686 // For setting some environment variables required while running from hap 687 void SetEnv(); 688 int32_t CorkStream(); 689 690 // Callbacks to be implemented 691 static void PAStreamStateCb(pa_stream *stream, void *userdata); 692 static void PAStreamMovedCb(pa_stream *stream, void *userdata); 693 static void PAStreamUnderFlowCb(pa_stream *stream, void *userdata); 694 static void PAContextStateCb(pa_context *context, void *userdata); 695 static void PAStreamReadCb(pa_stream *stream, size_t length, void *userdata); 696 static void PAStreamStartSuccessCb(pa_stream *stream, int32_t success, void *userdata); 697 static void PAStreamStopSuccessCb(pa_stream *stream, int32_t success, void *userdata); 698 static void PAStreamAsyncStopSuccessCb(pa_stream *stream, int32_t success, void *userdata); 699 static void PAStreamPauseSuccessCb(pa_stream *stream, int32_t success, void *userdata); 700 static void PAStreamWriteCb(pa_stream *stream, size_t length, void *userdata); 701 static void PAStreamDrainSuccessCb(pa_stream *stream, int32_t success, void *userdata); 702 static void PAStreamDrainInStopCb(pa_stream *stream, int32_t success, void *userdata); 703 static void PAStreamFlushSuccessCb(pa_stream *stream, int32_t success, void *userdata); 704 static void PAStreamLatencyUpdateCb(pa_stream *stream, void *userdata); 705 static void PAStreamSetBufAttrSuccessCb(pa_stream *stream, int32_t success, void *userdata); 706 707 static void GetSinkInputInfoCb(pa_context *c, const pa_sink_input_info *i, int eol, void *userdata); 708 static void SetPaVolume(const AudioServiceClient &client); 709 static AudioVolumeType GetVolumeTypeFromStreamType(AudioStreamType streamType); 710 711 // OnRenderMarkReach SetRenderMarkReached UnsetRenderMarkReach by eventHandler 712 void SendRenderMarkReachedRequestEvent(uint64_t mFrameMarkPosition); 713 void HandleRenderMarkReachedEvent(uint64_t mFrameMarkPosition); 714 void SendSetRenderMarkReachedRequestEvent(const std::shared_ptr<RendererPositionCallback> &callback); 715 void HandleSetRenderMarkReachedEvent(const std::shared_ptr<RendererPositionCallback> &callback); 716 void SendUnsetRenderMarkReachedRequestEvent(); 717 void HandleUnsetRenderMarkReachedEvent(); 718 719 // OnRenderPeriodReach SetRenderPeriodReach UnsetRenderPeriodReach by eventHandler 720 void SendRenderPeriodReachedRequestEvent(uint64_t mFramePeriodNumber); 721 void HandleRenderPeriodReachedEvent(uint64_t mFramePeriodNumber); 722 void SendSetRenderPeriodReachedRequestEvent(const std::shared_ptr<RendererPeriodPositionCallback> &callback); 723 void HandleSetRenderPeriodReachedEvent(const std::shared_ptr<RendererPeriodPositionCallback> &callback); 724 void SendUnsetRenderPeriodReachedRequestEvent(); 725 void HandleUnsetRenderPeriodReachedEvent(); 726 727 // OnCapturerMarkReach SetCapturerMarkReach UnsetCapturerMarkReach by eventHandler 728 void SendCapturerMarkReachedRequestEvent(uint64_t mFrameMarkPosition); 729 void HandleCapturerMarkReachedEvent(uint64_t mFrameMarkPosition); 730 void SendSetCapturerMarkReachedRequestEvent(const std::shared_ptr<CapturerPositionCallback> &callback); 731 void HandleSetCapturerMarkReachedEvent(const std::shared_ptr<CapturerPositionCallback> &callback); 732 void SendUnsetCapturerMarkReachedRequestEvent(); 733 void HandleUnsetCapturerMarkReachedEvent(); 734 735 // OnCapturerPeriodReach SetCapturerPeriodReach UnsetCapturerPeriodReach by eventHandler 736 void SendCapturerPeriodReachedRequestEvent(uint64_t mFramePeriodNumber); 737 void HandleCapturerPeriodReachedEvent(uint64_t mFramePeriodNumber); 738 void SendSetCapturerPeriodReachedRequestEvent( 739 const std::shared_ptr<CapturerPeriodPositionCallback> &callback); 740 void HandleSetCapturerPeriodReachedEvent( 741 const std::shared_ptr<CapturerPeriodPositionCallback> &callback); 742 void SendUnsetCapturerPeriodReachedRequestEvent(); 743 void HandleUnsetCapturerPeriodReachedEvent(); 744 745 enum { 746 WRITE_BUFFER_REQUEST = 0, 747 READ_BUFFER_REQUEST, 748 749 RENDERER_MARK_REACHED_REQUEST, 750 SET_RENDERER_MARK_REACHED_REQUEST, 751 UNSET_RENDERER_MARK_REACHED_REQUEST, 752 753 RENDERER_PERIOD_REACHED_REQUEST, 754 SET_RENDERER_PERIOD_REACHED_REQUEST, 755 UNSET_RENDERER_PERIOD_REACHED_REQUEST, 756 757 CAPTURER_PERIOD_REACHED_REQUEST, 758 SET_CAPTURER_PERIOD_REACHED_REQUEST, 759 UNSET_CAPTURER_PERIOD_REACHED_REQUEST, 760 761 CAPTURER_MARK_REACHED_REQUEST, 762 SET_CAPTURER_MARK_REACHED_REQUEST, 763 UNSET_CAPTURER_MARK_REACHED_REQUEST, 764 }; 765 }; 766 } // namespace AudioStandard 767 } // namespace OHOS 768 #endif // AUDIO_SERVICE_CLIENT_H 769