---
name: Implement LiveKit Video Streaming for Screenshare Bot
status: closed
created: 2025-10-21T20:00:00Z
updated: 2025-10-22T08:10:19Z
github: https://github.com/BuzzTrail/video-extension/issues/166
depends_on: [165]
parallel: false
effort_hours: 8-12
---

# Task: Implement LiveKit Video Streaming for Screenshare Bot

## Description

Complete the video streaming implementation for the Node.js screenshare bot. Currently, the bot captures screenshots at 30fps but doesn't encode or publish them to LiveKit as video frames. This task adds the missing video encoding pipeline to stream screenshots to the LiveKit room as a screenshare track.

## Current State

The bot implementation in `apps/bot-spawner/src/bots/ScreenshareBot.ts` has a TODO at lines 161-164:

```typescript
// TODO: Convert screenshot to video frame and publish to LiveKit
// This requires additional work with video encoding
// For now, we're just capturing - the actual LiveKit streaming
// will need to be implemented based on LiveKit's video track API
```

The `startCapture()` method captures PNG screenshots every ~33ms (30fps) but the frames are not published to LiveKit.

## Acceptance Criteria

**Core Functionality:**
- [ ] Convert PNG screenshots to video frames suitable for LiveKit
- [ ] Create and publish LiveKit video track with screenshare source
- [ ] Maintain ~30fps streaming rate
- [ ] Proper video format/codec selection (VP8/VP9/H264)
- [ ] Video track labeled as screenshare in LiveKit room

**Quality:**
- [ ] Efficient memory management (no frame buffer leaks)
- [ ] Graceful handling of encoding errors
- [ ] Video quality suitable for slide presentations (text readability)
- [ ] Proper cleanup on bot shutdown

**Testing:**
- [ ] Local testing: Video appears in LiveKit room
- [ ] Verify slides are readable in voice-demo-ui
- [ ] Test with multiple bots simultaneously
- [ ] Railway deployment validation
- [ ] Monitor memory usage over time

## Implementation Approach

### Option 1: Use LiveKit's Video Track API
Research LiveKit's Node.js SDK for creating custom video tracks from image buffers. This is the cleanest approach if the SDK supports it.

### Option 2: Use FFmpeg for Encoding
If direct frame publishing isn't supported, use FFmpeg to encode screenshots into a video stream that LiveKit can consume.

### Option 3: Canvas/WebRTC API
Use Node.js Canvas API to create a MediaStream from screenshots and publish to LiveKit.

## Technical Resources

- LiveKit Node SDK docs: https://docs.livekit.io/reference/node/
- LiveKit video track documentation
- Playwright screenshot API (already implemented)
- Consider using `sharp` library for efficient image processing

## Files to Modify

- `apps/bot-spawner/src/bots/ScreenshareBot.ts` - Main implementation
- `apps/bot-spawner/package.json` - Add any new dependencies (ffmpeg, sharp, etc.)

## Dependencies

**Blocked by:** #165 (core bot implementation must be complete)
**Blocks:** End-to-end testing and production deployment

## Effort Estimate

**Size:** M (Medium)
**Hours:** 8-12 hours

**Breakdown:**
- Research LiveKit video track API: 2-3 hours
- Implement video encoding pipeline: 4-6 hours
- Testing and optimization: 2-3 hours

## Success Metrics

- Screenshots successfully streamed as video to LiveKit room
- Video quality meets readability requirements for slides
- No significant memory leaks after 30+ minute sessions
- Bot works reliably on Railway
