startCallOneToMany method

dynamic startCallOneToMany({
  1. String? from,
  2. Map<String, dynamic>? customData,
  3. List<String>? to,
  4. String? mediaType,
  5. String? mcToken,
  6. String? callType,
  7. String? sessionType,
  8. bool? isPublicBroadcast,
  9. String? broadcastType,
  10. String? authorizationToken,
})

Implementation

startCallOneToMany(
    {String? from,
    Map<String, dynamic>? customData,
    List<String>? to,
    String? mediaType,
    String? mcToken,
    String? callType,
    String? sessionType,
    bool? isPublicBroadcast,
    String? broadcastType,
    String? authorizationToken}) async {
  // bool screenShareWithAppAudio = true;
  print("startcallonetomany  $broadcastType $customData");
  userType = "caller";
  broadcastType = broadcastType;

  if (kIsWeb) {
    print("here for camera");
    screenShareflag = false;
    isCallInProgress = true;
    startCallObject = {"mediaType": mediaType, "sessionType": sessionType};
    callend = false;
    print("i am here in start call oneto one");

    starttime = DateTime.now();
    iscallonetomany = true;
    print("i am here in  start call ne to many");
    if (broadcastType == "appaudioandcamera") {
      print("here in appaudioandcamera");
      ismultisession = true;
      screenSessionUUID = _generateMd5(
          DateTime.now().millisecondsSinceEpoch.toString() +
              tenantID +
              ref_id! +
              "12345678");
      callSessionUUID = _generateMd5(
          DateTime.now().millisecondsSinceEpoch.toString() +
              tenantID +
              ref_id!);
      if (kIsWeb) {
        DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); // e.g. "iPod7,1"

        WebBrowserInfo webBrowserInfo = await deviceInfo.webBrowserInfo;
        String ua = webBrowserInfo.userAgent!.toLowerCase();
        if (ua.indexOf('safari') != -1) {
          if (ua.indexOf('chrome') > -1) {
            // inCase of Chrome
            _peerConnection = await _createPeerConnectiononetomanycallDial(
                mediaType!, "camera");

            _peerConnectionformultisession =
                await _createPeerConnectiononetomanycallDialmultisession(
                    mediaType, "appaudio", false);
          } else {
            //safari
            _peerConnectionformultisession =
                await _createPeerConnectiononetomanycallDialmultisession(
                    mediaType!, "appaudio", false);
            _peerConnection = await _createPeerConnectiononetomanycallDial(
                mediaType, "camera");
          }
        }
      } else {
        _peerConnection = await _createPeerConnectiononetomanycallDial(
            mediaType!, "camera");

        _peerConnectionformultisession =
            await _createPeerConnectiononetomanycallDialmultisession(
                mediaType, "appaudio", false);
      }
      _createOfferonetomanymultisession(
          from!,
          to!,
          mcToken!,
          mediaType!,
          callType!,
          sessionType!,
          isPublicBroadcast!,
          broadcastType!,
          customData,
          screenSessionUUID,
          callSessionUUID);
    } else if (broadcastType == "micaudioandcamera") {
      print("here in micaudioandcamera");
      ismultisession = true;
      screenSessionUUID = _generateMd5(
          DateTime.now().millisecondsSinceEpoch.toString() +
              tenantID +
              ref_id! +
              "12345678");
      callSessionUUID = _generateMd5(
          DateTime.now().millisecondsSinceEpoch.toString() +
              tenantID +
              ref_id!);
      if (kIsWeb) {
        DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); // e.g. "iPod7,1"

        WebBrowserInfo webBrowserInfo = await deviceInfo.webBrowserInfo;
        String ua = webBrowserInfo.userAgent!.toLowerCase();
        if (ua.indexOf('safari') != -1) {
          if (ua.indexOf('chrome') > -1) {
            _peerConnection = await _createPeerConnectiononetomanycallDial(
                mediaType!, "camera");
            _peerConnectionformultisession =
                await _createPeerConnectiononetomanycallDialmultisession(
                    mediaType, "micaudio", true);
          } else {
            //safari
            _peerConnectionformultisession =
                await _createPeerConnectiononetomanycallDialmultisession(
                    mediaType!, "micaudio", true);
            _peerConnection = await _createPeerConnectiononetomanycallDial(
                mediaType, "camera");
          }
        }
      } else {
        _peerConnection = await _createPeerConnectiononetomanycallDial(
            mediaType!, "camera");
        _peerConnectionformultisession =
            await _createPeerConnectiononetomanycallDialmultisession(
                mediaType, "micaudio", true);
      }

      _createOfferonetomanymultisession(
          from!,
          to!,
          mcToken!,
          mediaType!,
          callType!,
          sessionType!,
          isPublicBroadcast!,
          broadcastType!,
          customData,
          screenSessionUUID,
          callSessionUUID);
    } else {
      callSessionUUID = _generateMd5(
          DateTime.now().millisecondsSinceEpoch.toString() +
              tenantID +
              ref_id!);
      _peerConnection = await _createPeerConnectiononetomanycallDial(
          mediaType!, broadcastType!);
      print('2233 $_peerConnection');
      _createOfferonetomany(from!, to!, mcToken!, "video", callType!,
          sessionType!, isPublicBroadcast!, broadcastType, customData);
    }
    // } catch (e) {
    //   print("this is error in start call $e");
    // }
    onCallDial?.call();
  } else if (Platform.isIOS) {
    isCallInProgress = true;
    startCallObject = {"mediaType": mediaType, "sessionType": sessionType};
    callend = false;
    print("i am here in start call oneto one");

    starttime = DateTime.now();
    iscallonetomany = true;
    if (broadcastType == "appaudioandcamera") {
      ismultisession = true;
      screenSessionUUID = _generateMd5(
          DateTime.now().millisecondsSinceEpoch.toString() +
              tenantID +
              ref_id! +
              "12345678");
      callSessionUUID = _generateMd5(
          DateTime.now().millisecondsSinceEpoch.toString() +
              tenantID +
              ref_id!);

      print("these are uuids $callSessionUUID $screenSessionUUID");

      _peerConnection = await _createPeerConnectiononetomanycallDialIOS(
          mediaType!, "camera");
      _createOfferonetomanyMultiSessionIOS(
          from!,
          to!,
          mcToken!,
          mediaType,
          callType!,
          sessionType!,
          isPublicBroadcast!,
          broadcastType!,
          customData!,
          screenSessionUUID,
          callSessionUUID);

      // // ios Screen share multi session
      Map<String, dynamic> ScreenshareConfiguration = {
        "authorizationToken": authorizationToken,
        "referenceId": from,
        "to": to,
        "requestId": _generateMd5(
            DateTime.now().millisecondsSinceEpoch.toString() +
                tenantID +
                ref_id!),
        "sessionUuid": screenSessionUUID,
        "associatedSessionUuid": callSessionUUID,
        "completeAddress": completeAddresssForOne2Many,
        "projectId": project_ID,
        "isPublic": isPublicBroadcast ? true : false,
        "appAudio": broadcastType == "appaudioandcamera" ? true : false,
        "data": customData
      };
      BroadCast broadCast = await startScreenShare(ScreenshareConfiguration);
      screenShareflag = true;
      broadCast.iosSessionBroadCastPublicURLCallBack = (String message) {
        print("yes i'm here with public url $message");
        onReceiveUrlCallback?.call(message);
      };
      broadCast.iosSessionBroadCastUpdatesCallBack = (String message) {
        print("yes i'm here with updates ${message}");
        Map<String, dynamic> res = json.decode(message);
        print("yes i'm here with res ${res}");
        switch (res["state"]) {
          case 'onParticipantAdd':
            {
              if (res["connectedUsers"] != "0") {
                print("kdhfjgffhjsdj ${res["connectedUsers"]}");
                onAddparticpant?.call(
                    int.parse(res["connectedUsers"]), callType);
              }
            }
            break;
          case 'HungUp':
            {
              statsEndCall().then((value) {
                print("here in reponse code else%%%%%%%");
                onCallHungUpByUser?.call(false);
                ReplayKitLauncher.launchReplayKitBroadcast('ScreenShare');
                //resetConfigurations();
                Map<String, dynamic> jsonData = {
                  "type": "request",
                  "requestType": "session_cancel",
                  "requestId": _generateMd5(
                      DateTime.now().millisecondsSinceEpoch.toString() +
                          tenantID +
                          ref_id!),
                  "sessionUuid": callSessionUUID,
                  "mcToken": mcToken
                };
                print("kdfhdjfkghfjg $jsonData");
                _socket?.send(jsonData);
                closeSession(false);

                print("THSIISSIISISISISI IS SESSION");
                callversions = 0;
                callend = true;
                reCall = false;
                reInviteCheckRequestID = "";
                inCall = false;
                iscallonetoone = false;
                 totalCount = 0;
                iscallonetomany = false;
                iscallmanytomany = false;
                isCallInProgress = false;
                busyCount=0;
              });
            }
            break;
          case 'SessionTerminated':
            {
              statsEndCall().then((value) {
                print(
                    "here in reponse code else******** $callSessionUUID $screenSessionUUID");
                onCallHungUpByUser?.call(false);
                if (userType != "caller") {
                  ReplayKitLauncher.launchReplayKitBroadcast('ScreenShare');
                }
                closeSession(false);
                print("THSIISSIISISISISI IS SESSION");
                callversions = 0;
                callend = true;
                reCall = false;
                reInviteCheckRequestID = "";
                inCall = false;
                iscallonetoone = false;
                 totalCount = 0;
                iscallonetomany = false;
                iscallmanytomany = false;
                isCallInProgress = false;
                busyCount=0;
              });
            }
            break;
          case 'Ringing':
            {
              onTargetAlerting?.call();
            }
            break;
          case 'MissedCall':
            {
              //onTargetAlerting.call();
            }
            break;
          case 'InValidTarget':
            {
              //onTargetAlerting.call();
            }
            break;
          case 'TryingConnect':
            {
              //onTargetAlerting.call();
            }
            break;
          case 'Reconnecting':
            {}
            break;
        }
        // switch (message.) {
        //   case ''
        // }
        // onReceiveUrlCallback.call(message);
      };
      ReplayKitLauncher.launchReplayKitBroadcast('ScreenShare');
    } else if (broadcastType == "micaudioandcamera") {
      ismultisession = true;
      screenSessionUUID = _generateMd5(
          DateTime.now().millisecondsSinceEpoch.toString() +
              tenantID +
              ref_id! +
              "12345678");
      callSessionUUID = _generateMd5(
          DateTime.now().millisecondsSinceEpoch.toString() +
              tenantID +
              ref_id!);

      print("these are uuids $callSessionUUID $screenSessionUUID");

      _peerConnection = await _createPeerConnectiononetomanycallDialIOS(
          mediaType!, "camera");
      _createOfferonetomanyMultiSessionIOS(
          from!,
          to!,
          mcToken!,
          mediaType,
          callType!,
          sessionType!,
          isPublicBroadcast!,
          broadcastType!,
          customData!,
          screenSessionUUID,
          callSessionUUID);

      // // ios Screen share multi session
      Map<String, dynamic> ScreenshareConfiguration = {
        "authorizationToken": authorizationToken,
        "referenceId": from,
        "to": to,
        "requestId": _generateMd5(
            DateTime.now().millisecondsSinceEpoch.toString() +
                tenantID +
                ref_id!),
        "sessionUuid": screenSessionUUID,
        "associatedSessionUuid": callSessionUUID,
        "completeAddress": completeAddresssForOne2Many,
        "projectId": project_ID,
        "isPublic": isPublicBroadcast ? true : false,
        "appAudio": broadcastType == "appaudioandcamera" ? true : false,
        "data": customData
      };
      BroadCast broadCast = await startScreenShare(ScreenshareConfiguration);
      screenShareflag = true;
      broadCast.iosSessionBroadCastPublicURLCallBack = (String message) {
        print("yes i'm here with public url $message");
        onReceiveUrlCallback?.call(message);
      };
      broadCast.iosSessionBroadCastUpdatesCallBack = (String message) {
        print("yes i'm here with updates ${message}");
        Map<String, dynamic> res = json.decode(message);
        switch (res["state"]) {
          case 'onParticipantAdd':
            {
              if (res["connectedUsers"] != "0") {
                print("kdhfjgffhjsdj ${res["connectedUsers"]}");
                onAddparticpant?.call(
                    int.parse(res["connectedUsers"]), callType);
              }
            }
            break;
          case 'HungUp':
            {
              statsEndCall().then((value) {
                print("here in reponse code else))))");
                onCallHungUpByUser?.call(false);
                ReplayKitLauncher.launchReplayKitBroadcast('ScreenShare');
                Map<String, dynamic> jsonData = {
                  "type": "request",
                  "requestType": "session_cancel",
                  "requestId": _generateMd5(
                      DateTime.now().millisecondsSinceEpoch.toString() +
                          tenantID +
                          ref_id!),
                  "sessionUuid": callSessionUUID,
                  "mcToken": mcToken
                };
                print("kdfhdjfkghfjg $jsonData");
                _socket?.send(jsonData);

                closeSession(false);
                callversions = 0;
                callend = true;
                reCall = false;
                inCall = false;
                reInviteCheckRequestID = "";
                iscallonetoone = false;
                iscallonetomany = false;
                 totalCount = 0;
                iscallmanytomany = false;
                isCallInProgress = false;
                busyCount=0;
              });
            }
            break;
          case 'SessionTerminated':
            {
              statsEndCall().then((value) {
                print("here in reponse code else(((((");
                onCallHungUpByUser?.call(false);
                if (userType != "caller") {
                  ReplayKitLauncher.launchReplayKitBroadcast('ScreenShare');
                }
                closeSession(false);
                print("THSIISSIISISISISI IS SESSION");
                callversions = 0;
                callend = true;
                reCall = false;
                reInviteCheckRequestID = "";
                inCall = false;
                iscallonetoone = false;
                 totalCount = 0;
                iscallonetomany = false;
                iscallmanytomany = false;
                isCallInProgress = false;
                busyCount=0;
              });
            }
            break;
          case 'Ringing':
            {
              onTargetAlerting?.call();
            }
            break;
          case 'MissedCall':
            {
              //onTargetAlerting.call();
            }
            break;
          case 'InValidTarget':
            {
              //onTargetAlerting.call();
            }
            break;
          case 'TryingConnect':
            {
              //onTargetAlerting.call();
            }
            break;
          case 'Reconnecting':
            {}
            break;
        }
        // switch (message.) {
        //   case ''
        // }
        // onReceiveUrlCallback.call(message);
      };
      ReplayKitLauncher.launchReplayKitBroadcast('ScreenShare');
    } else if (broadcastType == "camera") {
      screenShareflag = false;
      // ios single camera call
      callSessionUUID = _generateMd5(
          DateTime.now().millisecondsSinceEpoch.toString() +
              tenantID +
              ref_id! +
              "12345678");
      _peerConnection = await _createPeerConnectiononetomanycallDial(
          mediaType!, broadcastType!);
      _createOfferonetomany(from!, to!, mcToken!, mediaType, callType!,
          sessionType!, isPublicBroadcast!, broadcastType, customData!);
    } else {
      // ios Screen share single session
      screenShareflag = true;
      callSessionUUID = _generateMd5(
          DateTime.now().millisecondsSinceEpoch.toString() +
              tenantID +
              ref_id!);
      Map<String, dynamic> screenshareConfiguration = {
        "authorizationToken": authorizationToken,
        "referenceId": from,
        "to": to,
        "requestId": _generateMd5(
            DateTime.now().millisecondsSinceEpoch.toString() +
                tenantID +
                ref_id!),
        "sessionUuid": callSessionUUID,
        "completeAddress": completeAddresssForOne2Many,
        "associatedSessionUuid": "",
        "projectId": project_ID,
        "isPublic": isPublicBroadcast! ? true : false,
        "appAudio": broadcastType == "appaudio" ? true : false,
        "data": customData
      };
      BroadCast broadCast = await startScreenShare(screenshareConfiguration);
      // screenShareflag = true;
      broadCast.iosSessionBroadCastPublicURLCallBack = (String message) {
        print("yes i'm here with public url $message");
        onReceiveUrlCallback?.call(message);
      };
      broadCast.iosSessionBroadCastUpdatesCallBack = (String message) {
        print("yes i'm here with updates ${message}");
        Map<String, dynamic> res = json.decode(message);
        switch (res["state"]) {
          case 'onParticipantAdd':
            {
              if (res["connectedUsers"] != "0") {
                print("kdhfjgffhjsdj ${res["connectedUsers"]}");
                onAddparticpant?.call(
                    int.parse(res["connectedUsers"]), callType!);
              }
            }
            break;
          case 'HungUp':
            {
              statsEndCall().then((value) {
                print("here in reponse code elseeeeeeee");
                onCallHungUpByUser?.call(false);
                //resetConfigurations();
                ReplayKitLauncher.launchReplayKitBroadcast('ScreenShare');
                closeSession(false);
                print("THSIISSIISISISISI IS SESSION");
                callversions = 0;
                reInviteCheckRequestID = "";
                callend = true;
                reCall = false;
                reInviteCheckRequestID = "";
                inCall = false;
                iscallonetoone = false;
                 totalCount = 0;
                iscallonetomany = false;
                iscallmanytomany = false;
                isCallInProgress = false;
                busyCount=0;
              });
            }
            break;
          case 'SessionTerminated':
            {
              statsEndCall().then((value) {
                print("here in reponse code elsefkfo");
                onCallHungUpByUser?.call(false);
                //resetConfigurations();
                if (userType != "caller") {
                  ReplayKitLauncher.launchReplayKitBroadcast('ScreenShare');
                }
                //
                closeSession(false);
                print("THSIISSIISISISISI IS SESSION");
                callversions = 0;
                reInviteCheckRequestID = "";
                callend = true;
                reCall = false;
                inCall = false;
                iscallonetoone = false;
                 totalCount = 0;
                iscallonetomany = false;
                iscallmanytomany = false;
                isCallInProgress = false;
                busyCount=0;
              });
            }
            break;
          case 'Ringing':
            {
              onTargetAlerting?.call();
            }
            break;
          case 'MissedCall':
            {
              //onTargetAlerting.call();
            }
            break;
          case 'InValidTarget':
            {
              //onTargetAlerting.call();
            }
            break;
          case 'TryingConnect':
            {
              //onTargetAlerting.call();
            }
            break;
          case 'Reconnecting':
            {}
            break;
        }
        // switch (message.) {
        //   case ''
        // }
        // onReceiveUrlCallback.call(message);
      };
      ReplayKitLauncher.launchReplayKitBroadcast('ScreenShare');
    }

    onCallDial?.call();
  } else {
    if (broadcastType != "camera") {
      return _startVdotokForegroundService().then((t) async {
        if (t) {
          print("here for camera");
          screenShareflag = false;
          try {
            startCallObject = {
              "mediaType": mediaType,
              "sessionType": sessionType
            };
            callend = false;
            print("i am here in start call oneto one");

            starttime = DateTime.now();
            iscallonetomany = true;
            print("i am here in  start call ne to many");

            if (broadcastType == "appaudioandcamera") {
              screenSessionUUID = _generateMd5(
                  DateTime.now().millisecondsSinceEpoch.toString() +
                      tenantID +
                      ref_id! +
                      "12345678");
              callSessionUUID = _generateMd5(
                  DateTime.now().millisecondsSinceEpoch.toString() +
                      tenantID +
                      ref_id!);
              print("here in appaudioandcamera");
              ismultisession = true;

              _peerConnection = await _createPeerConnectiononetomanycallDial(
                  mediaType!, "camera");
              _peerConnectionformultisession =
                  await _createPeerConnectiononetomanycallDialmultisession(
                      mediaType, "appaudio", false);
              _createOfferonetomanymultisession(
                  from!,
                  to!,
                  mcToken!,
                  mediaType,
                  callType!,
                  sessionType!,
                  isPublicBroadcast!,
                  broadcastType!,
                  customData!,
                  screenSessionUUID,
                  callSessionUUID);
            } else if (broadcastType == "micaudioandcamera") {
              print("here in micaudioandcamera");
              screenSessionUUID = _generateMd5(
                  DateTime.now().millisecondsSinceEpoch.toString() +
                      tenantID +
                      ref_id! +
                      "12345678");
              callSessionUUID = _generateMd5(
                  DateTime.now().millisecondsSinceEpoch.toString() +
                      tenantID +
                      ref_id!);
              ismultisession = true;

              _peerConnection = await _createPeerConnectiononetomanycallDial(
                  mediaType!, "camera");
              _peerConnectionformultisession =
                  await _createPeerConnectiononetomanycallDialmultisession(
                      mediaType, "micaudio", true);
              _createOfferonetomanymultisession(
                  from!,
                  to!,
                  mcToken!,
                  mediaType,
                  callType!,
                  sessionType!,
                  isPublicBroadcast!,
                  broadcastType!,
                  customData!,
                  screenSessionUUID,
                  callSessionUUID);
            } else {
              print("heheheehehehehhe");
              callSessionUUID = _generateMd5(
                  DateTime.now().millisecondsSinceEpoch.toString() +
                      tenantID +
                      ref_id!);
              _peerConnection = await _createPeerConnectiononetomanycallDial(
                  mediaType!, broadcastType!);
              _createOfferonetomany(
                  from!,
                  to!,
                  mcToken!,
                  mediaType,
                  callType!,
                  sessionType!,
                  isPublicBroadcast!,
                  broadcastType,
                  customData!);
              isCallInProgress = true;
            }
          } catch (e) {
            print("this is error in start call $e");
          }
        }
        onCallDial?.call();
      });
    } else {
      print("here for camera");
      screenShareflag = false;
      try {
        startCallObject = {
          "mediaType": mediaType,
          "sessionType": sessionType
        };
        callend = false;
        print("i am here in start call oneto one");

        starttime = DateTime.now();
        iscallonetomany = true;
        print("i am here in  start call ne to many");
        print("heheheehehehehhe for camera");
        callSessionUUID = _generateMd5(
            DateTime.now().millisecondsSinceEpoch.toString() +
                tenantID +
                ref_id!);
        _peerConnection = await _createPeerConnectiononetomanycallDial(
            mediaType!, broadcastType!);
        _createOfferonetomany(from!, to!, mcToken!, mediaType, callType!,
            sessionType!, isPublicBroadcast!, broadcastType, customData!);
        isCallInProgress = true;
        onCallDial?.call();
      } catch (e) {
        print("this is error in start calllllllllll $e");
      }
    }
  }

  // }
}