-
Notifications
You must be signed in to change notification settings - Fork 24
/
Cromis.Audio.pas
331 lines (288 loc) · 9.6 KB
/
Cromis.Audio.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
(*
* This software is distributed under BSD license.
*
* Copyright (c) 2008-2012 Iztok Kacin, Cromis ([email protected]).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* - Neither the name of the Iztok Kacin nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ==================================================================================
* Collection of various Audio helpers and classes
* ==================================================================================
* 3/6/2012 (1.0.0)
* - Initial implementation.
* ==================================================================================
*)unit Cromis.Audio;
interface
uses
Windows, SysUtils, Classes, Math,
// cromis units
Cromis.Threading, Cromis.Threading.TS, Cromis.AnyValue;
const
cDefaultInitializeTime = 3000;
cDefaultShutdownTime = 3000;
type
// RTP declarations
TRTPHeader = packed record
Control: Byte;
PayloadType: Byte;
SeqNo: WORD;
TimeStamp: DWORD;
SSRC: DWORD;
end;
TRTPHeader2 = packed record
Control: Byte;
PayloadType: Byte;
SeqNo: WORD;
TimeStamp: DWORD;
SSRC: DWORD;
SCRC: DWORD;
end;
TRTP = packed record
H: TRTPHeader;
Payload: array[0..159] of Byte;
end;
TBufferWrapper = class
private
FRTP: TRTP;
FTimestamp: Int64;
public
constructor Create(const RTP: TRTP; const Timestamp: Int64);
property Timestamp: Int64 read FTimestamp;
property RTP: TRTP read FRTP;
end;
// on RTP packet process function
TOnBufferEmptyCallback = procedure(const Sender: TObject; const EmptyCount: Integer) of Object;
TOnRTPPacketProcess = procedure(const Sender: TObject; var RTP: TRTP) of Object;
TAudioJitter = class
private
FFrequency: Int64;
FSampleRate: Integer;
FCurrentJitter: Real;
FPreviousJitter: Real;
FPrevSentTime: Cardinal;
FPrevReceivedTime: Int64;
public
constructor Create(const SampleRate: Integer);
procedure ResetJitter;
procedure AddTimestamp(const Value: Cardinal);
property CurrentJitter: Real read FCurrentJitter;
end;
TJitterBuffer = class(TThread)
private
FEnabled: Boolean;
FFrequency: Int64;
FProcessing: Boolean;
FFirstPacket: Boolean;
FFinishedFlag: THandle;
FEmptyCounter: Integer;
FRTPPacketRate: Integer;
FStartingDelay: Cardinal;
FStartTimestamp: Int64;
FProcessingFlag: THandle;
FInitializedFlag: THandle;
FFirstPacketFlag: THandle;
FThreadSafeQueue: TThreadSafeQueue;
FOnRTPPacketProcess: TOnRTPPacketProcess;
FOnBufferEmptyCallback: TOnBufferEmptyCallback;
protected
procedure Execute; override;
public
constructor Create;
destructor Destroy; override;
function GetTimestamp: Int64;
function StopRTPProcessing: Boolean;
function StartRTPProcessing: Boolean;
procedure AddBufferPacket(const RTP: TRTP; const Timestamp: Int64);
property RTPPacketRate: Integer read FRTPPacketRate write FRTPPacketRate;
property StartingDelay: Cardinal read FStartingDelay write FStartingDelay;
property OnRTPPacketProcess: TOnRTPPacketProcess read FOnRTPPacketProcess write FOnRTPPacketProcess;
property OnBufferEmptyCallback: TOnBufferEmptyCallback read FOnBufferEmptyCallback write FOnBufferEmptyCallback;
property Enabled: Boolean read FEnabled write FEnabled;
end;
implementation
{ TAudioJitter }
procedure TAudioJitter.AddTimestamp(const Value: Cardinal);
var
Diff: Real;
ReceivedTime: Int64;
begin
QueryPerformanceCounter(ReceivedTime);
try
if FPrevSentTime <> 0 then
begin
Diff := (((ReceivedTime - FPrevReceivedTime) / FFrequency) * 1000);
Diff := Diff - (Value - FPrevSentTime) / (FSampleRate / 1000);
FCurrentJitter := FPreviousJitter + (Abs(Diff) - FPreviousJitter);
FCurrentJitter := FCurrentJitter / ((FSampleRate / 1000) * 2);
FPreviousJitter := FCurrentJitter;
end;
finally
// always set previous timestamp
FPrevReceivedTime := ReceivedTime;
FPrevSentTime := Value;
end;
end;
constructor TAudioJitter.Create(const SampleRate: Integer);
begin
QueryPerformanceFrequency(FFrequency);
FSampleRate := SampleRate;
end;
procedure TAudioJitter.ResetJitter;
begin
FCurrentJitter := 0;
end;
{ TJitterBuffer }
procedure TJitterBuffer.AddBufferPacket(const RTP: TRTP; const Timestamp: Int64);
begin
FThreadSafeQueue.Enqueue(TBufferWrapper.Create(RTP, Timestamp));
if FFirstPacket then
begin
QueryPerformanceCounter(FStartTimestamp);
SetEvent(FFirstPacketFlag);
FFirstPacket := False;
end;
end;
constructor TJitterBuffer.Create;
begin
FFirstPacketFlag := CreateEvent(nil, False, False, nil);
FInitializedFlag := CreateEvent(nil, False, False, nil);
FProcessingFlag := CreateEvent(nil, False, False, nil);
FFinishedFlag := CreateEvent(nil, False, False, nil);
FThreadSafeQueue := TThreadSafeQueue.Create;
QueryPerformanceFrequency(FFrequency);
FFirstPacket := True;
FEnabled := True;
inherited Create(False);
end;
destructor TJitterBuffer.Destroy;
begin
FreeAndNil(FThreadSafeQueue);
CloseHandle(FFinishedFlag);
CloseHandle(FProcessingFlag);
CloseHandle(FInitializedFlag);
CloseHandle(FFirstPacketFlag);
inherited;
end;
procedure TJitterBuffer.Execute;
const
cAvgBufferCount = 20;
var
RTP: TRTP;
Timestamp: Int64;
SleepDelta: Integer;
DequeueValue: TAnyValue;
BufferWrapper: TBufferWrapper;
begin
inherited;
// this thread should have a very high priority
SetThreadPriority(Self.Handle, THREAD_PRIORITY_HIGHEST);
while not Terminated do
begin
// thread is ready to go
SetEvent(FInitializedFlag);
// wait for processing flag to be set
WaitForSingleObject(FProcessingFlag, INFINITE);
// fill the buffer when signaled, it is empty
WaitForSingleObject(FFirstPacketFlag, INFINITE);
Sleep(FStartingDelay);
while not Terminated and FProcessing do
begin
if FThreadSafeQueue.Dequeue(DequeueValue) then
begin
BufferWrapper := TBufferWrapper(DequeueValue.AsObject);
try
FEmptyCounter := 1;
RTP := BufferWrapper.RTP;
FOnRTPPacketProcess(Self, RTP);
QueryPerformanceCounter(Timestamp);
if FEnabled then
begin
// sleep for the specified sample interval
SleepDelta := Round((((Timestamp - BufferWrapper.Timestamp) / FFrequency) * 1000) - FStartingDelay);
if SleepDelta > 5 then
SleepDelta := 5
else if SleepDelta < -5 then
SleepDelta := -5;
Sleep(Max(1, FRTPPacketRate - SleepDelta));
end;
finally
BufferWrapper.Free;
end;
end
else
begin
if FEnabled and Assigned(FOnBufferEmptyCallback) then
begin
FOnBufferEmptyCallback(Self, FEmptyCounter);
Inc(FEmptyCounter);
end;
if FEnabled then
Sleep(FStartingDelay)
else
Sleep(1);
end;
end;
end;
// clean the remaining voice RTP packages
while FThreadSafeQueue.Dequeue(DequeueValue) do
begin
BufferWrapper := TBufferWrapper(DequeueValue.AsObject);
BufferWrapper.Free;
end;
// signal that we are done
SetEvent(FFinishedFlag);
end;
function TJitterBuffer.GetTimestamp: Int64;
begin
QueryPerformanceCounter(Result);
end;
function TJitterBuffer.StartRTPProcessing: Boolean;
begin
if WaitForSingleObject(FInitializedFlag, cDefaultInitializeTime) = WAIT_TIMEOUT then
begin
Result := False;
Exit;
end;
FProcessing := True;
// signal thread to continue
SetEvent(FProcessingFlag);
// we have success
Result := True;
end;
function TJitterBuffer.StopRTPProcessing: Boolean;
begin
Terminate;
FProcessing := False;
SetEvent(FProcessingFlag);
SetEvent(FFirstPacketFlag);
Result := WaitForSingleObject(FFinishedFlag, cDefaultShutdownTime) <> WAIT_TIMEOUT;
end;
{ TBufferWrapper }
constructor TBufferWrapper.Create(const RTP: TRTP; const Timestamp: Int64);
begin
FTimestamp := Timestamp;
FRTP := RTP;
end;
end.