-
Notifications
You must be signed in to change notification settings - Fork 163
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
4
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.
+380
−10
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
63a96b8
feat(audio): negotiate stereo opus on the subscriber answer
kirill-jjj 04925ce
refactor: extract stereo fallback into helper
kirill-jjj 4e07201
fix: use when-based type narrowing in stereo fallback helper
kirill-jjj d787073
fix: replace conflicting stereo fmtp param instead of appending
kirill-jjj 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,5 @@ | ||
| --- | ||
| '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. Mirrors client-sdk-js behavior. | ||
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
164 changes: 164 additions & 0 deletions
164
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,164 @@ | ||
| /* | ||
| * 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 STEREO_PARAM_PREFIX = "stereo=" | ||
| 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. An | ||
| existing conflicting value such as stereo=0 is replaced, since the decoder | ||
| honors the first stereo parameter and would otherwise stay mono. | ||
| */ | ||
| private fun ensureStereoFmtpParam(mediaDesc: MediaDescription, payloadType: Long) { | ||
| var fmtpFound = false | ||
| for ((attribute, fmtp) in mediaDesc.getFmtps()) { | ||
| if (fmtp.payload != payloadType) { | ||
| continue | ||
| } | ||
| fmtpFound = true | ||
| val params = fmtp.config.split(";").map { it.trim() } | ||
| val hasStereo = params.any { it.equals(STEREO_FMTP_PARAM, ignoreCase = true) } | ||
| val hasConflictingStereo = params.any { | ||
| it.startsWith(STEREO_PARAM_PREFIX, ignoreCase = true) && | ||
| !it.equals(STEREO_FMTP_PARAM, ignoreCase = true) | ||
| } | ||
| if (!hasStereo || hasConflictingStereo) { | ||
| try { | ||
| val updated = params | ||
| .filterNot { it.startsWith(STEREO_PARAM_PREFIX, ignoreCase = true) } | ||
| .plus(STEREO_FMTP_PARAM) | ||
| attribute.setValue("${fmtp.payload} ${updated.joinToString(";")}") | ||
| } 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(), | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are API changes in here, so it should be marked as a minor upgrade.