|
| 1 | +/* |
| 2 | + * Copyright 2026 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; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.concurrent.ConcurrentHashMap; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | +import java.util.concurrent.atomic.AtomicLong; |
| 26 | + |
| 27 | +import dev.onvoid.webrtc.client.AudioChunk; |
| 28 | +import dev.onvoid.webrtc.client.LocalAudio; |
| 29 | +import dev.onvoid.webrtc.client.LocalVideo; |
| 30 | +import dev.onvoid.webrtc.client.Peer; |
| 31 | +import dev.onvoid.webrtc.client.PeerOptions; |
| 32 | +import dev.onvoid.webrtc.client.RemoteAudio; |
| 33 | +import dev.onvoid.webrtc.client.ScreenOptions; |
| 34 | +import dev.onvoid.webrtc.client.Signaling; |
| 35 | +import dev.onvoid.webrtc.client.SignalingPair; |
| 36 | +import dev.onvoid.webrtc.client.WebRtc; |
| 37 | +import dev.onvoid.webrtc.media.audio.SystemAudioSource; |
| 38 | +import dev.onvoid.webrtc.media.video.desktop.DesktopSource; |
| 39 | + |
| 40 | +/** |
| 41 | + * Example demonstrating how to share a screen together with the microphone and |
| 42 | + * the audio the system plays, from one runtime and over one peer, with the |
| 43 | + * client API. |
| 44 | + * <p> |
| 45 | + * This example shows how to: |
| 46 | + * <ul> |
| 47 | + * <li>Capture a screen, the microphone and the system output as three tracks</li> |
| 48 | + * <li>Send the system output through a {@link SystemAudioSource}, beside the microphone</li> |
| 49 | + * <li>Put all three tracks into one stream, so the receiver keeps them in sync</li> |
| 50 | + * <li>Tell the received audio tracks apart by their IDs</li> |
| 51 | + * <li>Stop the system audio capture and check it for a failure</li> |
| 52 | + * </ul> |
| 53 | + * <p> |
| 54 | + * Each audio sender gets the audio of its own track: the microphone, with echo |
| 55 | + * cancellation, noise suppression and gain control, on one, and the system |
| 56 | + * output, unprocessed, on the other. Both peers run in this process, so the |
| 57 | + * call connects without a server. The viewer does not play what it receives: |
| 58 | + * on Windows and Linux the system audio capture would record that playout and |
| 59 | + * send it again. |
| 60 | + * <p> |
| 61 | + * Play audio in another application, speak into the microphone, run the |
| 62 | + * example and press Enter to stop. On macOS, grant screen and audio recording |
| 63 | + * permission when asked: |
| 64 | + * <pre> |
| 65 | + * mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.ScreenShareAudioExample" |
| 66 | + * </pre> |
| 67 | + * |
| 68 | + * @author Alex Andres |
| 69 | + */ |
| 70 | +public class ScreenShareAudioExample { |
| 71 | + |
| 72 | + /** A 16-bit sample above this is counted as sound rather than silence. */ |
| 73 | + private static final int AUDIBLE = 300; |
| 74 | + |
| 75 | + |
| 76 | + public static void main(String[] args) throws Exception { |
| 77 | + // AudioMode.DEVICES, the default: the microphone is captured, and pushed |
| 78 | + // audio such as the system output can be sent beside it. |
| 79 | + WebRtc rtc = WebRtc.create(); |
| 80 | + SystemAudioSource systemOutput = null; |
| 81 | + |
| 82 | + try { |
| 83 | + List<DesktopSource> screens = rtc.media().devices().screens(); |
| 84 | + |
| 85 | + if (screens.isEmpty()) { |
| 86 | + System.out.println("There is no screen to share"); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + DesktopSource source = screens.get(0); |
| 91 | + LocalVideo screen = rtc.media().screen(ScreenOptions.screen(source) |
| 92 | + .frameRate(30) |
| 93 | + .maxSize(1920, 1080) |
| 94 | + .build()); |
| 95 | + LocalAudio microphone = rtc.media().microphone(); |
| 96 | + |
| 97 | + // The source stays the application's: it must outlive its track, |
| 98 | + // which the runtime closes. |
| 99 | + systemOutput = new SystemAudioSource(); |
| 100 | + LocalAudio systemAudio = rtc.media().audioTrack("system-audio", systemOutput); |
| 101 | + |
| 102 | + SignalingPair signaling = Signaling.loopback(); |
| 103 | + |
| 104 | + Peer sharer = rtc.createPeer(PeerOptions.builder() |
| 105 | + .name("sharer") |
| 106 | + .signaling(signaling.first()) |
| 107 | + .build()); |
| 108 | + Peer viewer = rtc.createPeer(PeerOptions.builder() |
| 109 | + .name("viewer") |
| 110 | + .polite(true) |
| 111 | + .signaling(signaling.second()) |
| 112 | + .build()); |
| 113 | + |
| 114 | + AtomicInteger frames = new AtomicInteger(); |
| 115 | + Map<String, AtomicLong> audibleChunks = new ConcurrentHashMap<>(); |
| 116 | + |
| 117 | + viewer.onTrack(track -> { |
| 118 | + if (track.isVideo()) { |
| 119 | + track.asVideo().onFrame(frame -> frames.incrementAndGet()); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + String name = track.id().equals(systemAudio.id()) ? "system audio" : "microphone"; |
| 124 | + AtomicLong audible = audibleChunks.computeIfAbsent(name, key -> new AtomicLong()); |
| 125 | + RemoteAudio audio = track.asAudio(); |
| 126 | + |
| 127 | + // Count the audio instead of playing it, which the system audio |
| 128 | + // capture would pick up again. |
| 129 | + audio.setVolume(0); |
| 130 | + audio.onAudio(chunk -> { |
| 131 | + if (isAudible(chunk)) { |
| 132 | + audible.incrementAndGet(); |
| 133 | + } |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + // One stream for all three tracks, so the viewer lines them up. |
| 138 | + sharer.addTrack(screen, "presentation"); |
| 139 | + sharer.addTrack(microphone, "presentation"); |
| 140 | + sharer.addTrack(systemAudio, "presentation"); |
| 141 | + |
| 142 | + // Waits for the platform to open the output, and on macOS for the |
| 143 | + // permission prompt. An application with a user interface calls it |
| 144 | + // on a background thread. |
| 145 | + systemOutput.start(); |
| 146 | + |
| 147 | + // The main thread may wait; a handler must not. |
| 148 | + sharer.whenConnected().get(15, TimeUnit.SECONDS); |
| 149 | + |
| 150 | + System.out.println("Sharing " + source.title + " with the microphone and the system audio"); |
| 151 | + |
| 152 | + sharer.onStats(Duration.ofSeconds(5), stats -> { |
| 153 | + System.out.printf("Viewer got %d frames, audible 10 ms chunks: %s%n", |
| 154 | + frames.get(), audibleChunks); |
| 155 | + }); |
| 156 | + |
| 157 | + System.out.println("Press Enter to stop sharing..."); |
| 158 | + System.in.read(); |
| 159 | + |
| 160 | + systemOutput.stop(); |
| 161 | + |
| 162 | + if (systemOutput.getFailure() != null) { |
| 163 | + System.out.println("System audio capture failed: " + systemOutput.getFailure()); |
| 164 | + } |
| 165 | + } |
| 166 | + finally { |
| 167 | + // Closing the runtime closed the peers and the tracks, so the system |
| 168 | + // audio source can go now. |
| 169 | + rtc.close(); |
| 170 | + |
| 171 | + if (systemOutput != null) { |
| 172 | + systemOutput.close(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + System.out.println("Closed"); |
| 177 | + } |
| 178 | + |
| 179 | + private static boolean isAudible(AudioChunk chunk) { |
| 180 | + byte[] samples = chunk.samples(); |
| 181 | + |
| 182 | + for (int i = 0; i + 1 < samples.length; i += 2) { |
| 183 | + // 16-bit little-endian PCM. |
| 184 | + int sample = (short) ((samples[i] & 0xff) | (samples[i + 1] << 8)); |
| 185 | + |
| 186 | + if (Math.abs(sample) > AUDIBLE) { |
| 187 | + return true; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return false; |
| 192 | + } |
| 193 | +} |
0 commit comments