# PRD: Recording Playback System

## Document Control
- **Status**: Draft (Future Enhancement)
- **Priority**: P3 (Deferred - after UI MVP)
- **Estimated Effort**: 6-8 weeks
- **Owner**: TBD
- **Created**: 2025-10-04
- **Last Updated**: 2025-10-04
- **Related PRDs**:
  - `ui-for-clients-and-admins.md` (recording tab placeholder)
  - `backend-api-for-ui.md` (recording file list)
  - `tavus-conversation-management-system.md` (recording storage)

## Executive Summary

Build a unified recording playback system that reconstructs multi-stream conversations from separate S3 files, supporting:
- **Video mode**: Avatar video + customer video + slide screenshare (synchronized)
- **Voice mode**: Avatar audio + customer audio (dual-channel)
- **Transcript sync**: Playback synchronized with transcript highlighting
- **Adaptive streaming**: HLS/DASH for bandwidth optimization

## Problem Statement

### Current State
Conversation recordings stored as separate S3 files:
- **Phase 1 (Tavus Discovery)**:
  - `phase1_avatar_audio.wav`
  - `phase1_avatar_video.mp4`
  - `phase1_customer_audio.wav`
  - `phase1_customer_video.mp4` (if camera enabled)

- **Phase 2 (Pipecat Slide Demo)**:
  - `phase2_avatar_audio.wav`
  - `phase2_slides_screenshare.mp4` (slide deck)
  - `phase2_customer_audio.wav` (if enabled)

No UI to:
- Play these streams in sync
- Switch between avatar view, customer view, slide view
- Sync playback with transcript
- Handle bandwidth constraints (adaptive quality)

### Complexity Factors

1. **Multi-stream synchronization**: 3-5 separate files must play in perfect sync
2. **Platform differences**: Daily.co vs Zoom vs Teams have different recording formats
3. **Voice-only mode**: No video files, only audio
4. **Slide presentation**: Screenshare video needs slide boundary markers
5. **Bandwidth**: Large video files (500MB+) need adaptive streaming
6. **Transcript sync**: Transcript highlighting must match playback timestamp

## Requirements (High-Level)

### Functional Requirements

#### FR1: Multi-Stream Reconstruction
- Combine separate audio/video streams into unified playback experience
- Synchronize streams using timestamps
- Handle missing streams gracefully (e.g., customer had no camera)

#### FR2: Playback Modes
- **Picture-in-Picture**: Avatar large, customer small (switchable)
- **Side-by-side**: Avatar left, customer right
- **Slides-only**: Full-screen slide presentation with avatar audio
- **Audio-only**: For voice conversations (no video)

#### FR3: Transcript Synchronization
- Highlight current transcript line during playback
- Click transcript line → jump to timestamp
- Auto-scroll transcript as playback progresses

#### FR4: Adaptive Streaming
- HLS or DASH streaming for large files
- Multiple quality levels (auto-switch based on bandwidth)
- Start playback quickly (progressive download)

#### FR5: Slide Navigation Overlay
- Show slide boundaries during Phase 2 playback
- Display slide numbers overlaid on screenshare
- Jump to specific slide sections

### Non-Functional Requirements

#### NFR1: Performance
- Playback starts in <3 seconds
- Streams stay synchronized within ±200ms
- Adaptive quality switching smooth (no buffering)

#### NFR2: Browser Compatibility
- Chrome, Safari, Firefox, Edge
- Mobile browsers (iOS Safari, Android Chrome)

#### NFR3: Security
- S3 signed URLs with short expiration (15 min)
- Client-scoped access validation
- No direct S3 bucket exposure

## Technical Approach (Options)

### Option 1: Client-Side Reconstruction (Complex)
Browser downloads all streams, syncs playback client-side using Web Audio API + MediaSource API.

❌ **Cons**: Complex JavaScript, high bandwidth usage, browser compatibility issues

### Option 2: Server-Side Pre-Processing (Recommended)
Background job stitches streams into single MP4 on S3 upload complete.

✅ **Pros**: Simple playback, single file, works everywhere
⚠️ **Cons**: Processing delay (5-10 min after recording), storage costs (original + stitched)

### Option 3: HLS Streaming with Manifest (Best)
Generate HLS manifest pointing to original S3 files, let HLS player handle sync.

✅ **Pros**: Adaptive streaming, fast start, no pre-processing
⚠️ **Cons**: Requires HLS-compatible files (may need transcoding)

## Recommended Architecture

### Phase 1: Simple Video Player (MVP)
- Single stream playback only (avatar video OR customer video OR slides)
- No synchronization
- Standard HTML5 `<video>` element
- S3 signed URLs (15 min expiration)

### Phase 2: Multi-Stream Sync
- Server-side stitching (FFmpeg) on recording complete
- Single output MP4 with:
  - Picture-in-picture layout (avatar + customer)
  - Slide overlay for Phase 2
  - Mixed audio (avatar + customer)
- Store stitched file: `session_id/playback.mp4`

### Phase 3: HLS Adaptive Streaming
- Transcode to HLS on S3 upload
- Multiple quality levels (1080p, 720p, 480p, 360p)
- Generate `.m3u8` manifest
- Use video.js or Plyr for playback

### Phase 4: Advanced Features
- Transcript sync with highlight
- Slide boundary markers
- Clickable transcript navigation
- Playback speed control (0.5x, 1x, 1.5x, 2x)

## Out of Scope (Initial Version)

- ❌ Live stream recording (only post-conversation playback)
- ❌ Editing/clipping recordings
- ❌ Screen annotations during playback
- ❌ Downloadable recordings (view-only)
- ❌ Multi-angle camera switching

## Open Questions

1. **FFmpeg processing cost**: How much does server-side stitching cost at scale?
2. **Storage duplication**: Keep original files + stitched file, or delete originals?
3. **Processing delay**: Is 5-10 min delay acceptable, or need real-time?
4. **Transcript format**: Do we have millisecond-level timestamps for perfect sync?
5. **Mobile playback**: Should we generate lower-res versions for mobile?

## Dependencies

- FFmpeg (for video stitching)
- S3 presigned URL generation
- HLS transcoding service (AWS MediaConvert or open-source)
- Video.js or Plyr (HTML5 player library)

---

**Status**: Draft (Future Work)
**Next Steps**: Investigate FFmpeg cloud processing costs, validate transcript timestamp precision, prototype simple single-stream player
