# End Call Functionality

## Overview
The voice demo UI now has a proper "End Call" button that allows users to cleanly disconnect from a session and clean up all resources.

## User Flow

1. **During Active Call**: When connected to a voice demo session, users will see a red "End Call" button in the top-right of the side panel.

2. **Click "End Call"**: Clicking the button triggers the following sequence:
   - Disconnects from the LiveKit room
   - Closes all WebRTC connections
   - Cleans up audio streams
   - Shows "Session Ended" screen

3. **Start New Demo**: Users can click "Start New Demo" to return to the launcher and create a new session.

## Technical Implementation

### Components Modified

1. **App Component** (`voice-demo-ui/src/app/app.tsx`)
   - Added `disconnect` from `useVoiceDemo` hook
   - Created `handleEndCall()` function
   - Added "End Call" button in the side panel
   - Added CSS styles for the button

2. **LiveKitRoom Component** (`voice-demo-ui/src/components/LiveKitRoom.tsx`)
   - Added `RoomCleanupHandler` component
   - Ensures `room.disconnect()` is called on unmount
   - Prevents resource leaks

3. **useVoiceDemo Hook** (`voice-demo-ui/src/hooks/useVoiceDemo.ts`)
   - Already had `disconnect()` function
   - Sets sessionState to 'disconnected'
   - Resets to 'idle' after 500ms for cleanup

### Cleanup Sequence

```
User clicks "End Call"
  ↓
handleEndCall() called
  ↓
disconnect() in useVoiceDemo
  ↓
sessionState → 'disconnected'
  ↓
App unmounts LiveKitRoom
  ↓
RoomCleanupHandler cleanup runs
  ↓
room.disconnect() called
  ↓
LiveKit room closed
  ↓
WebRTC connections closed
  ↓
Audio streams released
  ↓
After 500ms: sessionState → 'idle'
  ↓
"Session Ended" screen shown
```

## Resource Cleanup

The end call functionality ensures the following resources are properly cleaned up:

1. **LiveKit Room Connection**
   - WebSocket connection to LiveKit server closed
   - Room participants notified of disconnect

2. **WebRTC Connections**
   - Peer connections closed
   - ICE candidates cleared

3. **Media Streams**
   - Microphone access released
   - Audio tracks stopped
   - Video tracks stopped (if any)

4. **Backend Resources**
   - Voice agent on backend detects room disconnect
   - Screenshare bot stops streaming
   - Session marked as ended in database

## Testing

To test the end call functionality:

1. Start the voice-demo-ui: `pnpm nx dev voice-demo-ui`
2. Visit http://localhost:3006
3. Click "See a Demo Now" to create a session
4. Once connected, you'll see the "End Call" button in the top-right
5. Click "End Call"
6. Verify you see "Session Ended" screen
7. Check browser console for cleanup logs:
   - `[App] Ending call`
   - `[useVoiceDemo] Disconnecting from session`
   - `[RoomCleanupHandler] Disconnecting room on unmount`
   - `[LiveKitRoom] Disconnected from room`

## Styling

The "End Call" button uses a red gradient to clearly indicate it's a destructive action:
- Background: `linear-gradient(135deg, #e74c3c 0%, #c0392b 100%)`
- Hover state: Darker gradient with elevated shadow
- Positioned in top-right of side panel for easy access

## Future Enhancements

Potential improvements:
- Add confirmation dialog before ending call
- Show call duration before ending
- Add "End & Feedback" option for collecting user feedback
- Send explicit end-session API call to backend for analytics
