-
Notifications
You must be signed in to change notification settings - Fork 160
feat(audio): negotiate stereo opus on the subscriber answer #1007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kirill-jjj
wants to merge
3
commits into
livekit:main
Choose a base branch
from
kirill-jjj:stereo-answer-munging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
| --- | ||
| 'livekit-android': patch | ||
| --- | ||
|
|
||
| Negotiate stereo Opus on the subscriber answer: add `stereo=1` to the fmtp line for media sections where the server offer advertised `sprop-stereo=1`. Without this, a stereo track published by another participant is decoded as mono on Android. | ||
|
|
||
| Also adds a `stereo` option to `LocalAudioTrackOptions` (surfaced as the `TF_STEREO` track feature) and `AudioTrackPublishOptions` (sent as `stereo` in the AddTrackRequest), mirroring client-sdk-js `forceStereo`. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
152 changes: 152 additions & 0 deletions
152
livekit-android-sdk/src/main/java/io/livekit/android/webrtc/StereoSdpMunging.kt
This file contains hidden or 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,152 @@ | ||
| /* | ||
| * Copyright 2023-2026 LiveKit, 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 io.livekit.android.webrtc | ||
|
|
||
| import android.javax.sdp.MediaDescription | ||
| import android.javax.sdp.SdpException | ||
| import android.javax.sdp.SdpFactory | ||
| import android.javax.sdp.SdpParseException | ||
| import androidx.annotation.VisibleForTesting | ||
| import io.livekit.android.util.LKLog | ||
| import livekit.org.webrtc.SessionDescription | ||
|
|
||
| private const val OPUS_CODEC = "opus" | ||
| private const val STEREO_FMTP_PARAM = "stereo=1" | ||
| private const val SPROP_STEREO_FMTP_PARAM = "sprop-stereo=1" | ||
| private const val MID_ATTRIBUTE = "mid" | ||
|
|
||
| /** | ||
| * Adds `stereo=1` to the Opus fmtp line of the answer for every audio media | ||
| * section where [offer] advertised `sprop-stereo=1`. | ||
| * | ||
| * The native Opus decoder downmixes incoming stereo packets to mono unless the | ||
| * local answer negotiates `stereo=1`, so without this munging a stereo track | ||
| * published by another participant is received as mono. | ||
| * | ||
| * Mirrors the behavior of client-sdk-js, which extracts `remoteStereoMids` from | ||
| * the server offer and rewrites the matching fmtp lines when creating the answer. | ||
| * | ||
| * @suppress | ||
| */ | ||
| @VisibleForTesting | ||
| internal fun SessionDescription.ensureStereoOpus(offer: SessionDescription): SessionDescription { | ||
| val sdpFactory = SdpFactory.getInstance() | ||
| val parsedAnswer = parseSessionDescription(sdpFactory, description) ?: return this | ||
| val parsedOffer = parseSessionDescription(sdpFactory, offer.description) ?: return this | ||
|
|
||
| val stereoMids = findStereoMids(parsedOffer) | ||
| if (stereoMids.isEmpty()) { | ||
| return this | ||
| } | ||
|
|
||
| for (mediaDesc in mediaDescriptionsOf(parsedAnswer)) { | ||
| val mid = midOf(mediaDesc) ?: continue | ||
| if (mid !in stereoMids) continue | ||
|
|
||
| val payloadType = findOpusPayloadType(mediaDesc) ?: continue | ||
| ensureStereoFmtpParam(mediaDesc, payloadType) | ||
| } | ||
|
|
||
| return try { | ||
| SessionDescription(type, parsedAnswer.toString()) | ||
| } catch (_: SdpException) { | ||
| this | ||
| } | ||
| } | ||
|
|
||
| private fun parseSessionDescription( | ||
| sdpFactory: SdpFactory, | ||
| description: String, | ||
| ): android.javax.sdp.SessionDescription? = try { | ||
| sdpFactory.createSessionDescription(description) | ||
| } catch (_: SdpParseException) { | ||
| LKLog.w { "stereo munging: could not parse sdp" } | ||
| null | ||
| } | ||
|
|
||
| private fun mediaDescriptionsOf(parsed: android.javax.sdp.SessionDescription): List<MediaDescription> { | ||
| val raw = try { | ||
| parsed.getMediaDescriptions(true) | ||
| } catch (_: SdpException) { | ||
| return emptyList() | ||
| } | ||
| return raw.filterIsInstance<MediaDescription>() | ||
| } | ||
|
|
||
| private fun findStereoMids(offer: android.javax.sdp.SessionDescription): List<String> { | ||
| val mids = mutableListOf<String>() | ||
| for (mediaDesc in mediaDescriptionsOf(offer)) { | ||
| if (isPublisherStereo(mediaDesc)) { | ||
| midOf(mediaDesc)?.let { mids.add(it) } | ||
| } | ||
| } | ||
| return mids | ||
| } | ||
|
|
||
| private fun midOf(mediaDesc: MediaDescription): String? = try { | ||
| mediaDesc.getAttribute(MID_ATTRIBUTE) | ||
| } catch (_: SdpParseException) { | ||
| null | ||
| } | ||
|
|
||
| private fun isPublisherStereo(mediaDesc: MediaDescription): Boolean { | ||
| val payloadType = findOpusPayloadType(mediaDesc) ?: return false | ||
| for ((_, fmtp) in mediaDesc.getFmtps()) { | ||
| if (fmtp.payload == payloadType && fmtp.config.split(";").any { it.trim() == SPROP_STEREO_FMTP_PARAM }) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| private fun findOpusPayloadType(mediaDesc: MediaDescription): Long? { | ||
| for ((_, rtp) in mediaDesc.getRtps()) { | ||
| if (rtp.codec.equals(OPUS_CODEC, ignoreCase = true)) { | ||
| return rtp.payload | ||
| } | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| /* The native Opus decoder requires both sides of the negotiation to carry | ||
| stereo=1. The server only puts sprop-stereo=1 into its offer; the answer must | ||
| add the stereo=1 parameter itself or received packets are decoded as mono. | ||
| */ | ||
| private fun ensureStereoFmtpParam(mediaDesc: MediaDescription, payloadType: Long) { | ||
| var fmtpFound = false | ||
| for ((attribute, fmtp) in mediaDesc.getFmtps()) { | ||
| if (fmtp.payload != payloadType) { | ||
| continue | ||
| } | ||
| fmtpFound = true | ||
| if (!fmtp.config.split(";").any { it.trim() == STEREO_FMTP_PARAM }) { | ||
| try { | ||
| attribute.setValue("${fmtp.payload} ${fmtp.config};$STEREO_FMTP_PARAM") | ||
| } catch (_: SdpException) { | ||
| LKLog.w { "stereo munging: failed to update opus fmtp line" } | ||
| } | ||
| } | ||
| break | ||
| } | ||
|
|
||
| // Not found, add manually | ||
| if (!fmtpFound) { | ||
| mediaDesc.addAttribute( | ||
| SdpFmtp(payloadType, STEREO_FMTP_PARAM).toAttributeField(), | ||
| ) | ||
| } | ||
| } |
146 changes: 146 additions & 0 deletions
146
livekit-android-test/src/test/java/io/livekit/android/webrtc/StereoSdpMungingTest.kt
This file contains hidden or 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,146 @@ | ||
| /* | ||
| * Copyright 2023-2024 LiveKit, 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 io.livekit.android.webrtc | ||
|
|
||
| import android.javax.sdp.MediaDescription | ||
| import android.javax.sdp.SdpFactory | ||
| import livekit.org.webrtc.SessionDescription | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
|
|
||
| class StereoSdpMungingTest { | ||
|
|
||
| private val sdpFactory = SdpFactory.getInstance() | ||
|
|
||
| @Test | ||
| fun ensureStereoOpusAddsStereoWhenOfferHasSpropStereo() { | ||
| val answer = answerOf(STEREO_OFFER_DESCRIPTION) | ||
| val offer = offerOf(STEREO_OFFER_DESCRIPTION) | ||
|
|
||
| val munged = answer.ensureStereoOpus(offer) | ||
|
|
||
| val audioMedia = audioMid(munged, "1") | ||
| val fmtp = audioMedia.getFmtps().first { (_, fmtp) -> fmtp.payload == 111L } | ||
| assertTrue(fmtp.second.config.split(";").any { it.trim() == "stereo=1" }) | ||
| } | ||
|
|
||
| @Test | ||
| fun ensureStereoOpusDoesNothingWhenOfferLacksSpropStereo() { | ||
| val answer = answerOf(NO_STEREO_OFFER_DESCRIPTION) | ||
| val offer = offerOf(NO_STEREO_OFFER_DESCRIPTION) | ||
|
|
||
| val munged = answer.ensureStereoOpus(offer) | ||
|
|
||
| assertEquals(NO_STEREO_OFFER_DESCRIPTION, munged.description) | ||
| } | ||
|
|
||
| @Test | ||
| fun ensureStereoOpusDoesNotDuplicateStereoParam() { | ||
| val answer = answerOf(ANSWER_WITH_STEREO_DESCRIPTION) | ||
| val offer = offerOf(STEREO_OFFER_DESCRIPTION) | ||
|
|
||
| val munged = answer.ensureStereoOpus(offer) | ||
|
|
||
| val audioMedia = audioMid(munged, "1") | ||
| val stereoFmtps = audioMedia.getFmtps() | ||
| .filter { (_, fmtp) -> | ||
| fmtp.payload == 111L && fmtp.config.split(";").any { it.trim() == "stereo=1" } | ||
| } | ||
| assertEquals(1, stereoFmtps.size) | ||
| } | ||
|
|
||
| private fun answerOf(sdp: String): SessionDescription = | ||
| SessionDescription(SessionDescription.Type.ANSWER, sdp) | ||
|
|
||
| private fun offerOf(sdp: String): SessionDescription = | ||
| SessionDescription(SessionDescription.Type.OFFER, sdp) | ||
|
|
||
| private fun audioMid(munged: SessionDescription, mid: String): MediaDescription = | ||
| sdpFactory.createSessionDescription(munged.description) | ||
| .getMediaDescriptions(true) | ||
| .filterIsInstance<MediaDescription>() | ||
| .first { it.getAttribute("mid") == mid } | ||
|
|
||
| companion object { | ||
| // Mirrors a LiveKit subscriber offer: audio mid "1" carries opus with | ||
| // sprop-stereo=1 (publisher published a stereo track). | ||
| private const val STEREO_OFFER_DESCRIPTION = "v=0\r\n" + | ||
| "o=- 8980856298632007851 1787470315 IN IP4 0.0.0.0\r\n" + | ||
| "s=-\r\n" + | ||
| "t=0 0\r\n" + | ||
| "a=msid-semantic:WMS *\r\n" + | ||
| "a=group:BUNDLE 0 1\r\n" + | ||
| "m=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\n" + | ||
| "c=IN IP4 0.0.0.0\r\n" + | ||
| "a=mid:0\r\n" + | ||
| "a=sendrecv\r\n" + | ||
| "a=sctp-port:5000\r\n" + | ||
| "m=audio 9 UDP/TLS/RTP/SAVPF 111 63\r\n" + | ||
| "c=IN IP4 0.0.0.0\r\n" + | ||
| "a=mid:1\r\n" + | ||
| "a=rtcp-mux\r\n" + | ||
| "a=rtpmap:111 opus/48000/2\r\n" + | ||
| "a=fmtp:111 minptime=10;useinbandfec=1;sprop-stereo=1\r\n" + | ||
| "a=rtpmap:63 red/48000/2\r\n" + | ||
| "a=fmtp:63 111/111\r\n" + | ||
| "a=recvonly\r\n" | ||
|
|
||
| // Publisher published a mono track: no sprop-stereo anywhere. | ||
| private const val NO_STEREO_OFFER_DESCRIPTION = "v=0\r\n" + | ||
| "o=- 8980856298632007851 1787470315 IN IP4 0.0.0.0\r\n" + | ||
| "s=-\r\n" + | ||
| "t=0 0\r\n" + | ||
| "a=msid-semantic:WMS *\r\n" + | ||
| "a=group:BUNDLE 0 1\r\n" + | ||
| "m=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\n" + | ||
| "c=IN IP4 0.0.0.0\r\n" + | ||
| "a=mid:0\r\n" + | ||
| "a=sendrecv\r\n" + | ||
| "a=sctp-port:5000\r\n" + | ||
| "m=audio 9 UDP/TLS/RTP/SAVPF 111 63\r\n" + | ||
| "c=IN IP4 0.0.0.0\r\n" + | ||
| "a=mid:1\r\n" + | ||
| "a=rtcp-mux\r\n" + | ||
| "a=rtpmap:111 opus/48000/2\r\n" + | ||
| "a=fmtp:111 minptime=10;useinbandfec=1\r\n" + | ||
| "a=rtpmap:63 red/48000/2\r\n" + | ||
| "a=fmtp:63 111/111\r\n" + | ||
| "a=recvonly\r\n" | ||
|
|
||
| // The answer already negotiated stereo=1: munging must not duplicate it. | ||
| private const val ANSWER_WITH_STEREO_DESCRIPTION = "v=0\r\n" + | ||
| "o=- 3119613797835240840 4 IN IP4 127.0.0.1\r\n" + | ||
| "s=-\r\n" + | ||
| "t=0 0\r\n" + | ||
| "a=group:BUNDLE 0 1\r\n" + | ||
| "a=msid-semantic: WMS\r\n" + | ||
| "m=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\n" + | ||
| "c=IN IP4 0.0.0.0\r\n" + | ||
| "a=mid:0\r\n" + | ||
| "a=sctp-port:5000\r\n" + | ||
| "m=audio 9 UDP/TLS/RTP/SAVPF 111 63\r\n" + | ||
| "c=IN IP4 0.0.0.0\r\n" + | ||
| "a=mid:1\r\n" + | ||
| "a=rtcp-mux\r\n" + | ||
| "a=rtpmap:111 opus/48000/2\r\n" + | ||
| "a=fmtp:111 minptime=10;useinbandfec=1;stereo=1\r\n" + | ||
| "a=rtpmap:63 red/48000/2\r\n" + | ||
| "a=fmtp:63 111/111\r\n" + | ||
| "a=recvonly\r\n" | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.