davidliu

undo even scale changes

WebRTC m97 handles odd dimensions
@@ -14,7 +14,6 @@ import io.livekit.android.room.DefaultsManager @@ -14,7 +14,6 @@ import io.livekit.android.room.DefaultsManager
14 import io.livekit.android.room.RTCEngine 14 import io.livekit.android.room.RTCEngine
15 import io.livekit.android.room.track.* 15 import io.livekit.android.room.track.*
16 import io.livekit.android.room.util.EncodingUtils 16 import io.livekit.android.room.util.EncodingUtils
17 -import io.livekit.android.room.util.EncodingUtils.findEvenScaleDownBy  
18 import io.livekit.android.util.LKLog 17 import io.livekit.android.util.LKLog
19 import kotlinx.coroutines.CoroutineDispatcher 18 import kotlinx.coroutines.CoroutineDispatcher
20 import kotlinx.coroutines.cancel 19 import kotlinx.coroutines.cancel
@@ -323,7 +322,6 @@ internal constructor( @@ -323,7 +322,6 @@ internal constructor(
323 val midPreset = presets[1] 322 val midPreset = presets[1]
324 val lowPreset = presets[0] 323 val lowPreset = presets[0]
325 324
326 -  
327 fun addEncoding(videoEncoding: VideoEncoding, scale: Double) { 325 fun addEncoding(videoEncoding: VideoEncoding, scale: Double) {
328 if (encodings.size >= EncodingUtils.VIDEO_RIDS.size) { 326 if (encodings.size >= EncodingUtils.VIDEO_RIDS.size) {
329 throw IllegalStateException("Attempting to add more encodings than we have rids for!") 327 throw IllegalStateException("Attempting to add more encodings than we have rids for!")
@@ -336,18 +334,19 @@ internal constructor( @@ -336,18 +334,19 @@ internal constructor(
336 // if resolution is high enough, we send both h and q res. 334 // if resolution is high enough, we send both h and q res.
337 // otherwise only send h 335 // otherwise only send h
338 val size = max(width, height) 336 val size = max(width, height)
  337 +
  338 + fun calculateScaleDown(captureParam: VideoCaptureParameter): Double {
  339 + val targetSize = max(captureParam.width, captureParam.height)
  340 + return size / targetSize.toDouble()
  341 + }
339 if (size >= 960) { 342 if (size >= 960) {
340 - var lowScale = findEvenScaleDownBy(width, height, lowPreset.capture.width, lowPreset.capture.height)  
341 - var midScale = findEvenScaleDownBy(width, height, midPreset.capture.width, midPreset.capture.height) 343 + val lowScale = calculateScaleDown(lowPreset.capture)
  344 + val midScale = calculateScaleDown(midPreset.capture)
342 345
343 - if (midScale == null || lowScale == null) {  
344 - lowScale = 4.0  
345 - midScale = 2.0  
346 - }  
347 addEncoding(lowPreset.encoding, lowScale) 346 addEncoding(lowPreset.encoding, lowScale)
348 addEncoding(midPreset.encoding, midScale) 347 addEncoding(midPreset.encoding, midScale)
349 } else { 348 } else {
350 - val lowScale = findEvenScaleDownBy(width, height, lowPreset.capture.width, lowPreset.capture.height) ?: 2.0 349 + val lowScale = calculateScaleDown(lowPreset.capture)
351 addEncoding(lowPreset.encoding, lowScale) 350 addEncoding(lowPreset.encoding, lowScale)
352 } 351 }
353 addEncoding(encoding, 1.0) 352 addEncoding(encoding, 1.0)
@@ -32,39 +32,6 @@ internal object EncodingUtils { @@ -32,39 +32,6 @@ internal object EncodingUtils {
32 VideoPreset43.FHD 32 VideoPreset43.FHD
33 ) 33 )
34 34
35 -  
36 - /**  
37 - * Encoders will often not be able to handle odd dimensions, so we should try to find a scale that will  
38 - * result in even dimensions.  
39 - *  
40 - * @return a scale that will result in dimensions that are both even, or null if none found.  
41 - */  
42 - fun findEvenScaleDownBy(  
43 - sourceWidth: Int,  
44 - sourceHeight: Int,  
45 - targetWidth: Int,  
46 - targetHeight: Int,  
47 - ): Double? {  
48 - fun Int.isEven() = this % 2 == 0  
49 -  
50 - val sourceSize = min(sourceWidth, sourceHeight)  
51 - val targetSize = min(targetWidth, targetHeight)  
52 - for (i in 0..20) {  
53 - val scaleDownBy = sourceSize.toDouble() / (targetSize + i)  
54 - // Internally, WebRTC casts directly to int without rounding.  
55 - // https://github.com/webrtc-sdk/webrtc/blob/8c7139f8e6fa19ddf2c91510c177a19746e1ded3/media/engine/webrtc_video_engine.cc#L3676  
56 - val scaledWidth = (sourceWidth / scaleDownBy).toInt()  
57 - val scaledHeight = (sourceHeight / scaleDownBy).toInt()  
58 -  
59 - if (scaledHeight.isEven() && scaledWidth.isEven()) {  
60 - return scaleDownBy  
61 - }  
62 - }  
63 -  
64 - return null  
65 - }  
66 -  
67 -  
68 fun determineAppropriateEncoding(width: Int, height: Int): VideoEncoding { 35 fun determineAppropriateEncoding(width: Int, height: Int): VideoEncoding {
69 val presets = presetsForResolution(width, height) 36 val presets = presetsForResolution(width, height)
70 37
1 package io.livekit.android.room.util 1 package io.livekit.android.room.util
2 2
3 -import org.junit.Assert  
4 -import org.junit.Test  
5 -  
6 class EncodingUtilsTest { 3 class EncodingUtilsTest {
7 - @Test  
8 - fun evenScale() {  
9 - fun Int.isEven() = this % 2 == 0  
10 -  
11 - val sourceWidth = 800  
12 - val sourceHeight = 600  
13 - val scaleDownBy = EncodingUtils.findEvenScaleDownBy(sourceWidth, sourceHeight, 240, 180)  
14 - ?: throw Exception("scale should not be null!")  
15 -  
16 - Assert.assertTrue((sourceWidth / scaleDownBy).toInt().isEven())  
17 - Assert.assertTrue((sourceHeight / scaleDownBy).toInt().isEven())  
18 - }  
19 -  
20 - @Test  
21 - fun evenScaleWeirdSource() {  
22 - fun Int.isEven() = this % 2 == 0  
23 -  
24 - val sourceWidth = 800  
25 - val sourceHeight = 602  
26 - val scaleDownBy = EncodingUtils.findEvenScaleDownBy(sourceWidth, sourceHeight, 240, 180)  
27 - ?: throw Exception("scale should not be null!")  
28 -  
29 - Assert.assertTrue((sourceWidth / scaleDownBy).toInt().isEven())  
30 - Assert.assertTrue((sourceHeight / scaleDownBy).toInt().isEven())  
31 - }  
32 } 4 }