Skip to content

Commit

Permalink
Set stereo if any packet in a track has stereo, not just the first one.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrozev committed Oct 29, 2024
1 parent 52de696 commit 9d44948
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/main/kotlin/org/jitsi/recorder/MediaJsonMkaRecorder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ class MediaJsonMkaRecorder(directory: File, parentLogger: Logger) : MediaJsonRec
private class TrackRecorder(
private val mkaRecorder: MkaRecorder,
private val trackName: String,
private val endpointId: String?
endpointId: String?
) {

private val logger = createLogger().apply {
addContext("track", trackName)
}
private var lastChunk = -1

init {
logger.info("Starting new track $trackName")
mkaRecorder.startTrack(trackName, endpointId)
}

@OptIn(ExperimentalEncodingApi::class)
fun addPacket(event: MediaEvent) {
Expand All @@ -105,11 +108,10 @@ private class TrackRecorder(
return
}

if (lastChunk == -1) {
// Start was delayed until now, so we can extract channel count.
mkaRecorder.startTrack(trackName, endpointId, if (OpusToc(payload[0]).stereo()) 2 else 1)
if (OpusToc(payload.first()).stereo()) {
mkaRecorder.setTrackChannels(trackName, 2)
}
lastChunk = event.media.chunk

mkaRecorder.addFrame(
trackName,
event.media.timestamp,
Expand Down
11 changes: 8 additions & 3 deletions src/main/kotlin/org/jitsi/recorder/MkaRecorder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class MkaRecorder(directory: File, parentLogger: Logger = LoggerImpl("MkaRecorde
private val tracks = mutableMapOf<String, MatroskaFileTrack>()
private var initialTimestampMs = -1L

fun startTrack(name: String, endpointId: String? = null, numChannels: Int = 1) {
logger.info("Starting new track $name, endpointId=$endpointId, channels=$numChannels")
fun startTrack(name: String, endpointId: String? = null) {
logger.info("Starting new track $name, endpointId=$endpointId")
val track = MatroskaFileTrack().apply {
trackNo = tracks.size + 1
trackUID = trackNo.toLong()
Expand All @@ -51,7 +51,7 @@ class MkaRecorder(directory: File, parentLogger: Logger = LoggerImpl("MkaRecorde
defaultDuration = 20_000_000
isFlagLacing = false
audio = MatroskaFileTrack.MatroskaAudioTrack().apply {
channels = numChannels.toShort()
channels = 1
samplingFrequency = 48000F
outputSamplingFrequency = 48000F
}
Expand All @@ -75,6 +75,11 @@ class MkaRecorder(directory: File, parentLogger: Logger = LoggerImpl("MkaRecorde
}
}

fun setTrackChannels(trackName: String, numChannels: Int) {
val track = tracks[trackName] ?: throw Exception("Track not started")
track.audio.channels = numChannels.toShort()
}

fun addFrame(trackName: String, timestampRtp: Long, payload: ByteArray) {
val track = tracks[trackName] ?: throw Exception("Track not started")
val frame = MatroskaFileFrame()
Expand Down

0 comments on commit 9d44948

Please sign in to comment.