Skip to content

Commit

Permalink
Refactor opus writing logic to OpusFrameWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
tukez authored and ThibaultBee committed Sep 7, 2024
1 parent c56f42d commit d620396
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import io.github.thibaultbee.streampack.internal.muxers.ts.utils.MuxerConst
import io.github.thibaultbee.streampack.internal.muxers.ts.utils.TSConst
import io.github.thibaultbee.streampack.internal.utils.av.audio.aac.ADTSFrameWriter
import io.github.thibaultbee.streampack.internal.utils.av.audio.aac.LATMFrameWriter
import io.github.thibaultbee.streampack.internal.utils.av.audio.opus.OpusFrameWriter
import java.nio.ByteBuffer
import java.util.MissingFormatArgumentException
import kotlin.random.Random
Expand Down Expand Up @@ -150,13 +151,7 @@ class TSMuxer(
}

MediaFormat.MIMETYPE_AUDIO_OPUS -> {
val payload = frame.rawBuffer
val controlHeader = opusControlHeader(payload.remaining())
val opusAccessUnit = ByteBuffer.allocate(controlHeader.remaining() + payload.remaining())
opusAccessUnit.put(controlHeader)
opusAccessUnit.put(payload)
opusAccessUnit.rewind()
frame.copy(rawBuffer = opusAccessUnit)
frame.copy(rawBuffer = OpusFrameWriter.fromPayload(frame.rawBuffer).toByteBuffer())
}
else -> throw IllegalArgumentException("Unsupported mimeType ${frame.mimeType}")
}
Expand All @@ -166,31 +161,6 @@ class TSMuxer(
}
}

private fun opusControlHeader(payloadSize: Int): ByteBuffer {
val payloadSizeFullBytesCount = payloadSize / 255
val payloadSizeRemainderByte = payloadSize % 255
val headerSize = 2 + payloadSizeFullBytesCount + payloadSizeRemainderByte.coerceAtMost(1)
val header = ByteBuffer.allocate(headerSize)

// 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)
header.put(0x7F.toByte())
header.put(0xE0.toByte())

repeat(payloadSizeFullBytesCount) {
header.put(0xFF.toByte())
}
if (payloadSizeRemainderByte > 0) {
header.put(payloadSizeRemainderByte.toByte())
}

header.rewind()
return header
}

/**
* Generate MPEG-TS table and elementary stream from the frame
* @param pes Pes containing infos on the stream
Expand Down
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)
}
}
}

0 comments on commit d620396

Please sign in to comment.