|
| 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.client; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Locale; |
| 21 | + |
| 22 | +/** An explicit export schema: never serializes raw stats or arbitrary string attributes. */ |
| 23 | +final class DiagnosticExport { |
| 24 | + private DiagnosticExport() { } |
| 25 | + |
| 26 | + static String json(PeerStats stats) { |
| 27 | + StringBuilder out = new StringBuilder("{\"schemaVersion\":1"); |
| 28 | + field(out, "timestamp", stats.timestamp().toString()); |
| 29 | + field(out, "state", stats.diagnostics().state().name()); |
| 30 | + field(out, "os", platform(System.getProperty("os.name", ""))); |
| 31 | + field(out, "architecture", allowed(System.getProperty("os.arch", ""), |
| 32 | + "amd64", "x86_64", "aarch64", "arm", "x86", "i386")); |
| 33 | + String javaVersion = System.getProperty("java.specification.version", ""); |
| 34 | + field(out, "javaVersion", javaVersion.matches("[0-9]{1,3}(\\.[0-9]{1,3})?") ? javaVersion : "other"); |
| 35 | + String version = PeerStats.class.getPackage().getImplementationVersion(); |
| 36 | + field(out, "libraryVersion", version != null && version.matches("[0-9]+\\.[0-9]+\\.[0-9]+(-SNAPSHOT)?") |
| 37 | + ? version : "unversioned"); |
| 38 | + number(out, "availableProcessors", Runtime.getRuntime().availableProcessors()); |
| 39 | + number(out, "bytesSent", stats.bytesSent()); |
| 40 | + number(out, "bytesReceived", stats.bytesReceived()); |
| 41 | + number(out, "outgoingBitrate", stats.outgoingBitrate()); |
| 42 | + number(out, "incomingBitrate", stats.incomingBitrate()); |
| 43 | + number(out, "packetLoss", stats.packetLoss()); |
| 44 | + number(out, "recentPacketLoss", stats.recentPacketLoss()); |
| 45 | + out.append(",\"policy\":{\"confirmationSeconds\":").append(DiagnosticsReader.WINDOW.getSeconds()); |
| 46 | + number(out, "minimumMeasurements", DiagnosticsReader.MIN_SAMPLES); |
| 47 | + number(out, "lossEnter", DiagnosticsReader.LOSS_ENTER); |
| 48 | + number(out, "lossExit", DiagnosticsReader.LOSS_EXIT); |
| 49 | + number(out, "maximumGapSeconds", DiagnosticsReader.MAX_GAP.getSeconds()); |
| 50 | + out.append('}'); |
| 51 | + out.append(",\"conditions\":["); |
| 52 | + boolean comma = false; |
| 53 | + for (QualityCondition condition : stats.diagnostics().conditions()) { |
| 54 | + if (comma) { |
| 55 | + out.append(','); |
| 56 | + } |
| 57 | + out.append("{\"cause\":"); |
| 58 | + Json.writeString(out, condition.name()); |
| 59 | + field(out, "reason", condition.reason()); |
| 60 | + out.append(",\"problem\":").append(condition.isProblem()).append('}'); |
| 61 | + comma = true; |
| 62 | + } |
| 63 | + out.append("],\"transports\":["); |
| 64 | + comma = false; |
| 65 | + for (TransportStats transport : stats.transports()) { |
| 66 | + if (comma) { |
| 67 | + out.append(','); |
| 68 | + } |
| 69 | + out.append("{\"relayed\":").append(transport.relayed()); |
| 70 | + field(out, "candidateType", allowed(transport.candidateType(), "host", "srflx", "prflx", "relay")); |
| 71 | + number(out, "rttSeconds", transport.roundTripTime() == null ? null |
| 72 | + : transport.roundTripTime().toNanos() / 1e9); |
| 73 | + out.append('}'); |
| 74 | + comma = true; |
| 75 | + } |
| 76 | + out.append("],\"video\":["); |
| 77 | + comma = false; |
| 78 | + for (boolean outbound : new boolean[] { false, true }) { |
| 79 | + for (VideoStats video : outbound ? stats.outboundVideo() : stats.inboundVideo()) { |
| 80 | + if (comma) { |
| 81 | + out.append(','); |
| 82 | + } |
| 83 | + out.append("{\"outbound\":").append(outbound); |
| 84 | + field(out, "codec", codec(video.codec())); |
| 85 | + number(out, "width", video.width()); |
| 86 | + number(out, "height", video.height()); |
| 87 | + number(out, "framesPerSecond", video.framesPerSecond()); |
| 88 | + field(out, "qualityLimitation", allowed(video.qualityLimitation(), "none", "cpu", "bandwidth", "other")); |
| 89 | + out.append('}'); |
| 90 | + comma = true; |
| 91 | + } |
| 92 | + } |
| 93 | + out.append("],\"audio\":["); |
| 94 | + comma = false; |
| 95 | + for (boolean outbound : new boolean[] { false, true }) { |
| 96 | + for (AudioStats audio : outbound ? stats.outboundAudio() : stats.inboundAudio()) { |
| 97 | + if (comma) { |
| 98 | + out.append(','); |
| 99 | + } |
| 100 | + out.append("{\"outbound\":").append(outbound); |
| 101 | + field(out, "codec", codec(audio.codec())); |
| 102 | + number(out, "packetsReceived", audio.packetsReceived()); |
| 103 | + number(out, "jitterSeconds", audio.jitter() == null ? null : audio.jitter().toNanos() / 1e9); |
| 104 | + number(out, "concealedSamples", audio.concealedSamples()); |
| 105 | + number(out, "concealmentEvents", audio.concealmentEvents()); |
| 106 | + number(out, "audioLevel", audio.audioLevel()); |
| 107 | + number(out, "totalSamplesReceived", audio.totalSamplesReceived()); |
| 108 | + number(out, "pcmCallbacks", audio.pcmCallbacks()); |
| 109 | + out.append('}'); |
| 110 | + comma = true; |
| 111 | + } |
| 112 | + } |
| 113 | + return out.append("]}").toString(); |
| 114 | + } |
| 115 | + |
| 116 | + private static String codec(String value) { |
| 117 | + if (value == null) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + return allowed(value.toLowerCase(Locale.ROOT), "audio/opus", "audio/pcmu", "audio/pcma", |
| 121 | + "audio/g722", "audio/isac", "audio/cn", "audio/telephone-event", "audio/red", |
| 122 | + "video/vp8", "video/vp9", "video/h264", "video/h265", "video/av1", "video/rtx", |
| 123 | + "video/red", "video/ulpfec", "video/flexfec-03"); |
| 124 | + } |
| 125 | + |
| 126 | + private static String allowed(String value, String... allowed) { |
| 127 | + return value == null ? null : Arrays.asList(allowed).contains(value) ? value : "other"; |
| 128 | + } |
| 129 | + |
| 130 | + private static String platform(String name) { |
| 131 | + if (name.startsWith("Windows")) { |
| 132 | + return "Windows"; |
| 133 | + } |
| 134 | + if (name.equals("Mac OS X")) { |
| 135 | + return "macOS"; |
| 136 | + } |
| 137 | + return name.equals("Linux") ? "Linux" : "other"; |
| 138 | + } |
| 139 | + |
| 140 | + private static void field(StringBuilder out, String name, String value) { |
| 141 | + out.append(','); |
| 142 | + Json.writeString(out, name); |
| 143 | + out.append(':'); |
| 144 | + if (value == null) { |
| 145 | + out.append("null"); |
| 146 | + } |
| 147 | + else Json.writeString(out, value); |
| 148 | + } |
| 149 | + |
| 150 | + private static void number(StringBuilder out, String name, Number value) { |
| 151 | + out.append(','); |
| 152 | + Json.writeString(out, name); |
| 153 | + out.append(':'); |
| 154 | + out.append(value == null || !Double.isFinite(value.doubleValue()) ? "null" : value.toString()); |
| 155 | + } |
| 156 | +} |
0 commit comments