connectRecvTransport function

Future<void> connectRecvTransport(
  1. ConnectRecvTransportOptions options
)

Establishes a connection for the receiving transport to consume media from a remote producer and resumes the consumer stream.

Parameters:

  • options (ConnectRecvTransportOptions): Contains all necessary details to connect and manage the receiving transport:
    • consumer (Consumer): The media consumer instance.
    • consumerTransport (Transport): The transport associated with the consumer.
    • remoteProducerId (String): The ID of the producer being consumed.
    • serverConsumerTransportId (String): The server-generated transport ID.
    • nsock (io.Socket): The socket instance for real-time communication.
    • parameters (ConnectRecvTransportParameters): Includes parameters for updating transports, resuming the consumer, and device details.

Workflow:

  1. Consumption Initiation: Emits a consume event to the socket to initiate media consumption, passing in the device's rtpCapabilities.
  2. Consumer Transport Management: Adds the transport and consumer details to the consumerTransports list for tracking active connections.
  3. Stream Resumption: If the consumer is successfully created, emits a consumer-resume event to resume media reception and triggers consumerResume to update the UI and media handling.

Returns:

  • A Future<void> that completes once the transport is connected and the consumer is resumed.

Example Usage:

final options = ConnectRecvTransportOptions(
  consumer: myConsumer,
  consumerTransport: myConsumerTransport,
  remoteProducerId: 'producer-id-123',
  serverConsumerTransportId: 'transport-id-abc',
  nsock: mySocket,
  parameters: myConnectRecvTransportParameters,
);

connectRecvTransport(options).then(() {
  print('Receiving transport connected and consumer resumed');
}).catchError((error) {
  print('Error connecting transport: $error');
});

Error Handling:

  • Logs any errors encountered during the consumption or resumption process in debug mode.

Implementation

Future<void> connectRecvTransport(ConnectRecvTransportOptions options) async {
  var parameters = options.parameters.getUpdatedAllParams();

  // Extract parameters
  final consumer = options.consumer;
  final consumerTransports = parameters.consumerTransports;
  final updateConsumerTransports = parameters.updateConsumerTransports;
  final consumerResume = parameters.consumerResume;

  final nsock = options.nsock;
  final consumerTransport = options.consumerTransport;
  final remoteProducerId = options.remoteProducerId;
  final serverConsumerTransportId = options.serverConsumerTransportId;

  try {
    // Update consumerTransports array with the new consumer
    consumerTransports.add(TransportType(
      consumerTransport: consumerTransport,
      serverConsumerTransportId: serverConsumerTransportId,
      producerId: remoteProducerId,
      consumer: consumer,
      socket_: nsock,
    ));

    updateConsumerTransports(consumerTransports);

    // Extract track from the consumer
    MediaStream stream = consumer.stream;

    // Emit 'consumer-resume' event to signal consumer resumption
    nsock.emitWithAck('consumer-resume', {
      'serverConsumerId': consumer.id,
    }, ack: (resumeData) async {
      if (resumeData['resumed'] == true) {
        // Consumer resumed and ready to be used
        try {
          await consumerResume(ConsumerResumeOptions(
            stream: stream,
            consumer: consumer,
            kind: consumer.kind ?? '',
            remoteProducerId: remoteProducerId,
            parameters: parameters,
            nsock: nsock,
          ));
        } catch (error) {
          // Handle error
          if (kDebugMode) {
            print('consumerResume error: $error');
          }
        }
      }
    });
  } catch (error) {
    // Handle error
    if (kDebugMode) {
      print('consume error: $error');
    }
  }
}