Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose DegradationPreference for publish options. #556

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 19 additions & 7 deletions lib/src/participant/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,13 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
track.codec = publishOptions.videoCodec;
}

// prefer to maintainResolution for screen share
if (track.source == TrackSource.screenShareVideo) {
var sender = track.transceiver!.sender;
var parameters = sender.parameters;
parameters.degradationPreference =
rtc.RTCDegradationPreference.MAINTAIN_RESOLUTION;
await sender.setParameters(parameters);
if ([TrackSource.camera, TrackSource.screenShareVideo]
.contains(track.source)) {
var degradationPreference = publishOptions.degradationPreference ??
getDefaultDegradationPreference(
track,
);
track.setDegradationPreference(degradationPreference);
}

if (kIsWeb &&
Expand Down Expand Up @@ -397,6 +397,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 @@ -17,6 +17,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';
import '../../logger.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);
}
}
Loading