Skip to content

Commit ed87344

Browse files
authored
Fix inverted AudioPublishOptions.red (#1272)
Fixes #1271 `AudioPublishOptions.red` was passed to `AddTrackRequest.disableRed` without negation. The default `red: true` sent `disableRed: true`, so RED has been off for every Flutter audio publish since 2.0.0, and `red: false` turned it on. ```dart // before disableRed: room.e2eeManager != null ? true : publishOptions.red ?? true, // after disableRed: room.e2eeManager != null || !(publishOptions.red ?? true), ``` This matches the JS (`this.isE2EEEnabled || !(opts.red ?? true)`) and Rust SDKs. E2EE still forces RED off. ### Why it matters The SFU decides from `disableRed` whether to prefer `audio/red` in the publisher answer. Flutter sends no audio simulcast codecs, so `IsRedEnabled` falls back to `!disableRed`. libwebrtc always offers RED after Opus, so with this fix RED gets negotiated on all platforms. ### History The inverted line came in with #453 (2024-01-23), whose goal was to disable RED under E2EE for a Firefox decryption failure. That half was right. #720 later rewrote the line above it as `disableDtx: !publishOptions.dtx` and left RED as is. ### Behavior change Apps that set `red: false` to get RED working will now get RED disabled. The changeset says so. JS defaults RED off for stereo tracks. That does not apply here: Flutter never sends `stereo` or `TF_STEREO`, so the server answer strips `stereo=1` and Flutter always publishes mono Opus. The stereo `AudioEncoding` presets only set a bitrate. JS also enables RED by default for mono. ### Tests `test/participant/audio_publish_red_test.dart` publishes a mic track through the room mocks and checks the sent `AddTrackRequest` for the default, `red: false`, and E2EE cases. The first two fail on main. To let a publish run in a unit test, `MockWebSocketConnector` now records sent data and `MockPeerConnection.addTransceiver` returns a stub transceiver instead of throwing. Not yet checked against a real server.
1 parent 7652723 commit ed87344

5 files changed

Lines changed: 135 additions & 7 deletions

File tree

‎.changes/fix-audio-red-inverted‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
patch type="fixed" "Audio tracks now publish with RED (redundant audio data) enabled by default as documented. AudioPublishOptions.red was inverted, so the default red: true disabled RED and red: false enabled it. Apps that set red: false to get RED working should remove it"

‎lib/src/participant/local.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
189189
muted: track.muted,
190190
stream: buildStreamId(publishOptions, track.source),
191191
disableDtx: !publishOptions.dtx,
192-
disableRed: room.e2eeManager != null ? true : publishOptions.red ?? true,
192+
disableRed: room.e2eeManager != null || !(publishOptions.red ?? true),
193193
encryption: room.roomOptions.lkEncryptionType,
194194
);
195195

‎test/mock/peerconnection_mock.dart‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,7 @@ class MockPeerConnection extends RTCPeerConnection {
171171
MediaStreamTrack? track,
172172
RTCRtpMediaType? kind,
173173
RTCRtpTransceiverInit? init,
174-
}) {
175-
// TODO: implement addTransceiver
176-
throw UnimplementedError();
177-
}
174+
}) async => MockRtpTransceiver(MockRtpSender(track));
178175

179176
@override
180177
Future<void> close() async {
@@ -303,3 +300,26 @@ a=rtpmap:32 MPV/90000
303300
// TODO: implement restartIce
304301
Future<void> restartIce() => throw UnimplementedError();
305302
}
303+
304+
class MockRtpSender implements RTCRtpSender {
305+
MockRtpSender(this.track);
306+
307+
@override
308+
MediaStreamTrack? track;
309+
310+
@override
311+
Future<void> replaceTrack(MediaStreamTrack? track) async => this.track = track;
312+
313+
@override
314+
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
315+
}
316+
317+
class MockRtpTransceiver implements RTCRtpTransceiver {
318+
MockRtpTransceiver(this.sender);
319+
320+
@override
321+
final RTCRtpSender sender;
322+
323+
@override
324+
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
325+
}

‎test/mock/websocket_mock.dart‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ import 'package:livekit_client/src/options.dart';
1616
import 'package:livekit_client/src/support/websocket.dart';
1717

1818
class MockWebSocket extends LiveKitWebSocket {
19+
MockWebSocket([this.onSend]);
20+
21+
final void Function(List<int> data)? onSend;
22+
1923
@override
20-
void send(List<int> data) {}
24+
void send(List<int> data) => onSend?.call(data);
2125
}
2226

