Skip to content

Commit

Permalink
fix: Fix double return of buffers to the pool. (jitsi#306)
Browse files Browse the repository at this point in the history
* fix: Fix double return of buffers to the pool.

* feat: Flush the cache on stop(), add docs.
  • Loading branch information
bgrozev authored Sep 25, 2020
1 parent 1137808 commit dbb53c7
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/main/kotlin/org/jitsi/nlj/transform/node/AudioRedHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,19 @@ class AudioRedHandler(
addNumber("redundancy_packets_added", stats.redundancyPacketsAdded)
}

override fun stop() = super.stop().also {
ssrcRedHandlers.values.forEach { it.stop() }
ssrcRedHandlers.clear()
}

/**
* Handler for a specific stream (SSRC)
*/
private inner class SsrcRedHandler {
/**
* Saves audio (non-RED) packets that we've sent. Packets are cloned on insert and the inserted copies are owned
* by the cache, meaning that the cache is responsible for returning their buffers to the pool.
*/
val sentAudioCache = RtpPacketCache(20, synchronize = false)

/**
Expand Down Expand Up @@ -145,7 +154,8 @@ class AudioRedHandler(
val redPacket = RedAudioRtpPacket.builder.build(redPayloadType, audioRtpPacket, redundancy)
packetInfo.packet = redPacket

redundancy.forEach { BufferPool.returnBuffer(it.buffer) }
// We replaced packetInfo.packet with our newly allocated packet, so the original can now be returned.
// We do not return the redundancy packets, because we only peek()ed at them from the cache.
BufferPool.returnBuffer(audioRtpPacket.buffer)

stats.audioPacketEncapsulated()
Expand All @@ -172,6 +182,8 @@ class AudioRedHandler(
getHeaderExtension(extId)?.let { AudioLevelHeaderExtension.getVad(it) } ?: false
} ?: false

fun stop() = sentAudioCache.flush()

/**
* Process an incoming RED packet. Depending on the configured policy and whether the receiver supports the
* RED format, it is either forwarded as it is or it is "stripped" to its primary encoding, with redundancy
Expand Down

0 comments on commit dbb53c7

Please sign in to comment.