Examples
This section provides an overview of the example applications included in the webrtc-examples Maven module. These examples demonstrate various features and capabilities of the webrtc-java library.
Client API
The ClientApiExample connects two peers in one process with the Client API, and sends video, audio and chat messages from one to the other. It needs no audio or video hardware and no server.
Key features demonstrated:
- Creating a headless
WebRtcruntime, which needs no microphone or speakers - Connecting two peers through a signaling that carries JSON text, as a WebSocket would
- Letting perfect negotiation run the offer and answer exchange, with one polite peer
- Pushing video frames, and audio in pieces of any length
- Receiving remote tracks, their frames and their audio
- Exchanging messages on a data channel
- Reading the connection's statistics once a second
- Closing the peers, tracks and factory by closing the runtime
This example is the shortest way to see a complete call working, and a starting point for applications built on the client API.
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.ClientApiExample" -D"exec.args=10"Native Image
The NativeImageExample is made to be compiled into a GraalVM native image, a single executable that needs no installed Java runtime. It connects two peers in one process and checks itself: it exits with status 0 only if everything arrived.
Key features demonstrated:
- Loading the native library that the image carries inside itself
- Callbacks from WebRTC's threads: negotiation, ICE candidates, tracks and channel messages
- Video frames, audio and statistics that the native code hands to Java
- A log sink, called by WebRTC's logging from any thread
Build it with GraalVM 24 or later as JAVA_HOME, then run it:
mvn -pl webrtc-examples -Pnative package
webrtc-examples/target/webrtc-native-image-exampleFile Transfer
The FileTransferExample connects two peers in one process with the Client API, and sends a file from one to the other with File Transfer. It needs no audio or video hardware and no server.
Key features demonstrated:
- Running
FileTransfersover a data channel on both sides - Accepting an offer under a safe file name, in a directory of the application's choice
- Following the progress the receiver confirms
- Waiting until the receiver checked the SHA-256 of the file and moved it into place
Pass a file to send, or a size in MiB for a file of random data, 20 by default:
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.FileTransferExample" -D"exec.args=50"PeerConnection
The PeerConnectionExample sends the camera and the microphone from one peer to another in the same process, with the Client API, and logs the video frames and audio that arrive.
Key features demonstrated:
- Creating a
WebRtcruntime that captures from and plays to the audio devices - Listing the cameras and microphones
- Opening the camera and the microphone as local tracks
- Connecting two peers through a loopback signaling, in place of a server
- Receiving the remote tracks, their frames and their audio
- Muting a received audio track, while its audio still reaches a handler
This example provides a foundation for building WebRTC applications that need to handle audio and video communication. Press Enter to end the call:
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.PeerConnectionExample"WHEP
The WhepExample implements a client of the WebRTC HTTP Egress Protocol (WHEP), which receives a stream from a server, with the Client API.
Key features demonstrated:
- Receiving a stream from a WHEP endpoint with
Whep - Logging the stream's video frames and audio
- Ending the session, which deletes it on the server
This example is useful for applications that need to receive media streams from WHEP-compatible servers, such as live streaming platforms. The endpoint and the number of seconds to receive are optional:
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.WhepExample" -D"exec.args=http://localhost:8889/mystream/whep 10"Codec List
The CodecListExample demonstrates how to list all supported codecs with the WebRTC peer-connection-factory.
Key features demonstrated:
- Creating a
PeerConnectionFactorywith theHardwareVideoEncoderFactoryandHardwareVideoDecoderFactory - Getting the supported codecs for both sending and receiving audio and video
- Displaying detailed information about each codec
- Listing the video codecs of the hardware factories and marking those the GPU encodes or decodes, with
getHardwareCodecs()
This example is useful for understanding what codecs are available on the current system, which can help with debugging compatibility issues or optimizing media quality.
Desktop Video
The DesktopVideoExample shares a screen or a window from one peer to another in the same process, with the Client API.
Key features demonstrated:
- Listing the screens and windows that can be captured
- Capturing one of them as a local video track, with a frame rate and a largest size
- Connecting two peers through a loopback signaling, in place of a server
- Reading the resolution and frame rate the sender encodes
This example is particularly useful for applications that need to implement screen sharing or remote desktop functionality. It shares the first screen, or the first window with window. Under Wayland, the system asks which one to share instead.
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.DesktopVideoExample"
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.DesktopVideoExample" -D"exec.args=window"System Audio
The SystemAudioExample captures the audio the system plays on its default output with a SystemAudioSource, and counts the frames that reach a sink of its track, without sending or playing anything.
Key features demonstrated:
- Capturing the system output as a local audio track
- Reading the captured audio through a sink of the track
- Stopping the capture and checking it for a failure
Play audio in another application while it runs, and press Enter to stop. On macOS, grant screen and audio recording permission when asked.
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.SystemAudioExample"Screen Share with Audio
The ScreenShareAudioExample shares a screen together with the microphone and the system audio, from one runtime and over one peer, with the Client API.
Key features demonstrated:
- Sending a screen, the microphone and the system output as three tracks of one stream
- Sending a
SystemAudioSourcethroughaudioTrack(), beside the microphone - Telling the received audio tracks apart by their IDs
- Counting received audio without playing it back, which the system audio capture would record again
Each audio sender gets the audio of its own track, so the microphone keeps its echo cancellation and the system output is sent as it plays. Play audio in another application and speak into the microphone while it runs, and press Enter to stop.
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.ScreenShareAudioExample"Desktop Picker
The DesktopPickerExample lets the user choose a screen or window in the operating system's own picker, on macOS 14 and newer and under Wayland.
Key features demonstrated:
- Checking whether the system offers a picker
- Showing the picker by starting a
VideoDesktopSource - Reacting to the end of the track when the picker is cancelled or sharing stops
Desktop Capturer Picker
The DesktopCapturerPickerExample is a small Swing application that takes frames from a ScreenCapturer without a video track, after the user has chosen a screen or window in the operating system's picker.
Key features demonstrated:
- Setting
setUseSystemPicker(true)beforestart(), which shows the picker - Calling
captureFrame()at the frame rate, on the one thread that uses the capturer - Showing the frames in a Swing
VideoView - Telling a pending choice (
ERROR_TEMPORARY) from a cancelled picker or stopped sharing (ERROR_PERMANENT)
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.DesktopCapturerPickerExample"Media File
The MediaFileExample demonstrates how to send a media file over a peer connection, in place of a camera and a microphone. See the Media Files guide for the API it uses.
Key features demonstrated:
- Opening a media file, or a live RTSP stream, with a
MediaFileSource - Reading what the source contains from its
MediaInfo - Sending the media sources it feeds as Client API tracks, with
videoTrackandaudioTrack - Receiving them on a second peer, connected through a loopback signaling
- Following playback through a
MediaPlayerListener - Closing the tracks before the source, which owns what they were made from
This example is useful for applications that stream pre-recorded media, relay an IP camera into WebRTC, or need a dependable stand-in for a camera in testing.
INFO
This example needs the webrtc-java-media module, which builds FFmpeg from the third-party/ffmpeg submodule. Make sure the submodule is checked out before building, as the Media Files guide describes.
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.MediaFileExample" -D"exec.args=movie.mp4"
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.MediaFileExample" -D"exec.args=rtsp://camera.local/stream1"Media File Player
The MediaFilePlayerExample is a Swing application built on the Client API. It sends a media file, or a live RTSP stream, from one peer to another in the same process. It then shows the received video, plays the received audio in sync, and records what is received.
Start and Stop buttons set up and tear down the whole session, and Record starts and stops a recording.
Key features demonstrated:
- Sending the sources of a
MediaFileSourceas client API tracks, withvideoTrackandaudioTrack - Connecting two peers in one application through a loopback signaling
- Using one runtime in the
CUSTOMaudio mode, which sends the file's audio and plays what is received - Running the peers' handlers on the Swing event dispatch thread
- Keeping audio and video in sync by sending both tracks in the same media stream
- Raising the video sender's maximum bitrate once it is connected, so a high-resolution file is not held to a fraction of its size
- Rendering received video in the Swing
VideoViewof thewebrtc-java-swingmodule - Showing the source's format, the playback position, and live receive metrics (codec, resolution, frame rate, bitrate, packet loss, jitter, audio level) read from
onStats - Recording the received tracks into a Matroska file with
MediaRecorder - Closing the peers, then the tracks, then the source on Stop
INFO
Like the Media File example, this one needs the webrtc-java-media module. The source argument is optional; a file can also be chosen from the window, or a stream URL typed into it. A stream URL's credentials are never shown, and the Loop box is disabled while a live stream plays, since it has no length to start over from.
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.MediaFilePlayerExample" -D"exec.args=movie.mp4"JavaFX Camera
The JavaFxCameraExample is a JavaFX application built on the Client API. It sends the camera from one peer to another in the same process, and shows the camera's preview beside the video that arrives.
Key features demonstrated:
- Running the peers' handlers on the JavaFX Application Thread, with
Platform::runLateras the event executor - Showing a local preview and a remote track in the JavaFX
VideoViewof thewebrtc-java-javafxmodule - Mirroring the preview through the view's
mirroredProperty() - Switching the camera while it is being sent, without renegotiating
- Showing the connection state, and the encoded resolution and frame rate read from
onStats - Closing the runtime off the JavaFX thread before JavaFX exits
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.JavaFxCameraExample"Encrypted Recording
The EncryptedRecordingExample encrypts a call end to end with a key provider of End-to-End Encryption, with the Client API, and records what the receiving side decrypted into a media file. See the Encoded Transforms and Media Recording guides for the APIs it uses.
Key features demonstrated:
- Encrypting every encoded audio and video frame in the SFrame format with
e2ee(keys)of the peer options, and decrypting it, from the first frame on - Keeping the codec header of each frame in the clear for the packetizer, and authenticating it instead
- Dropping frames that fail to encrypt or to authenticate, rather than passing them on
- Recording the remote tracks with a
MediaRecorder, without decoding or re-encoding
INFO
Like the Media File example, this one needs the webrtc-java-media module. The output file is optional; its extension picks the container.
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.EncryptedRecordingExample" -D"exec.args=call.mkv"Web Client
The WebClientExample calls a browser from Java over WebSocket signaling, with the Client API. The browser client is served by the example's WebServer.
Key features demonstrated:
- Connecting to a signaling server using WebSockets
- Implementing a
Signalingover the application's own message format, which also names the sender and receiver of each message - Sending generated audio and video, pushed into tracks of the runtime
- Letting the peer negotiate by itself, and starting a new peer whenever the browser joins the room
- Joining and leaving rooms
- Logging the audio and video the browser sends
This example is valuable for applications that need to establish WebRTC connections between Java clients and web browsers, enabling cross-platform real-time communication with audio and video.
INFO
Before running the WebClientExample, you need to start the WebServer first. The WebServer provides the signaling service required for WebRTC connection establishment:
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.web.server.WebServer"And the browser client is reachable at this url: https://localhost:8443
Running the Examples
To run these examples, you need to navigate into the directory webrtc-java/webrtc-example in the project. You can then execute the main method of each example class.
For example, to run the CodecListExample:
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.CodecListExample"Or to run the WebClientExample:
mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.web.WebClientExample"Note that these examples focus on setting up the local components for WebRTC communication. In a real application, you would need to establish a connection with a remote peer through a signaling channel (e.g., WebSocket). The WebClientExample demonstrates this by connecting to a signaling server.
