|
| 1 | +package com.piasy.kmp.webrtc.utils |
| 2 | + |
| 3 | +import com.piasy.kmp.webrtc.PeerConnectionClientFactory |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by Piasy{github.com/Piasy} on 2019-11-29. |
| 7 | + */ |
| 8 | +object SdpManipulator { |
| 9 | + private val DISABLE_CVO_FILTER: (String) -> Boolean = { |
| 10 | + !it.contains("urn:3gpp:video-orientation") |
| 11 | + } |
| 12 | + |
| 13 | + fun preferCodecs( |
| 14 | + sdp: String, |
| 15 | + codecTypes: List<Int>, |
| 16 | + video: Boolean |
| 17 | + ): String { |
| 18 | + val hasAv1x = sdp.contains("AV1X/") |
| 19 | + val av1CodecName = if (hasAv1x) "AV1X" else "AV1" |
| 20 | + |
| 21 | + val preferredCodecs = LinkedHashSet<String>() |
| 22 | + for (codec in codecTypes) { |
| 23 | + preferredCodecs.add(codecName(codec, av1CodecName)) |
| 24 | + } |
| 25 | + preferredCodecs.add("red") |
| 26 | + preferredCodecs.add("ulpfec") |
| 27 | + preferredCodecs.add("flexfec-03") |
| 28 | + |
| 29 | + return preferCodec(sdp, preferredCodecs, video) |
| 30 | + } |
| 31 | + |
| 32 | + fun disableCVO(sdp: String): String { |
| 33 | + val lines = sdp.split("(\r\n|\n)".toRegex()) |
| 34 | + .dropLastWhile { it.isEmpty() } |
| 35 | + .filter(DISABLE_CVO_FILTER) |
| 36 | + return joinString(lines.filter(DISABLE_CVO_FILTER), "\r\n", true) |
| 37 | + } |
| 38 | + |
| 39 | + private fun codecName(codec: Int, av1CodecName: String = "AV1") = when (codec) { |
| 40 | + PeerConnectionClientFactory.VIDEO_CODEC_VP8 -> "VP8" |
| 41 | + PeerConnectionClientFactory.VIDEO_CODEC_VP9 -> "VP9" |
| 42 | + PeerConnectionClientFactory.VIDEO_CODEC_H264_BASELINE, PeerConnectionClientFactory.VIDEO_CODEC_H264_HIGH_PROFILE -> "H264" |
| 43 | + PeerConnectionClientFactory.VIDEO_CODEC_H265 -> "H265" |
| 44 | + PeerConnectionClientFactory.VIDEO_CODEC_AV1 -> av1CodecName |
| 45 | + else -> "UNKNOWN" |
| 46 | + } |
| 47 | + |
| 48 | + private fun preferCodec( |
| 49 | + originalSdp: String, |
| 50 | + preferredCodecs: LinkedHashSet<String>, |
| 51 | + video: Boolean |
| 52 | + ): String { |
| 53 | + val lines = originalSdp.split("(\r\n|\n)".toRegex()) |
| 54 | + .dropLastWhile { it.isEmpty() } |
| 55 | + val newLines = ArrayList<String>() |
| 56 | + |
| 57 | + var audioMLineIndex = -1 |
| 58 | + var videoMLineIndex = -1 |
| 59 | + var processingAudioSection = false |
| 60 | + var processingVideoSection = false |
| 61 | + // <codecName, payloadType> |
| 62 | + val preferredPayloadTypes = HashMap<String, ArrayList<String>>() |
| 63 | + for (i in lines.indices) { |
| 64 | + val line = lines[i] |
| 65 | + // we only check it for rtpmap/rtcp-fb/fmtp, and they only exist in audio/video section. |
| 66 | + val processingRightSection = |
| 67 | + (video && processingVideoSection) || (!video && processingAudioSection) |
| 68 | + if (line.startsWith("a=rtpmap:")) { |
| 69 | + val payloadType = line.split(" ")[0].split(":")[1] |
| 70 | + val codecName = line.split(" ")[1].split("/")[0] |
| 71 | + // match our preferred codec? |
| 72 | + val codecPreferred = preferredCodecs.contains(codecName) |
| 73 | + // is rtx for our preferred codec? |
| 74 | + val rtxPreferred = codecName == "rtx" |
| 75 | + && containsValue(preferredPayloadTypes, lines[i + 1].split("apt=")[1]) |
| 76 | + if (codecPreferred || rtxPreferred) { |
| 77 | + putEntry(preferredPayloadTypes, codecName, payloadType) |
| 78 | + } else if (processingRightSection) { |
| 79 | + continue |
| 80 | + } |
| 81 | + } else if (line.startsWith("a=rtcp-fb:") || line.startsWith("a=fmtp:")) { |
| 82 | + val payloadType = line.split(" ")[0].split(":")[1] |
| 83 | + if (processingRightSection && !containsValue(preferredPayloadTypes, payloadType)) { |
| 84 | + continue |
| 85 | + } |
| 86 | + } else if (line.startsWith("m=audio")) { |
| 87 | + audioMLineIndex = newLines.size |
| 88 | + processingAudioSection = true |
| 89 | + processingVideoSection = false |
| 90 | + } else if (line.startsWith("m=video")) { |
| 91 | + videoMLineIndex = newLines.size |
| 92 | + processingAudioSection = false |
| 93 | + processingVideoSection = true |
| 94 | + } |
| 95 | + newLines.add(line) |
| 96 | + } |
| 97 | + |
| 98 | + if (!video && audioMLineIndex != -1) { |
| 99 | + newLines[audioMLineIndex] = changeMLine( |
| 100 | + newLines[audioMLineIndex], |
| 101 | + preferredCodecs, |
| 102 | + preferredPayloadTypes |
| 103 | + ) |
| 104 | + } |
| 105 | + if (video && videoMLineIndex != -1) { |
| 106 | + newLines[videoMLineIndex] = changeMLine( |
| 107 | + newLines[videoMLineIndex], |
| 108 | + preferredCodecs, |
| 109 | + preferredPayloadTypes |
| 110 | + ) |
| 111 | + } |
| 112 | + return joinString(newLines.filter(DISABLE_CVO_FILTER), "\r\n", true) |
| 113 | + } |
| 114 | + |
| 115 | + private fun containsValue( |
| 116 | + payloadTypes: HashMap<String, ArrayList<String>>, |
| 117 | + value: String |
| 118 | + ): Boolean { |
| 119 | + for (v in payloadTypes.values) { |
| 120 | + for (s in v) { |
| 121 | + if (s == value) { |
| 122 | + return true |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + return false |
| 127 | + } |
| 128 | + |
| 129 | + private fun putEntry( |
| 130 | + payloadTypes: HashMap<String, ArrayList<String>>, |
| 131 | + key: String, |
| 132 | + value: String |
| 133 | + ) { |
| 134 | + var payload = payloadTypes[key] |
| 135 | + if (payload == null) { |
| 136 | + payload = ArrayList() |
| 137 | + payloadTypes[key] = payload |
| 138 | + } |
| 139 | + payload.add(value) |
| 140 | + } |
| 141 | + |
| 142 | + private fun changeMLine( |
| 143 | + mLine: String, |
| 144 | + preferredCodecs: LinkedHashSet<String>, |
| 145 | + preferredPayloadTypes: HashMap<String, ArrayList<String>> |
| 146 | + ): String { |
| 147 | + val oldMLineParts = mLine.split(" ") |
| 148 | + val mLineHeader = oldMLineParts.subList(0, 3) |
| 149 | + |
| 150 | + val newMLineParts = ArrayList(mLineHeader) |
| 151 | + for (preferredCodec in preferredCodecs) { |
| 152 | + val payload = preferredPayloadTypes[preferredCodec] |
| 153 | + if (payload != null) { |
| 154 | + newMLineParts.addAll(payload) |
| 155 | + } |
| 156 | + } |
| 157 | + val rtxPayload = preferredPayloadTypes["rtx"] |
| 158 | + if (rtxPayload != null) { |
| 159 | + newMLineParts.addAll(rtxPayload) |
| 160 | + } |
| 161 | + return joinString(newMLineParts, " ", false) |
| 162 | + } |
| 163 | + |
| 164 | + private fun joinString( |
| 165 | + strings: List<String>, |
| 166 | + delimiter: String, |
| 167 | + delimiterAtEnd: Boolean |
| 168 | + ): String { |
| 169 | + val iterator = strings.iterator() |
| 170 | + if (!iterator.hasNext()) { |
| 171 | + return "" |
| 172 | + } |
| 173 | + val builder = StringBuilder(iterator.next()) |
| 174 | + while (iterator.hasNext()) { |
| 175 | + builder.append(delimiter) |
| 176 | + .append(iterator.next()) |
| 177 | + } |
| 178 | + if (delimiterAtEnd) { |
| 179 | + builder.append(delimiter) |
| 180 | + } |
| 181 | + return builder.toString() |
| 182 | + } |
| 183 | +} |
0 commit comments