Migrate enum conversions from map lookups to switch expressions - #1156
Merged
Conversation
toRTCType built a map without an entry for disabled and force unwrapped the lookup, so an explicit disabled preference threw a null check error during publish. WebRTC has renamed DISABLED to MAINTAIN_FRAMERATE_AND_RESOLUTION and defines the old name as an alias, so map it accordingly. The conversion is now an exhaustive switch, which turns any future enum addition into a compile error instead of a runtime crash.
The map-with-null-assertion pattern crashes at runtime when a value is missing from the map, which is how the DegradationPreference.disabled bug happened. Switch expressions over Dart enums are compile time exhaustive, so a future enum addition becomes an analyzer error instead. Protobuf enums are classes rather than Dart enums, so their switches keep a wildcard arm. Where the old code force unwrapped the lookup, the wildcard now falls back to a safe default instead of crashing on values from newer servers. DisconnectReason was already affected in practice, the proto defines 17 reasons but only 8 were mapped, so a server sending ROOM_CLOSED or MIGRATION in a leave request crashed disconnect handling.
hiroshihorie
marked this pull request as ready for review
August 7, 2026 10:48
hiroshihorie
requested review from
cloudwebrtc and
xianshijing-lk
as code owners
August 7, 2026 10:48
toRid had no callers and mapped VideoQuality.OFF to the low quality rid, which would be wrong if it ever gained one. The new test pins the value count of every converted protobuf enum, so a proto regen that adds a value fails the test and points at the conversion, since the wildcard arms mean the analyzer can no longer flag it.
hiroshihorie
added a commit
that referenced
this pull request
Aug 8, 2026
…values (#1158) Follow-up to #1156 (replaces #1157, which GitHub auto-closed when the stack base merged). The protocol defines 17 disconnect reasons but the public `DisconnectReason` enum only had members for 8 of them, so newer reasons arriving in a leave request (for example when the server closes a room or a SIP trunk fails) were all collapsed to `unknown`. This adds members for the nine missing reasons and maps them in `toSDKType`: `migration`, `signalClose`, `roomClosed`, `userUnavailable`, `userRejected`, `sipTrunkFailure`, `connectionTimeout`, `mediaFailure`, `agentError` Unrecognized values from servers newer than the SDK still fall back to `unknown` via the wildcard arm introduced in #1156. Note for apps: adding enum members means an exhaustive `switch` over `DisconnectReason` in app code will need new cases, which is why the changeset is `minor`. ## Tests New `test/types/disconnect_reason_test.dart` asserts every proto value maps to a distinct SDK value and pins the nine new mappings. Full suite passes (380 tests), analyze, format, and import sorter clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Cleans up how we convert between our enums and their WebRTC or protobuf counterparts. All 14 conversion sites (
extensions.dart,track/options.dart,support/native_audio.dart) move from the{...}[this]!map lookup pattern to switch expressions.Why
Switch expressions are the better tool for this in modern Dart:
_arm with a sensible default. Values that a newer server sends and an older SDK does not know about now degrade gracefully, for example an unrecognizedDisconnectReasonin a leave request maps toDisconnectReason.unknown.Mapping correction for the deprecated
disabledWhile converting
DegradationPreferenceExt, the deprecatedDegradationPreference.disabled(previously absent from the map) now maps toMAINTAIN_FRAMERATE_AND_RESOLUTION, which is exactly how upstream WebRTC defines it (api/rtp_parameters.h):The deprecation message now points users to
maintainFramerateAndResolution. Removing the value entirely is left for the next major, mirroring upstream's own TODO.Follow-up (separate PR): add public
DisconnectReasonmembers for the newer proto reasons so apps can distinguish them from genuinely unknown.Tests
New
test/types/degradation_preference_rtc_test.dartasserts everyDegradationPreferencevalue converts cleanly and thatdisabledmaps toMAINTAIN_FRAMERATE_AND_RESOLUTION. Full suite passes (378 tests), analyze, format, and import sorter clean.🤖 Generated with Claude Code