-
Notifications
You must be signed in to change notification settings - Fork 1
/
BasicDemo.java
69 lines (64 loc) · 2.56 KB
/
BasicDemo.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
/*
* 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.FontImage;
import com.codename1.ui.Form;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.webrtc.MediaStreamConstraints;
import com.codename1.webrtc.RTC;
import com.codename1.webrtc.RTCVideoElement;
/**
* This sample was adapted from https://webrtc.github.io/samples/src/content/getusermedia/gum/
*
* The source for that sample is available at https://github.com/webrtc/samples/tree/gh-pages/src/content/getusermedia/gum
*
* @author shannah
*/
public class BasicDemo extends Form implements AutoCloseable {
private RTC rtc;
public BasicDemo() {
super("Basic WebRTC Demo", new BorderLayout());
Form hi = this;
String intro = "This sample was adapted from the \"Basic getUserMedia() 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/BasicDemo.java"));
hi.add(BorderLayout.NORTH, BoxLayout.encloseY(new SpanLabel(intro), viewSource));
RTC.createRTC().onSuccess(rtc->{
this.rtc = rtc;
MediaStreamConstraints constraints = new MediaStreamConstraints()
.audio()
.echoCancellation(true)
.noiseSuppression(true)
.video(true)
.stream();
rtc.getUserMedia(constraints).onSuccess(stream->{
RTCVideoElement video = rtc.createVideo();
video.setAutoplay(true);
video.setSrcObject(stream);
rtc.append(video);
}).onFail(t-> {
Throwable error = (Throwable)t;
System.out.println("Failed to get user media: "+error.getMessage());
Log.e(error);
});
hi.add(BorderLayout.CENTER, rtc.getVideoComponent());
hi.revalidate();
});
}
@Override
public void close() throws Exception {
if (rtc != null) {
rtc.close();
rtc = null;
}
}
}