353
|
1 |
/** |
355
|
2 |
* |
353
|
3 |
*/ |
|
4 |
package com.beem.project.beem.jingle; |
|
5 |
|
|
6 |
import java.util.ArrayList; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
import org.jivesoftware.smack.XMPPConnection; |
|
10 |
import org.jivesoftware.smack.XMPPException; |
|
11 |
import org.jivesoftware.smackx.jingle.JingleManager; |
|
12 |
import org.jivesoftware.smackx.jingle.JingleSession; |
|
13 |
import org.jivesoftware.smackx.jingle.JingleSessionRequest; |
|
14 |
import org.jivesoftware.smackx.jingle.listeners.JingleSessionListener; |
|
15 |
import org.jivesoftware.smackx.jingle.listeners.JingleSessionRequestListener; |
|
16 |
import org.jivesoftware.smackx.jingle.media.JingleMediaManager; |
|
17 |
import org.jivesoftware.smackx.jingle.media.PayloadType; |
|
18 |
import org.jivesoftware.smackx.jingle.nat.BasicTransportManager; |
|
19 |
import org.jivesoftware.smackx.jingle.nat.TransportCandidate; |
|
20 |
|
|
21 |
/** |
355
|
22 |
* Beem Jingle Service, manage jingle call. |
353
|
23 |
* @author nikita |
|
24 |
*/ |
|
25 |
public class JingleService { |
355
|
26 |
private JingleManager mJingleManager; |
353
|
27 |
private List<JingleMediaManager> mMediaManagers; |
355
|
28 |
private JingleSession mIn; |
|
29 |
private JingleSession mOut; |
353
|
30 |
|
355
|
31 |
/** |
|
32 |
* JingleService constructor. |
|
33 |
* @param xmppConnection a valid XMPPConnection |
|
34 |
*/ |
|
35 |
public JingleService(final XMPPConnection xmppConnection) { |
353
|
36 |
BasicTransportManager bt = new BasicTransportManager(); |
|
37 |
|
|
38 |
mMediaManagers = new ArrayList<JingleMediaManager>(); |
356
|
39 |
mMediaManagers.add(new MicrophoneRTPManager(bt)); |
353
|
40 |
} |
|
41 |
|
355
|
42 |
/** |
|
43 |
* finish to construct the instance. |
|
44 |
* @param conn the xmppConnection used with constructor |
|
45 |
*/ |
353
|
46 |
public void initWhenConntected(XMPPConnection conn) { |
|
47 |
mJingleManager = new JingleManager(conn, mMediaManagers); |
|
48 |
mJingleManager.addJingleSessionRequestListener(new BeemJingleSessionRequestListener()); |
|
49 |
} |
|
50 |
|
355
|
51 |
/** |
|
52 |
* begin a jingle call. |
|
53 |
* @param receiver the call receiver |
|
54 |
*/ |
|
55 |
public void call(final String receiver) { |
353
|
56 |
try { |
355
|
57 |
mOut = mJingleManager.createOutgoingJingleSession(receiver); |
353
|
58 |
// TODO configure out avec addMediaSession et addNegociator |
|
59 |
mOut.addListener(new BeemJingleCallerSessionListener()); |
|
60 |
mOut.startOutgoing(); |
|
61 |
} catch (XMPPException e) { |
|
62 |
// TODO Auto-generated catch block |
|
63 |
e.printStackTrace(); |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
/* |
355
|
68 |
* private void receiveData(String ip, int port) throws IOException { Socket s = null; try { s = new Socket(ip, |
|
69 |
* port); System.out.println("Waiting data"); InputStream in = s.getInputStream(); int a; while ((a = in.read()) != |
|
70 |
* -1) { System.out.println("Received " + a); } System.out.println("End receiving data"); } finally { if (s != null) |
|
71 |
* s.close(); } } |
|
72 |
*/ |
|
73 |
/** |
|
74 |
* Listen on receiver session events. |
|
75 |
* @author nikita |
|
76 |
*/ |
|
77 |
private class BeemJingleReceiverSessionListener implements JingleSessionListener { |
|
78 |
|
|
79 |
/** |
|
80 |
* constructor. |
|
81 |
*/ |
|
82 |
public BeemJingleReceiverSessionListener() { |
|
83 |
super(); |
|
84 |
// TODO Auto-generated constructor stub |
353
|
85 |
} |
355
|
86 |
|
|
87 |
@Override |
|
88 |
public void sessionClosed(String reason, JingleSession jingleSession) { |
356
|
89 |
//System.out.println("Session " + jingleSession.getResponder() + "closedd because " + reason); |
355
|
90 |
} |
353
|
91 |
|
355
|
92 |
@Override |
|
93 |
public void sessionClosedOnError(XMPPException e, JingleSession jingleSession) { |
|
94 |
// TODO Auto-generated method stub |
356
|
95 |
// System.out.println("Session " + jingleSession.getResponder() + " closed"); |
353
|
96 |
|
355
|
97 |
} |
|
98 |
|
|
99 |
@Override |
|
100 |
public void sessionDeclined(String reason, JingleSession jingleSession) { |
|
101 |
// TODO Auto-generated method stub |
356
|
102 |
//System.out.println("Session " + jingleSession.getResponder() + "declined because " + reason); |
355
|
103 |
} |
353
|
104 |
|
355
|
105 |
@Override |
|
106 |
public void sessionEstablished(PayloadType pt, TransportCandidate remoteCandidate, |
|
107 |
TransportCandidate localCandidate, JingleSession jingleSession) { |
356
|
108 |
//System.out.println("Session established"); |
|
109 |
//System.out.println("Je recois sur " + remoteCandidate.getIp() + ":" + remoteCandidate.getPort()); |
|
110 |
// TODO choose the right RTPReceiver depending on the payload type |
|
111 |
//RTPReceiver rtpReceiver = new MicroRTPReceiver(remoteCandidate.getPort()); |
355
|
112 |
} |
353
|
113 |
|
355
|
114 |
@Override |
|
115 |
public void sessionMediaReceived(JingleSession jingleSession, String participant) { |
356
|
116 |
//System.out.println("Session Media received from " + participant); |
355
|
117 |
} |
353
|
118 |
|
355
|
119 |
@Override |
|
120 |
public void sessionRedirected(String redirection, JingleSession jingleSession) { |
|
121 |
} |
353
|
122 |
} |
|
123 |
|
355
|
124 |
/** |
|
125 |
* Listen on caller session events. |
|
126 |
* @author nikita |
|
127 |
*/ |
353
|
128 |
private class BeemJingleCallerSessionListener implements JingleSessionListener { |
355
|
129 |
|
|
130 |
/** |
|
131 |
* constructor. |
|
132 |
*/ |
|
133 |
public BeemJingleCallerSessionListener() { |
|
134 |
super(); |
|
135 |
// TODO Auto-generated constructor stub |
|
136 |
} |
|
137 |
|
353
|
138 |
@Override |
|
139 |
public void sessionClosed(final String reason, final JingleSession jingleSession) { |
356
|
140 |
// System.out.println("Session " + jingleSession.getResponder() + "closed because " + reason); |
353
|
141 |
} |
|
142 |
|
|
143 |
@Override |
|
144 |
public void sessionClosedOnError(final XMPPException e, final JingleSession jingleSession) { |
356
|
145 |
// System.out.println("Session " + jingleSession.getResponder() + " closed on error"); |
353
|
146 |
|
|
147 |
} |
|
148 |
|
|
149 |
@Override |
|
150 |
public void sessionDeclined(final String reason, final JingleSession jingleSession) { |
356
|
151 |
// System.out.println("Session " + jingleSession.getResponder() + "declined because " + reason); |
353
|
152 |
} |
|
153 |
|
|
154 |
@Override |
|
155 |
public void sessionEstablished(final PayloadType pt, final TransportCandidate remoteCandidate, |
|
156 |
final TransportCandidate localCandidate, final JingleSession jingleSession) { |
356
|
157 |
// System.out.println("Session established"); |
353
|
158 |
// String name = localCandidate.getName(); |
|
159 |
String ip = localCandidate.getIp(); |
|
160 |
int port = localCandidate.getPort(); |
356
|
161 |
// System.out.println("Session established waiting connection on " + ip + ":" + port); |
353
|
162 |
|
356
|
163 |
RTPTransmitter transm = new PCMTransmitter(ip, port); |
353
|
164 |
|
|
165 |
transm.run(); |
|
166 |
try { |
|
167 |
Thread.sleep(20000); |
|
168 |
} catch (InterruptedException e) { |
|
169 |
// TODO Auto-generated catch block |
|
170 |
e.printStackTrace(); |
|
171 |
} |
|
172 |
transm.stop(); |
356
|
173 |
// System.out.println("End of transfer"); |
353
|
174 |
|
|
175 |
} |
|
176 |
|
|
177 |
@Override |
|
178 |
public void sessionMediaReceived(final JingleSession jingleSession, final String participant) { |
|
179 |
// TODO Auto-generated method stub |
|
180 |
System.out.println("Session Media received from " + participant); |
|
181 |
} |
|
182 |
|
|
183 |
@Override |
|
184 |
public void sessionRedirected(final String redirection, final JingleSession jingleSession) { |
|
185 |
// TODO Auto-generated method stub |
|
186 |
} |
|
187 |
} |
|
188 |
|
355
|
189 |
/** |
|
190 |
* Listen for a Jingle session request. |
|
191 |
* @author nikita |
|
192 |
*/ |
353
|
193 |
private class BeemJingleSessionRequestListener implements JingleSessionRequestListener { |
355
|
194 |
|
|
195 |
/** |
|
196 |
* Constructor. |
|
197 |
*/ |
|
198 |
public BeemJingleSessionRequestListener() { |
|
199 |
super(); |
|
200 |
} |
|
201 |
|
353
|
202 |
@Override |
|
203 |
public void sessionRequested(JingleSessionRequest request) { |
|
204 |
System.out.println("Jingle Session request from " + request.getFrom()); |
|
205 |
try { |
|
206 |
mIn = request.accept(); |
|
207 |
mIn.addListener(new BeemJingleReceiverSessionListener()); |
|
208 |
mIn.startIncoming(); |
|
209 |
} catch (XMPPException e) { |
|
210 |
e.printStackTrace(); |
|
211 |
} |
|
212 |
} |
|
213 |
} |
|
214 |
|
|
215 |
} |