---
id: "0158"
title: "matrix-client-android calls LiveKit nativo: mic/cam/screen + PiP"
status: pendiente
priority: alta
created: 2026-05-24
related_flows: ["0011"]
related_issues: ["0157", "0159", "0161"]
dependencies: ["0157"]
tags: [matrix, android, livekit, calls, webrtc, pip, audio-focus]
---

## Objetivo

Llamadas nativas via `io.livekit:livekit-android` SDK oficial. Codecs HW (H.264/VP9 hardware decoder), audio focus + AEC/NS nativos, MediaSession para controls en lockscreen, Picture-in-Picture mode Android nativo. Soporta 1:1 + grupales (limite 16 del LiveKit config actual).

## Tareas

1. Backend (compartido con cliente PC):
   - Reusar `livekit_token_gen_go_infra` que esta en flow 0010.
   - Cliente Android pide token al mismo endpoint `/api/call/token` que el cliente PC.
2. ViewModel:
   - `CallViewModel(matrixClient, roomId)`:
     - `joinCall()` — pide token + conecta `Room.connect()`.
     - `toggleMic()`, `toggleCamera()`, `toggleScreenShare()`.
     - `hangup()`.
     - `Flow<CallState>` con participants, tracks, connection state.
3. Compose:
   - `CallScreen` fullscreen:
     - Grid tiles participantes (`Flow` layout responsive 1/2/4/9/16).
     - Tile principal: active speaker (track audio level del SDK).
     - Controles bottom: mic, cam, screen, raise hand, hangup.
   - `IncomingCallScreen` fullscreen con accept/decline (system overlay activity).
   - `CallTile` composable con `VideoView` (SurfaceViewRenderer del SDK).
4. PiP (Picture-in-Picture):
   - `Activity` con `setPictureInPictureParams()`.
   - Auto-enter PiP al minimizar la app durante call.
   - PiP tile: video remoto + boton hangup.
5. Audio routing:
   - `AudioFocusRequest` (Android 8+) — focus exclusivo durante call.
   - Switch speaker/earpiece/bluetooth via `AudioManager.setSpeakerphoneOn()` + connection state listeners para audifonos BT.
   - Echo cancellation + noise suppression: SDK los habilita por defecto, verificar.
6. ICE/TURN: igual que cliente PC, depende del LiveKit config server-side.
7. Tests:
   - Instrumented `Call1to1Test` con emulator + segundo cliente (PC) — connect, video, hangup.
   - Manual `ScreenShareTest` con device fisico.
   - Manual `4ParticipantsTest`.
   - Manual `PiPTest` — call activa + Home button -> PiP aparece.

## Funciones del registry a crear

- `livekit_call_kotlin_infra` — wrapper `Room` SDK + permission helpers.
- `audio_routing_kotlin_infra` — speaker/earpiece/BT switching.
- `CallScreen_kotlin_ui` — fullscreen call UI.
- `CallTile_kotlin_ui` — tile con VideoView.
- `IncomingCallScreen_kotlin_ui` — accept/decline overlay activity.

## Acceptance

- [ ] Start call desde Android -> PC Wails recibe y conecta.
- [ ] 30s call con video+audio nativo (verificar HW codec via `adb shell dumpsys media.codec`).
- [ ] Mute mic + apagar cam refleja en otro cliente.
- [ ] Screen share desde Android (con `MediaProjection`) visible en PC.
- [ ] PiP: minimizar app durante call -> tile flotante con video remoto.
- [ ] Bluetooth headphones: cambio automatico al conectar/desconectar.
- [ ] Battery: call 30min con AC + WiFi <15% drain.

## Notas

- Permissions runtime: `RECORD_AUDIO`, `CAMERA`, `POST_NOTIFICATIONS` (Android 13+), `FOREGROUND_SERVICE`, `FOREGROUND_SERVICE_MEDIA_PROJECTION` (Android 14+).
- Foreground service requerido para mantener call con app en background (issue 0161).
- E2EE en call (insertable streams): TBD post-DoD, igual que en cliente PC.
- Connection service Android (sistema): TBD, opcional. Permite integracion con dialer system + Bluetooth Car. Valorar coste/beneficio.
