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