|
| 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.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.Paths; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Random; |
| 24 | +import java.util.concurrent.CompletableFuture; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +import dev.onvoid.webrtc.client.AudioMode; |
| 28 | +import dev.onvoid.webrtc.client.Peer; |
| 29 | +import dev.onvoid.webrtc.client.PeerOptions; |
| 30 | +import dev.onvoid.webrtc.client.Signaling; |
| 31 | +import dev.onvoid.webrtc.client.SignalingPair; |
| 32 | +import dev.onvoid.webrtc.client.WebRtc; |
| 33 | +import dev.onvoid.webrtc.client.transfer.FileTransfers; |
| 34 | +import dev.onvoid.webrtc.client.transfer.IncomingTransfer; |
| 35 | +import dev.onvoid.webrtc.client.transfer.OutgoingTransfer; |
| 36 | + |
| 37 | +/** |
| 38 | + * Example of file transfer over a data channel ({@code dev.onvoid.webrtc.client.transfer}): two |
| 39 | + * peers in one process, one sends a file, the other saves it. |
| 40 | + * <p> |
| 41 | + * This example shows how to: |
| 42 | + * <ul> |
| 43 | + * <li>Run {@link FileTransfers} over a data channel on both sides</li> |
| 44 | + * <li>Accept an offer under a safe file name in a directory of the application's choice</li> |
| 45 | + * <li>Follow the progress the receiver confirms</li> |
| 46 | + * <li>Wait until the receiver checked the SHA-256 of the file and moved it into place</li> |
| 47 | + * </ul> |
| 48 | + * <p> |
| 49 | + * Pass a file to send, or a size in MiB for a file of random data, 20 by default: |
| 50 | + * <pre> |
| 51 | + * mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.FileTransferExample" -D"exec.args=50" |
| 52 | + * </pre> |
| 53 | + * |
| 54 | + * @author Alex Andres |
| 55 | + */ |
| 56 | +public class FileTransferExample { |
| 57 | + |
| 58 | + public static void main(String[] args) throws Exception { |
| 59 | + Path downloads = Files.createTempDirectory("webrtc-downloads"); |
| 60 | + Path source = source(args.length > 0 ? args[0] : "20"); |
| 61 | + |
| 62 | + try (WebRtc rtc = WebRtc.builder().audio(AudioMode.HEADLESS).build()) { |
| 63 | + // Both peers in one process. A real application signals through its own server. |
| 64 | + SignalingPair link = Signaling.loopback(); |
| 65 | + Peer sender = rtc.createPeer(PeerOptions.builder() |
| 66 | + .name("sender") |
| 67 | + .signaling(link.first()) |
| 68 | + .build()); |
| 69 | + Peer receiver = rtc.createPeer(PeerOptions.builder() |
| 70 | + .name("receiver") |
| 71 | + .signaling(link.second()) |
| 72 | + .polite(true) |
| 73 | + .build()); |
| 74 | + |
| 75 | + CompletableFuture<IncomingTransfer> incoming = new CompletableFuture<>(); |
| 76 | + |
| 77 | + receiver.onChannel(channel -> { |
| 78 | + if (!channel.label().equals("files")) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + FileTransfers.over(channel).onOffer(offer -> { |
| 83 | + // The name comes from the remote peer: never use it as a path as it is. |
| 84 | + System.out.printf("Receiver is offered %s, %d bytes%n", offer.safeFileName(), offer.size()); |
| 85 | + incoming.complete(offer.accept(downloads.resolve(offer.safeFileName()))); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + FileTransfers files = FileTransfers.over(sender.createChannel("files")); |
| 90 | + OutgoingTransfer upload = files.send(source); |
| 91 | + |
| 92 | + upload.onProgress(progress -> System.out.printf("Receiver confirmed %.0f%% (%d of %d bytes)%n", |
| 93 | + 100 * progress.fraction(), progress.bytes(), progress.size())); |
| 94 | + |
| 95 | + // The main thread may wait; a handler must not. |
| 96 | + upload.whenDone().get(60, TimeUnit.SECONDS); |
| 97 | + |
| 98 | + IncomingTransfer received = incoming.get(5, TimeUnit.SECONDS); |
| 99 | + received.whenDone().get(5, TimeUnit.SECONDS); |
| 100 | + |
| 101 | + boolean same = Arrays.equals(Files.readAllBytes(source), Files.readAllBytes(received.file())); |
| 102 | + |
| 103 | + System.out.println("Receiver saved " + received.file() + (same ? ", identical to " : ", differing from ") |
| 104 | + + source); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return the file named, or a new file of random data of that many MiB. |
| 110 | + */ |
| 111 | + private static Path source(String argument) throws Exception { |
| 112 | + Path file = Paths.get(argument); |
| 113 | + |
| 114 | + if (Files.isRegularFile(file)) { |
| 115 | + return file; |
| 116 | + } |
| 117 | + |
| 118 | + byte[] data = new byte[Integer.parseInt(argument) * 1024 * 1024]; |
| 119 | + new Random().nextBytes(data); |
| 120 | + |
| 121 | + Path random = Files.createTempFile("webrtc-upload", ".bin"); |
| 122 | + random.toFile().deleteOnExit(); |
| 123 | + return Files.write(random, data); |
| 124 | + } |
| 125 | +} |
0 commit comments