Skip to content

Commit f7ff640

Browse files
committed
I tried so hard, and got so far
But in the end, it doesn't even matter 😭
1 parent 16711e2 commit f7ff640

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+5718
-1800
lines changed

Diff for: Android.mk

+12
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../opus/include $(LOCAL_PATH)/../boringssl/inc
1111

1212
ifeq ($(TARGET_ARCH_ABI),$(filter $(TARGET_ARCH_ABI),armeabi-v7a arm64-v8a))
1313
CC_NEON := cc.neon
14+
CPP_NEON := cpp.neon
1415
LOCAL_CFLAGS += -DWEBRTC_HAS_NEON
1516
else
1617
CC_NEON := cc
18+
CPP_NEON := cpp.neon
1719
endif
1820

1921
LOCAL_CFLAGS += $(TGVOIP_ADDITIONAL_CFLAGS)
@@ -48,6 +50,10 @@ LOCAL_SRC_FILES := \
4850
./video/VideoRenderer.cpp \
4951
./video/VideoSource.cpp \
5052
./video/ScreamCongestionController.cpp \
53+
./video/VideoPacketSender.cpp \
54+
./video/VideoFEC.cpp \
55+
./video/cm256/cm256.$(CPP_NEON) \
56+
./video/cm256/gf256.$(CPP_NEON) \
5157
./os/android/VideoSourceAndroid.cpp \
5258
./os/android/VideoRendererAndroid.cpp \
5359
./client/android/tg_voip_jni.cpp
@@ -330,6 +336,9 @@ LOCAL_SRC_FILES += \
330336
./webrtc_dsp/common_audio/signal_processing/downsample_fast_neon.c.neon \
331337
./webrtc_dsp/common_audio/signal_processing/min_max_operations_neon.c.neon \
332338
./webrtc_dsp/common_audio/signal_processing/cross_correlation_neon.c.neon
339+
340+
LOCAL_CPPFLAGS += -DUSE_NEON
341+
333342
endif
334343

335344
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
@@ -345,6 +354,9 @@ LOCAL_SRC_FILES += \
345354
./webrtc_dsp/modules/audio_processing/utility/ooura_fft_sse2.cc \
346355
./webrtc_dsp/common_audio/fir_filter_sse.cc \
347356
./webrtc_dsp/common_audio/resampler/sinc_resampler_sse.cc
357+
358+
LOCAL_CPPFLAGS += -DUSE_SSSE3
359+
348360
endif
349361

350362
include $(BUILD_STATIC_LIBRARY)

Diff for: BlockingQueue.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class BlockingQueue{
5959
return GetInternal();
6060
}
6161

