-
Notifications
You must be signed in to change notification settings - Fork 349
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
Monitor visitors' codec lists, and aggregate them into a conference property. #1137
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
33f724a
Monitor visitors' codec lists, and aggregate them into a conference p…
JonathanLennox e257dfb
Fix typo in test.
JonathanLennox e926015
Add unit test for PreferenceAggregator.X
JonathanLennox e71e23e
Guard against repeated values in preferences.
JonathanLennox 22d8240
Support older clients which send jitsi_participant_codecType.
JonathanLennox 208e1e7
Protect against codec list being changed in updated presence messages.
JonathanLennox c764b53
Don't re-warn about codec lists not containing vp8 on presence updates.
JonathanLennox 3629ab4
Make sure to force codecType to lowercase.
JonathanLennox b7b53f3
Update jitsi-xmpp-extensions.
JonathanLennox d85aa96
Checkstyle fix.
JonathanLennox c48563d
Add debug state for visitor codec aggregation.
JonathanLennox 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 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
150 changes: 150 additions & 0 deletions
150
jicofo/src/main/kotlin/org/jitsi/jicofo/util/PreferenceAggregator.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,150 @@ | ||
/* | ||
* Jicofo, the Jitsi Conference Focus. | ||
* | ||
* Copyright @ 2015-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.jicofo.util | ||
|
||
import org.jitsi.utils.OrderedJsonObject | ||
import org.jitsi.utils.logging2.Logger | ||
import org.jitsi.utils.logging2.createChildLogger | ||
import org.json.simple.JSONArray | ||
|
||
/** Aggregate lists of preferences coming from a large group of people, such that the resulting aggregated | ||
* list consists of preference items supported by everyone, and in a rough consensus of preference order. | ||
* | ||
* The intended use case is maintaining the list of supported codecs for conference visitors. | ||
* | ||
* Preference orders are aggregated using the Borda count; this isn't theoretically optimal, but it should be | ||
* good enough, and it's computationally cheap. | ||
*/ | ||
class PreferenceAggregator( | ||
parentLogger: Logger, | ||
private val onChanged: (List<String>) -> Unit | ||
) { | ||
private val logger = createChildLogger(parentLogger) | ||
private val lock = Any() | ||
|
||
var aggregate: List<String> = emptyList() | ||
private set | ||
|
||
var count = 0 | ||
private set | ||
|
||
private val values = mutableMapOf<String, ValueInfo>() | ||
|
||
/** | ||
* Add a preference to the aggregator. | ||
*/ | ||
fun addPreference(prefs: List<String>) { | ||
val distinctPrefs = prefs.distinct() | ||
if (distinctPrefs != prefs) { | ||
logger.warn("Preferences $prefs contains repeated values") | ||
} | ||
synchronized(lock) { | ||
count++ | ||
distinctPrefs.forEachIndexed { index, element -> | ||
val info = values.computeIfAbsent(element) { ValueInfo() } | ||
info.count++ | ||
info.rankAggregate += index | ||
} | ||
updateAggregate() | ||
} | ||
} | ||
|
||
/** | ||
* Remove a preference from the aggregator. | ||
*/ | ||
fun removePreference(prefs: List<String>) { | ||
val distinctPrefs = prefs.distinct() | ||
if (distinctPrefs != prefs) { | ||
logger.warn("Preferences $prefs contains repeated values") | ||
} | ||
synchronized(lock) { | ||
count-- | ||
check(count >= 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could also be force to fail by sending initial presence with no codecs, then updating with some codecs, then leaving repeatedly |
||
"Preference count $count should not be negative" | ||
} | ||
distinctPrefs.forEachIndexed { index, element -> | ||
val info = values[element] | ||
check(info != null) { | ||
"Preference info for $element should exist when preferences are being removed" | ||
} | ||
info.count-- | ||
check(info.count >= 0) { | ||
"Preference count for $element ${info.count} should not be negative" | ||
} | ||
info.rankAggregate -= index | ||
check(info.rankAggregate >= 0) { | ||
"Preference rank aggregate for $element ${info.rankAggregate} should not be negative" | ||
} | ||
if (info.count == 0) { | ||
check(info.rankAggregate == 0) { | ||
"Preference rank aggregate for $element ${info.rankAggregate} should be zero " + | ||
"when preference count is 0" | ||
} | ||
values.remove(element) | ||
} | ||
} | ||
updateAggregate() | ||
} | ||
} | ||
|
||
fun reset() { | ||
synchronized(lock) { | ||
aggregate = emptyList() | ||
count = 0 | ||
values.clear() | ||
} | ||
} | ||
|
||
fun debugState() = OrderedJsonObject().apply { | ||
synchronized(lock) { | ||
put("count", count) | ||
put( | ||
"ranks", | ||
OrderedJsonObject().apply { | ||
[email protected]() | ||
.sortedBy { it.value.rankAggregate } | ||
.forEach { put(it.key, it.value.debugState()) } | ||
} | ||
) | ||
put("aggregate", JSONArray().apply { addAll(aggregate) }) | ||
} | ||
} | ||
|
||
private fun updateAggregate() { | ||
val newAggregate = values.asSequence() | ||
.filter { it.value.count == count } | ||
.sortedBy { it.value.rankAggregate } | ||
.map { it.key } | ||
.toList() | ||
if (aggregate != newAggregate) { | ||
aggregate = newAggregate | ||
/* ?? Do we need to drop the lock before calling this? */ | ||
onChanged(aggregate) | ||
} | ||
} | ||
|
||
private class ValueInfo { | ||
var count = 0 | ||
var rankAggregate = 0 | ||
|
||
fun debugState() = OrderedJsonObject().apply { | ||
put("count", count) | ||
put("rank_aggregate", rankAggregate) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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.
We allow codecs to change from null to a non-null, so if a visitor joins with no codecs and then adds some it would mess with the count. It's a bit of a stretch, but one could trick jicofo into upgrading to av1 thus breaking video for some participants.
Maybe make
ChatRoomMemberImpl.videoCodecs
alazy val
and initialize it with the first presence received?Unless I misunderstand how the aggregator works