-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathSenderReport.java
More file actions
226 lines (191 loc) · 5.88 KB
/
Copy pathSenderReport.java
File metadata and controls
226 lines (191 loc) · 5.88 KB
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
/*
* Copyright (C) 2011-2014 GUIGUI Simon, fyhertz@gmail.com
*
* This file is part of libstreaming (https://github.com/fyhertz/libstreaming)
*
* Spydroid is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this source code; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package net.majorkernelpanic.streaming.rtcp;
import static net.majorkernelpanic.streaming.rtp.RtpSocket.TRANSPORT_TCP;
import static net.majorkernelpanic.streaming.rtp.RtpSocket.TRANSPORT_UDP;
import java.io.IOException;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.nio.channels.IllegalSelectorException;
import android.os.SystemClock;
import android.util.Log;
/**
* Implementation of Sender Report RTCP packets.
*/
public class SenderReport {
public static final int MTU = 1500;
private static final int PACKET_LENGTH = 28;
private MulticastSocket usock;
private DatagramPacket upack;
private int mTransport;
private OutputStream mOutputStream = null;
private byte[] mBuffer = new byte[MTU];
private int mSSRC, mPort = -1;
private int mOctetCount = 0, mPacketCount = 0;
private long interval, delta, now, oldnow;
private byte[] mTcpHeader;
public SenderReport(int ssrc) throws IOException {
super();
this.mSSRC = ssrc;
}
public SenderReport() {
mTransport = TRANSPORT_UDP;
mTcpHeader = new byte[] {'$',0,0,PACKET_LENGTH};
/* Version(2) Padding(0) */
/* ^ ^ PT = 0 */
/* | | ^ */
/* | -------- | */
/* | |--------------------- */
/* | || */
/* | || */
mBuffer[0] = (byte) Integer.parseInt("10000000",2);
/* Packet Type PT */
mBuffer[1] = (byte) 200;
/* Byte 2,3 -> Length */
setLong(PACKET_LENGTH/4-1, 2, 4);
/* Byte 4,5,6,7 -> SSRC */
/* Byte 8,9,10,11 -> NTP timestamp hb */
/* Byte 12,13,14,15 -> NTP timestamp lb */
/* Byte 16,17,18,19 -> RTP timestamp */
/* Byte 20,21,22,23 -> packet count */
/* Byte 24,25,26,27 -> octet count */
try {
usock = new MulticastSocket();
} catch (IOException e) {
// Very unlikely to happen. Means that all UDP ports are already being used
throw new RuntimeException(e.getMessage());
}
upack = new DatagramPacket(mBuffer, 1);
// By default we sent one report every 3 secconde
interval = 3000;
}
public void close() {
usock.close();
}
/**
* Sets the temporal interval between two RTCP Sender Reports.
* Default interval is set to 3 seconds.
* Set 0 to disable RTCP.
* @param interval The interval in milliseconds
*/
public void setInterval(long interval) {
this.interval = interval;
}
/**
* Updates the number of packets sent, and the total amount of data sent.
* @param length The length of the packet
* @param rtpts
* The RTP timestamp.
* @throws IOException
**/
public void update(int length, long rtpts) throws IOException {
mPacketCount += 1;
mOctetCount += length;
setLong(mPacketCount, 20, 24);
setLong(mOctetCount, 24, 28);
now = SystemClock.elapsedRealtime();
delta += oldnow != 0 ? now-oldnow : 0;
oldnow = now;
if (interval>0) {
if (delta>=interval) {
// We send a Sender Report
send(System.nanoTime(), rtpts);
delta = 0;
}
}
}
public void setSSRC(int ssrc) {
this.mSSRC = ssrc;
setLong(ssrc,4,8);
mPacketCount = 0;
mOctetCount = 0;
setLong(mPacketCount, 20, 24);
setLong(mOctetCount, 24, 28);
}
public void setDestination(InetAddress dest, int dport) {
mTransport = TRANSPORT_UDP;
mPort = dport;
upack.setPort(dport);
upack.setAddress(dest);
}
/**
* If a TCP is used as the transport protocol for the RTP session,
* the output stream to which RTP packets will be written to must
* be specified with this method.
*/
public void setOutputStream(OutputStream os, byte channelIdentifier) {
mTransport = TRANSPORT_TCP;
mOutputStream = os;
mTcpHeader[1] = channelIdentifier;
}
public int getPort() {
return mPort;
}
public int getLocalPort() {
return usock.getLocalPort();
}
public int getSSRC() {
return mSSRC;
}
/**
* Resets the reports (total number of bytes sent, number of packets sent, etc.)
*/
public void reset() {
mPacketCount = 0;
mOctetCount = 0;
setLong(mPacketCount, 20, 24);
setLong(mOctetCount, 24, 28);
delta = now = oldnow = 0;
}
private void setLong(long n, int begin, int end) {
for (end--; end >= begin; end--) {
mBuffer[end] = (byte) (n % 256);
n >>= 8;
}
}
/**
* Sends the RTCP packet over the network.
*
* @param ntpts
* the NTP timestamp.
* @param rtpts
* the RTP timestamp.
*/
private void send(long ntpts, long rtpts) throws IOException {
long hb = ntpts/1000000000;
long lb = ( ( ntpts - hb*1000000000 ) * 4294967296L )/1000000000;
setLong(hb, 8, 12);
setLong(lb, 12, 16);
setLong(rtpts, 16, 20);
if (mTransport == TRANSPORT_UDP) {
upack.setLength(PACKET_LENGTH);
usock.send(upack);
} else {
synchronized (mOutputStream) {
try {
mOutputStream.write(mTcpHeader);
mOutputStream.write(mBuffer, 0, PACKET_LENGTH);
} catch (Exception e) {}
}
}
}
}