Skip to content

Commit

Permalink
Add max bitrate calculation for AV1 and VP9. (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc authored Sep 2, 2023
1 parent 31cebff commit 0a1e881
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
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

0 comments on commit 0a1e881

Please sign in to comment.