Skip to content

Commit

Permalink
fix: Fixed confusing room options causing settings to overwrite. (#570)
Browse files Browse the repository at this point in the history
* fix: Fixed confusing room options causing settings to overwrite.

* if publishOptions.videoEncoding is not specified, use the default encoding

* fix.

* revert changes.

* Change VideoParameters.encoding to optional.

* update.

* Deprecate some APIs in Room.

* update.

* update.

* Update build.yaml

* update.

* Update build.yaml
  • Loading branch information
cloudwebrtc authored Aug 9, 2024
1 parent c0c4140 commit 05800e7
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 42 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: '12.x'
java-version: '17.x'
- uses: actions/checkout@v2
- uses: subosito/flutter-action@v2
with:
channel: 'stable'
- name: Install project dependencies
run: flutter pub get
- name: Upgrade to major versions
run: flutter pub upgrade --major-versions
- name: Dart Format Check
run: dart format lib/ test/ --set-exit-if-changed
- name: Import Sorter Check
Expand Down
48 changes: 30 additions & 18 deletions example/lib/pages/prejoin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,15 @@ class _PreJoinPageState extends State<PreJoinPage> {

try {
//create new room
final room = Room();
var cameraEncoding = VideoEncoding(
maxBitrate: 5 * 1000 * 1000,
maxFramerate: 30,
);

// Create a Listener before connecting
final listener = room.createListener();
var screenEncoding = VideoEncoding(
maxBitrate: 3 * 1000 * 1000,
maxFramerate: 15,
);

E2EEOptions? e2eeOptions;
if (args.e2ee && args.e2eeKey != null) {
Expand All @@ -177,36 +182,43 @@ class _PreJoinPageState extends State<PreJoinPage> {
await keyProvider.setKey(args.e2eeKey!);
}

// Try to connect to the room
// This will throw an Exception if it fails for any reason.
await room.connect(
args.url,
args.token,
final room = Room(
roomOptions: RoomOptions(
adaptiveStream: args.adaptiveStream,
dynacast: args.dynacast,
defaultAudioPublishOptions: const AudioPublishOptions(
name: 'custom_audio_track_name',
),
defaultCameraCaptureOptions: CameraCaptureOptions(
maxFrameRate: 30,
params: VideoParameters(
dimensions: const VideoDimensions(1280, 720),
)),
defaultScreenShareCaptureOptions: const ScreenShareCaptureOptions(
useiOSBroadcastExtension: true,
params: VideoParameters(
dimensions: VideoDimensionsPresets.h1080_169,
)),
defaultVideoPublishOptions: VideoPublishOptions(
simulcast: args.simulcast,
videoCodec: args.preferredCodec,
backupVideoCodec: BackupVideoCodec(
enabled: args.enableBackupVideoCodec,
),
videoEncoding: cameraEncoding,
screenShareEncoding: screenEncoding,
),
defaultScreenShareCaptureOptions: const ScreenShareCaptureOptions(
useiOSBroadcastExtension: true,
params: VideoParameters(
dimensions: VideoDimensionsPresets.h1080_169,
encoding: VideoEncoding(
maxBitrate: 3 * 1000 * 1000,
maxFramerate: 15,
))),
defaultCameraCaptureOptions: CameraCaptureOptions(
maxFrameRate: 30, params: _selectedVideoParameters),
e2eeOptions: e2eeOptions,
),
);
// Create a Listener before connecting
final listener = room.createListener();

// Try to connect to the room
// This will throw an Exception if it fails for any reason.
await room.connect(
args.url,
args.token,
fastConnectOptions: FastConnectOptions(
microphone: TrackOption(track: _audioTrack),
camera: TrackOption(track: _videoTrack),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^3.0.2
flutter_lints: ^4.0.0

# The following section is specific to Flutter.
flutter:
Expand Down
3 changes: 1 addition & 2 deletions lib/src/core/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
String? url;
String? token;

ConnectOptions connectOptions;
late ConnectOptions connectOptions;
RoomOptions roomOptions;
FastConnectOptions? fastConnectOptions;

Expand Down Expand Up @@ -143,7 +143,6 @@ class Engine extends Disposable with EventsEmittable<EngineEvent> {
}

Engine({
required this.connectOptions,
required this.roomOptions,
SignalClient? signalClient,
PeerConnectionCreate? peerConnectionCreate,
Expand Down
6 changes: 4 additions & 2 deletions lib/src/core/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
late EventsListener<SignalEvent> _signalListener;

Room({
@Deprecated('deprecated, please use connectOptions in room.connect()')
ConnectOptions connectOptions = const ConnectOptions(),
RoomOptions roomOptions = const RoomOptions(),
Engine? engine,
}) : engine = engine ??
Engine(
connectOptions: connectOptions,
roomOptions: roomOptions,
) {
//
Expand Down Expand Up @@ -152,10 +152,12 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
String url,
String token, {
ConnectOptions? connectOptions,
@Deprecated('deprecated, please use roomOptions in Room constructor')
RoomOptions? roomOptions,
FastConnectOptions? fastConnectOptions,
}) async {
roomOptions ??= this.roomOptions;
var roomOptions = this.roomOptions;
connectOptions ??= ConnectOptions();
if (roomOptions.e2eeOptions != null) {
if (!lkPlatformSupportsE2EE()) {
throw LiveKitE2EEException('E2EE is not supported on this platform');
Expand Down
5 changes: 5 additions & 0 deletions lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ class VideoPublishOptions extends PublishOptions {
/// Defaults to null.
final VideoEncoding? videoEncoding;

final VideoEncoding? screenShareEncoding;

/// Whether to enable simulcast or not.
/// https://blog.livekit.io/an-introduction-to-webrtc-simulcast-6c5f1f6402eb
/// Defaults to true.
Expand All @@ -249,6 +251,7 @@ class VideoPublishOptions extends PublishOptions {
super.stream,
this.videoCodec = defaultVideoCodec,
this.videoEncoding,
this.screenShareEncoding,
this.simulcast = true,
this.videoSimulcastLayers = const [],
this.screenShareSimulcastLayers = const [],
Expand All @@ -258,6 +261,7 @@ class VideoPublishOptions extends PublishOptions {

VideoPublishOptions copyWith({
VideoEncoding? videoEncoding,
VideoEncoding? screenShareEncoding,
bool? simulcast,
List<VideoParameters>? videoSimulcastLayers,
List<VideoParameters>? screenShareSimulcastLayers,
Expand All @@ -270,6 +274,7 @@ class VideoPublishOptions extends PublishOptions {
}) =>
VideoPublishOptions(
videoEncoding: videoEncoding ?? this.videoEncoding,
screenShareEncoding: screenShareEncoding ?? this.screenShareEncoding,
simulcast: simulcast ?? this.simulcast,
videoSimulcastLayers: videoSimulcastLayers ?? this.videoSimulcastLayers,
screenShareSimulcastLayers:
Expand Down
12 changes: 6 additions & 6 deletions lib/src/types/video_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import 'video_encoding.dart';
class VideoParameters implements Comparable<VideoParameters> {
final String? description;
final VideoDimensions dimensions;
final VideoEncoding encoding;
final VideoEncoding? encoding;

const VideoParameters({
this.description,
required this.dimensions,
required this.encoding,
this.description,
this.encoding,
});

// ----------------------------------------------------------------------
Expand All @@ -51,8 +51,8 @@ class VideoParameters implements Comparable<VideoParameters> {
// compare by dimension's area
final result = dimensions.area().compareTo(other.dimensions.area());
// if dimensions have equal area, compare by encoding
if (result == 0) {
return encoding.compareTo(other.encoding);
if (result == 0 && encoding != null && other.encoding != null) {
return encoding!.compareTo(other.encoding!);
}

return result;
Expand All @@ -65,7 +65,7 @@ class VideoParameters implements Comparable<VideoParameters> {
Map<String, dynamic> toMediaConstraintsMap() => <String, dynamic>{
'width': dimensions.width,
'height': dimensions.height,
'frameRate': encoding.maxFramerate,
'frameRate': encoding?.maxFramerate ?? 30,
};
}

Expand Down
26 changes: 16 additions & 10 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ class Utils {
encoding: VideoEncoding(
maxBitrate: math.max(
150 * 1000,
(original.encoding.maxBitrate /
(original.encoding!.maxBitrate /
(math.pow(scale, 2) *
(original.encoding.maxFramerate / fps)))
(original.encoding!.maxFramerate / fps)))
.floor(),
),
maxFramerate: fps,
Expand Down Expand Up @@ -274,13 +274,13 @@ class Utils {
String? codec,
}) {
assert(presets.isNotEmpty, 'presets should not be empty');
VideoEncoding result = presets.first.encoding;
VideoEncoding result = presets.first.encoding!;

// handle portrait by swapping dimensions
final size = dimensions.max();

for (final preset in presets) {
result = preset.encoding;
result = preset.encoding!;
if (preset.dimensions.width >= size) break;
}

Expand Down Expand Up @@ -319,11 +319,12 @@ class Utils {
}
final size = dimensions.min();
final rid = videoRids[i];

result.add(e.encoding.toRTCRtpEncoding(
rid: rid,
scaleResolutionDownBy: math.max(1, size / e.dimensions.min()),
));
if (e.encoding != null) {
result.add(e.encoding!.toRTCRtpEncoding(
rid: rid,
scaleResolutionDownBy: math.max(1, size / e.dimensions.min()),
));
}
});
return result;
}
Expand Down Expand Up @@ -390,6 +391,11 @@ class Utils {
options ??= const VideoPublishOptions();

VideoEncoding? videoEncoding = options.videoEncoding;

if (isScreenShare) {
videoEncoding = options.screenShareEncoding;
}

var scalabilityMode = options.scalabilityMode;

if ((videoEncoding == null &&
Expand Down Expand Up @@ -436,7 +442,7 @@ class Utils {
encodings.add(rtc.RTCRtpEncoding(
rid: videoRids[2 - i],
maxBitrate: videoEncoding.maxBitrate ~/ math.pow(3, i),
maxFramerate: original.encoding.maxFramerate,
maxFramerate: original.encoding!.maxFramerate,
));
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/widgets/screen_select_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class ScreenSelectDialog extends Dialog {
),
SizedBox(
width: double.infinity,
child: ButtonBar(
child: OverflowBar(
children: <Widget>[
MaterialButton(
child: const Text(
Expand Down
1 change: 0 additions & 1 deletion test/mock/e2e_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class E2EContainer {
engine = Engine(
signalClient: client,
peerConnectionCreate: MockPeerConnection.create,
connectOptions: const ConnectOptions(),
roomOptions: const RoomOptions(),
);
room = Room(engine: engine);
Expand Down

0 comments on commit 05800e7

Please sign in to comment.