-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConstraintsDemo.java
267 lines (234 loc) · 10.4 KB
/
ConstraintsDemo.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
/*
* 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.CheckBox;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.FontImage;
import com.codename1.ui.Form;
import com.codename1.ui.Label;
import com.codename1.ui.TextArea;
import com.codename1.ui.TextField;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.layouts.FlowLayout;
import com.codename1.ui.layouts.LayeredLayout;
import com.codename1.webrtc.ConstrainNumber;
import com.codename1.webrtc.Event;
import com.codename1.webrtc.MediaStream;
import com.codename1.webrtc.MediaStreamConstraints;
import com.codename1.webrtc.MediaStreamTrack;
import com.codename1.webrtc.RTC;
import com.codename1.webrtc.RTCVideoElement;
import com.codename1.webrtc.VideoTrackConstraints;
/**
* This demo was adapted from https://webrtc.github.io/samples/src/content/getusermedia/resolution/
*
* Source code for original Javscript sample: https://github.com/webrtc/samples/blob/gh-pages/src/content/getusermedia/resolution/js/main.js
*
*
* @author shannah
*/
public class ConstraintsDemo extends Form implements AutoCloseable {
private MediaStream stream;
private RTCVideoElement video;
private RTC rtc;
private SpanLabel messagebox = new SpanLabel();
private Component videoblock;
private TextField widthInput = new TextField();
private Label widthOutput = new Label(), heightOutput = new Label();
private Label dimensions = new Label();
private int currentWidth, currentHeight;
private CheckBox aspectLock = new CheckBox("Lock aspect ratio"), sizeLock = new CheckBox("Lock video size");
private Button vgaButton=new Button("VGA"),
qvgaButton = new Button("QVGA"),
hdButton = new Button("HD"),
fullHdButton = new Button("Full HD"),
fourKButton = new Button("4K"),
eightKButton = new Button("8K");
private static MediaStreamConstraints createConstraints(int width, int height) {
return new MediaStreamConstraints()
.video()
.width(ConstrainNumber.exact(width))
.height(ConstrainNumber.exact(height))
.stream();
}
private static final MediaStreamConstraints qvgaConstraints = createConstraints(320, 240),
vgaConstraints = createConstraints(640, 480),
hdConstraints = createConstraints(1280, 720),
fullHdConstraints = createConstraints(1920, 1080),
fourKConstraints = createConstraints(4096, 2160),
eightKConstraints = createConstraints(7680, 4320);
private void gotStream(MediaStream stream) {
this.stream = stream;
if (video == null) {
video = rtc.createVideo();
video.setAutoplay(true);
rtc.append(video);
}
video.setSrcObject(stream);
messagebox.setVisible(false);
videoblock.setVisible(true);
MediaStreamTrack track = stream.getVideoTracks().get(0);
VideoTrackConstraints constraints = (VideoTrackConstraints)track.getConstraints();
System.out.println("Result constraints: " + constraints.toJSONStruct());
if (constraints != null && constraints.getWidth() != null && constraints.getWidth().getExact() != null) {
widthInput.setText(String.valueOf(constraints.getWidth().getExact()));
widthOutput.setText(String.valueOf(constraints.getWidth().getExact())+"px");
} else if (constraints != null && constraints.getWidth() != null && constraints.getWidth().getMin() != null) {
widthInput.setText(constraints.getWidth().getMin()+"");
widthOutput.setText(constraints.getWidth().getMin()+"px");
}
}
private void errorMessage(String fromWhere, String msg) {
messagebox.setVisible(true);
messagebox.setText(msg);
revalidate();
System.out.println(fromWhere+": "+msg);
}
private void clearErrorMessage() {
messagebox.setVisible(false);
revalidate();
}
private void displayVideoDimensions(String whereSeen) {
if (video.getVideoWidth() > 0) {
dimensions.setText("Actual video dimensions: "+video.getVideoWidth() + "x" + video.getVideoHeight() + "px.");
if (currentWidth != video.getVideoWidth() || currentHeight != video.getVideoHeight()) {
System.out.println(whereSeen+": "+dimensions.getText());
currentWidth = video.getVideoWidth();
currentHeight = video.getVideoHeight();
}
} else {
dimensions.setText("Video not ready");
}
revalidate();
}
private void constraintChange(ActionEvent e) {
TextArea src = (TextArea)e.getSource();
int val;
try {
val = Integer.parseInt(src.getText());
} catch (Throwable t) {
Log.e(t);
return;
}
widthOutput.setText(src.getText());
MediaStreamTrack track = stream.getVideoTracks().get(0);
VideoTrackConstraints constraints;
if (aspectLock.isSelected()) {
constraints = new MediaStreamConstraints()
.video()
.width(ConstrainNumber.exact(val))
.aspectRatio(ConstrainNumber.exact(video.getVideoWidth()/(double)video.getVideoHeight()));
} else {
constraints = new MediaStreamConstraints()
.video()
.width(ConstrainNumber.exact(val));
}
clearErrorMessage();
System.out.println("applying "+constraints.toJSONStruct());
track.applyConstraints(constraints)
.onSuccess(res->{
System.out.println("applyConstraint success");
displayVideoDimensions("applyConstraints");
})
.onFail(err->{
errorMessage("applyConstraints", ((Throwable)err).getMessage());
});
}
private void getMedia(MediaStreamConstraints constraints) {
if (stream != null) {
for (MediaStreamTrack track : stream.getTracks()) {
track.stop();
}
stream.release();
stream = null;
}
clearErrorMessage();
videoblock.setVisible(false);
rtc.getUserMedia(constraints).onSuccess(stream->{
gotStream(stream);
}).onFail(err -> {
errorMessage("getUserMedia", ((Throwable)err).getMessage());
});
}
@Override
public void close() throws Exception {
if (rtc != null) {
rtc.close();
rtc = null;
}
}
private static class Link extends Button {
private String href;
public Link(String label, String href) {
super(label);
setUIID("Label");
this.href = href;
addActionListener(evt->{
CN.execute(href);
});
}
}
public ConstraintsDemo() {
super("Constraints Demo", new BorderLayout());
Container north = new Container(BoxLayout.y());
String intro = "This sample was adapted from the \"getUserMedia(): select resolution\" sample 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/ConstraintsDemo.java"));
north.add(BoxLayout.encloseY(new SpanLabel(intro), viewSource));
north.add(FlowLayout.encloseIn(new Label("WebRTC samples"), new Label("getUserMedia: select resolution")));
north.add(FlowLayout.encloseIn(new Label("This example uses"), new Link("constraints", "https://w3c.github.io/mediacapture-main/getusermedia.html#media-track-constraints")));
north.add(new SpanLabel("Click a button to call getUserMedia() with appropriate resolution."));
north.add(FlowLayout.encloseIn(qvgaButton, vgaButton, hdButton, fullHdButton, fourKButton, eightKButton));
north.add(dimensions);
add(BorderLayout.NORTH, north);
Container south = new Container(BoxLayout.y());
south.add(BoxLayout.encloseX(new Label("Width: "), widthOutput, new Label(":"), widthInput));
south.add(sizeLock);
south.add(aspectLock);
south.add(new Label("For more information see..."));
south.add(new Link("View source on Github", "http://example.com"));
Container center = new Container(new LayeredLayout());
center.add(messagebox);
add(BorderLayout.CENTER, center);
RTC.createRTC().onSuccess(rtc_->{
rtc = rtc_;
videoblock = rtc.getVideoComponent();
center.add(videoblock);
video = rtc.createVideo();
video.setAutoplay(true);
rtc.append(video);
video.onloadedmetadata(evt->{
displayVideoDimensions("loadedmetadata");
});
video.addEventListener("resize", evt->{
displayVideoDimensions("resize");
});
widthInput.addActionListener(evt->{
constraintChange(evt);
});
sizeLock.addActionListener(evt->{
if (sizeLock.isSelected()) {
System.out.println("Setting fixed size");
//.. Not worred about this yet
}
});
vgaButton.addActionListener(evt->getMedia(vgaConstraints));
qvgaButton.addActionListener(evt->getMedia(qvgaConstraints));
hdButton.addActionListener(evt->getMedia(hdConstraints));
fullHdButton.addActionListener(evt->getMedia(fullHdConstraints));
fourKButton.addActionListener(evt->getMedia(fourKConstraints));
eightKButton.addActionListener(evt->getMedia(eightKConstraints));
});
}
}