Skip to content

Commit 3b227f5

Browse files
committed
feat: added a new web-based example
Signaling server with WebSocket support and audio/video handling
1 parent dbdd6a5 commit 3b227f5

29 files changed

Lines changed: 4074 additions & 5 deletions

webrtc-examples/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<maven.deploy.skip>true</maven.deploy.skip>
1717
<maven.install.skip>true</maven.install.skip>
1818
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<jetty.version>12.0.23</jetty.version>
1920
</properties>
2021

2122
<build>
@@ -48,6 +49,13 @@
4849
</configuration>
4950
</plugin>
5051
</plugins>
52+
53+
<resources>
54+
<resource>
55+
<directory>src/main/resources</directory>
56+
<targetPath>resources</targetPath>
57+
</resource>
58+
</resources>
5159
</build>
5260

5361
<dependencies>
@@ -56,5 +64,34 @@
5664
<artifactId>webrtc-java</artifactId>
5765
<version>${project.version}</version>
5866
</dependency>
67+
68+
<dependency>
69+
<groupId>org.eclipse.jetty</groupId>
70+
<artifactId>jetty-server</artifactId>
71+
<version>${jetty.version}</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.eclipse.jetty.websocket</groupId>
75+
<artifactId>jetty-websocket-jetty-server</artifactId>
76+
<version>${jetty.version}</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.eclipse.jetty</groupId>
80+
<artifactId>jetty-util</artifactId>
81+
<version>${jetty.version}</version>
82+
</dependency>
83+
84+
<!-- JSON Processing -->
85+
<dependency>
86+
<groupId>com.fasterxml.jackson.core</groupId>
87+
<artifactId>jackson-databind</artifactId>
88+
<version>2.16.1</version>
89+
</dependency>
90+
91+
<dependency>
92+
<groupId>org.slf4j</groupId>
93+
<artifactId>slf4j-jdk14</artifactId>
94+
<version>2.0.17</version>
95+
</dependency>
5996
</dependencies>
6097
</project>

webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/CodecListExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 WebRTC Java Contributors
2+
* Copyright 2025 Alex Andres
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/DesktopVideoExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 WebRTC Java Contributors
2+
* Copyright 2025 Alex Andres
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/PeerConnectionExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 WebRTC Java Contributors
2+
* Copyright 2025 Alex Andres
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/WhepExample.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
/*
2-
* Example application that demonstrates how to set up a WebRTC peer connection,
3-
* create an offer, and accept a remote answer.
2+
* Copyright 2025 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
415
*/
516

617
package dev.onvoid.webrtc.examples;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2025 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dev.onvoid.webrtc.examples.util;
18+
19+
import dev.onvoid.webrtc.media.audio.AudioTrackSink;
20+
import dev.onvoid.webrtc.media.video.VideoFrame;
21+
import dev.onvoid.webrtc.media.video.VideoTrackSink;
22+
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
/**
27+
* Utility class for logging audio and video frame information.
28+
*
29+
* @author Alex Andres
30+
*/
31+
public class MediaFrameLogger {
32+
33+
private static final Logger LOG = LoggerFactory.getLogger(MediaFrameLogger.class);
34+
35+
36+
/**
37+
* Creates a new audio track sink that logs information about received audio data.
38+
*
39+
* @return An AudioTrackSink that logs audio frame information.
40+
*/
41+
public static AudioTrackSink createAudioLogger() {
42+
return new AudioFrameLogger();
43+
}
44+
45+
/**
46+
* Creates a new video track sink that logs information about received video frames.
47+
*
48+
* @return A VideoTrackSink that logs video frame information.
49+
*/
50+
public static VideoTrackSink createVideoLogger() {
51+
return new VideoFrameLogger();
52+
}
53+
54+
55+
56+
/**
57+
* A simple implementation of AudioTrackSink that logs information about received audio data.
58+
*/
59+
private static class AudioFrameLogger implements AudioTrackSink {
60+
61+
private static final long LOG_INTERVAL_MS = 1000; // Log every second
62+
private int frameCount = 0;
63+
private long lastLogTime = System.currentTimeMillis();
64+
65+
@Override
66+
public void onData(byte[] data, int bitsPerSample, int sampleRate, int channels, int frames) {
67+
frameCount++;
68+
69+
long now = System.currentTimeMillis();
70+
if (now - lastLogTime >= LOG_INTERVAL_MS) {
71+
LOG.info(String.format("Received %d audio frames in the last %.1f seconds",
72+
frameCount, (now - lastLogTime) / 1000.0));
73+
LOG.info(String.format("Last audio data: %d bytes, %d bits/sample, %d Hz, %d channels, %d frames",
74+
data.length, bitsPerSample, sampleRate, channels, frames));
75+
76+
frameCount = 0;
77+
lastLogTime = now;
78+
}
79+
}
80+
}
81+
82+
83+
84+
/**
85+
* A simple implementation of VideoTrackSink that logs information about received frames.
86+
*/
87+
private static class VideoFrameLogger implements VideoTrackSink {
88+
89+
private static final long LOG_INTERVAL_MS = 1000; // Log every second
90+
private int frameCount = 0;
91+
private long lastLogTime = System.currentTimeMillis();
92+
93+
@Override
94+
public void onVideoFrame(VideoFrame frame) {
95+
frameCount++;
96+
97+
long now = System.currentTimeMillis();
98+
if (now - lastLogTime >= LOG_INTERVAL_MS) {
99+
LOG.info(String.format("Received %d video frames in the last %.1f seconds",
100+
frameCount, (now - lastLogTime) / 1000.0));
101+
LOG.info(String.format("Last frame: %dx%d, rotation: %d, timestamp: %dms",
102+
frame.buffer.getWidth(), frame.buffer.getHeight(), frame.rotation,
103+
frame.timestampNs / 1000000));
104+
105+
frameCount = 0;
106+
lastLogTime = now;
107+
}
108+
109+
// Release the native resources associated with this frame to prevent memory leaks.
110+
frame.release();
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)