62-
unsigned int Size(){
62+
size_t Size(){
6363
return queue.size();
6464
}
6565

Diff for: Buffers.cpp

-54
Original file line numberDiff line numberDiff line change
@@ -234,57 +234,3 @@ void BufferOutputStream::Rewind(size_t numBytes){
234234
throw std::out_of_range("buffer underflow");
235235
offset-=numBytes;
236236
}
237-
238-
#pragma mark - BufferPool
239-
240-
BufferPool::BufferPool(unsigned int size, unsigned int count){
241-
assert(count<=64);
242-
buffers[0]=(unsigned char*) malloc(size*count);
243-
bufferCount=count;
244-
unsigned int i;
245-
for(i=1;i<count;i++){
246-
buffers[i]=buffers[0]+i*size;
247-
}
248-
usedBuffers=0;
249-
this->size=size;
250-
}
251-
252-
BufferPool::~BufferPool(){
253-
free(buffers[0]);
254-
}
255-
256-
unsigned char* BufferPool::Get(){
257-
MutexGuard m(mutex);
258-
int i;
259-
for(i=0;i<bufferCount;i++){
260-
if(!((usedBuffers >> i) & 1)){
261-
usedBuffers|=(1LL << i);
262-
return buffers[i];
263-
}
264-
}
265-
return NULL;
266-
}
267-
268-
void BufferPool::Reuse(unsigned char* buffer){
269-
MutexGuard m(mutex);
270-
int i;
271-
for(i=0;i<bufferCount;i++){
272-
if(buffers[i]==buffer){
273-
usedBuffers&= ~(1LL << i);
274-
return;
275-
}
276-
}
277-
LOGE("pointer passed isn't a valid buffer from this pool");
278-
abort();
279-
}
280-
281-
size_t BufferPool::GetSingleBufferSize(){
282-
return size;
283-
}
284-
285-
size_t BufferPool::GetBufferCount(){
286-
return (size_t) bufferCount;
287-
}
288-
289-
#pragma mark - Buffer
290-

Diff for: Buffers.h

+115-36
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <stdexcept>
1616
#include <array>
1717
#include <limits>
18+
#include <bitset>
1819
#include <stddef.h>
1920
#include "threading.h"
2021
#include "utils.h"
@@ -88,37 +89,24 @@ namespace tgvoip{
8889
bool bufferProvided;
8990
};
9091

91-
class BufferPool{
92-
public:
93-
TGVOIP_DISALLOW_COPY_AND_ASSIGN(BufferPool);
94-
BufferPool(unsigned int size, unsigned int count);
95-
~BufferPool();
96-
unsigned char* Get();
97-
void Reuse(unsigned char* buffer);
98-
size_t GetSingleBufferSize();
99-
size_t GetBufferCount();
100-
101-
private:
102-
uint64_t usedBuffers;
103-
int bufferCount;
104-
size_t size;
105-
unsigned char* buffers[64];
106-
Mutex mutex;
107-
};
108-
10992
class Buffer{
11093
public:
11194
Buffer(size_t capacity){
112-
if(capacity>0)
95+
if(capacity>0){
11396
data=(unsigned char *) malloc(capacity);
114-
else
97+
if(!data)
98+
throw std::bad_alloc();
99+
}else{
115100
data=NULL;
101+
}
116102
length=capacity;
117103
};
118104
TGVOIP_DISALLOW_COPY_AND_ASSIGN(Buffer); // use Buffer::CopyOf to copy contents explicitly
119105
Buffer(Buffer&& other) noexcept {
120106
data=other.data;
121107
length=other.length;
108+
freeFn=other.freeFn;
109+
reallocFn=other.reallocFn;
122110
other.data=NULL;
123111
};
124112
Buffer(BufferOutputStream&& stream){
@@ -131,17 +119,29 @@ namespace tgvoip{
131119
length=0;
132120
}
133121
~Buffer(){
134-
if(data)
135-
free(data);
122+
if(data){
123+
if(freeFn)
124+
freeFn(data);
125+
else
126+
free(data);
127+
}
136128
data=NULL;
129+
length=0;
137130
};
138131
Buffer& operator=(Buffer&& other){
139132
if(this!=&other){
140-
if(data)
141-
free(data);
133+
if(data){
134+
if(freeFn)
135+
freeFn(data);
136+
else
137+
free(data);
138+
}
142139
data=other.data;
143140
length=other.length;
141+
freeFn=other.freeFn;
142+
reallocFn=other.reallocFn;
144143
other.data=NULL;
144+
other.length=0;
145145
}
146146
return *this;
147147
}
@@ -174,23 +174,47 @@ namespace tgvoip{
174174
memcpy(data+dstOffset, ptr, count);
175175
}
176176
void Resize(size_t newSize){
177-
data=(unsigned char *) realloc(data, newSize);
177+
if(reallocFn)
178+
data=(unsigned char *) reallocFn(data, newSize);
179+
else
180+
data=(unsigned char *) realloc(data, newSize);
181+
if(!data)
182+
throw std::bad_alloc();
178183
length=newSize;
179184
}
180185
size_t Length() const{
181186
return length;
182187
}
183188
bool IsEmpty() const{
184-
return length==0;
189+
return length==0 || !data;
185190
}
186191
static Buffer CopyOf(const Buffer& other){
192+
if(other.IsEmpty())
193+
return Buffer();
187194
Buffer buf(other.length);
188195
buf.CopyFrom(other, other.length);
189196
return buf;
190197
}
198+
static Buffer CopyOf(const Buffer& other, size_t offset, size_t length){
199+
if(offset+length>other.Length())
200+
throw std::out_of_range("offset+length out of bounds");
201+
Buffer buf(length);
202+
buf.CopyFrom(other, length, offset);
203+
return buf;
204+
}
205+
static Buffer Wrap(unsigned char* data, size_t size, std::function<void(void*)> freeFn, std::function<void*(void*, size_t)> reallocFn){
206+
Buffer b=Buffer();
207+
b.data=data;
208+
b.length=size;
209+
b.freeFn=freeFn;
210+
b.reallocFn=reallocFn;
211+
return b;
212+
}
191213
private:
192214
unsigned char* data;
193215
size_t length;
216+
std::function<void(void*)> freeFn;
217+
std::function<void*(void*, size_t)> reallocFn;
194218
};
195219

196220
template <typename T, size_t size, typename AVG_T=T> class HistoricBuffer{
@@ -199,26 +223,26 @@ namespace tgvoip{
199223
std::fill(data.begin(), data.end(), (T)0);
200224
}
201225

202-
AVG_T Average(){
226+
AVG_T Average() const {
203227
AVG_T avg=(AVG_T)0;
204-
for(T& i:data){
228+
for(T i:data){
205229
avg+=i;
206230
}
207231
return avg/(AVG_T)size;
208232
}
209233

210-
AVG_T Average(size_t firstN){
234+
AVG_T Average(size_t firstN) const {
211235
AVG_T avg=(AVG_T)0;
212236
for(size_t i=0;i<firstN;i++){
213237
avg+=(*this)[i];
214238
}
215239
return avg/(AVG_T)firstN;
216240
}
217241

218-
AVG_T NonZeroAverage(){
242+
AVG_T NonZeroAverage() const {
219243
AVG_T avg=(AVG_T)0;
220244
int nonZeroCount=0;
221-
for(T& i:data){
245+
for(T i:data){
222246
if(i!=0){
223247
nonZeroCount++;
224248
avg+=i;
@@ -234,18 +258,18 @@ namespace tgvoip{
234258
offset=(offset+1)%size;
235259
}
236260

237-
T Min(){
261+
T Min() const {
238262
T min=std::numeric_limits<T>::max();
239-
for(T& i:data){
263+
for(T i:data){
240264
if(i<min)
241265
min=i;
242266
}
243267
return min;
244268
}
245269

246-
T Max(){
270+
T Max() const {
247271
T max=std::numeric_limits<T>::min();
248-
for(T& i:data){
272+
for(T i:data){
249273
if(i>max)
250274
max=i;
251275
}
@@ -257,6 +281,15 @@ namespace tgvoip{
257281
offset=0;
258282
}
259283

284+
T operator[](size_t i) const {
285+
assert(i<size);
286+
// [0] should return the most recent entry, [1] the one before it, and so on
287+
ptrdiff_t _i=offset-i-1;
288+
if(_i<0)
289+
_i=size+_i;
290+
return data[_i];
291+
}
292+
260293
T& operator[](size_t i){
261294
assert(i<size);
262295
// [0] should return the most recent entry, [1] the one before it, and so on
@@ -266,13 +299,59 @@ namespace tgvoip{
266299
return data[_i];
267300
}
268301

269-
size_t Size(){
302+
size_t Size() const {
270303
return size;
271304
}
272305
private:
273306
std::array<T, size> data;
274307
ptrdiff_t offset=0;
275308
};
309+
310+
template <size_t bufSize, size_t bufCount> class BufferPool{
311+
public:
312+
TGVOIP_DISALLOW_COPY_AND_ASSIGN(BufferPool);
313+
BufferPool(){
314+
bufferStart=(unsigned char*)malloc(bufSize*bufCount);
315+
if(!bufferStart)
316+
throw std::bad_alloc();
317+
};
318+
~BufferPool(){
319+
assert(usedBuffers.none());
320+
free(bufferStart);
321+
};
322+
Buffer Get(){
323+
auto freeFn=[this](void* _buf){
324+
assert(_buf!=NULL);
325+
unsigned char* buf=(unsigned char*)_buf;
326+
size_t offset=buf-bufferStart;
327+
assert(offset%bufSize==0);
328+
size_t index=offset/bufSize;
329+
assert(index<bufCount);
330+
331+
MutexGuard m(mutex);
332+
assert(usedBuffers.test(index));
333+
usedBuffers[index]=0;
334+
};
335+
auto resizeFn=[](void* buf, size_t newSize)->void*{
336+
if(newSize>bufSize)
337+
throw std::invalid_argument("newSize>bufferSize");
338+
return buf;
339+
};
340+
MutexGuard m(mutex);
341+
for(size_t i=0;i<bufCount;i++){
342+
if(!usedBuffers[i]){
343+
usedBuffers[i]=1;
344+
return Buffer::Wrap(bufferStart+(bufSize*i), bufSize, freeFn, resizeFn);
345+
}
346+
}
347+
throw std::bad_alloc();
348+
}
349+
350+
private:
351+
std::bitset<bufCount> usedBuffers;
352+
unsigned char* bufferStart;
353+
Mutex mutex;
354+
};
276355
}
277356

278357
#endif //LIBTGVOIP_BUFFERINPUTSTREAM_H

0 commit comments

Comments
 (0)