Skip to content

Commit

Permalink
wip: ice4j push API.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrozev committed Jul 18, 2024
1 parent 89dde6e commit 62960e1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
29 changes: 10 additions & 19 deletions jvb/src/main/kotlin/org/jitsi/videobridge/Endpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.jitsi.videobridge

import org.ice4j.util.Buffer
import org.jitsi.config.JitsiConfig
import org.jitsi.dcsctp4j.DcSctpMessage
import org.jitsi.dcsctp4j.ErrorKind
Expand Down Expand Up @@ -45,13 +46,11 @@ import org.jitsi.nlj.util.NEVER
import org.jitsi.nlj.util.PacketInfoQueue
import org.jitsi.nlj.util.RemoteSsrcAssociation
import org.jitsi.nlj.util.sumOf
import org.jitsi.rtp.Packet
import org.jitsi.rtp.UnparsedPacket
import org.jitsi.rtp.rtcp.RtcpSrPacket
import org.jitsi.rtp.rtcp.rtcpfb.RtcpFbPacket
import org.jitsi.rtp.rtcp.rtcpfb.payload_specific_fb.RtcpFbFirPacket
import org.jitsi.rtp.rtcp.rtcpfb.payload_specific_fb.RtcpFbPliPacket
import org.jitsi.rtp.rtp.RtpPacket
import org.jitsi.utils.MediaType
import org.jitsi.utils.concurrent.RecurringRunnableExecutor
import org.jitsi.utils.logging2.Logger
Expand Down Expand Up @@ -406,24 +405,16 @@ class Endpoint @JvmOverloads constructor(
var audioSources: ArrayList<AudioSourceDesc> = ArrayList()

private fun setupIceTransport() {
iceTransport.incomingDataHandler = object : IceTransport.IncomingDataHandler {
override fun dataReceived(data: ByteArray, offset: Int, length: Int, receivedTime: Instant) {
// DTLS data will be handled by the DtlsTransport, but SRTP data can go
// straight to the transceiver
if (looksLikeDtls(data, offset, length)) {
// DTLS transport is responsible for making its own copy, because it will manage its own
// buffers
dtlsTransport.dtlsDataReceived(data, offset, length)
iceTransport.incomingDataHandler2 = object : IceTransport.IncomingDataHandler2 {
override fun dataReceived(buffer: Buffer) {
if (looksLikeDtls(buffer.buffer, buffer.offset, buffer.length)) {
// DTLS transport is responsible for making its own copy, because it will manage its own buffers
// TODO: place on a queue, we can't risk blocking the ice4j thread.
dtlsTransport.dtlsDataReceived(buffer.buffer, buffer.offset, buffer.length)
} else {
val copy = ByteBufferPool.getBuffer(
length +
RtpPacket.BYTES_TO_LEAVE_AT_START_OF_PACKET +
Packet.BYTES_TO_LEAVE_AT_END_OF_PACKET
)
System.arraycopy(data, offset, copy, RtpPacket.BYTES_TO_LEAVE_AT_START_OF_PACKET, length)
val pktInfo =
PacketInfo(UnparsedPacket(copy, RtpPacket.BYTES_TO_LEAVE_AT_START_OF_PACKET, length)).apply {
this.receivedTime = receivedTime
PacketInfo(UnparsedPacket(buffer.buffer, buffer.offset, buffer.length)).apply {
this.receivedTime = buffer.receivedTime
}
transceiver.handleIncomingPacket(pktInfo)
}
Expand All @@ -438,7 +429,7 @@ class Endpoint @JvmOverloads constructor(
outgoingSrtpPacketQueue.add(packetInfo)
}
})
TaskPools.IO_POOL.execute(iceTransport::startReadingData)
// TaskPools.IO_POOL.execute(iceTransport::startReadingData)
TaskPools.IO_POOL.execute(dtlsTransport::startDtlsHandshake)
}

Expand Down
12 changes: 12 additions & 0 deletions jvb/src/main/kotlin/org/jitsi/videobridge/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.jitsi.videobridge
import org.eclipse.jetty.servlet.ServletHolder
import org.glassfish.jersey.servlet.ServletContainer
import org.ice4j.ice.harvest.MappingCandidateHarvesters
import org.ice4j.util.Buffer
import org.jitsi.config.JitsiConfig
import org.jitsi.metaconfig.ConfigException
import org.jitsi.metaconfig.MetaconfigLogger
Expand All @@ -29,6 +30,8 @@ import org.jitsi.rest.createServer
import org.jitsi.rest.enableCors
import org.jitsi.rest.isEnabled
import org.jitsi.rest.servletContextHandler
import org.jitsi.rtp.Packet
import org.jitsi.rtp.rtp.RtpPacket
import org.jitsi.shutdown.ShutdownServiceImpl
import org.jitsi.utils.logging2.LoggerImpl
import org.jitsi.utils.queue.PacketQueue
Expand Down Expand Up @@ -239,4 +242,13 @@ private fun setupBufferPools() {
org.jitsi.rtp.util.BufferPool.returnArray = { ByteBufferPool.returnBuffer(it) }
org.jitsi.nlj.util.BufferPool.getBuffer = { ByteBufferPool.getBuffer(it) }
org.jitsi.nlj.util.BufferPool.returnBuffer = { ByteBufferPool.returnBuffer(it) }
org.ice4j.util.BufferPool.getBuffer = { len ->
val b = ByteBufferPool.getBuffer(len)
Buffer(b, 0, b.size)
}
org.ice4j.util.BufferPool.returnBuffer = { ByteBufferPool.returnBuffer(it.buffer) }
org.ice4j.ice.harvest.AbstractUdpListener.BYTES_TO_LEAVE_AT_START_OF_PACKET =
RtpPacket.BYTES_TO_LEAVE_AT_START_OF_PACKET
org.ice4j.ice.harvest.AbstractUdpListener.BYTES_TO_LEAVE_AT_END_OF_PACKET =
Packet.BYTES_TO_LEAVE_AT_END_OF_PACKET
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import org.ice4j.ice.LocalCandidate
import org.ice4j.ice.RemoteCandidate
import org.ice4j.ice.harvest.MappingCandidateHarvesters
import org.ice4j.socket.SocketClosedException
import org.ice4j.util.Buffer
import org.ice4j.util.BufferHandler
import org.jitsi.utils.OrderedJsonObject
import org.jitsi.utils.logging2.Logger
import org.jitsi.utils.logging2.cdebug
Expand Down Expand Up @@ -85,6 +87,9 @@ class IceTransport @JvmOverloads constructor(
@JvmField
var incomingDataHandler: IncomingDataHandler? = null

@JvmField
var incomingDataHandler2: IncomingDataHandler2? = null

/**
* The handler which will be invoked when events fired by [IceTransport]
* occur. This field should be set by another entity who wishes to handle
Expand Down Expand Up @@ -135,7 +140,13 @@ class IceTransport @JvmOverloads constructor(
addPairChangeListener(iceStreamPairChangedListener)
}

private val iceComponent = iceAgent.createComponent(iceStream, IceConfig.config.keepAliveStrategy, true)
private val iceComponent = iceAgent.createComponent(iceStream, IceConfig.config.keepAliveStrategy, true).apply {
setBufferCallback(object : BufferHandler {
override fun handleBuffer(buffer: Buffer) {
incomingDataHandler2?.dataReceived(buffer)
}
})
}
private val packetStats = PacketStats()
val icePassword: String
get() = iceAgent.localPassword
Expand Down Expand Up @@ -437,6 +448,13 @@ class IceTransport @JvmOverloads constructor(
*/
fun dataReceived(data: ByteArray, offset: Int, length: Int, receivedTime: Instant)
}
interface IncomingDataHandler2 {
/**
* Notify the handler that data was received (contained
* within [data] at [offset] with [length]) at [receivedTime]
*/
fun dataReceived(buffer: org.ice4j.util.Buffer)
}

interface EventHandler {
/**
Expand Down
1 change: 1 addition & 0 deletions jvb/src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# This file contains overrides for libraries JVB uses
ice4j {
use-push-api = true
consent-freshness {
// Sends "consent freshness" check every 5 seconds.
interval = 5 seconds
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ice4j</artifactId>
<version>3.0-72-g824cd4b</version>
<version>3.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
Expand Down

0 comments on commit 62960e1

Please sign in to comment.