getParticipantMedia function

Future<MediaStream?> getParticipantMedia(
  1. GetParticipantMediaOptions options
)

Retrieves the media stream of a participant by ID or name.

Implementation

Future<MediaStream?> getParticipantMedia(
    GetParticipantMediaOptions options) async {
  try {
    MediaStream? stream;

    // Get required parameters
    final allVideoStreams = options.parameters.allVideoStreams;
    final allAudioStreams = options.parameters.allAudioStreams;
    final participants = options.parameters.participants;

    // Search by ID if provided
    if (options.id.isNotEmpty) {
      if (options.kind == 'video') {
        // Find video stream by producer ID
        try {
          final videoStreamObj = allVideoStreams.firstWhere(
            (obj) => obj.producerId == options.id,
          );
          stream = videoStreamObj.stream;
        } catch (e) {
          // Not found
          stream = null;
        }
      } else if (options.kind == 'audio') {
        // Find audio stream by producer ID
        try {
          final audioStreamObj = allAudioStreams.firstWhere(
            (obj) => obj.producerId == options.id,
          );
          stream = audioStreamObj.stream;
        } catch (e) {
          // Not found
          stream = null;
        }
      }
    } else if (options.name.isNotEmpty) {
      // Search by name if ID not provided
      try {
        final participant = participants.firstWhere(
          (part) => part.name == options.name,
        );

        final participantId = participant.id ?? '';

        if (options.kind == 'video') {
          // Find video stream by participant ID
          try {
            final videoStreamObj = allVideoStreams.firstWhere(
              (obj) => obj.producerId == participantId,
            );
            stream = videoStreamObj.stream;
          } catch (e) {
            // Not found
            stream = null;
          }
        } else if (options.kind == 'audio') {
          // Find audio stream by participant ID
          try {
            final audioStreamObj = allAudioStreams.firstWhere(
              (obj) => obj.producerId == participantId,
            );
            stream = audioStreamObj.stream;
          } catch (e) {
            // Not found
            stream = null;
          }
        }
      } catch (e) {
        // Participant not found
        stream = null;
      }
    }

    return stream;
  } catch (e) {
    // Return null if an error occurs
    if (kDebugMode) {
      print('Error getting participant media: $e');
    }
    return null;
  }
}