> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Standalone Calling

> CometChat Calling SDK v4 - Stable Release - Standalone Calling for Flutter

## Overview

This section demonstrates how to implement calling functionality using only the CometChat Calls SDK, without requiring the Chat SDK. This is ideal for applications that need video/audio calling capabilities without the full chat infrastructure.

<Note>
  Before you begin, ensure you have completed the [Calls SDK setup](/calls/v4/flutter/setup).
</Note>

## User Authentication

To start a call session, you need a user auth token. Since this implementation doesn't use the Chat SDK, you'll need to obtain the auth token via the CometChat REST API.

<Note>
  To understand user authentication in CometChat, see the [User Auth](/fundamentals/user-auth) documentation.
</Note>

You can obtain the auth token using one of these REST API endpoints:

* [Create Auth Token](/rest-api/auth-tokens/create) — Creates a new auth token for a user
* [Get Auth Token](/rest-api/auth-tokens/get) — Retrieves an existing auth token

<Note>
  For testing or POC purposes, you can create an auth token directly from the [CometChat Dashboard](https://app.cometchat.com). Navigate to **Users & Groups → Users**, select a user, and click **+ Create Auth Token**.
</Note>

Store the auth token securely in your application for use when generating call tokens.

## Generate Call Token

A call token is required for secure access to a call session. Each token is unique to a specific session and user combination, ensuring that only authorized users can join the call.

You can generate the token just before starting the call, or generate and store it ahead of time based on your use case.

Use the `generateToken()` method to create a call token:

```dart theme={null}
String sessionId = "UNIQUE_SESSION_ID"; // Generate a unique session ID
String userAuthToken = "USER_AUTH_TOKEN"; // Obtained from REST API

CometChatCalls.generateToken(
  sessionId,
  userAuthToken,
  onSuccess: (GenerateToken generateToken) {
    debugPrint("Call token generated: ${generateToken.token}");
    // Use generateToken to start the session
  },
  onError: (CometChatCallsException e) {
    debugPrint("Token generation failed: $e");
  },
);
```

| Parameter       | Description                                                                                                         |
| --------------- | ------------------------------------------------------------------------------------------------------------------- |
| `sessionId`     | A unique session ID for the call. Generate this yourself or use a shared ID for participants to join the same call. |
| `userAuthToken` | The user auth token obtained from the CometChat REST API.                                                           |

## Start Call Session

Use the `startSession()` method to join a call session. This method requires a call token (generated in the previous step) and a `CallSettings` object that configures the call UI and behavior.

```dart theme={null}
String generatedToken = ""; // Token from generateToken() onSuccess

CallSettings callSettings = (CallSettingsBuilder()
  ..defaultLayout = true
  ..setAudioOnlyCall = false
  ..listener = this // CometChatCallsEventsListener
).build();

CometChatCalls.startSession(
  generatedToken,
  callSettings,
  onSuccess: (Widget? callingWidget) {
    debugPrint("Call session started");
    // Display the callingWidget in your UI
  },
  onError: (CometChatCallsException e) {
    debugPrint("Start session failed: $e");
  },
);
```

### Call Settings

Configure the call experience using the following `CallSettingsBuilder` properties:

| Property                       | Description                                                                                                                                                                                          |
| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `defaultLayout`                | Enables or disables the default call UI layout with built-in controls. `true` shows the default layout. `false` hides the button layout. Default: `true`                                             |
| `setAudioOnlyCall`             | Sets whether the call is audio-only or audio-video. `true` for audio-only, `false` for audio-video. Default: `false`                                                                                 |
| `listener`                     | Sets the `CometChatCallsEventsListener` to receive call events. See [Call Listeners](#call-listeners).                                                                                               |
| `setMode`                      | Sets the call UI layout mode. Available: `Mode.MODE_DEFAULT`, `Mode.MODE_SPOTLIGHT`, `Mode.MODE_SINGLE`. Default: `Mode.MODE_SPOTLIGHT`                                                              |
| `setAvatarMode`                | Sets how avatars are displayed when video is off. Available: `AvatarModes.AVATAR_MODE_CIRCLE`, `AvatarModes.AVATAR_MODE_SQUARE`, `AvatarModes.AVATAR_MODE_FULLSCREEN`. Default: `AVATAR_MODE_CIRCLE` |
| `setDefaultAudioMode`          | Sets the initial audio output device. Available: `AudioModes.AUDIO_MODE_SPEAKER`, `AudioModes.AUDIO_MODE_EARPIECE`, `AudioModes.AUDIO_MODE_BLUETOOTH`, `AudioModes.AUDIO_MODE_HEADPHONES`            |
| `startWithAudioMuted`          | Starts the call with the microphone muted. Default: `false`                                                                                                                                          |
| `startWithVideoMuted`          | Starts the call with the camera turned off. Default: `false`                                                                                                                                         |
| `showEndCallButton`            | Shows or hides the end call button in the default layout. Default: `true`                                                                                                                            |
| `showSwitchCameraButton`       | Shows or hides the switch camera button (front/back). Default: `true`                                                                                                                                |
| `showMuteAudioButton`          | Shows or hides the mute audio button. Default: `true`                                                                                                                                                |
| `showPauseVideoButton`         | Shows or hides the pause video button. Default: `true`                                                                                                                                               |
| `showAudioModeButton`          | Shows or hides the audio mode selection button. Default: `true`                                                                                                                                      |
| `showSwitchToVideoCallButton`  | Shows or hides the button to upgrade an audio call to video. Default: `true`                                                                                                                         |
| `showCallRecordButton`         | Shows or hides the recording button. Default: `false`                                                                                                                                                |
| `startRecordingOnCallStart`    | Automatically starts recording when the call begins. Default: `false`                                                                                                                                |
| `enableVideoTileClick`         | Enables or disables click interactions on video tiles in Spotlight mode. Default: `true`                                                                                                             |
| `enableVideoTileDrag`          | Enables or disables drag functionality for video tiles in Spotlight mode. Default: `true`                                                                                                            |
| `setMainVideoContainerSetting` | Customizes the main video container. See [Video View Customization](/calls/v4/flutter/video-view-customisation).                                                                                     |
| `setIdleTimeoutPeriod`         | Sets idle timeout in seconds. Warning appears 60 seconds before auto-termination. Default: `180` seconds. *v4.1.0+*                                                                                  |

## Call Listeners

The `CometChatCallsEventsListener` provides real-time callbacks for call session events, including participant changes, call state updates, and error conditions.

Each listener requires a unique `listenerId` string. This ID is used to:

* **Prevent duplicate registrations** — Re-registering with the same ID replaces the existing listener
* **Enable targeted removal** — Remove specific listeners without affecting others

```dart theme={null}
String listenerId = "UNIQUE_LISTENER_ID";

class YourClassName extends State<YourScreen> with CometChatCallsEventsListener {
  @override
  void initState() {
    super.initState();
    CometChatCalls.addCallsEventListeners(listenerId, this);
  }

  @override
  void dispose() {
    super.dispose();
    CometChatCalls.removeCallsEventListeners(listenerId);
  }

  @override
  void onCallEnded() {
    debugPrint("Call ended");
    CometChatCalls.endSession(
      onSuccess: (success) {
        // Close calling screen
      },
      onError: (e) {},
    );
  }

  @override
  void onCallEndButtonPressed() {
    debugPrint("End call button pressed");
    CometChatCalls.endSession(
      onSuccess: (success) {
        // Close calling screen
      },
      onError: (e) {},
    );
  }

  @override
  void onSessionTimeout() {
    debugPrint("Session timed out");
  }

  @override
  void onUserJoined(RTCUser user) {
    debugPrint("User joined: ${user.name}");
  }

  @override
  void onUserLeft(RTCUser user) {
    debugPrint("User left: ${user.name}");
  }

  @override
  void onUserListChanged(List<RTCUser> users) {
    debugPrint("User list updated: ${users.length} participants");
  }

  @override
  void onAudioModeChanged(List<AudioMode> devices) {
    debugPrint("Audio modes changed");
  }

  @override
  void onCallSwitchedToVideo(CallSwitchRequestInfo info) {
    debugPrint("Call switched to video");
  }

  @override
  void onUserMuted(RTCMutedUser muteObj) {
    debugPrint("User muted");
  }

  @override
  void onRecordingToggled(RTCRecordingInfo info) {
    debugPrint("Recording toggled");
  }

  @override
  void onError(CometChatCallsException e) {
    debugPrint("Call error: $e");
  }
}
```

### Events

| Event                                               | Description                                                                                                                                         |
| --------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `onCallEnded()`                                     | Invoked when the call session terminates for a 1:1 call. Both participants receive this callback. Only fires for calls with exactly 2 participants. |
| `onSessionTimeout()`                                | Invoked when the call is auto-terminated due to inactivity (default: 180 seconds). Warning appears 60 seconds before. *v4.1.0+*                     |
| `onCallEndButtonPressed()`                          | Invoked when the local user taps the end call button. Call `CometChatCalls.endSession()` to leave the session.                                      |
| `onUserJoined(RTCUser user)`                        | Invoked when a remote participant joins. The `user` contains UID, name, and avatar.                                                                 |
| `onUserLeft(RTCUser user)`                          | Invoked when a remote participant leaves the call session.                                                                                          |
| `onUserListChanged(List<RTCUser> users)`            | Invoked whenever the participant list changes (join or leave events).                                                                               |
| `onAudioModeChanged(List<AudioMode> devices)`       | Invoked when available audio devices change (e.g., Bluetooth connected).                                                                            |
| `onCallSwitchedToVideo(CallSwitchRequestInfo info)` | Invoked when an audio call is upgraded to a video call.                                                                                             |
| `onUserMuted(RTCMutedUser muteObj)`                 | Invoked when a participant's mute state changes.                                                                                                    |
| `onRecordingToggled(RTCRecordingInfo info)`         | Invoked when call recording starts or stops.                                                                                                        |
| `onError(CometChatCallsException e)`                | Invoked when an error occurs during the call session.                                                                                               |

## End Call Session

To end the call session and release all media resources (camera, microphone, network connections), call `CometChatCalls.endSession()` in the `onCallEndButtonPressed()` callback.

```dart theme={null}
@override
void onCallEndButtonPressed() {
  CometChatCalls.endSession(
    onSuccess: (success) {
      debugPrint("Session ended");
      // Close the calling screen
    },
    onError: (e) {
      debugPrint("End session failed: $e");
    },
  );
}
```

## Methods

These methods are available for performing custom actions during an active call session. Use them to build custom UI controls or implement specific behaviors based on your use case.

<Note>
  These methods can only be called when a call session is active.
</Note>

### Switch Camera

Toggles between the front and rear camera during a video call.

```dart theme={null}
CometChatCalls.switchCamera(
  onSuccess: (success) {
    debugPrint("Camera switched");
  },
  onError: (e) {
    debugPrint("Switch camera failed: $e");
  },
);
```

### Mute Audio

Controls the local audio stream transmission.

```dart theme={null}
CometChatCalls.muteAudio(
  true,
  onSuccess: (success) {
    debugPrint("Audio muted");
  },
  onError: (e) {
    debugPrint("Mute audio failed: $e");
  },
);
```

### Pause Video

Controls the local video stream transmission.

```dart theme={null}
CometChatCalls.pauseVideo(
  true,
  onSuccess: (success) {
    debugPrint("Video paused");
  },
  onError: (e) {
    debugPrint("Pause video failed: $e");
  },
);
```

### Set Audio Mode

Routes the audio output to a specific device.

```dart theme={null}
CometChatCalls.setAudioMode(
  AudioModes.AUDIO_MODE_SPEAKER,
  onSuccess: (success) {
    debugPrint("Audio mode set");
  },
  onError: (e) {
    debugPrint("Set audio mode failed: $e");
  },
);
```

### Enter PIP Mode

Enters Picture-in-Picture mode.

```dart theme={null}
CometChatCalls.enterPIPMode(
  onSuccess: (success) {
    debugPrint("Entered PIP mode");
  },
  onError: (e) {
    debugPrint("Enter PIP failed: $e");
  },
);
```

### Exit PIP Mode

Exits Picture-in-Picture mode.

```dart theme={null}
CometChatCalls.exitPIPMode(
  onSuccess: (success) {
    debugPrint("Exited PIP mode");
  },
  onError: (e) {
    debugPrint("Exit PIP failed: $e");
  },
);
```

### Switch To Video Call

Upgrades an ongoing audio call to a video call.

```dart theme={null}
CometChatCalls.switchToVideoCall(
  onSuccess: (success) {
    debugPrint("Switched to video call");
  },
  onError: (e) {
    debugPrint("Switch to video failed: $e");
  },
);
```

### Start Recording

Starts recording the call session.

```dart theme={null}
CometChatCalls.startRecording(
  onSuccess: (success) {
    debugPrint("Recording started");
  },
  onError: (e) {
    debugPrint("Start recording failed: $e");
  },
);
```

### Stop Recording

Stops an ongoing call recording.

```dart theme={null}
CometChatCalls.stopRecording(
  onSuccess: (success) {
    debugPrint("Recording stopped");
  },
  onError: (e) {
    debugPrint("Stop recording failed: $e");
  },
);
```

### End Call

Terminates the current call session and releases all media resources.

```dart theme={null}
CometChatCalls.endSession(
  onSuccess: (success) {
    debugPrint("Session ended");
    // Close the calling screen
  },
  onError: (e) {
    debugPrint("End session failed: $e");
  },
);
```
