getSupportedAudioCodecs method

Future<List<AudioCodec>> getSupportedAudioCodecs()

Returns a list of audio codecs supported by WebRTC for this device.

This method queries the native WebRTC RTP sender capabilities API to retrieve the actual audio codecs supported by the WebRTC library. This lightweight query uses the platform's built-in codec discovery without creating any peer connections. The returned codec list matches exactly what WebRTC will use during actual calls.

Common codecs returned include: Opus, PCMU, PCMA, G722, RED, CN, and telephone-event.

Usage:

  • Call this method before initiating a call to get the list of supported WebRTC codecs
  • Use the returned list to construct your preferred codec order
  • Pass your preferences to newInvite or acceptCall via the preferredCodecs parameter

Returns a Future<List<AudioCodec>> containing the audio codecs supported by the device. Returns an empty list if codec detection fails.

Example:

// Query supported codecs before making a call
final supportedCodecs = await telnyxClient.getSupportedAudioCodecs();
print('WebRTC supports: ${supportedCodecs.map((c) => c.mimeType)}');

// Prefer Opus, then PCMU as fallback
final preferredCodecs = supportedCodecs.where(
  (c) => c.mimeType == 'audio/opus' || c.mimeType == 'audio/PCMU'
).toList();

// Use in call
final call = telnyxClient.newInvite(
  'John',
  '+1234567890',
  'sip:destination',
  'state',
  preferredCodecs: preferredCodecs,
);

Implementation

Future<List<AudioCodec>> getSupportedAudioCodecs() async {
  try {
    GlobalLogger().d('Querying WebRTC audio codecs via RTP capabilities');
    // Convert to AudioCodec list
    final codecs = await CodecUtils.getSupportedAudioCodecs();

    GlobalLogger().d(
      'Retrieved ${codecs.length} audio codecs: ${codecs.map((c) => c.mimeType).toList()}',
    );

    return codecs;
  } catch (e) {
    GlobalLogger().e('Error retrieving supported audio codecs: $e');
    return [];
  }
}