-
Notifications
You must be signed in to change notification settings - Fork 995
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Export audio to a recorder over a WS.
- Loading branch information
Showing
8 changed files
with
246 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
jvb/src/main/kotlin/org/jitsi/videobridge/export/Exporter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright @ 2024 - Present, 8x8 Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jitsi.videobridge.export | ||
|
||
import org.eclipse.jetty.websocket.api.WebSocketAdapter | ||
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest | ||
import org.eclipse.jetty.websocket.client.WebSocketClient | ||
import org.jitsi.nlj.PacketInfo | ||
import org.jitsi.nlj.rtp.AudioRtpPacket | ||
import org.jitsi.nlj.util.PacketInfoQueue | ||
import org.jitsi.utils.logging2.createLogger | ||
import org.jitsi.videobridge.PotentialPacketHandler | ||
import org.jitsi.videobridge.colibri2.FeatureNotImplementedException | ||
import org.jitsi.videobridge.util.ByteBufferPool | ||
import org.jitsi.videobridge.util.TaskPools | ||
import org.jitsi.videobridge.websocket.config.WebsocketServiceConfig | ||
import org.jitsi.xmpp.extensions.colibri2.Connect | ||
|
||
class Exporter : PotentialPacketHandler { | ||
val logger = createLogger() | ||
var started = false | ||
val queue = PacketInfoQueue( | ||
"${javaClass.simpleName}-packet-queue", | ||
TaskPools.IO_POOL, | ||
this::doHandlePacket, | ||
128 | ||
) | ||
|
||
private var wsNotConnectedErrors = 0 | ||
private fun logWsNotConnectedError(): Boolean = (wsNotConnectedErrors++ % 1000) == 0 | ||
private val encoder = MediaJsonEncoder { | ||
if (recorderWebSocket.isConnected) { | ||
recorderWebSocket.remote?.sendString(it.toJson()) | ||
?: logger.info("Websocket is connected, but remote is null") | ||
} else if (logWsNotConnectedError()) { | ||
logger.info("Can not send packet, websocket is not connected (count=$wsNotConnectedErrors).") | ||
} | ||
} | ||
private var recorderWebSocket = WebSocketAdapter() | ||
|
||
fun setConnects(exports: List<Connect>) { | ||
when { | ||
started && exports.isNotEmpty() -> throw FeatureNotImplementedException("Changing exports once enabled.") | ||
exports.isEmpty() -> stop() | ||
exports.size > 1 -> throw FeatureNotImplementedException("Multiple exports") | ||
exports[0].video -> throw FeatureNotImplementedException("Video") | ||
else -> start(exports[0]) | ||
} | ||
} | ||
|
||
private fun doHandlePacket(packet: PacketInfo): Boolean { | ||
if (started) { | ||
encoder.encode(packet.packetAs(), packet.endpointId!!) | ||
} | ||
ByteBufferPool.returnBuffer(packet.packet.buffer) | ||
return true | ||
} | ||
|
||
override fun wants(packet: PacketInfo): Boolean = started && packet.packet is AudioRtpPacket | ||
|
||
override fun send(packet: PacketInfo) { | ||
if (started) { | ||
queue.add(packet) | ||
} else { | ||
ByteBufferPool.returnBuffer(packet.packet.buffer) | ||
} | ||
} | ||
|
||
fun stop() { | ||
started = false | ||
logger.info("Stopping.") | ||
recorderWebSocket.session?.close(org.eclipse.jetty.websocket.core.CloseStatus.SHUTDOWN, "closing") | ||
} | ||
|
||
fun start(connect: Connect) { | ||
if (connect.video) throw FeatureNotImplementedException("Video") | ||
if (connect.protocol != Connect.Protocols.MEDIAJSON) { | ||
throw FeatureNotImplementedException("Protocol ${connect.protocol}") | ||
} | ||
if (connect.type != Connect.Types.RECORDER) { | ||
throw FeatureNotImplementedException("Type ${connect.type}") | ||
} | ||
|
||
logger.info("Starting with url=${connect.url}") | ||
webSocketClient.connect(recorderWebSocket, connect.url, ClientUpgradeRequest()) | ||
started = true | ||
} | ||
|
||
companion object { | ||
val webSocketClient = WebSocketClient().apply { | ||
idleTimeout = WebsocketServiceConfig.config.idleTimeout | ||
start() | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
jvb/src/main/kotlin/org/jitsi/videobridge/export/MediaJsonEncoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright @ 2024 - Present, 8x8 Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jitsi.videobridge.export | ||
|
||
import org.jitsi.mediajson.CustomParameters | ||
import org.jitsi.mediajson.Event | ||
import org.jitsi.mediajson.Media | ||
import org.jitsi.mediajson.MediaEvent | ||
import org.jitsi.mediajson.MediaFormat | ||
import org.jitsi.mediajson.Start | ||
import org.jitsi.mediajson.StartEvent | ||
import org.jitsi.nlj.rtp.AudioRtpPacket | ||
import org.jitsi.nlj.util.Rfc3711IndexTracker | ||
import org.jitsi.rtp.rtp.RtpPacket | ||
import org.jitsi.utils.logging2.createLogger | ||
import java.time.Clock | ||
import java.time.Duration | ||
import kotlin.io.encoding.Base64 | ||
import kotlin.io.encoding.ExperimentalEncodingApi | ||
|
||
/** | ||
* Encodes the media in a conference into a mediajson format. Maintains state for each SSRC in order to maintain a | ||
* common space for timestamps. | ||
* | ||
* Note we're using a common clock with a rate of 48000 for all SSRCs (that's equivalent to the RTP timestamp for opus). | ||
*/ | ||
class MediaJsonEncoder( | ||
/** Encoded mediajson events are sent to this function */ | ||
val handleEvent: (Event) -> Unit | ||
) { | ||
val logger = createLogger() | ||
val ref = Clock.systemUTC().instant() | ||
|
||
private data class SsrcState( | ||
val ssrc: Long, | ||
val initialRtpTs: Long, | ||
// Offset of this SSRC since the start time in RTP units | ||
val offset: Long | ||
) | ||
|
||
private val ssrcsStarted = mutableSetOf<SsrcState>() | ||
var seq = 0 | ||
|
||
fun encode(p: AudioRtpPacket, epId: String) = synchronized(ssrcsStarted) { | ||
if (ssrcsStarted.none { it.ssrc == p.ssrc }) { | ||
// This is a new SSRC, save it and produce a StartEvent | ||
val state = SsrcState( | ||
p.ssrc, | ||
p.timestamp, | ||
(Duration.between(ref, Clock.systemUTC().instant()).toNanos() * 48.0e-6).toLong() | ||
) | ||
ssrcsStarted.add(state) | ||
val startEvent = StartEvent( | ||
++seq, | ||
Start( | ||
"$epId-${p.ssrc}", | ||
MediaFormat( | ||
"opus", | ||
48000, | ||
2 | ||
), | ||
CustomParameters(endpointId = epId) | ||
) | ||
) | ||
handleEvent(startEvent) | ||
} | ||
|
||
seq++ | ||
handleEvent(p.encodeAsJson(epId)) | ||
} | ||
|
||
@OptIn(ExperimentalEncodingApi::class) | ||
private fun RtpPacket.encodeAsJson(epId: String): Event { | ||
val ssrcState = ssrcsStarted.find { it.ssrc == this.ssrc }!! | ||
val elapsedRtpTime = this.timestamp - ssrcState.initialRtpTs | ||
val ts = elapsedRtpTime + ssrcState.offset | ||
val indexTracker = Rfc3711IndexTracker() | ||
val p = MediaEvent( | ||
seq, | ||
media = Media( | ||
"$epId-${this.ssrc}", | ||
indexTracker.update(this.sequenceNumber), | ||
ts, | ||
Base64.encode(this.buffer, this.payloadOffset, this.payloadOffset + this.payloadLength) | ||
) | ||
) | ||
return p | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters