publishAdditionalCodecForPublication method

Future<void> publishAdditionalCodecForPublication(
  1. LocalTrackPublication<LocalTrack> publication,
  2. String backupCodec
)

Implementation

Future<void> publishAdditionalCodecForPublication(
  LocalTrackPublication publication,
  String backupCodec,
) async {
  if (publication.track is! LocalVideoTrack) {
    throw Exception('multi-codec simulcast is supported only for video');
  }
  var track = publication.track as LocalVideoTrack;

  final backupCodecOpts = publication.backupVideoCodec;
  if (backupCodecOpts == null) {
    throw Exception('backupCodec settings not specified');
  }

  var options = room.roomOptions.defaultVideoPublishOptions;
  options = options.copyWith(simulcast: backupCodecOpts.simulcast);

  if (backupCodec.toLowerCase() == publication.track?.codec?.toLowerCase()) {
    // not needed, same codec already published
    return;
  }

  if (backupCodec != backupCodecOpts.codec.toLowerCase()) {
    logger.warning(
      'requested a different codec than specified as backup serverRequested: ${backupCodec}, backup: ${backupCodecOpts.codec}',
    );
  }

  var encodings = Utils.computeTrackBackupEncodings(track, backupCodecOpts);
  if (encodings == null) {
    logger.fine(
        'backup codec has been disabled, ignoring request to add additional codec for track');
    return;
  }

  var simulcastTrack = track.addSimulcastTrack(backupCodec, encodings);
  var dimensions = track.currentOptions.params.dimensions;

  var layers = Utils.computeVideoLayers(
      dimensions, encodings, isSVCCodec(backupCodec));

  simulcastTrack.sender = await room.engine.createSimulcastTransceiverSender(
    track,
    simulcastTrack,
    encodings,
    publication,
    backupCodec,
  );

  var cid = simulcastTrack.sender!.senderId;

  final trackInfo = await room.engine.addTrack(
      cid: cid,
      name: options.name ??
          (track.source == TrackSource.screenShareVideo
              ? VideoPublishOptions.defaultScreenShareName
              : VideoPublishOptions.defaultCameraName),
      kind: track.kind,
      source: track.source.toPBType(),
      dimensions: dimensions,
      videoLayers: layers,
      sid: publication.sid,
      simulcastCodecs: <lk_rtc.SimulcastCodec>[
        lk_rtc.SimulcastCodec(
          codec: backupCodec.toLowerCase(),
          cid: cid,
        ),
      ],
      videoCodec: backupCodec);

  await room.engine.negotiate();

  logger.info(
      'published backupCodec $backupCodec for track ${track.sid}, track info ${trackInfo}');
}