53
|
1 |
package com.beem.project.beem.jingle;
|
|
2 |
|
|
3 |
import java.io.IOException;
|
|
4 |
import java.net.DatagramSocket;
|
|
5 |
import java.net.ServerSocket;
|
|
6 |
|
|
7 |
import org.jivesoftware.smackx.jingle.SmackLogger;
|
|
8 |
|
|
9 |
import jlibrtp.DataFrame;
|
|
10 |
import jlibrtp.Participant;
|
|
11 |
import jlibrtp.RTPAppIntf;
|
|
12 |
import jlibrtp.RTPSession;
|
|
13 |
|
|
14 |
public class RTPTransmitter implements Runnable, RTPAppIntf {
|
|
15 |
|
|
16 |
private static final SmackLogger LOGGER = SmackLogger
|
|
17 |
.getLogger(SenderMediaManager.class);
|
|
18 |
private RTPSession rtpSession;
|
|
19 |
private boolean killme = false;
|
|
20 |
|
|
21 |
public RTPTransmitter(String remoteIP, int port) {
|
|
22 |
|
|
23 |
DatagramSocket rtpSocket = null;
|
|
24 |
int rtpPort = 0;
|
|
25 |
|
|
26 |
try {
|
|
27 |
rtpPort = getFreePort();
|
|
28 |
rtpSocket = new DatagramSocket(rtpPort);
|
|
29 |
} catch (Exception e) {
|
|
30 |
System.out.println("RTPSession failed to obtain port");
|
|
31 |
return;
|
|
32 |
}
|
|
33 |
rtpSession = new RTPSession(rtpSocket, null);
|
|
34 |
rtpSession.naivePktReception(true);
|
|
35 |
rtpSession.RTPSessionRegister(this, null, null);
|
|
36 |
rtpSession.addParticipant(new Participant(remoteIP,rtpPort, 0));
|
|
37 |
}
|
|
38 |
|
|
39 |
private void start() {
|
|
40 |
LOGGER.info("Debut envoi de donnees par RTPTransmitter");
|
|
41 |
while (!killme) {
|
|
42 |
rtpSession.sendData(null);
|
|
43 |
}
|
|
44 |
|
|
45 |
try {Thread.sleep(200);} catch (Exception e) {}
|
|
46 |
this.rtpSession.endSession();
|
|
47 |
}
|
|
48 |
|
|
49 |
@Override
|
|
50 |
public void run() {
|
|
51 |
start();
|
|
52 |
}
|
|
53 |
|
|
54 |
@Override
|
|
55 |
public int frameSize(int payloadType) {
|
|
56 |
return 1;
|
|
57 |
}
|
|
58 |
|
|
59 |
@Override
|
|
60 |
public void receiveData(DataFrame frame, Participant participant) {
|
|
61 |
//On envoie uniquement
|
|
62 |
}
|
|
63 |
|
|
64 |
@Override
|
|
65 |
public void userEvent(int type, Participant[] participant) {
|
|
66 |
//je sais pas ce que c'est
|
|
67 |
|
|
68 |
}
|
|
69 |
|
|
70 |
protected int getFreePort() {
|
|
71 |
ServerSocket ss;
|
|
72 |
int freePort = 0;
|
|
73 |
|
|
74 |
for (int i = 0; i < 10; i++) {
|
|
75 |
freePort = (int) (10000 + Math.round(Math.random() * 10000));
|
|
76 |
freePort = freePort % 2 == 0 ? freePort : freePort + 1;
|
|
77 |
try {
|
|
78 |
ss = new ServerSocket(freePort);
|
|
79 |
freePort = ss.getLocalPort();
|
|
80 |
ss.close();
|
|
81 |
return freePort;
|
|
82 |
} catch (IOException e) {
|
|
83 |
e.printStackTrace();
|
|
84 |
}
|
|
85 |
}
|
|
86 |
try {
|
|
87 |
ss = new ServerSocket(0);
|
|
88 |
freePort = ss.getLocalPort();
|
|
89 |
ss.close();
|
|
90 |
} catch (IOException e) {
|
|
91 |
e.printStackTrace();
|
|
92 |
}
|
|
93 |
return freePort;
|
|
94 |
}
|
|
95 |
|
|
96 |
public void stop() {
|
|
97 |
this.killme = true;
|
|
98 |
}
|
|
99 |
|
|
100 |
}
|