Skip to content

Commit

Permalink
feat: expose DegradationPreference for publish options.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Jul 15, 2024
1 parent 8bd2653 commit 31eec97
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
12 changes: 12 additions & 0 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;
import 'e2ee/options.dart';
import 'events.dart';
import 'managers/event.dart';
import 'options.dart';
import 'proto/livekit_models.pb.dart' as lk_models;
import 'proto/livekit_rtc.pb.dart' as lk_rtc;
import 'types/other.dart';
Expand Down Expand Up @@ -261,3 +262,14 @@ extension ParticipantTypeExt on lk_models.ParticipantInfo_Kind {
lk_models.ParticipantInfo_Kind.AGENT: ParticipantKind.AGENT,
}[this]!;
}

extension DegradationPreferenceExt on DegradationPreference {
rtc.RTCDegradationPreference toRTCType() => {
DegradationPreference.disabled: rtc.RTCDegradationPreference.DISABLED,
DegradationPreference.maintainFramerate:
rtc.RTCDegradationPreference.MAINTAIN_FRAMERATE,
DegradationPreference.maintainResolution:
rtc.RTCDegradationPreference.MAINTAIN_RESOLUTION,
DegradationPreference.balanced: rtc.RTCDegradationPreference.BALANCED,
}[this]!;
}
34 changes: 23 additions & 11 deletions lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ class RoomOptions {
}
}

enum DegradationPreference {
disabled,
maintainFramerate,
maintainResolution,
balanced,
}

class BackupVideoCodec {
const BackupVideoCodec({
this.enabled = true,
Expand Down Expand Up @@ -227,6 +234,8 @@ class VideoPublishOptions extends PublishOptions {
/// Defaults to true.
final bool simulcast;

final DegradationPreference? degradationPreference;

final List<VideoParameters> videoSimulcastLayers;

final List<VideoParameters> screenShareSimulcastLayers;
Expand All @@ -235,17 +244,17 @@ class VideoPublishOptions extends PublishOptions {

final BackupVideoCodec backupVideoCodec;

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

VideoPublishOptions copyWith({
VideoEncoding? videoEncoding,
Expand All @@ -254,6 +263,7 @@ class VideoPublishOptions extends PublishOptions {
List<VideoParameters>? screenShareSimulcastLayers,
String? videoCodec,
BackupVideoCodec? backupVideoCodec,
DegradationPreference? degradationPreference,
String? scalabilityMode,
String? name,
String? stream,
Expand All @@ -266,6 +276,8 @@ class VideoPublishOptions extends PublishOptions {
screenShareSimulcastLayers ?? this.screenShareSimulcastLayers,
videoCodec: videoCodec ?? this.videoCodec,
backupVideoCodec: backupVideoCodec ?? this.backupVideoCodec,
degradationPreference:
degradationPreference ?? this.degradationPreference,
scalabilityMode: scalabilityMode ?? this.scalabilityMode,
name: name ?? this.name,
stream: stream ?? this.stream,
Expand Down
21 changes: 21 additions & 0 deletions lib/src/participant/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,15 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
await sender.setParameters(parameters);
}

if ([TrackSource.camera, TrackSource.screenShareVideo]
.contains(track.source)) {
var degradationPreference = publishOptions.degradationPreference ??
getDefaultDegradationPreference(
track,
);
track.setDegradationPreference(degradationPreference);
}

if (kIsWeb &&
lkBrowser() == BrowserType.firefox &&
track.kind == TrackType.AUDIO) {
Expand Down Expand Up @@ -397,6 +406,18 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
await pub.dispose();
}

DegradationPreference getDefaultDegradationPreference(LocalVideoTrack track) {
// a few of reasons we have different default paths:
// 1. without this, Chrome seems to aggressively resize the SVC video stating `quality-limitation: bandwidth` even when BW isn't an issue
// 2. since we are overriding contentHint to motion (to workaround L1T3 publishing), it overrides the default degradationPreference to `balanced`
VideoDimensions dimensions = track.currentOptions.params.dimensions;
if (track.source == TrackSource.screenShareVideo ||
dimensions.height >= 1080) {
return DegradationPreference.maintainResolution;
}
return DegradationPreference.balanced;
}

/// Convenience method to unpublish all tracks.
Future<void> unpublishAllTracks(
{bool notify = true, bool? stopOnUnpublish}) async {
Expand Down
10 changes: 10 additions & 0 deletions lib/src/track/local/video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:flutter/foundation.dart';

import 'package:collection/collection.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;
import 'package:livekit_client/src/extensions.dart';

import '../../events.dart';
import '../../exceptions.dart';
Expand Down Expand Up @@ -490,4 +491,13 @@ extension LocalVideoTrackExt on LocalVideoTrack {
simulcastCodecs[codec] = simulcastCodecInfo;
return simulcastCodecInfo;
}

void setDegradationPreference(DegradationPreference preference) {
final params = sender?.parameters;
if (params == null) {
return;
}
params.degradationPreference = preference.toRTCType();
sender?.setParameters(params);
}
}

0 comments on commit 31eec97

Please sign in to comment.