1 /* 2 * Copyright (c) 2023 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 OH_AUDIO_BUFFER_H 17 #define OH_AUDIO_BUFFER_H 18 19 #include <atomic> 20 #include <string> 21 22 #include "message_parcel.h" 23 24 #include "audio_info.h" 25 #include "audio_shared_memory.h" 26 27 namespace OHOS { 28 namespace AudioStandard { 29 30 // client or server. 31 enum AudioBufferHolder : uint32_t { 32 // normal stream, Client buffer created when readFromParcel 33 AUDIO_CLIENT = 0, 34 // normal stream, Server buffer shared with Client 35 AUDIO_SERVER_SHARED, 36 // normal stream, Server buffer shared with hdi 37 AUDIO_SERVER_ONLY, 38 // Independent stream 39 AUDIO_SERVER_INDEPENDENT 40 }; 41 42 enum StreamStatus : uint32_t { 43 STREAM_IDEL = 0, 44 STREAM_STARTING, 45 STREAM_RUNNING, 46 STREAM_PAUSING, 47 STREAM_PAUSED, 48 STREAM_STOPPING, 49 STREAM_STOPPED, 50 STREAM_RELEASED, 51 STREAM_STAND_BY, 52 STREAM_INVALID 53 }; 54 55 /** 56 * totalSizeInFrame = spanCount * spanSizeInFrame 57 * 58 * 0 <= write - base < 2 * totalSize 59 * 0 <= read - base < 1 * totalSize 60 * 0 <= write - read < 1 * totalSize 61 */ 62 struct BasicBufferInfo { 63 uint32_t totalSizeInFrame; 64 uint32_t spanSizeInFrame; 65 uint32_t byteSizePerFrame; 66 67 std::atomic<uint32_t> futexObj; 68 69 std::atomic<StreamStatus> streamStatus; 70 71 // basic read/write postion 72 std::atomic<uint64_t> basePosInFrame; 73 std::atomic<uint64_t> curWriteFrame; 74 std::atomic<uint64_t> curReadFrame; 75 76 std::atomic<uint32_t> underrunCount; 77 78 std::atomic<uint64_t> handlePos; 79 std::atomic<int64_t> handleTime; 80 81 std::atomic<float> streamVolume; 82 std::atomic<float> duckFactor; 83 std::atomic<float> muteFactor; 84 std::atomic<RestoreStatus> restoreStatus = NO_NEED_FOR_RESTORE; 85 86 RestoreInfo restoreInfo; 87 }; 88 89 enum SpanStatus : uint32_t { 90 SPAN_IDEL = 0, 91 SPAN_WRITTING, 92 SPAN_WRITE_DONE, 93 SPAN_READING, 94 SPAN_READ_DONE, 95 SPAN_INVALID 96 }; 97 98 // one span represents a collection of audio sampling data for a short period of time 99 struct SpanInfo { 100 std::atomic<SpanStatus> spanStatus; 101 uint64_t offsetInFrame = 0; 102 103 int64_t writeStartTime; 104 int64_t writeDoneTime; 105 106 int64_t readStartTime; 107 int64_t readDoneTime; 108 109 // volume info for each span 110 bool isMute; 111 int32_t volumeStart; 112 int32_t volumeEnd; 113 }; 114 115 class OHAudioBuffer { 116 public: 117 static const int INVALID_BUFFER_FD = -1; 118 OHAudioBuffer(AudioBufferHolder bufferHolder, uint32_t totalSizeInFrame, uint32_t spanSizeInFrame, 119 uint32_t byteSizePerFrame); 120 ~OHAudioBuffer(); 121 122 // create OHAudioBuffer locally or remotely 123 static std::shared_ptr<OHAudioBuffer> CreateFromLocal(uint32_t totalSizeInFrame, uint32_t spanSizeInFrame, 124 uint32_t byteSizePerFrame); 125 static std::shared_ptr<OHAudioBuffer> CreateFromRemote(uint32_t totalSizeInFrame, uint32_t spanSizeInFrame, 126 uint32_t byteSizePerFrame, AudioBufferHolder holder, int dataFd, int infoFd = INVALID_BUFFER_FD); 127 128 // for ipc. 129 static int32_t WriteToParcel(const std::shared_ptr<OHAudioBuffer> &buffer, MessageParcel &parcel); 130 static std::shared_ptr<OHAudioBuffer> ReadFromParcel(MessageParcel &parcel); 131 132 AudioBufferHolder GetBufferHolder(); 133 134 int32_t GetSizeParameter(uint32_t &totalSizeInFrame, uint32_t &spanSizeInFrame, uint32_t &byteSizePerFrame); 135 136 std::atomic<StreamStatus> *GetStreamStatus(); 137 138 uint32_t GetUnderrunCount(); 139 140 bool SetUnderrunCount(uint32_t count); 141 142 bool GetHandleInfo(uint64_t &frames, int64_t &nanoTime); 143 144 void SetHandleInfo(uint64_t frames, int64_t nanoTime); 145 146 float GetStreamVolume(); 147 bool SetStreamVolume(float streamVolume); 148 149 float GetDuckFactor(); 150 bool SetDuckFactor(float duckFactor); 151 152 float GetMuteFactor(); 153 bool SetMuteFactor(float muteFactor); 154 155 int32_t GetAvailableDataFrames(); 156 157 int32_t ResetCurReadWritePos(uint64_t readFrame, uint64_t writeFrame); 158 159 uint64_t GetCurWriteFrame(); 160 uint64_t GetCurReadFrame(); 161 162 int32_t SetCurWriteFrame(uint64_t writeFrame); 163 int32_t SetCurReadFrame(uint64_t readFrame); 164 165 uint32_t GetSessionId(); 166 int32_t SetSessionId(uint32_t sessionId); 167 168 int32_t GetWriteBuffer(uint64_t writePosInFrame, BufferDesc &bufferDesc); 169 170 int32_t GetReadbuffer(uint64_t readPosInFrame, BufferDesc &bufferDesc); 171 172 int32_t GetBufferByFrame(uint64_t posInFrame, BufferDesc &bufferDesc); 173 174 SpanInfo *GetSpanInfo(uint64_t posInFrame); 175 SpanInfo *GetSpanInfoByIndex(uint32_t spanIndex); 176 177 uint32_t GetSpanCount(); 178 179 int64_t GetLastWrittenTime(); 180 void SetLastWrittenTime(int64_t time); 181 182 std::atomic<uint32_t> *GetFutex(); 183 uint8_t *GetDataBase(); 184 size_t GetDataSize(); 185 RestoreStatus CheckRestoreStatus(); 186 RestoreStatus SetRestoreStatus(RestoreStatus restoreStatus); 187 void GetRestoreInfo(RestoreInfo &restoreInfo); 188 void SetRestoreInfo(RestoreInfo restoreInfo); 189 private: 190 int32_t Init(int dataFd, int infoFd); 191 int32_t SizeCheck(); 192 193 uint32_t sessionId_ = 0; 194 AudioBufferHolder bufferHolder_; 195 uint32_t totalSizeInFrame_; 196 uint32_t spanSizeInFrame_; 197 uint32_t byteSizePerFrame_; 198 199 // available only in single process 200 int64_t lastWrittenTime_ = 0; 201 202 // calculated in advance 203 size_t totalSizeInByte_ = 0; 204 size_t spanSizeInByte_ = 0; 205 uint32_t spanConut_ = 0; 206 207 // for render or capturer 208 AudioMode audioMode_; 209 210 // for StatusInfo buffer 211 std::shared_ptr<AudioSharedMemory> statusInfoMem_ = nullptr; 212 BasicBufferInfo *basicBufferInfo_ = nullptr; 213 SpanInfo *spanInfoList_ = nullptr; 214 215 // for audio data buffer 216 std::shared_ptr<AudioSharedMemory> dataMem_ = nullptr; 217 uint8_t *dataBase_ = nullptr; 218 }; 219 } // namespace AudioStandard 220 } // namespace OHOS 221 #endif // OH_AUDIO_BUFFER_H 222