David Liu

better simulcast dimension calculations

@@ -14,6 +14,7 @@ import livekit.LivekitModels @@ -14,6 +14,7 @@ import livekit.LivekitModels
14 import livekit.LivekitRtc 14 import livekit.LivekitRtc
15 import org.webrtc.* 15 import org.webrtc.*
16 import kotlin.math.abs 16 import kotlin.math.abs
  17 +import kotlin.math.roundToInt
17 18
18 class LocalParticipant 19 class LocalParticipant
19 @AssistedInject 20 @AssistedInject
@@ -206,13 +207,46 @@ internal constructor( @@ -206,13 +207,46 @@ internal constructor(
206 val midPreset = presets[1] 207 val midPreset = presets[1]
207 val lowPreset = presets[0] 208 val lowPreset = presets[0]
208 209
  210 + fun calculateScale(parameter: VideoCaptureParameter): Double {
  211 + return height / parameter.height.toDouble()
  212 + }
  213 +
  214 + fun checkEvenDimensions(parameter: VideoCaptureParameter): Boolean {
  215 + fun isEven(value: Double) = ((value.roundToInt()) % 2 == 0)
  216 + val scale = calculateScale(parameter)
  217 +
  218 + return isEven(parameter.height * scale) && isEven(parameter.width * scale)
  219 + }
  220 +
209 // if resolution is high enough, we send both h and q res. 221 // if resolution is high enough, we send both h and q res.
210 // otherwise only send h 222 // otherwise only send h
211 if (width >= 960) { 223 if (width >= 960) {
212 - encodings.add(midPreset.encoding.toRtpEncoding("h", 2.0))  
213 - encodings.add(lowPreset.encoding.toRtpEncoding("q", 4.0)) 224 + val hasEvenDimensions =
  225 + checkEvenDimensions(midPreset.capture) && checkEvenDimensions(lowPreset.capture)
  226 + val midScale = if (hasEvenDimensions) calculateScale(midPreset.capture) else 2.0
  227 + val lowScale = if (hasEvenDimensions) calculateScale(lowPreset.capture) else 4.0
  228 +
  229 + encodings.add(
  230 + midPreset.encoding.toRtpEncoding(
  231 + "h",
  232 + midScale
  233 + )
  234 + )
  235 + encodings.add(
  236 + lowPreset.encoding.toRtpEncoding(
  237 + "q",
  238 + lowScale
  239 + )
  240 + )
214 } else { 241 } else {
215 - encodings.add(lowPreset.encoding.toRtpEncoding("h", 2.0)) 242 + val hasEvenDimensions = checkEvenDimensions(lowPreset.capture)
  243 + val lowScale = if (hasEvenDimensions) calculateScale(lowPreset.capture) else 2.0
  244 + encodings.add(
  245 + lowPreset.encoding.toRtpEncoding(
  246 + "h",
  247 + lowScale
  248 + )
  249 + )
216 } 250 }
217 } else { 251 } else {
218 encodings.add(encoding.toRtpEncoding()) 252 encodings.add(encoding.toRtpEncoding())