-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): move logic to OpusControlHeader
- Loading branch information
1 parent
d620396
commit 6a604e3
Showing
5 changed files
with
77 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...in/java/io/github/thibaultbee/streampack/internal/muxers/ts/utils/av/OpusControlHeader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.github.thibaultbee.streampack.internal.muxers.ts.utils.av | ||
|
||
import io.github.thibaultbee.streampack.internal.utils.av.buffer.ByteBufferWriter | ||
import io.github.thibaultbee.streampack.internal.utils.extensions.put | ||
import io.github.thibaultbee.streampack.internal.utils.extensions.shl | ||
import java.nio.ByteBuffer | ||
import kotlin.experimental.and | ||
import kotlin.math.min | ||
|
||
data class OpusControlHeader( | ||
private val prefix: Short = 0x3FF, | ||
private val payloadSize: Int, | ||
private val startTrim: Short? = null, | ||
private val endTrim: Short? = null, | ||
) : ByteBufferWriter() { | ||
override val size = | ||
3 + payloadSize / UByte.MAX_VALUE.toInt() + (startTrim?.let { 2 } | ||
?: 0) + (endTrim?.let { 2 } ?: 0) | ||
|
||
override fun write(output: ByteBuffer) { | ||
// control_header_prefix 11 bits (0x3FF or 01111111111) | ||
// start_trim_flag 1 bit (0) | ||
// end_trim_flag 1 bit (0) | ||
// control_extension_flag 1 bit (0) | ||
// reserved 2 bits (0) | ||
output.putShort(((prefix shl 5) | ||
or ((startTrim?.let { 1 } ?: 0) shl 4) | ||
or ((endTrim?.let { 1 } ?: 0) shl 3)).toShort()) | ||
|
||
var n = payloadSize | ||
while (n >= 0) { | ||
output.put(min(n, UByte.MAX_VALUE.toInt())) | ||
n -= UByte.MAX_VALUE.toInt() | ||
} | ||
|
||
if (startTrim != null) { | ||
output.putShort(startTrim and 0x7FF) | ||
} | ||
|
||
if (endTrim != null) { | ||
output.putShort(endTrim and 0x7FF) | ||
} | ||
} | ||
} |
42 changes: 0 additions & 42 deletions
42
...ain/java/io/github/thibaultbee/streampack/internal/utils/av/audio/opus/OpusFrameWriter.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...ava/io/github/thibaultbee/streampack/internal/muxers/ts/utils/av/OpusControlHeaderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.github.thibaultbee.streampack.internal.muxers.ts.utils.av | ||
|
||
import io.github.thibaultbee.streampack.internal.utils.extensions.toByteArray | ||
import org.junit.Assert.assertArrayEquals | ||
import org.junit.Test | ||
|
||
class OpusControlHeaderTest { | ||
@Test | ||
fun `test write`() { | ||
val opusControlHeader = OpusControlHeader(payloadSize = 515) | ||
val output = opusControlHeader.toByteBuffer() | ||
assertArrayEquals( | ||
byteArrayOf( | ||
0x7F, 0xE0.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0x5 | ||
), | ||
output.toByteArray() | ||
) | ||
} | ||
} |