Skip to content

Commit

Permalink
Simplify backupCodec setting. (#396)
Browse files Browse the repository at this point in the history
* Simplify backupCodec setting.

* Update options.dart

* remove enableBackupCodec for VideoPublishOptions.

* update.

* Enabled backupcodec by defualt and disable it when E2EE is enabled.
  • Loading branch information
cloudwebrtc authored Dec 13, 2023
1 parent 3ef53d4 commit 1db1439
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 59 deletions.
45 changes: 5 additions & 40 deletions example/lib/pages/connect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class _ConnectPageState extends State<ConnectPage> {
bool _e2ee = false;
bool _multiCodec = false;
String _preferredCodec = 'Preferred Codec';
String _backupCodec = 'VP8';

@override
void initState() {
Expand Down Expand Up @@ -143,6 +142,8 @@ class _ConnectPageState extends State<ConnectPage> {
preferredCodec = _preferredCodec;
}

bool enableBackupVideoCodec = ['VP9', 'AV1'].contains(preferredCodec);

// create new room
final room = Room(
roomOptions: RoomOptions(
Expand All @@ -154,6 +155,9 @@ class _ConnectPageState extends State<ConnectPage> {
defaultVideoPublishOptions: VideoPublishOptions(
simulcast: _simulcast,
videoCodec: preferredCodec,
backupVideoCodec: BackupVideoCodec(
enabled: enableBackupVideoCodec,
),
),
defaultScreenShareCaptureOptions: const ScreenShareCaptureOptions(
useiOSBroadcastExtension: true,
Expand Down Expand Up @@ -396,45 +400,6 @@ class _ConnectPageState extends State<ConnectPage> {
}).toList(),
)
])),
if (_multiCodec &&
_preferredCodec != 'Preferred Codec' &&
['av1', 'vp9'].contains(_preferredCodec.toLowerCase()))
Padding(
padding: const EdgeInsets.only(bottom: 25),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text('Backup Codec:'),
DropdownButton<String>(
value: _backupCodec,
icon: const Icon(
Icons.arrow_drop_down,
color: Colors.blue,
),
elevation: 16,
style: const TextStyle(color: Colors.blue),
underline: Container(
height: 2,
color: Colors.blueAccent,
),
onChanged: (String? value) {
// This is called when the user selects an item.
setState(() {
_backupCodec = value!;
});
},
items: [
'Backup Codec',
'VP8',
'H264'
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
)
])),
ElevatedButton(
onPressed: _busy ? null : () => _connect(context),
child: Row(
Expand Down
9 changes: 9 additions & 0 deletions lib/src/core/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,16 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
}
_e2eeManager = E2EEManager(roomOptions.e2eeOptions!.keyProvider);
_e2eeManager!.setup(this);

// Disable backup codec when e2ee is enabled
roomOptions = roomOptions.copyWith(
defaultVideoPublishOptions:
roomOptions.defaultVideoPublishOptions.copyWith(
backupVideoCodec: const BackupVideoCodec(enabled: false),
),
);
}

return engine.connect(
url,
token,
Expand Down
41 changes: 31 additions & 10 deletions lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'track/track.dart';
import 'types/other.dart';
import 'types/video_encoding.dart';
import 'types/video_parameters.dart';
import 'utils.dart';

class TrackOption<E extends Object, T extends Object> {
final E? enabled;
Expand Down Expand Up @@ -162,21 +163,41 @@ class RoomOptions {
}

class BackupVideoCodec {
BackupVideoCodec({
this.codec = 'vp8',
const BackupVideoCodec({
this.enabled = true,
this.codec = defaultVideoCodec,
this.encoding,
this.simulcast = true,
});
String codec;
final bool enabled;
final String codec;
// optional, when unset, it'll be computed based on dimensions and codec
VideoEncoding? encoding;
bool simulcast;
final VideoEncoding? encoding;
final bool simulcast;
BackupVideoCodec copyWith({
bool? enabled,
String? codec,
VideoEncoding? encoding,
bool? simulcast,
}) {
return BackupVideoCodec(
enabled: enabled ?? this.enabled,
codec: codec ?? this.codec,
encoding: encoding ?? this.encoding,
simulcast: simulcast ?? this.simulcast,
);
}
}

/// Options used when publishing video.
class VideoPublishOptions {
static const defaultCameraName = 'camera';
static const defaultScreenShareName = 'screenshare';
static const defualtBackupVideoCodec = BackupVideoCodec(
enabled: true,
codec: defaultVideoCodec,
simulcast: true,
);

/// The video codec to use.
final String videoCodec;
Expand All @@ -200,16 +221,16 @@ class VideoPublishOptions {

final String? scalabilityMode;

final BackupVideoCodec? backupCodec;
final BackupVideoCodec backupVideoCodec;

const VideoPublishOptions({
this.videoCodec = 'H264',
this.videoCodec = defaultVideoCodec,
this.videoEncoding,
this.simulcast = true,
this.videoSimulcastLayers = const [],
this.screenShareSimulcastLayers = const [],
this.backupVideoCodec = defualtBackupVideoCodec,
this.name,
this.backupCodec,
this.scalabilityMode,
});

Expand All @@ -219,7 +240,7 @@ class VideoPublishOptions {
List<VideoParameters>? videoSimulcastLayers,
List<VideoParameters>? screenShareSimulcastLayers,
String? videoCodec,
BackupVideoCodec? backupCodec,
BackupVideoCodec? backupVideoCodec,
String? scalabilityMode,
}) =>
VideoPublishOptions(
Expand All @@ -229,7 +250,7 @@ class VideoPublishOptions {
screenShareSimulcastLayers:
screenShareSimulcastLayers ?? this.screenShareSimulcastLayers,
videoCodec: videoCodec ?? this.videoCodec,
backupCodec: backupCodec ?? this.backupCodec,
backupVideoCodec: backupVideoCodec ?? this.backupVideoCodec,
scalabilityMode: scalabilityMode ?? this.scalabilityMode,
);

Expand Down
14 changes: 5 additions & 9 deletions lib/src/participant/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,7 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
if (!room.roomOptions.dynacast) {
room.engine.roomOptions = room.roomOptions.copyWith(dynacast: true);
}
if (publishOptions.backupCodec == null) {
publishOptions = publishOptions.copyWith(
backupCodec: BackupVideoCodec(),
);
}

if (publishOptions.scalabilityMode == null) {
publishOptions = publishOptions.copyWith(
scalabilityMode: 'L3T3_KEY',
Expand Down Expand Up @@ -208,10 +204,10 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
),
];

if (publishOptions.backupCodec != null &&
publishOptions.backupCodec!.codec != publishOptions.videoCodec) {
if (publishOptions.backupVideoCodec.enabled &&
publishOptions.backupVideoCodec.codec != publishOptions.videoCodec) {
simulcastCodecs.add(lk_rtc.SimulcastCodec(
codec: publishOptions.backupCodec!.codec.toLowerCase(),
codec: publishOptions.backupVideoCodec.codec.toLowerCase(),
cid: '',
));
}
Expand Down Expand Up @@ -318,7 +314,7 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
track: track,
);
addTrackPublication(pub);
pub.backupVideoCodec = publishOptions.backupCodec;
pub.backupVideoCodec = publishOptions.backupVideoCodec;

// did publish
await track.onPublish();
Expand Down
2 changes: 2 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -631,3 +631,5 @@ String mimeTypeToVideoCodecString(String mimeType) {
}
return codec;
}

const defaultVideoCodec = 'vp8';

0 comments on commit 1db1439

Please sign in to comment.