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