2327
class MockWebSocketConnector {
@@ -27,6 +31,9 @@ class MockWebSocketConnector {
2731
NetworkOptions? networkOptions;
2832
Object? connectError;
2933

34+
/// Every message the SDK sent over any socket this connector opened.
35+
final List<List<int>> sentData = [];
36+
3037
WebSocketOnData get onData => handlers!.onData!;
3138

3239
WebSocketOnDispose get onDispose => handlers!.onDispose!;
@@ -49,6 +56,6 @@ class MockWebSocketConnector {
4956
}
5057

5158
handlers = options;
52-
return MockWebSocket();
59+
return MockWebSocket(sentData.add);
5360
}
5461
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2026 LiveKit, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import 'package:flutter/services.dart';
16+
17+
import 'package:flutter_test/flutter_test.dart';
18+
19+
import 'package:livekit_client/livekit_client.dart';
20+
import 'package:livekit_client/src/proto/livekit_models.pb.dart' as lk_models;
21+
import 'package:livekit_client/src/proto/livekit_rtc.pb.dart' as lk_rtc;
22+
import 'package:livekit_client/src/support/native.dart';
23+
import '../mock/e2e_container.dart';
24+
import '../mock/e2ee_fake_manager.dart';
25+
26+
void main() {
27+
TestWidgetsFlutterBinding.ensureInitialized();
28+
final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
29+
const webrtcChannel = MethodChannel('FlutterWebRTC.Method');
30+
31+
late E2EContainer container;
32+
33+
setUp(() {
34+
messenger.setMockMethodCallHandler(webrtcChannel, (call) async {
35+
if (call.method == 'getUserMedia') {
36+
return <String, dynamic>{
37+
'streamId': 'stream-1',
38+
'audioTracks': [
39+
<String, dynamic>{'id': 'mic-1', 'label': 'mic', 'kind': 'audio', 'enabled': true},
40+
],
41+
'videoTracks': <dynamic>[],
42+
};
43+
}
44+
return null;
45+
});
46+
messenger.setMockMethodCallHandler(Native.channel, (call) async => true);
47+
container = E2EContainer();
48+
});
49+
50+
tearDown(() async {
51+
await container.dispose();
52+
messenger.setMockMethodCallHandler(webrtcChannel, null);
53+
messenger.setMockMethodCallHandler(Native.channel, null);
54+
});
55+
56+
/// Publishes a microphone track and returns the AddTrackRequest the SDK sent.
57+
Future<lk_rtc.AddTrackRequest> publishAndCaptureAddTrack({AudioPublishOptions? publishOptions}) async {
58+
await container.connectRoom();
59+
final track = await LocalAudioTrack.create();
60+
final publish = container.room.localParticipant!.publishAudioTrack(track, publishOptions: publishOptions);
61+
62+
lk_rtc.AddTrackRequest? request;
63+
for (var i = 0; i < 100 && request == null; i++) {
64+
await Future<void>.delayed(const Duration(milliseconds: 5));
65+
request = container.wsConnector.sentData
66+
.map(lk_rtc.SignalRequest.fromBuffer)
67+
.where((r) => r.hasAddTrack())
68+
.map((r) => r.addTrack)
69+
.firstOrNull;
70+
}
71+
expect(request, isNotNull, reason: 'the SDK never sent an AddTrackRequest');
72+
73+
container.wsConnector.onData(
74+
lk_rtc.SignalResponse(
75+
trackPublished: lk_rtc.TrackPublishedResponse(
76+
cid: request!.cid,
77+
track: lk_models.TrackInfo(sid: 'TR_audio', type: lk_models.TrackType.AUDIO),
78+
),
79+
).writeToBuffer(),
80+
);
81+
await publish;
82+
return request;
83+
}
84+
85+
test('RED is requested by default', () async {
86+
final request = await publishAndCaptureAddTrack();
87+
expect(request.disableRed, isFalse);
88+
});
89+
90+
test('red: false disables RED', () async {
91+
final request = await publishAndCaptureAddTrack(publishOptions: const AudioPublishOptions(red: false));
92+
expect(request.disableRed, isTrue);
93+
});
94+
95+
test('E2EE disables RED even when red is true', () async {
96+
container.engine.setE2eeManager(TestE2EEManager());
97+
final request = await publishAndCaptureAddTrack(publishOptions: const AudioPublishOptions(red: true));
98+
expect(request.disableRed, isTrue);
99+
});
100+
}

0 commit comments

Comments
 (0)