Skip to content

Commit

Permalink
Write OPUS control header in TSMuxer.
Browse files Browse the repository at this point in the history
  • Loading branch information
tukez authored and ThibaultBee committed Sep 7, 2024
1 parent d4f9cbf commit c56f42d
Showing 1 changed file with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,14 @@ class TSMuxer(
}

MediaFormat.MIMETYPE_AUDIO_OPUS -> {
frame
} // TODO: optional control header
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)
}
else -> throw IllegalArgumentException("Unsupported mimeType ${frame.mimeType}")
}

Expand All @@ -160,6 +166,31 @@ 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

0 comments on commit c56f42d

Please sign in to comment.