Skip to content

Commit

Permalink
handling of incompatible published codecs (#391)
Browse files Browse the repository at this point in the history
* Improve handling of incompatible published codecs.

* revert changes.

* update.

* check mineType.
  • Loading branch information
cloudwebrtc authored Nov 1, 2023
1 parent c24053b commit 4192756
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
63 changes: 40 additions & 23 deletions lib/src/participant/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
'Compute encodings with resolution: ${dimensions}, options: ${publishOptions}');

// Video encodings and simulcasts
final encodings = Utils.computeVideoEncodings(
var encodings = Utils.computeVideoEncodings(
isScreenShare: track.source == TrackSource.screenShareVideo,
dimensions: dimensions,
options: publishOptions,
Expand All @@ -202,35 +202,28 @@ class LocalParticipant extends Participant<LocalTrackPublication> {

logger.fine('Using encodings: ${encodings?.map((e) => e.toMap())}');

var simulcastCodecs = <lk_rtc.SimulcastCodec>[
lk_rtc.SimulcastCodec(
codec: publishOptions.videoCodec,
cid: track.getCid(),
),
];

if (publishOptions.backupCodec != null &&
publishOptions.backupCodec!.codec != publishOptions.videoCodec) {
simulcastCodecs.add(lk_rtc.SimulcastCodec(
codec: publishOptions.backupCodec!.codec.toLowerCase(),
cid: '',
));
}

final layers = Utils.computeVideoLayers(
dimensions,
encodings,
isSVC,
);

logger.fine('Video layers: ${layers.map((e) => e)}');
var simulcastCodecs = <lk_rtc.SimulcastCodec>[];

if (publishOptions.backupCodec != null &&
publishOptions.backupCodec!.codec != publishOptions.videoCodec) {
simulcastCodecs = <lk_rtc.SimulcastCodec>[
lk_rtc.SimulcastCodec(
codec: publishOptions.videoCodec,
cid: track.getCid(),
),
lk_rtc.SimulcastCodec(
codec: publishOptions.backupCodec!.codec.toLowerCase(),
cid: '',
),
];
} else {
simulcastCodecs = <lk_rtc.SimulcastCodec>[
lk_rtc.SimulcastCodec(
codec: publishOptions.videoCodec,
cid: track.getCid(),
),
];
}

final trackInfo = await room.engine.addTrack(
cid: track.getCid(),
Expand All @@ -250,6 +243,30 @@ class LocalParticipant extends Participant<LocalTrackPublication> {

await track.start();

String? primaryCodecMime;
for (var codec in trackInfo.codecs) {
primaryCodecMime ??= codec.mimeType;
}

if (primaryCodecMime != null) {
final updatedCodec = mimeTypeToVideoCodecString(primaryCodecMime);
if (updatedCodec != publishOptions.videoCodec) {
logger.fine(
'requested a different codec than specified by serverRequested: ${publishOptions.videoCodec}, server: ${updatedCodec}',
);
publishOptions = publishOptions.copyWith(
videoCodec: updatedCodec,
);
// recompute encodings since bitrates/etc could have changed
encodings = Utils.computeVideoEncodings(
isScreenShare: track.source == TrackSource.screenShareVideo,
dimensions: dimensions,
options: publishOptions,
codec: publishOptions.videoCodec,
);
}
}

final transceiverInit = rtc.RTCRtpTransceiverInit(
direction: rtc.TransceiverDirection.SendOnly,
sendEncodings: encodings,
Expand Down
11 changes: 11 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,14 @@ class ScalabilityMode {
return 'L${spatial}T${temporal}${suffix ?? ''}';
}
}

String mimeTypeToVideoCodecString(String mimeType) {
if (!mimeType.contains('/') && mimeType.split('/').length != 2) {
throw Exception('Invalid mimeType: $mimeType');
}
final codec = mimeType.split('/')[1].toLowerCase();
if (!videoCodecs.contains(codec)) {
throw Exception('Video codec not supported: $codec');
}
return codec;
}

0 comments on commit 4192756

Please sign in to comment.