# pixelStreaming

The `pixelStreaming` object is the core streaming instance returned by `StreamPixelApplication()`.

## Methods

### `addEventListener(event, callback)`

Register a listener for WebRTC lifecycle events.

{% code lineNumbers="true" %}

```javascript
pixelStreaming.addEventListener('webRtcConnected', () => { });
```

{% endcode %}

### `connect()`

Manually initiate a connection (use when `AutoConnect` is `false`).

{% hint style="info" %}
You only need to call `connect()` when `AutoConnect` is set to `false`. If `AutoConnect` is `true`, the connection is established automatically on initialization.
{% endhint %}

```javascript
pixelStreaming.connect();
```

### `disconnect()`

Disconnect the stream.

```javascript
pixelStreaming.disconnect();
```

### `reconnect()`

Reconnect after a disconnect.

```javascript
pixelStreaming.reconnect();
```

### `emitConsoleCommand(command)`

Send a console command to Unreal Engine.

| Parameter | Type     | Description        |
| --------- | -------- | ------------------ |
| `command` | `string` | UE console command |

```javascript
pixelStreaming.emitConsoleCommand('stat fps');
pixelStreaming.emitConsoleCommand('r.SetRes 1920x1080f');
```

### `addResponseEventListener(name, callback)`

Listen for custom responses sent from UE to the web application.

| Parameter  | Type       | Description                                |
| ---------- | ---------- | ------------------------------------------ |
| `name`     | `string`   | Listener name (e.g., `'handle_responses'`) |
| `callback` | `function` | Called with the response string            |

{% code lineNumbers="true" %}

```javascript
pixelStreaming.addResponseEventListener('handle_responses', (response) => {
  const data = JSON.parse(response);
});
```

{% endcode %}

### `unmuteMicrophone(enabled)`

Enable or disable microphone input to UE.

| Parameter | Type      | Description                          |
| --------- | --------- | ------------------------------------ |
| `enabled` | `boolean` | `true` to enable, `false` to disable |

```javascript
pixelStreaming.unmuteMicrophone(true);
```

### `unmuteCamera(enabled)`

Enable or disable camera input to UE.

| Parameter | Type      | Description                          |
| --------- | --------- | ------------------------------------ |
| `enabled` | `boolean` | `true` to enable, `false` to disable |

```javascript
pixelStreaming.unmuteCamera(true);
```

## Events

### Connection Lifecycle

| Event               | When It Fires                     |
| ------------------- | --------------------------------- |
| `webRtcAutoConnect` | Auto-connect initiated            |
| `webRtcConnecting`  | Peer connection being established |
| `webRtcSdp`         | SDP offer/answer exchanged        |
| `webRtcConnected`   | Peer connection established       |
| `streamLoading`     | Video data arriving               |
| `playStream`        | Video about to start playback     |

### Failure

| Event                | When It Fires                  |
| -------------------- | ------------------------------ |
| `webRtcFailed`       | Connection failed to establish |
| `webRtcDisconnected` | Established connection dropped |

### AFK

| Event                  | Data                                    | Description                                                                        |
| ---------------------- | --------------------------------------- | ---------------------------------------------------------------------------------- |
| `afkWarningActivate`   | `e.data.countDown`, `e.data.dismissAfk` | Warning shown. `countDown` = seconds remaining, `dismissAfk` = function to dismiss |
| `afkWarningUpdate`     | `e.data.countDown`                      | Countdown updated (every second)                                                   |
| `afkWarningDeactivate` | —                                       | User interacted, warning dismissed                                                 |
| `afkTimedOut`          | —                                       | Session ended due to inactivity                                                    |

### Statistics

| Event           | Data                     | Description                                                                                              |
| --------------- | ------------------------ | -------------------------------------------------------------------------------------------------------- |
| `statsReceived` | `e.data.aggregatedStats` | Stream statistics available. Contains `inboundVideoStats`, `inboundAudioStats`, `sessionStats`, `codecs` |
