53
|
1 |
package com.beem.project.beem.jingle;
|
|
2 |
|
|
3 |
import java.io.IOException;
|
|
4 |
import java.net.DatagramSocket;
|
|
5 |
import java.net.ServerSocket;
|
356
|
6 |
import java.net.SocketException;
|
53
|
7 |
|
353
|
8 |
import org.jlibrtp.jlibrtp.DataFrame;
|
|
9 |
import org.jlibrtp.jlibrtp.Participant;
|
|
10 |
import org.jlibrtp.jlibrtp.RTPAppIntf;
|
|
11 |
import org.jlibrtp.jlibrtp.RTPSession;
|
53
|
12 |
|
212
|
13 |
public class RTPReceiver implements Runnable, RTPAppIntf {
|
53
|
14 |
|
356
|
15 |
private RTPSession mRtpSession;
|
|
16 |
private boolean mKillme;
|
212
|
17 |
|
|
18 |
public RTPReceiver(int rtpPort) {
|
|
19 |
DatagramSocket rtpSocket = null;
|
53
|
20 |
|
212
|
21 |
try {
|
|
22 |
rtpSocket = new DatagramSocket(rtpPort);
|
356
|
23 |
} catch (SocketException e) {
|
|
24 |
e.printStackTrace();
|
212
|
25 |
return;
|
|
26 |
}
|
353
|
27 |
mRtpSession = new RTPSession(rtpSocket, null);
|
|
28 |
mRtpSession.naivePktReception(true);
|
|
29 |
mRtpSession.RTPSessionRegister(this, null, null);
|
212
|
30 |
}
|
53
|
31 |
|
212
|
32 |
@Override
|
|
33 |
public int frameSize(int payloadType) {
|
|
34 |
return 1;
|
|
35 |
}
|
53
|
36 |
|
212
|
37 |
protected int getFreePort() {
|
|
38 |
ServerSocket ss;
|
|
39 |
int freePort = 0;
|
53
|
40 |
|
212
|
41 |
for (int i = 0; i < 10; i++) {
|
|
42 |
freePort = (int) (10000 + Math.round(Math.random() * 10000));
|
|
43 |
try {
|
|
44 |
ss = new ServerSocket(freePort);
|
|
45 |
freePort = ss.getLocalPort();
|
|
46 |
ss.close();
|
|
47 |
return freePort;
|
|
48 |
} catch (IOException e) {
|
|
49 |
e.printStackTrace();
|
|
50 |
}
|
53
|
51 |
}
|
212
|
52 |
try {
|
|
53 |
ss = new ServerSocket(0);
|
|
54 |
freePort = ss.getLocalPort();
|
|
55 |
ss.close();
|
|
56 |
} catch (IOException e) {
|
|
57 |
e.printStackTrace();
|
|
58 |
}
|
|
59 |
return freePort;
|
|
60 |
}
|
53
|
61 |
|
212
|
62 |
@Override
|
|
63 |
public void receiveData(DataFrame frame, Participant participant) {
|
|
64 |
// byte[] data = frame.getConcatenatedData();
|
|
65 |
}
|
53
|
66 |
|
212
|
67 |
@Override
|
|
68 |
public void run() {
|
|
69 |
start();
|
|
70 |
}
|
53
|
71 |
|
212
|
72 |
private void start() {
|
353
|
73 |
while (!mKillme) {
|
212
|
74 |
try {
|
|
75 |
Thread.sleep(1000);
|
356
|
76 |
} catch (InterruptedException e) {
|
|
77 |
// TODO Auto-generated catch block
|
|
78 |
e.printStackTrace();
|
212
|
79 |
}
|
53
|
80 |
}
|
|
81 |
|
212
|
82 |
try {
|
|
83 |
Thread.sleep(200);
|
356
|
84 |
} catch (InterruptedException e) {
|
|
85 |
// TODO Auto-generated catch block
|
|
86 |
e.printStackTrace();
|
53
|
87 |
}
|
353
|
88 |
this.mRtpSession.endSession();
|
53
|
89 |
|
212
|
90 |
}
|
53
|
91 |
|
212
|
92 |
public void stop() {
|
353
|
93 |
this.mKillme = true;
|
212
|
94 |
}
|
53
|
95 |
|
212
|
96 |
@Override
|
353
|
97 |
public void userEvent(int arg0, Participant[] arg1) {
|
|
98 |
// TODO Auto-generated method stub
|
355
|
99 |
|
212
|
100 |
}
|
53
|
101 |
}
|