-
Notifications
You must be signed in to change notification settings - Fork 1
/
PeerConnectionDemo.java
359 lines (280 loc) · 12.5 KB
/
PeerConnectionDemo.java
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.codename1.webrtc.demos;
import com.codename1.components.SpanLabel;
import com.codename1.io.Log;
import com.codename1.ui.Button;
import com.codename1.ui.CN;
import com.codename1.ui.ComboBox;
import com.codename1.ui.Dialog;
import com.codename1.ui.FontImage;
import com.codename1.ui.Form;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.layouts.FlowLayout;
import com.codename1.webrtc.Event;
import com.codename1.webrtc.MediaStream;
import com.codename1.webrtc.MediaStreamConstraints;
import com.codename1.webrtc.MediaStreamTrack;
import com.codename1.webrtc.MediaStreamTracks;
import com.codename1.webrtc.Promise;
import com.codename1.webrtc.RTC;
import com.codename1.webrtc.RTCConfiguration;
import com.codename1.webrtc.RTCPeerConnection;
import com.codename1.webrtc.RTCPeerConnection.RTCOfferOptions;
import com.codename1.webrtc.RTCPeerConnectionIceEvent;
import com.codename1.webrtc.RTCSessionDescription;
import com.codename1.webrtc.RTCTrackEvent;
import com.codename1.webrtc.RTCVideoElement;
import java.util.Date;
/**
* This example is adapted from https://webrtc.github.io/samples/src/content/peerconnection/pc1/
* https://github.com/webrtc/samples/blob/gh-pages/src/content/peerconnection/pc1/js/main.js
* @author shannah
*/
public class PeerConnectionDemo extends Form implements AutoCloseable {
private Button startButton = new Button("Start"),
callButton = new Button("Call"),
hangupButton = new Button("Hang up");
{
callButton.setEnabled(false);
hangupButton.setEnabled(false);
startButton.addActionListener(evt->{
start();
});
callButton.addActionListener(evt->{
call();
});
hangupButton.addActionListener(evt->{
hangup();
});
}
private Date startTime;
private RTCVideoElement localVideo, remoteVideo;
private MediaStream localStream;
private RTCPeerConnection pc1, pc2;
private static final RTCOfferOptions offerOptions = new RTCOfferOptions()
.offerToReceiveAudio(true)
.offerToReceiveVideo(true);
private RTC rtc;
public PeerConnectionDemo() {
super("Peer Connection Demo", new BorderLayout());
String intro = "This sample was adapted from the \"Basic PeerConnection Demo\" on the WebRTC web site.";
Button viewSource = new Button("View Source");
FontImage.setMaterialIcon(viewSource, FontImage.MATERIAL_LINK);
viewSource.addActionListener(evt->CN.execute("https://github.com/shannah/CN1WebRTC/blob/master/src/com/codename1/webrtc/demos/PeerConnectionDemo.java"));
add(BorderLayout.NORTH, BoxLayout.encloseY(new SpanLabel(intro), viewSource));
add(BorderLayout.SOUTH, FlowLayout.encloseCenter(startButton, callButton, hangupButton));
RTC.createRTC().onSuccess(r->{
rtc = r;
localVideo = rtc.createVideo();
localVideo.setAutoplay(true);
localVideo.setMuted(true);
localVideo.applyStyle("position:fixed;width:50%;height:100%;top:0;left:0;bottom:0;");
remoteVideo = rtc.createVideo();
remoteVideo.setAutoplay(true);
remoteVideo.applyStyle("position:fixed;width:50%;height:100%;top:0;right:0;bottom:0;");
rtc.append(localVideo);
rtc.append(remoteVideo);
localVideo.onloadedmetadata(evt->{
System.out.println("Local video videoWidth: "+localVideo.getVideoWidth()+"px, videoHeight: "+localVideo.getVideoHeight()+"px");
});
remoteVideo.onloadedmetadata( evt->{
System.out.println("Remote video size changed to "+remoteVideo.getVideoWidth()+"x"+remoteVideo.getVideoHeight());
if (startTime != null) {
long elapsedTime = System.currentTimeMillis() - startTime.getTime();
System.out.println("Setup time: "+elapsedTime+"ms");
startTime = null;
}
});
add(BorderLayout.CENTER, rtc.getVideoComponent());
revalidateWithAnimationSafety();
});
}
private String getName(RTCPeerConnection pc) {
return pc == pc1 ? "pc1" : "pc2";
}
private RTCPeerConnection getOtherPc(RTCPeerConnection pc) {
return pc == pc1 ? pc2 : pc1;
}
private void start() {
System.out.println("Requesting local stream");
startButton.setEnabled(false);
rtc.getUserMedia(new MediaStreamConstraints().audio(true).video(true)).onSuccess(stream->{
System.out.println("Received local stream");
localVideo.setSrcObject(stream);
localStream = stream;
callButton.setEnabled(true);
}).onFail(t->{
Log.e((Throwable)t);
Dialog.show("Error", "getUserMedia() error: "+((Throwable)t).getMessage(), "OK", null);
});
}
private void call() {
callButton.setEnabled(false);
hangupButton.setEnabled(true);
startTime = new Date();
MediaStreamTracks videoTracks = localStream.getVideoTracks();
MediaStreamTracks audioTracks = localStream.getAudioTracks();
if (videoTracks.size() > 0) {
System.out.println("Using video device "+videoTracks.get(0).getLabel());
}
if (audioTracks.size() > 0) {
System.out.println("Using audio device "+audioTracks.get(0).getLabel());
}
RTCConfiguration configuration = new RTCConfiguration();
pc1 = rtc.newRTCPeerConnection(configuration);
System.out.println("Created local peer connection object pc1");
pc1.onicecandidate(pevt->{
String candidate = pevt.getCandidate() == null ? null : pevt.getCandidate().getCandidate();
Log.p("[CALL] pc1.onicecandidate "+candidate);
onIceCandidate(pc1, pevt);
});
pc2 = rtc.newRTCPeerConnection(configuration);
System.out.println("Created remote peer connection object pc2");
pc2.onicecandidate(pevt->{
String candidate = pevt.getCandidate() == null ? null : pevt.getCandidate().getCandidate();
Log.p("[CALL] pc2.onicecandidate "+candidate);
onIceCandidate(pc2, pevt);
});
pc1.oniceconnectionstatechange(evt->{
Log.p("[CALL] pc1.oniceconnectionstatechange "+pc1.getIceConnectionState());
onIceStateChange(pc1, evt);
});
pc2.oniceconnectionstatechange(evt->{
Log.p("[CALL] pc2.oniceconnectionstatechange "+pc2.getIceConnectionState());
onIceStateChange(pc2, evt);
});
pc2.ontrack(revt->{
Log.p("[CALL] pc2.ontrack " + revt.getTrack().getId());
gotRemoteStream(revt);
});
for (MediaStreamTrack track : localStream.getTracks()) {
Log.p("[CALL] pc1.addTrack "+track.getId());
pc1.addTrack(track, localStream);
}
System.out.println("Added local stream to pc1");
System.out.println("pc1 createOffer start");
pc1.createOffer(offerOptions).onSuccess(offer->{
Log.p("[CALL] createOffer.onSucces");
onCreateOfferSuccess(offer);
})
.onFail(e-> {
onCreateSessionDescriptionError((Throwable)e);
});
}
private void onCreateSessionDescriptionError(Throwable e) {
Log.p("Failed to create session description: "+e.getMessage());
Log.e(e);
}
private void onCreateOfferSuccess(RTCSessionDescription desc) {
System.out.println("offer from pc1\n"+desc.getSdp());
System.out.println("pc1 setLocalDescription start");
Promise p1 = pc1.setLocalDescription(desc).onSuccess(res->{
Log.p("pc1.setLocalDescription.onSuccess");
onSetLocalSuccess(pc1);
}).onFail(e->{
Log.p("pc1.setLocalDescription.onFail");
Log.e((Throwable)e);
onSetSessionDescriptionError();
});
System.out.println("pc2 setRemoteDescription start");
Promise p2 = pc2.setRemoteDescription(desc).onSuccess(res->{
Log.p("pc2.setRemoteDescription.onSuccess");
onSetRemoteSuccess(pc2);
}).onFail(t->{
Log.p("pc2.setRemoteDescription.onFail");
Log.e((Throwable)t);
onSetSessionDescriptionError();
});
Promise.all(p1, p2).onSuccess(res->{
System.out.println("pc2 createAnswer start");
pc2.createAnswer().onSuccess(answer->onCreateAnswerSuccess(answer))
.onFail(e->onCreateSessionDescriptionError((Throwable)e));
});
}
private void onCreateAnswerSuccess(RTCSessionDescription desc) {
System.out.println("Anwswer from pc2:\n"+desc.getSdp());
System.out.println("pc2 setLocalDescription start");
pc2.setLocalDescription(desc).onSuccess(res -> onSetLocalSuccess(pc2))
.onFail(e->{
Log.e((Throwable)e);
onSetSessionDescriptionError((Throwable)e);
});
System.out.println("pc1 setRemoteDescription start");
pc1.setRemoteDescription(desc).onSuccess(res->onSetRemoteSuccess(pc1))
.onFail(e->{
Log.e((Throwable)e);
onSetSessionDescriptionError((Throwable)e);
});
}
private void onSetSessionDescriptionError() {
System.out.println("Error in setSessionDescription");
Log.e(new RuntimeException("Error"));
}
private void onSetLocalSuccess(RTCPeerConnection pc) {
System.out.println(getName(pc)+" setLocalDescription complete");
}
private void onSetRemoteSuccess(RTCPeerConnection pc) {
System.out.println(getName(pc)+" setRemoteDescription complete");
}
private void gotRemoteStream(RTCTrackEvent e) {
if (remoteVideo.getSrcObject() != e.getStreams().get(0)) {
System.out.println("pc2 received remote stream");
remoteVideo.setSrcObject(e.getStreams().get(0));
}
}
private void onIceCandidate(RTCPeerConnection pc, RTCPeerConnectionIceEvent event) {
if (event.getCandidate() == null) {
System.out.println("No ICE candidate included in event");
return;
}
getOtherPc(pc).addIceCandidate(event.getCandidate()).onSuccess(res->onAddIceCandidateSuccess(pc))
.onFail(e->{
Log.e((Throwable)e);
onAddIceCandidateError(pc, (Throwable)e);
}).onComplete(e->{
System.out.println(getName(pc)+" ICE candidate:\n"+(event.getCandidate() != null ? event.getCandidate().getCandidate() : "(null"));
});
}
private void onSetSessionDescriptionError(Throwable e) {
System.out.println("Failed to set session description: "+e.getMessage());
}
private void onAddIceCandidateSuccess(RTCPeerConnection pc) {
System.out.println(getName(pc)+" addIceCandidate success");
}
private void onAddIceCandidateError(RTCPeerConnection pc, Throwable error) {
System.out.println(getName(pc)+" failed to add ICE Candidate: "+error.getMessage());
}
private void onIceStateChange(RTCPeerConnection pc, Event event) {
if (pc != null) {
System.out.println(getName(pc)+" ICE state: "+pc.getIceConnectionState());
System.out.println("Ice state change event: " + event);
}
}
private void hangup() {
System.out.println("Ending call");
if (pc1 != null) {
pc1.close();
pc1.release();
pc1 = null;
}
if (pc2 != null) {
pc2.close();
pc2.release();
pc2 = null;
}
hangupButton.setEnabled(false);
callButton.setEnabled(true);
}
@Override
public void close() throws Exception {
if (rtc != null) {
rtc.close();
rtc = null;
}
}
}