syncTranslationStateAfterBreakoutChange function

Future<void> syncTranslationStateAfterBreakoutChange(
  1. Map<String, dynamic> translationProducerMap,
  2. Map<String, String> speakerIdByProducerId,
  3. TranslationConsumerSwitchParameters parameters
)

Handle breakout room changes for translation audio.

When a user moves between breakout rooms, we need to re-evaluate which translation streams should be active:

  • Resume original audio for speakers now in our room (if no translation active)
  • The breakout room audio logic (resumePauseAudioStreams) will handle pausing speakers not in our room

This function is called after breakout room updates to sync translation state.

Implementation

Future<void> syncTranslationStateAfterBreakoutChange(
  Map<String, dynamic> translationProducerMap,
  Map<String, String> speakerIdByProducerId,
  TranslationConsumerSwitchParameters parameters,
) async {
  try {
    final consumerTransports = parameters.consumerTransports;

    // For each active translation, check if the speaker is now in our room or not
    for (final entry in translationProducerMap.entries) {
      final originalProducerId = entry.key;
      final langMap = entry.value;

      final speakerId = speakerIdByProducerId[originalProducerId];
      if (speakerId == null) continue;

      final inMyRoom = isSpeakerInMyBreakoutRoom(speakerId, parameters);
      final hasTranslation = langMap.isNotEmpty;

      // Find the original producer's consumer
      TransportType? originalTransport;
      try {
        originalTransport = consumerTransports.firstWhere(
          (t) =>
              t.producerId == originalProducerId && t.consumer.kind == 'audio',
        );
      } catch (e) {
        continue; // Not found
      }

      if (inMyRoom && hasTranslation) {
        // Speaker is in our room and we have translation - original should be paused
        if (!originalTransport.consumer.paused) {
          await pauseOriginalProducer(PauseOriginalProducerOptions(
            originalProducerId: originalProducerId,
            speakerId: speakerId,
            parameters: parameters,
          ));
        }
      }
      // Note: If speaker is NOT in our room, the breakout room audio logic
      // (resumePauseAudioStreams) will handle pausing their audio.
    }
  } catch (e) {
    // Handle error silently
  }
}