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 #ifndef AUDIO_STREAM_H 16 #define AUDIO_STREAM_H 17 18 #include <mutex> 19 #include <condition_variable> 20 #include "timestamp.h" 21 #include "event_handler.h" 22 #include "event_runner.h" 23 #include "audio_info.h" 24 #include "audio_service_client.h" 25 #include "audio_stream_tracker.h" 26 27 namespace OHOS { 28 namespace AudioStandard { 29 static constexpr int32_t MAX_WRITECB_NUM_BUFFERS = 1; 30 static constexpr int32_t MAX_READCB_NUM_BUFFERS = 3; 31 32 class AudioStream : public AudioServiceClient { 33 public: 34 AudioStream(AudioStreamType eStreamType, AudioMode eMode, int32_t appUid); 35 virtual ~AudioStream(); 36 37 void SetRendererInfo(const AudioRendererInfo &rendererInfo); 38 void SetCapturerInfo(const AudioCapturerInfo &capturerInfo); 39 int32_t SetAudioStreamInfo(const AudioStreamParams info, 40 const std::shared_ptr<AudioClientTracker> &proxyObj); 41 int32_t GetAudioStreamInfo(AudioStreamParams &info); 42 bool VerifyClientPermission(const std::string &permissionName, uint32_t appTokenId, int32_t appUid, 43 bool privacyFlag, AudioPermissionState state); 44 bool getUsingPemissionFromPrivacy(const std::string &permissionName, uint32_t appTokenId, 45 AudioPermissionState state); 46 int32_t GetAudioSessionID(uint32_t &sessionID); 47 State GetState(); 48 bool GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbase base); 49 int32_t GetBufferSize(size_t &bufferSize) const; 50 int32_t GetFrameCount(uint32_t &frameCount) const; 51 int32_t GetLatency(uint64_t &latency); 52 static AudioStreamType GetStreamType(ContentType contentType, StreamUsage streamUsage); 53 int32_t SetAudioStreamType(AudioStreamType audioStreamType); 54 int32_t SetVolume(float volume); 55 float GetVolume(); 56 int32_t SetRenderRate(AudioRendererRate renderRate); 57 AudioRendererRate GetRenderRate(); 58 int32_t SetStreamCallback(const std::shared_ptr<AudioStreamCallback> &callback); 59 60 // callback mode api 61 int32_t SetRenderMode(AudioRenderMode renderMode); 62 AudioRenderMode GetRenderMode(); 63 int32_t SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> &callback); 64 int32_t SetCaptureMode(AudioCaptureMode captureMode); 65 AudioCaptureMode GetCaptureMode(); 66 int32_t SetCapturerReadCallback(const std::shared_ptr<AudioCapturerReadCallback> &callback); 67 int32_t GetBufferDesc(BufferDesc &bufDesc); 68 int32_t GetBufQueueState(BufferQueueState &bufState); 69 int32_t Enqueue(const BufferDesc &bufDesc); 70 int32_t Clear(); 71 void SubmitAllFreeBuffers(); 72 73 int32_t SetLowPowerVolume(float volume); 74 float GetLowPowerVolume(); 75 float GetSingleStreamVolume(); 76 77 std::vector<AudioSampleFormat> GetSupportedFormats() const; 78 std::vector<AudioEncodingType> GetSupportedEncodingTypes() const; 79 std::vector<AudioSamplingRate> GetSupportedSamplingRates() const; 80 81 // Common APIs 82 bool StartAudioStream(StateChangeCmdType cmdType = CMD_FROM_CLIENT); 83 bool PauseAudioStream(StateChangeCmdType cmdType = CMD_FROM_CLIENT); 84 bool StopAudioStream(); 85 bool ReleaseAudioStream(bool releaseRunner = true); 86 bool FlushAudioStream(); 87 88 // Playback related APIs 89 bool DrainAudioStream(); 90 size_t Write(uint8_t *buffer, size_t buffer_size); 91 92 // Recording related APIs 93 int32_t Read(uint8_t &buffer, size_t userSize, bool isBlockingRead); 94 95 private: 96 AudioStreamType eStreamType_; 97 AudioMode eMode_; 98 State state_; 99 std::atomic<bool> isReadInProgress_; 100 std::atomic<bool> isWriteInProgress_; 101 bool resetTime_; 102 uint64_t resetTimestamp_; 103 struct timespec baseTimestamp_ = {0}; 104 AudioRenderMode renderMode_; 105 AudioCaptureMode captureMode_; 106 std::queue<BufferDesc> freeBufferQ_; 107 std::queue<BufferDesc> filledBufferQ_; 108 std::array<std::unique_ptr<uint8_t[]>, MAX_WRITECB_NUM_BUFFERS> writeBufferPool_ = {}; 109 std::array<std::unique_ptr<uint8_t[]>, MAX_READCB_NUM_BUFFERS> readBufferPool_ = {}; 110 std::unique_ptr<std::thread> writeThread_ = nullptr; 111 std::unique_ptr<std::thread> readThread_ = nullptr; 112 bool isReadyToWrite_; 113 bool isReadyToRead_; 114 void WriteCbTheadLoop(); 115 void ReadCbThreadLoop(); 116 std::unique_ptr<AudioStreamTracker> audioStreamTracker_; 117 AudioRendererInfo rendererInfo_; 118 AudioCapturerInfo capturerInfo_; 119 uint32_t sessionId_; 120 121 static const std::map<std::pair<ContentType, StreamUsage>, AudioStreamType> streamTypeMap_; 122 static std::map<std::pair<ContentType, StreamUsage>, AudioStreamType> CreateStreamMap(); 123 bool isFirstRead_; 124 bool isFirstWrite_; 125 126 std::mutex bufferQueueLock_; 127 std::condition_variable bufferQueueCV_; 128 }; 129 } // namespace AudioStandard 130 } // namespace OHOS 131 #endif // AUDIO_STREAM_H 132