findAudioTransceiver static method

Future<RTCRtpTransceiver?> findAudioTransceiver(
  1. RTCPeerConnection peerConnection
)

Finds the audio transceiver from a peer connection. This method attempts multiple strategies to identify the audio transceiver, similar to the Android SDK implementation.

peerConnection The RTCPeerConnection to search for audio transceiver Returns the audio RTCRtpTransceiver if found, null otherwise

Implementation

static Future<RTCRtpTransceiver?> findAudioTransceiver(
  RTCPeerConnection peerConnection,
) async {
  try {
    final transceivers = await peerConnection.getTransceivers();

    GlobalLogger().d(
      'CodecUtils :: Searching for audio transceiver among ${transceivers.length} transceivers',
    );

    for (final transceiver in transceivers) {
      // Try sender track kind first
      final senderKind = transceiver.sender.track?.kind;
      if (senderKind == 'audio') {
        GlobalLogger().d(
          'CodecUtils :: Found audio transceiver via sender track kind',
        );
        return transceiver;
      }

      // Fallback to receiver track kind
      final receiverKind = transceiver.receiver.track?.kind;
      if (receiverKind == 'audio') {
        GlobalLogger().d(
          'CodecUtils :: Found audio transceiver via receiver track kind',
        );
        return transceiver;
      }
    }

    GlobalLogger().w(
      'CodecUtils :: No audio transceiver found among ${transceivers.length} transceivers',
    );
    return null;
  } catch (e) {
    GlobalLogger().e('CodecUtils :: Error finding audio transceiver: $e');
    return null;
  }
}