eventParser method

void eventParser(
  1. WsEvent wsEvent
)

Implementation

void eventParser(WsEvent wsEvent) {
  if (wsEvent.isRemoteSDPEventAnswer()) {
    logTime(
        "RemoteSDPEventAnswer wsEvent with sdp type= ${wsEvent.jsep!.type}",
        TAG);
    _janusResponseCallback?.onRemoteSDPEventAnswer(
        wsEvent.sender == _publisherHandleId, wsEvent.jsep!.sdp);
  } else if (wsEvent.isRemoteSDPEventOffer()) {
    int? opponentId = wsEvent.plugindata!.data.id;
    logTime(
        "RemoteSDPEventOffer wsEvent with sdp type= ${wsEvent.jsep!.type}, opponentId= $opponentId",
        TAG);

    if (wsEvent.plugindata?.data.streams != null) {
      wsEvent.plugindata?.data.streams?.forEach((stream) {
        if (stream.mid != null && stream.feedId != null) {
          subStreams[stream.mid!] = int.parse(stream.feedId.toString());
          streamTypes[stream.mid!] = stream.type!;
        }
      });
    }

    _janusResponseCallback?.onRemoteSDPEventOffer(
        opponentId, wsEvent.jsep?.sdp);

    if (wsEvent.plugindata?.data.videoroom == 'attached') {
      _janusResponseCallback?.onAttachedAsSubscriber();
    }
  } else if (wsEvent.isJoiningEvent()) {
    int? participantId = wsEvent.plugindata!.data.participant!.id;
    String? displayRole = wsEvent.plugindata!.data.participant!.display;
    ConferenceRole? conferenceRole;
    if (displayRole != null) {
      conferenceRole = ConferenceRole.values.firstWhere((e) =>
          e.toString() == 'ConferenceRole.${displayRole.toUpperCase()}');
    }
    logTime(
        "isJoiningEvent participantId= $participantId , conferenceRole= $displayRole",
        TAG);
    _janusResponseCallback?.onJoiningEvent(participantId!, conferenceRole);
  } else if (wsEvent.isJoinEvent()) {
    List<Publisher>? publishers = wsEvent.plugindata!.data.publishers;
    List<Subscriber>? subscribers = wsEvent.plugindata!.data.subscribers;
    logTime(
        "JoinEvent publishers= $publishers , subscribers= $subscribers", TAG);

    publishers?.forEach((publisher) {
      publisher.streams?.forEach((stream) {
        if (!stream.disabled!) {
          if (activePublisherStreams[publisher.id] == null) {
            activePublisherStreams[publisher.id!] = {};
          }
          activePublisherStreams[publisher.id]!.add(stream.mid!);
        }
      });
    });

    List<int> publishersList = convertPublishersToArray(publishers);
    List<int> subscribersList = convertSubscribersToArray(subscribers);
    _janusResponseCallback?.onJoinEvent(
        publishersList, subscribersList, wsEvent.sender!);
  } else if (wsEvent.isEventError()) {
    _janusResponseCallback?.onEventError(
        wsEvent.plugindata!.data.error, wsEvent.plugindata!.data.errorCode);
  } else if (wsEvent.isPublisherEvent()) {
    List<Publisher>? publishers = wsEvent.plugindata!.data.publishers;
    logTime("PublisherEvent wsEvent publishers= $publishers", TAG);

    publishers?.forEach((publisher) {
      publisher.streams?.forEach((stream) {
        if (!stream.disabled!) {
          if (activePublisherStreams[publisher.id] == null) {
            activePublisherStreams[publisher.id!] = {};
          }
          activePublisherStreams[publisher.id]!.add(stream.mid!);
        }
      });
    });

    _janusResponseCallback
        ?.onPublishedEvent(convertPublishersToArray(publishers));
  } else if (wsEvent.isUnPublishedEvent()) {
    int? usedId = wsEvent.plugindata!.data.unpublished;
    logTime("UnPublishedEvent  unpublished usedId= $usedId", TAG);
    _janusResponseCallback?.onUnPublishedEvent(usedId);
  } else if (wsEvent.isStartedEvent()) {
    logTime(
        "StartedEvent subscribed started= ${wsEvent.plugindata!.data.started}",
        TAG);
    _janusResponseCallback?.onStartedEvent(wsEvent.plugindata!.data.started);
  } else if (wsEvent.isLeaveParticipantEvent()) {
    final leaving = wsEvent.plugindata!.data.leaving;
    int? userId = leaving is String ? int.tryParse(leaving) : leaving;
    logTime("LeavePublisherEvent left userId= ${userId.toString()}", TAG);
    _janusResponseCallback?.onLeaveParticipantEvent(userId);
  } else if (wsEvent.isLeaveCurrentUserEvent() ||
      wsEvent.isLeftCurrentUserEvent()) {
    bool leavingOk = wsEvent.plugindata!.data.leaving == "ok" ||
        wsEvent.plugindata!.data.left == "ok";
    logTime("isLeaveCurrentUserEvent leavingOk? $leavingOk", TAG);
    _janusResponseCallback?.onLeaveCurrentUserEvent(leavingOk);
  } else if (wsEvent.isStreamChangedEvent()) {
    var userId = subStreams[wsEvent.plugindata?.data.mId];
    var streamType = intToStreamType(wsEvent.plugindata?.data.subStream);
    logTime(
        "isStreamChangedEvent userId: $userId, streamType: $streamType", TAG);
    _janusResponseCallback?.onSubStreamChangedForOpponent(
        userId ?? -1, streamType);
  } else if (wsEvent.isLayerChangedEvent()) {
    var userId = subStreams[wsEvent.plugindata?.data.mId];
    var layer = wsEvent.plugindata?.data.layer;
    logTime("isLayerChangedEvent userId: $userId, layer: $layer", TAG);
    _janusResponseCallback?.onLayerChangedForOpponent(
        userId ?? -1, layer ?? -1);
  }
}