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

Add max bitrate calculation for AV1 and VP9. #348

Merged
merged 1 commit into from
Sep 2, 2023
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
1 change: 1 addition & 0 deletions lib/src/participant/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
isScreenShare: track.source == TrackSource.screenShareVideo,
dimensions: dimensions,
options: publishOptions,
codec: publishOptions.videoCodec,
);

logger.fine('Using encodings: ${encodings?.map((e) => e.toMap())}');
Expand Down
9 changes: 9 additions & 0 deletions lib/src/types/video_encoding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ class VideoEncoding implements Comparable<VideoEncoding> {
required this.maxBitrate,
});

VideoEncoding copyWith({
int? maxFramerate,
int? maxBitrate,
}) =>
VideoEncoding(
maxFramerate: maxFramerate ?? this.maxFramerate,
maxBitrate: maxBitrate ?? this.maxBitrate,
);

@override
String toString() =>
'${runtimeType}(maxFramerate: ${maxFramerate}, maxBitrate: ${maxBitrate})';
Expand Down
23 changes: 23 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class Utils {
required bool isScreenShare,
required VideoDimensions dimensions,
required List<VideoParameters> presets,
String? codec,
}) {
assert(presets.isNotEmpty, 'presets should not be empty');
VideoEncoding result = presets.first.encoding;
Expand All @@ -283,6 +284,26 @@ class Utils {
if (preset.dimensions.width >= size) break;
}

// presets are based on the assumption of vp8 as a codec
// for other codecs we adjust the maxBitrate if no specific videoEncoding has been provided
// users should override these with ones that are optimized for their use case
// NOTE: SVC codec bitrates are inclusive of all scalability layers. while
// bitrate for non-SVC codecs does not include other simulcast layers.
if (codec != null) {
switch (codec) {
case 'av1':
result =
result.copyWith(maxBitrate: (result.maxBitrate * 0.7) as int);
break;
case 'vp9':
result =
result.copyWith(maxBitrate: (result.maxBitrate * 0.85) as int);
break;
default:
break;
}
}

return result;
}

Expand Down Expand Up @@ -371,6 +392,7 @@ class Utils {
required bool isScreenShare,
VideoDimensions? dimensions,
VideoPublishOptions? options,
String? codec,
}) {
options ??= const VideoPublishOptions();

Expand All @@ -397,6 +419,7 @@ class Utils {
isScreenShare: isScreenShare,
dimensions: dimensions,
presets: presets,
codec: codec,
);

logger.fine('using video encoding', videoEncoding);
Expand Down
Loading