-
-
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 opus writing logic to OpusFrameWriter
- Loading branch information
1 parent
c56f42d
commit d620396
Showing
2 changed files
with
44 additions
and
32 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
42 changes: 42 additions & 0 deletions
42
...ain/java/io/github/thibaultbee/streampack/internal/utils/av/audio/opus/OpusFrameWriter.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,42 @@ | ||
package io.github.thibaultbee.streampack.internal.utils.av.audio.opus | ||
|
||
import io.github.thibaultbee.streampack.internal.utils.av.buffer.ByteBufferWriter | ||
import java.nio.ByteBuffer | ||
|
||
class OpusFrameWriter(private val frameBuffer: ByteBuffer) : ByteBufferWriter() { | ||
private val payloadSize: Int | ||
get() = frameBuffer.remaining() | ||
private val payloadSizeFullBytesCount: Int | ||
get() = payloadSize / 255 | ||
private val payloadSizeRemainderByte: Int | ||
get() = payloadSize % 255 | ||
private val controlHeaderSize: Int | ||
get() = 2 + payloadSizeFullBytesCount + payloadSizeRemainderByte.coerceAtMost(1) | ||
|
||
override val size = controlHeaderSize + payloadSize | ||
|
||
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.put(0x7F.toByte()) | ||
output.put(0xE0.toByte()) | ||
|
||
repeat(payloadSizeFullBytesCount) { | ||
output.put(0xFF.toByte()) | ||
} | ||
if (payloadSizeRemainderByte > 0) { | ||
output.put(payloadSizeRemainderByte.toByte()) | ||
} | ||
|
||
output.put(frameBuffer) | ||
} | ||
|
||
companion object { | ||
fun fromPayload(frameBuffer: ByteBuffer): OpusFrameWriter { | ||
return OpusFrameWriter(frameBuffer) | ||
} | ||
} | ||
} |