Repository navigation
Expand file tree
/
Copy pathMediaFilePlayerExample.java
More file actions
954 lines (799 loc) · 34.2 KB
/
Copy pathMediaFilePlayerExample.java
File metadata and controls
954 lines (799 loc) · 34.2 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
/*
* Copyright 2026 Alex Andres
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.onvoid.webrtc.examples;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import dev.onvoid.webrtc.RTCStats;
import dev.onvoid.webrtc.RTCStatsReport;
import dev.onvoid.webrtc.RTCStatsType;
import dev.onvoid.webrtc.client.AudioMode;
import dev.onvoid.webrtc.client.Encoding;
import dev.onvoid.webrtc.client.LocalAudio;
import dev.onvoid.webrtc.client.LocalVideo;
import dev.onvoid.webrtc.client.Peer;
import dev.onvoid.webrtc.client.PeerOptions;
import dev.onvoid.webrtc.client.PeerStats;
import dev.onvoid.webrtc.client.RemoteTrack;
import dev.onvoid.webrtc.client.Sender;
import dev.onvoid.webrtc.client.Signaling;
import dev.onvoid.webrtc.client.SignalingPair;
import dev.onvoid.webrtc.client.WebRtc;
import dev.onvoid.webrtc.client.swing.VideoView;
import dev.onvoid.webrtc.media.player.MediaFileSource;
import dev.onvoid.webrtc.media.player.MediaInfo;
import dev.onvoid.webrtc.media.player.MediaPlayerListener;
import dev.onvoid.webrtc.media.player.MediaPlayerState;
import dev.onvoid.webrtc.media.recorder.MediaRecorder;
import dev.onvoid.webrtc.media.recorder.MediaRecorderListener;
/**
* Desktop UI counterpart to {@link MediaFileExample}, built on the client API
* ({@code dev.onvoid.webrtc.client}): plays a media file, or a live stream
* from an {@code rtsp://} URL such as an IP camera's, through a pair of peers
* that live in the same application, shows the received video while the
* received audio plays on the speakers, and records what is received.
* <p>
* This example shows how to:
* <ul>
* <li>Send the sources of a MediaFileSource as tracks of the client API</li>
* <li>Connect two peers in one application through a loopback signaling</li>
* <li>Render received video in the Swing {@link VideoView}</li>
* <li>Play received audio through the default playout device</li>
* <li>Run the peers' handlers on the Swing event dispatch thread</li>
* <li>Show what the file contains, where playback is, and what arrives at
* the receiver, read from its statistics</li>
* <li>Record the received call with a MediaRecorder</li>
* <li>Start and stop a session repeatedly, releasing everything in order</li>
* </ul>
* <p>
* One runtime serves both peers. Its {@link AudioMode#CUSTOM} audio mode sends
* the file's pushed audio and plays the received audio, which is what two
* factories did before the client API had audio modes.
* <p>
* Audio and video stay in sync because both tracks are sent in the same media
* stream: the receiver lines them up with each other before handing them on,
* so the view only has to draw each frame as it arrives.
* <p>
* Run it with an optional media file or stream URL to preselect:
* <pre>
* java dev.onvoid.webrtc.examples.MediaFilePlayerExample movie.mp4
* java dev.onvoid.webrtc.examples.MediaFilePlayerExample rtsp://camera.local/stream1
* </pre>
* A live stream has no length, so the Loop box is disabled while one plays.
*
* @author Alex Andres
*/
public class MediaFilePlayerExample {
private static final Logger LOG = Logger.getLogger(MediaFilePlayerExample.class.getName());
/** Both tracks go in this stream, which is what ties them together for lip sync. */
private static final String STREAM_ID = "media-file";
/**
* The most the video may be sent with. Without a limit of its own, WebRTC
* holds video to about 2 Mbit/s, which fits a call but leaves a 1080p file
* at a fraction of its resolution. The two peers here share a machine, so
* there is room for more; the bandwidth estimate still decides how much of
* it is actually used.
*/
private static final int MAX_VIDEO_BITRATE = 10_000_000;
private static final DateTimeFormatter RECORDING_NAME = DateTimeFormatter.ofPattern("'recording-'yyyyMMdd-HHmmss'.mkv'");
/** Owns the peers and tracks of every session; its handlers run on the EDT. */
private final WebRtc rtc;
/** Setting up and tearing down a session blocks, so it stays off the EDT. */
private final ExecutorService worker = Executors.newSingleThreadExecutor(r -> {
Thread thread = new Thread(r, "media-session");
thread.setDaemon(true);
return thread;
});
private final JFrame frame = new JFrame("Media Player");
private final JTextField fileField = new JTextField(32);
private final JButton browseButton = new JButton("Browse...");
private final JButton startButton = new JButton("Start");
private final JButton stopButton = new JButton("Stop");
private final JToggleButton recordButton = new JToggleButton("Record");
private final JCheckBox loopBox = new JCheckBox("Loop");
private final JLabel statusLabel = new JLabel("Idle");
private final JProgressBar timeBar = new JProgressBar(0, 1000);
private final VideoView videoView = new VideoView();
private final MetricsPanel metricsPanel = new MetricsPanel();
private final Timer statusTimer = new Timer(250, e -> updateStatus());
/** Only touched from the worker thread and read on the EDT for status. */
private volatile Session session;
public static void main(String[] args) {
String file = args.length > 0 ? args[0] : "";
SwingUtilities.invokeLater(() -> {
usePlatformLookAndFeel();
new MediaFilePlayerExample().show(file);
});
}
/**
* A short name for a source: a file's name, or a stream URL without the
* credentials it may carry.
*/
private static String sourceName(String source) {
return source.contains("://") ? withoutCredentials(source) : new File(source).getName();
}
/**
* The given source without the user name and password a URL can carry in
* front of its host, so that it can be shown. Anything else, a file path
* included, is returned as it is.
*/
private static String withoutCredentials(String source) {
// Everything between the scheme and the last @ before the path. Not
// parsed as a URI: a password is free to hold characters a URI
// parser rejects, and then it would be shown after all.
return source.replaceFirst("://[^/?#]*@", "://");
}
/**
* Makes the window look like the platform's own applications rather than
* Swing's cross-platform default. It has to happen before the first
* component is created, which only picks up the look it was made with.
*/
private static void usePlatformLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
// The default look works everywhere, so this is only cosmetic.
LOG.log(Level.WARNING, "Could not use the platform look and feel", e);
}
}
private MediaFilePlayerExample() {
// Pushed audio out, played audio in, and every handler on the EDT.
rtc = WebRtc.builder()
.audio(AudioMode.CUSTOM)
.eventExecutor(SwingUtilities::invokeLater)
.build();
videoView.setPreferredSize(new Dimension(960, 540));
}
private void show(String file) {
fileField.setText(file);
fileField.setToolTipText("A media file, or an rtsp:// stream URL");
browseButton.addActionListener(e -> chooseFile());
startButton.addActionListener(e -> start());
stopButton.addActionListener(e -> stop());
recordButton.addActionListener(e -> record(recordButton.isSelected()));
loopBox.addActionListener(e -> {
Session current = session;
if (current != null) {
current.source.setLooping(loopBox.isSelected());
}
});
JPanel controls = new JPanel(new FlowLayout(FlowLayout.LEFT));
controls.add(fileField);
controls.add(browseButton);
controls.add(startButton);
controls.add(stopButton);
controls.add(recordButton);
controls.add(loopBox);
timeBar.setStringPainted(true);
timeBar.setString("");
JPanel statusBar = new JPanel(new BorderLayout(12, 0));
statusBar.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
statusBar.add(statusLabel, BorderLayout.WEST);
statusBar.add(timeBar, BorderLayout.CENTER);
frame.setLayout(new BorderLayout());
frame.add(controls, BorderLayout.NORTH);
frame.add(videoView, BorderLayout.CENTER);
frame.add(metricsPanel, BorderLayout.EAST);
frame.add(statusBar, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exit();
}
});
setRunning(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void chooseFile() {
JFileChooser chooser = new JFileChooser();
String current = fileField.getText().trim();
if (!current.isEmpty()) {
chooser.setSelectedFile(new File(current));
}
if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
fileField.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
private void start() {
String file = fileField.getText().trim();
if (file.isEmpty()) {
statusLabel.setText("Choose a media file or enter an rtsp:// URL first.");
return;
}
boolean looping = loopBox.isSelected();
setRunning(true);
statusLabel.setText("Connecting...");
metricsPanel.clear("");
worker.execute(() -> {
try {
Session started = new Session(file, looping);
session = started;
SwingUtilities.invokeLater(() -> {
metricsPanel.showMedia(file, started.info);
// Only a source with a length can start over.
loopBox.setEnabled(started.info.getDurationUs() > 0);
});
}
catch (Exception e) {
LOG.log(Level.SEVERE, "Could not start playback", e);
SwingUtilities.invokeLater(() -> {
setRunning(false);
statusLabel.setText("Failed: " + e.getMessage());
});
}
});
}
private void stop() {
startButton.setEnabled(false);
stopButton.setEnabled(false);
recordButton.setEnabled(false);
worker.execute(() -> {
closeSession();
SwingUtilities.invokeLater(() -> {
setRunning(false);
videoView.clear();
statusLabel.setText("Stopped");
timeBar.setValue(0);
timeBar.setString("");
// What the file contains stays up, what was playing does not.
metricsPanel.clear("play.");
metricsPanel.clear("video.");
metricsPanel.clear("audio.");
});
});
}
/**
* Starts or stops recording what the receiver gets. Starting and stopping
* a recorder waits on the peers and on the file, so it runs on the worker.
*/
private void record(boolean on) {
worker.execute(() -> {
Session current = session;
String message;
if (current == null) {
message = "Start playback to record it";
}
else if (on) {
message = current.startRecording();
}
else {
message = current.stopRecording();
}
SwingUtilities.invokeLater(() -> {
statusLabel.setText(message);
recordButton.setSelected(session != null && session.recorder != null);
});
});
}
private void exit() {
statusTimer.stop();
frame.dispose();
// Queued behind anything still running, so a session being set up
// is closed rather than left behind.
worker.execute(() -> {
closeSession();
rtc.close();
System.exit(0);
});
}
private void closeSession() {
Session current = session;
session = null;
if (current != null) {
current.close();
}
}
private void setRunning(boolean running) {
startButton.setEnabled(!running);
stopButton.setEnabled(running);
recordButton.setEnabled(running);
fileField.setEnabled(!running);
browseButton.setEnabled(!running);
if (!running) {
loopBox.setEnabled(true);
recordButton.setSelected(false);
statusTimer.stop();
}
else {
statusTimer.start();
}
}
private void updateStatus() {
Session current = session;
if (current == null) {
return;
}
MediaPlayerState state = current.source.getState();
long positionUs = current.source.getPositionUs();
long durationUs = current.info.getDurationUs();
// Live streams and some containers have no duration to measure against.
String time = durationUs > 0
? formatTime(positionUs) + " / " + formatTime(durationUs)
: formatTime(positionUs);
if (current.recorder == null) {
statusLabel.setText(state.toString());
}
timeBar.setValue(durationUs > 0 ? (int) Math.min(1000, positionUs * 1000 / durationUs) : 0);
timeBar.setString(time);
metricsPanel.set("play.state", state.toString());
metricsPanel.set("play.position", time);
metricsPanel.set("play.connection", current.receiver.state().toString());
}
private static String formatTime(long us) {
long tenths = us / 100_000;
long s = tenths / 10;
if (s >= 3600) {
return String.format("%d:%02d:%02d.%d", s / 3600, s / 60 % 60, s % 60, tenths % 10);
}
return String.format("%d:%02d.%d", s / 60, s % 60, tenths % 10);
}
private static String formatBitrate(Double bitsPerSecond) {
if (bitsPerSecond == null) {
return null;
}
if (bitsPerSecond >= 1_000_000) {
return String.format("%.2f Mbit/s", bitsPerSecond / 1_000_000);
}
return String.format("%.0f kbit/s", bitsPerSecond / 1_000);
}
/**
* Formats a codec entry of a stats report, such as {@code VP8} or
* {@code opus, 48000 Hz, 2 ch}.
*/
private static String formatCodec(RTCStats codec) {
if (codec == null) {
return null;
}
Map<String, Object> attributes = codec.getAttributes();
String mimeType = String.valueOf(attributes.get("mimeType"));
String name = mimeType.substring(mimeType.indexOf('/') + 1);
if (!mimeType.startsWith("audio/")) {
return name;
}
long channels = count(attributes, "channels");
return String.format("%s, %d Hz, %d ch", name, count(attributes, "clockRate"),
channels > 0 ? channels : 1);
}
private static String formatLoss(Map<String, Object> attributes) {
long lost = count(attributes, "packetsLost");
long total = lost + count(attributes, "packetsReceived");
if (total <= 0) {
return null;
}
return String.format("%d of %d (%.1f %%)", lost, total, lost * 100.0 / total);
}
private static String format(String pattern, Double value) {
return value != null ? String.format(pattern, value) : null;
}
/**
* Reads a counter from stats attributes. Counters come as Integer, Long or
* BigInteger depending on their native type, and are absent until there is
* something to count.
*/
private static long count(Map<String, Object> attributes, String name) {
Object value = attributes.get(name);
return value instanceof Number ? ((Number) value).longValue() : 0;
}
private static Double decimal(Map<String, Object> attributes, String name) {
Object value = attributes.get(name);
return value instanceof Number ? ((Number) value).doubleValue() : null;
}
/**
* One run of the file, from Start to Stop: the source, the sending and
* receiving peers, the tracks over the file's sources, and a recording.
*/
private class Session {
final MediaFileSource source;
final MediaInfo info;
final Peer sender;
final Peer receiver;
private final LocalVideo videoTrack;
private final LocalAudio audioTrack;
/** What the receiver gets, kept for recording. Set on the EDT. */
private volatile RemoteTrack remoteVideo;
private volatile RemoteTrack remoteAudio;
/** The recording running, if any. Only touched on the worker. */
volatile MediaRecorder recorder;
private Path recording;
/** Turn the byte counters of successive stats reports into bitrates. */
private final RateMeter videoRate = new RateMeter();
private final RateMeter audioRate = new RateMeter();
Session(String file, boolean looping) throws Exception {
source = new MediaFileSource(file);
info = source.getInfo();
// The peers find each other in process; across a network the
// messages would go over a WebSocket instead.
SignalingPair link = Signaling.loopback();
sender = rtc.createPeer(PeerOptions.builder().name("sender").signaling(link.first()).build());
receiver = rtc.createPeer(PeerOptions.builder().name("receiver").signaling(link.second())
.polite(true).build());
receiver.onStateChange(state -> LOG.info("Receiver connection: " + state));
receiver.onTrack(this::onRemoteTrack);
receiver.onStats(Duration.ofSeconds(1), this::showStats);
// A source with no video has no video source, and likewise for
// audio, so each track is only made if there is something to feed
// it. The tracks wrap the file's sources, which stay the file's.
if (source.getVideoSource() != null) {
videoTrack = rtc.media().videoTrack("video", source.getVideoSource());
Sender videoSender = sender.addTrack(videoTrack, STREAM_ID);
// Encodings exist once negotiation set the sender up.
sender.whenConnected()
.thenCompose(connected -> videoSender.configure(
Encoding.builder().maxBitrate(MAX_VIDEO_BITRATE).build()))
.exceptionally(error -> {
LOG.log(Level.WARNING, "Could not raise the video bitrate", error);
return null;
});
}
else {
videoTrack = null;
}
if (source.getAudioSource() != null) {
audioTrack = rtc.media().audioTrack("audio", source.getAudioSource());
sender.addTrack(audioTrack, STREAM_ID);
}
else {
audioTrack = null;
}
source.setLooping(looping);
source.setListener(new MediaPlayerListener() {
@Override
public void onStateChanged(MediaPlayerState state) {
LOG.info("Player state: " + state);
}
@Override
public void onEndOfStream() {
stopIfCurrent();
}
@Override
public void onError(String message) {
LOG.warning("Playback failed: " + message);
stopIfCurrent();
}
});
// Frames are sent in real time from the moment playback starts,
// and whatever goes out before the connection is up is dropped.
// Starting right away keeps the example short; an application
// that must not lose the first second would wait for
// receiver.whenConnected().
source.play();
}
/** On the EDT: shows the video, and keeps both tracks for recording. */
private void onRemoteTrack(RemoteTrack track) {
if (track.isVideo()) {
remoteVideo = track;
track.asVideo().onFrame(videoView);
}
else {
// Nothing to do for playback: the runtime plays received audio.
remoteAudio = track;
}
}
/** On the EDT, once a second. */
private void showStats(PeerStats stats) {
if (session == this) {
metricsPanel.setAll(readStats(stats.report()));
}
}
/**
* Stops from a player callback. Closing here would wait on the
* player's own thread, so it goes through the UI like the Stop button,
* and only if the user has not already moved on to another session.
*/
private void stopIfCurrent() {
SwingUtilities.invokeLater(() -> {
if (session == this) {
stop();
}
});
}
/** On the worker. */
String startRecording() {
if (recorder != null) {
return "Recording to " + recording;
}
if (remoteVideo == null && remoteAudio == null) {
return "Nothing received yet to record";
}
Path file = Paths.get(LocalDateTime.now().format(RECORDING_NAME)).toAbsolutePath();
try {
MediaRecorder started = new MediaRecorder(file);
started.setListener(new MediaRecorderListener() {
@Override
public void onWarning(String message) {
LOG.warning("Recording: " + message);
}
@Override
public void onError(String message) {
LOG.warning("Recording failed: " + message);
}
});
// The receiver's tracks as they arrived, before decoding.
if (remoteVideo != null) {
started.addTrack(remoteVideo);
}
if (remoteAudio != null) {
started.addTrack(remoteAudio);
}
started.start();
recorder = started;
recording = file;
return "Recording to " + file;
}
catch (Exception e) {
LOG.log(Level.WARNING, "Could not start recording", e);
return "Could not record: " + e.getMessage();
}
}
/** On the worker. */
String stopRecording() {
MediaRecorder running = recorder;
recorder = null;
if (running == null) {
return "Not recording";
}
running.close();
switch (running.getResult()) {
case COMPLETE:
return "Saved " + recording;
case PARTIAL:
return "Saved " + recording + ", but writing it failed";
case FAILED:
return "Recording failed";
default:
return "Nothing was recorded";
}
}
/**
* Picks the inbound RTP entries out of a stats report, one per
* received track, and turns them into the rows of the metrics panel.
*/
private Map<String, String> readStats(RTCStatsReport report) {
Map<String, RTCStats> all = report.getStats();
Map<String, String> values = new HashMap<>();
for (RTCStats stats : all.values()) {
if (stats.getType() != RTCStatsType.INBOUND_RTP) {
continue;
}
Map<String, Object> attributes = stats.getAttributes();
Object codecId = attributes.get("codecId");
RTCStats codec = codecId != null ? all.get(codecId) : null;
Double jitter = decimal(attributes, "jitter");
Double jitterMs = jitter != null ? jitter * 1000 : null;
if ("video".equals(attributes.get("kind"))) {
long width = count(attributes, "frameWidth");
long height = count(attributes, "frameHeight");
values.put("video.codec", formatCodec(codec));
values.put("video.resolution", width > 0 ? width + "x" + height : null);
values.put("video.fps", format("%.1f fps", decimal(attributes, "framesPerSecond")));
values.put("video.bitrate", formatBitrate(videoRate.update(stats)));
values.put("video.frames", String.format("%d decoded, %d dropped",
count(attributes, "framesDecoded"), count(attributes, "framesDropped")));
values.put("video.lost", formatLoss(attributes));
values.put("video.jitter", format("%.1f ms", jitterMs));
}
else if ("audio".equals(attributes.get("kind"))) {
Double level = decimal(attributes, "audioLevel");
long samples = count(attributes, "totalSamplesReceived");
long concealed = count(attributes, "concealedSamples");
values.put("audio.codec", formatCodec(codec));
values.put("audio.bitrate", formatBitrate(audioRate.update(stats)));
values.put("audio.level", level == null ? null
: level > 0 ? String.format("%.1f dBFS", 20 * Math.log10(level))
: "silent");
values.put("audio.concealed", samples > 0
? String.format("%.1f %%", concealed * 100.0 / samples) : null);
values.put("audio.lost", formatLoss(attributes));
values.put("audio.jitter", format("%.1f ms", jitterMs));
}
}
return values;
}
/** On the worker. */
void close() {
if (recorder != null) {
LOG.info(stopRecording());
}
try {
// The peers go first, then the tracks, which must be released
// before the file closes the sources they were made over.
sender.closeAsync().get(5, TimeUnit.SECONDS);
receiver.closeAsync().get(5, TimeUnit.SECONDS);
if (videoTrack != null) {
videoTrack.closeAsync().get(5, TimeUnit.SECONDS);
}
if (audioTrack != null) {
audioTrack.closeAsync().get(5, TimeUnit.SECONDS);
}
}
catch (Exception e) {
LOG.log(Level.WARNING, "Closing the session took too long", e);
}
source.close();
}
}
/**
* Turns a byte counter that only grows into a bitrate, measured over the
* time between two successive readings of it.
*/
private static class RateMeter {
private long lastBytes;
private long lastTimestampUs;
/**
* Takes the next reading from inbound RTP stats.
*
* @return The bitrate in bits per second since the last reading, or
* {@code null} if there is nothing to compare against yet.
*/
Double update(RTCStats stats) {
long bytes = count(stats.getAttributes(), "bytesReceived");
long timestampUs = stats.getTimestamp();
Double rate = null;
if (lastTimestampUs > 0 && timestampUs > lastTimestampUs && bytes >= lastBytes) {
rate = (bytes - lastBytes) * 8 * 1_000_000.0 / (timestampUs - lastTimestampUs);
}
lastBytes = bytes;
lastTimestampUs = timestampUs;
return rate;
}
}
/**
* A column of named values, grouped into sections: what the file
* contains, where playback is, and what the receiver gets for each track.
* Each value is addressed by a key such as {@code video.fps}, and shows a
* dash while there is nothing to show.
*/
private static class MetricsPanel extends JPanel {
private static final String NONE = "-";
private final Map<String, JLabel> values = new LinkedHashMap<>();
private int row;
MetricsPanel() {
super(new GridBagLayout());
setBorder(BorderFactory.createEmptyBorder(8, 12, 8, 12));
// Fixed, so the video does not shift as the values change width.
setPreferredSize(new Dimension(320, 0));
addSection("Media");
addRow("media.file", "Source");
addRow("media.duration", "Duration");
addRow("media.video", "Video");
addRow("media.audio", "Audio");
addSection("Playback");
addRow("play.state", "State");
addRow("play.position", "Position");
addRow("play.connection", "Connection");
addSection("Received video");
addRow("video.codec", "Codec");
addRow("video.resolution", "Resolution");
addRow("video.fps", "Frame rate");
addRow("video.bitrate", "Bitrate");
addRow("video.frames", "Frames");
addRow("video.lost", "Packets lost");
addRow("video.jitter", "Jitter");
addSection("Received audio");
addRow("audio.codec", "Codec");
addRow("audio.bitrate", "Bitrate");
addRow("audio.level", "Level");
addRow("audio.concealed", "Concealed");
addRow("audio.lost", "Packets lost");
addRow("audio.jitter", "Jitter");
// Takes up the remaining height, keeping the rows at the top.
GridBagConstraints filler = new GridBagConstraints();
filler.gridy = row;
filler.weighty = 1;
add(new JPanel(), filler);
}
void showMedia(String file, MediaInfo info) {
set("media.file", sourceName(file));
// Never the source as typed: a stream URL may carry a password.
values.get("media.file").setToolTipText(withoutCredentials(file));
set("media.duration", info.getDurationUs() > 0
? formatTime(info.getDurationUs()) : "unknown");
set("media.video", info.hasVideo()
? String.format("%s, %dx%d, %.2f fps", info.getVideoCodec(),
info.getVideoWidth(), info.getVideoHeight(), info.getFrameRate())
: "none");
set("media.audio", info.hasAudio()
? String.format("%s, %d Hz, %d ch", info.getAudioCodec(),
info.getSampleRate(), info.getChannels())
: "none");
}
void set(String key, String text) {
values.get(key).setText(text != null ? text : NONE);
}
void setAll(Map<String, String> newValues) {
newValues.forEach(this::set);
}
/**
* Resets every value whose key starts with the prefix, all of them
* for an empty one.
*/
void clear(String prefix) {
values.forEach((key, label) -> {
if (key.startsWith(prefix)) {
label.setText(NONE);
label.setToolTipText(null);
}
});
}
private void addSection(String title) {
JLabel label = new JLabel(title);
label.setFont(label.getFont().deriveFont(Font.BOLD));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = row;
c.gridwidth = 2;
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets(row == 0 ? 0 : 12, 0, 4, 0);
add(label, c);
row++;
}
private void addRow(String key, String name) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = row;
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets(1, 0, 1, 12);
add(new JLabel(name), c);
JLabel value = new JLabel(NONE);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = row;
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(1, 0, 1, 0);
add(value, c);
values.put(key, value);
row++;
}
}
}