Newer
Older
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jitsi.impl.neomedia;
import java.io.*;
import java.net.*;
import javax.media.protocol.*;
import org.ice4j.socket.*;

Lyubomir Marinov
committed
import org.jitsi.service.libjitsi.*;
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
import org.jitsi.service.packetlogging.*;
import org.jitsi.util.*;
/**
* @author Bing SU (nova.su@gmail.com)
* @author Lyubomir Marinov
*/
public abstract class RTPConnectorInputStream
implements PushSourceStream,
Runnable
{
/**
* The value of the property <tt>controls</tt> of
* <tt>RTPConnectorInputStream</tt> when there are no controls. Explicitly
* defined in order to reduce unnecessary allocations.
*/
private static final Object[] EMPTY_CONTROLS = new Object[0];
/**
* The length in bytes of the buffers of <tt>RTPConnectorInputStream</tt>
* receiving packets from the network.
*/
private static final int PACKET_RECEIVE_BUFFER_LENGTH = 4 * 1024;
/**
* Packet receive buffer
*/
private final byte[] buffer = new byte[PACKET_RECEIVE_BUFFER_LENGTH];
/**
* Whether this stream is closed. Used to control the termination of worker
* thread.
*/
protected boolean closed;
/**
* Caught an IO exception during read from socket
*/
protected boolean ioError = false;
/**
* The packet data to be read out of this instance through its
* {@link #read(byte[], int, int)} method.
*/
protected RawPacket pkt;
/**
* SourceTransferHandler object which is used to read packets.
*/
private SourceTransferHandler transferHandler;
/**
* The Thread receiving packets.
*/
protected Thread receiverThread = null;
/**
* The <tt>DatagramPacketFilter</tt>s which allow dropping
* <tt>DatagramPacket</tt>s before they are converted into
* <tt>RawPacket</tt>s.
*/
private DatagramPacketFilter[] datagramPacketFilters;
/**
* Initializes a new <tt>RTPConnectorInputStream</tt> which is to receive
* packet data from a specific UDP socket.
*/
public RTPConnectorInputStream()
{
// PacketLoggingService
addDatagramPacketFilter(
new DatagramPacketFilter()
{
/**
* Used for debugging. As we don't log every packet, we must
* count them and decide which to log.
*/
private long numberOfPackets = 0;
public boolean accept(DatagramPacket p)
{
numberOfPackets++;
if (RTPConnectorOutputStream.logPacket(numberOfPackets))
{
PacketLoggingService packetLogging

Lyubomir Marinov
committed
= LibJitsi.getPacketLoggingService();
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
if ((packetLogging != null)
&& packetLogging.isLoggingEnabled(
PacketLoggingService.ProtocolName
.RTP))
doLogPacket(p);
}
return true;
}
});
}
/**
* Close this stream, stops the worker thread.
*/
public synchronized void close()
{
}
/**
* Creates a new <tt>RawPacket</tt> from a specific <tt>DatagramPacket</tt>
* in order to have this instance receive its packet data through its
* {@link #read(byte[], int, int)} method. Allows extenders to intercept the
* packet data and possibly filter and/or modify it.
*
* @param datagramPacket the <tt>DatagramPacket</tt> containing the packet
* data
* @return a new <tt>RawPacket</tt> containing the packet data of the
* specified <tt>DatagramPacket</tt> or possibly its modification;
* <tt>null</tt> to ignore the packet data of the specified
* <tt>DatagramPacket</tt> and not make it available to this instance
* through its {@link #read(byte[], int, int)} method
*/
protected RawPacket createRawPacket(DatagramPacket datagramPacket)
{
if (pkt == null)
{
return
new RawPacket(
datagramPacket.getData(),
datagramPacket.getOffset(),
datagramPacket.getLength());
}
pkt.setBuffer(datagramPacket.getData());
pkt.setLength(datagramPacket.getLength());
pkt.setOffset(datagramPacket.getOffset());
return pkt;
}
/**
* Provides a dummy implementation to {@link
* RTPConnectorInputStream#endOfStream()} that always returns
* <tt>false</tt>.
*
* @return <tt>false</tt>, no matter what.
*/
public boolean endOfStream()
{
return false;
}
/**
* Provides a dummy implementation to {@link
* RTPConnectorInputStream#getContentDescriptor()} that always returns
* <tt>null</tt>.
*
* @return <tt>null</tt>, no matter what.
*/
public ContentDescriptor getContentDescriptor()
{
return null;
}
/**
* Provides a dummy implementation to {@link
* RTPConnectorInputStream#getContentLength()} that always returns
* <tt>LENGTH_UNKNOWN</tt>.
*
* @return <tt>LENGTH_UNKNOWN</tt>, no matter what.
*/
public long getContentLength()
{
return pkt.getLength();
}
/**
* Provides a dummy implementation to {@link
* RTPConnectorInputStream#getControl(String)} that always returns
* <tt>null</tt>.
*
* @param controlType ignored.
*
* @return <tt>null</tt>, no matter what.
*/
public Object getControl(String controlType)
{
return null;
}
/**
* Provides a dummy implementation to {@link
* RTPConnectorInputStream#getControls()} that always returns
* <tt>EMPTY_CONTROLS</tt>.
*
* @return <tt>EMPTY_CONTROLS</tt>, no matter what.
*/
public Object[] getControls()
{
return EMPTY_CONTROLS;
}
/**
* Provides a dummy implementation to {@link
* RTPConnectorInputStream#getMinimumTransferSize()} that always returns
* <tt>2 * 1024</tt>.
*
* @return <tt>2 * 1024</tt>, no matter what.
*/
public int getMinimumTransferSize()
{
return 2 * 1024; // twice the MTU size, just to be safe.
}
/**
* Copies the content of the most recently received packet into
* <tt>buffer</tt>.
*
* @param buffer the <tt>byte[]</tt> that we'd like to copy the content of
* the packet to.
* @param offset the position where we are supposed to start writing in
* <tt>buffer</tt>.
* @param length the number of <tt>byte</tt>s available for writing in
* <tt>buffer</tt>.
*
* @return the number of bytes read
*
* @throws IOException if <tt>length</tt> is less than the size of the
* packet.
*/
public int read(byte[] buffer, int offset, int length)
throws IOException
{
if (ioError)
return -1;
int pktLength = pkt.getLength();
if (length < pktLength)

Lyubomir Marinov
committed
{
throw new IOException(
"Input buffer not big enough for " + pktLength);
}

Lyubomir Marinov
committed
pkt.getBuffer(), pkt.getOffset(),
buffer, offset,
pktLength);
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
return pktLength;
}
/**
* Log the packet.
*
* @param packet packet to log
*/
protected abstract void doLogPacket(DatagramPacket packet);
/**
* Receive packet.
*
* @param p packet for receiving
* @throws IOException if something goes wrong during receiving
*/
protected abstract void receivePacket(DatagramPacket p)
throws IOException;
/**
* Listens for incoming datagrams, stores them for reading by the
* <tt>read</tt> method and notifies the local <tt>transferHandler</tt>
* that there's data to be read.
*/
public void run()
{
DatagramPacket p
= new DatagramPacket(buffer, 0, PACKET_RECEIVE_BUFFER_LENGTH);
while (!closed)
{
try
{
if (OSUtils.IS_ANDROID)
{
// http://code.google.com/p/android/issues/detail?id=24765
p.setLength(PACKET_RECEIVE_BUFFER_LENGTH);
}
receivePacket(p);
}
catch (IOException e)
{
ioError = true;
break;
}
/*
* Do the DatagramPacketFilters accept the received DatagramPacket?
*/
DatagramPacketFilter[] datagramPacketFilters
= getDatagramPacketFilters();
boolean accept;
if (datagramPacketFilters == null)
accept = true;
else
{
accept = true;
for (int i = 0; i < datagramPacketFilters.length; i++)
{
try
{
if (!datagramPacketFilters[i].accept(p))
{
accept = false;
break;
}
}
catch (Throwable t)
{
if (t instanceof ThreadDeath)
throw (ThreadDeath) t;
}
}
}
if (accept)
{
pkt = createRawPacket(p);
/*
* If we got extended, the delivery of the packet may have been
* canceled.
*/
if ((pkt != null) && (transferHandler != null) && !closed)
transferHandler.transferData(this);
}
}
}
/**
* Sets the <tt>transferHandler</tt> that this connector should be notifying
* when new data is available for reading.
*
* @param transferHandler the <tt>transferHandler</tt> that this connector
* should be notifying when new data is available for reading.
*/
public void setTransferHandler(SourceTransferHandler transferHandler)
{
if (!closed)
this.transferHandler = transferHandler;
}
/**
* Changes current thread priority.
* @param priority the new priority.
*/
public void setPriority(int priority)
{
// currently no priority is set
// if (receiverThread != null)
// receiverThread.setPriority(priority);
}
/**
* Gets the <tt>DatagramPacketFilter</tt>s which allow dropping
* <tt>DatagramPacket</tt>s before they are converted into
* <tt>RawPacket</tt>s.
*
* @return the <tt>DatagramPacketFilter</tt>s which allow dropping
* <tt>DatagramPacket</tt>s before they are converted into
* <tt>RawPacket</tt>s.
*/
public synchronized DatagramPacketFilter[] getDatagramPacketFilters()
{
return datagramPacketFilters;
}
/**
* Adds a <tt>DatagramPacketFilter</tt> which allows dropping
* <tt>DatagramPacket</tt>s before they are converted into
* <tt>RawPacket</tt>s.
*
* @param datagramPacketFilter the <tt>DatagramPacketFilter</tt> which
* allows dropping <tt>DatagramPacket</tt>s before they are converted into
* <tt>RawPacket</tt>s
*/
public synchronized void addDatagramPacketFilter(
DatagramPacketFilter datagramPacketFilter)
{
if (datagramPacketFilter == null)
throw new NullPointerException("datagramPacketFilter");
if (datagramPacketFilters == null)
{
datagramPacketFilters
= new DatagramPacketFilter[] { datagramPacketFilter };
}
else
{
final int length = datagramPacketFilters.length;
for (int i = 0; i < length; i++)
if (datagramPacketFilter.equals(datagramPacketFilters[i]))
return;
DatagramPacketFilter[] newDatagramPacketFilters
= new DatagramPacketFilter[length + 1];
System.arraycopy(
datagramPacketFilters, 0,
newDatagramPacketFilters, 0,
length);
newDatagramPacketFilters[length] = datagramPacketFilter;
datagramPacketFilters = newDatagramPacketFilters;
}
}
}