load method

Future<void> load({
  1. required RtpCapabilities routerRtpCapabilities,
})

Initialize the Device.

Implementation

Future<void> load({
  required RtpCapabilities routerRtpCapabilities,
}) async {
  _logger.debug(
      'load() [routerRtpCapabilities:${routerRtpCapabilities.toString()}]');

  routerRtpCapabilities = RtpCapabilities.copy(routerRtpCapabilities);

  // Temporal handler to get its capabilities.
  HandlerInterface? handler;

  try {
    if (_loaded) {
      throw ('already loaded');
    }

    // This may throw.
    Ortc.validateRtpCapabilities(routerRtpCapabilities);

    handler = HandlerInterface.handlerFactory();

    RtpCapabilities nativeRtpCapabilities =
        await handler.getNativeRtpCapabilities();

    _logger
        .debug('load() | got native RTP capabilities:$nativeRtpCapabilities');

    // This may throw.
    Ortc.validateRtpCapabilities(nativeRtpCapabilities);

    // Get extended RTP capabilities.
    _extendedRtpCapabilities = Ortc.getExtendedRtpCapabilities(
        nativeRtpCapabilities, routerRtpCapabilities);

    _logger.debug(
        'load() | got extended RTP capabilities:$_extendedRtpCapabilities');

    // Check wether we can produce audio/video.
    _canProduceByKind = CanProduceByKind(
      audio: Ortc.canSend(
          RTCRtpMediaType.RTCRtpMediaTypeAudio, _extendedRtpCapabilities!),
      video: Ortc.canSend(
          RTCRtpMediaType.RTCRtpMediaTypeVideo, _extendedRtpCapabilities!),
    );

    // Generate our receiving RTP capabilities for receiving media.
    _recvRtpCapabilities =
        Ortc.getRecvRtpCapabilities(_extendedRtpCapabilities!);

    // This may throw.
    Ortc.validateRtpCapabilities(_recvRtpCapabilities);

    _logger.debug(
        'load() | got receiving RTP capabilities:$_recvRtpCapabilities');

    // Generate our SCTP capabilities.
    _sctpCapabilities = handler.getNativeSctpCapabilities();

    _logger.debug('load() | got native SCTP capabilities:$_sctpCapabilities');

    // This may throw.
    Ortc.validateSctpCapabilities(_sctpCapabilities);

    _logger.debug('load() successed');

    _loaded = true;

    await handler.close();
  } catch (error) {
    if (handler != null) {
      await handler.close();
    }

    rethrow;
  }
}