• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INVALID
52 };
53 
54 /**
55  * totalSizeInFrame = spanCount * spanSizeInFrame
56  *
57  * 0 <= write - base < 2 * totalSize
58  * 0 <= read - base < 1 * totalSize
59  * 0 <= write - read < 1 * totalSize
60  */
61 struct BasicBufferInfo {
62     uint32_t totalSizeInFrame;
63     uint32_t spanSizeInFrame;
64     uint32_t byteSizePerFrame;
65 
66     std::atomic<StreamStatus> streamStatus;
67 
68     // basic read/write postion
69     std::atomic<uint64_t> basePosInFrame;
70     std::atomic<uint64_t> curWriteFrame;
71     std::atomic<uint64_t> curReadFrame;
72 
73     std::atomic<uint64_t> handlePos;
74     std::atomic<int64_t> handleTime;
75 };
76 
77 enum SpanStatus : uint32_t {
78     SPAN_IDEL = 0,
79     SPAN_WRITTING,
80     SPAN_WRITE_DONE,
81     SPAN_READING,
82     SPAN_READ_DONE,
83     SPAN_INVALID
84 };
85 
86 // one span represents a collection of audio sampling data for a short period of time
87 struct SpanInfo {
88     std::atomic<SpanStatus> spanStatus;
89     uint64_t offsetInFrame = 0;
90 
91     int64_t writeStartTime;
92     int64_t writeDoneTime;
93 
94     int64_t readStartTime;
95     int64_t readDoneTime;
96 
97     // volume info for each span
98     bool isMute;
99     int32_t volumeStart;
100     int32_t volumeEnd;
101 };
102 
103 class OHAudioBuffer {
104 public:
105     static const int INVALID_BUFFER_FD = -1;
106     OHAudioBuffer(AudioBufferHolder bufferHolder, uint32_t totalSizeInFrame, uint32_t spanSizeInFrame,
107         uint32_t byteSizePerFrame);
108     ~OHAudioBuffer();
109 
110     // create OHAudioBuffer locally or remotely
111     static std::shared_ptr<OHAudioBuffer> CreateFromLocal(uint32_t totalSizeInFrame, uint32_t spanSizeInFrame,
112         uint32_t byteSizePerFrame);
113     static std::shared_ptr<OHAudioBuffer> CreateFromRemote(uint32_t totalSizeInFrame, uint32_t spanSizeInFrame,
114         uint32_t byteSizePerFrame, AudioBufferHolder holder, int dataFd, int infoFd = INVALID_BUFFER_FD);
115 
116     // for ipc.
117     static int32_t WriteToParcel(const std::shared_ptr<OHAudioBuffer> &buffer, MessageParcel &parcel);
118     static std::shared_ptr<OHAudioBuffer> ReadFromParcel(MessageParcel &parcel);
119 
120     AudioBufferHolder GetBufferHolder();
121 
122     int32_t GetSizeParameter(uint32_t &totalSizeInFrame, uint32_t &spanSizeInFrame, uint32_t &byteSizePerFrame);
123 
124     std::atomic<StreamStatus> *GetStreamStatus();
125 
126     bool GetHandleInfo(uint64_t &frames, int64_t &nanoTime);
127 
128     void SetHandleInfo(uint64_t frames, int64_t nanoTime);
129 
130     int32_t GetAvailableDataFrames();
131 
132     int32_t ResetCurReadWritePos(uint64_t readFrame, uint64_t writeFrame);
133 
134     uint64_t GetCurWriteFrame();
135     uint64_t GetCurReadFrame();
136 
137     int32_t SetCurWriteFrame(uint64_t writeFrame);
138     int32_t SetCurReadFrame(uint64_t readFrame);
139 
140     int32_t GetWriteBuffer(uint64_t writePosInFrame, BufferDesc &bufferDesc);
141 
142     int32_t GetReadbuffer(uint64_t readPosInFrame, BufferDesc &bufferDesc);
143 
144     int32_t GetBufferByFrame(uint64_t posInFrame, BufferDesc &bufferDesc);
145 
146     SpanInfo *GetSpanInfo(uint64_t posInFrame);
147     SpanInfo *GetSpanInfoByIndex(uint32_t spanIndex);
148 
149     uint32_t GetSpanCount();
150 
151     uint8_t *GetDataBase();
152     size_t GetDataSize();
153 private:
154     int32_t Init(int dataFd, int infoFd);
155     int32_t SizeCheck();
156 
157     AudioBufferHolder bufferHolder_;
158     uint32_t totalSizeInFrame_;
159     uint32_t spanSizeInFrame_;
160     uint32_t byteSizePerFrame_;
161 
162     // calculated in advance
163     size_t totalSizeInByte_ = 0;
164     size_t spanSizeInByte_ = 0;
165     uint32_t spanConut_ = 0;
166 
167     // for render or capturer
168     AudioMode audioMode_;
169 
170     // for StatusInfo buffer
171     std::shared_ptr<AudioSharedMemory> statusInfoMem_ = nullptr;
172     BasicBufferInfo *basicBufferInfo_ = nullptr;
173     SpanInfo *spanInfoList_ = nullptr;
174 
175     // for audio data buffer
176     std::shared_ptr<AudioSharedMemory> dataMem_ = nullptr;
177     uint8_t *dataBase_ = nullptr;
178 };
179 } // namespace AudioStandard
180 } // namespace OHOS
181 #endif // OH_AUDIO_BUFFER_H
182