Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

VideoFeedEngine

A high-performance, OOM-safe iOS video feed built with The Composable Architecture (TCA) and AVFoundation. Designed to handle continuous scrolling with automatic memory pressure management.


Architecture Overview

┌─────────────────────────────────────────────────────┐
│                    FeedView (SwiftUI)                │
│  ScrollView + GeometryReader visibility detection   │
│  ┌──────────────────────────────────────────────┐   │
│  │  PostCardView × 30                           │   │
│  │  AVPlayer lifecycle via .task(id: isPlaying) │   │
│  └──────────────────────────────────────────────┘   │
│  Dev HUD: live RSS (MB) + active player count       │
└────────────────────┬────────────────────────────────┘
                     │ TCA actions
┌────────────────────▼────────────────────────────────┐
│               FeedFeature (TCA Reducer)              │
│  • videoBecameVisible / videoBecameHidden            │
│  • Memory monitoring loop (1 s interval)            │
│  • Panic action when RSS > 600 MB                   │
└──────────┬──────────────────────┬───────────────────┘
           │                      │
┌──────────▼──────────┐  ┌────────▼────────────────┐
│   VideoPlayerPool   │  │     MemoryMonitor        │
│   (Swift Actor)     │  │  mach_task_basic_info    │
│   max 3 instances   │  │  RSS in MB               │
│   pause + nil item  │  └─────────────────────────┘
│   on release        │
└─────────────────────┘

Features

Feature Detail
Player Pool Maximum 3 concurrent AVPlayer instances; LRU-style eviction on scroll
OOM Protection RSS monitored every second; panic-releases all idle players when > 600 MB
Visibility Detection GeometryReader + PreferenceKey identifies the center-most post on every scroll tick
Memory-safe Release pause() → replaceCurrentItem(nil) tears down the VideoToolbox decoder session explicitly
TCA State All business logic is pure reducer logic — testable without UI
Dev HUD Floating overlay shows live RAM usage and pool occupancy

Project Structure

VideoFeedEngine/
├── Sources/
│   ├── MemoryMonitor.swift          # RSS via mach_task_basic_info
│   ├── MemoryMonitorClient.swift    # TCA DependencyKey
│   ├── VideoPlayerPool.swift        # Actor-isolated player pool
│   ├── VideoPlayerPoolClient.swift  # TCA DependencyKey + SwiftUI EnvironmentKey
│   ├── VideoPlayerView.swift        # AVPlayerLayer → UIViewControllerRepresentable
│   ├── Post.swift                   # Feed post model
│   ├── FeedFeature.swift            # TCA Reducer (state + actions + effects)
│   ├── FeedView.swift               # Main feed UI + Dev HUD
│   └── PostCardView.swift           # Single post card (Threads-like layout)
├── Tests/
│   ├── VideoPlayerPoolTests.swift   # Pool capacity and eviction tests
│   ├── FeedFeatureTests.swift       # TCA reducer tests (TestStore)
│   └── MemoryMonitorTests.swift     # RSS sanity checks
├── App/
│   └── VideoFeedEngineApp.swift     # @main entry point
├── project.yml                      # XcodeGen spec
├── Gemfile                          # Fastlane via Bundler
└── fastlane/
    ├── Fastfile                     # test + beta lanes
    ├── Matchfile                    # Code signing via Match
    └── Appfile                      # Bundle ID + Apple ID

Requirements

  • Xcode 16+
  • iOS 17+
  • XcodeGen (brew install xcodegen)
  • Bundler (gem install bundler)

Getting Started

# 1. Clone
git clone https://github.com/alantin918/VideoFeedEngine.git
cd VideoFeedEngine

# 2. Generate Xcode project
xcodegen generate

# 3. Open in Xcode
open VideoFeedEngine.xcodeproj

Xcode will automatically resolve the ComposableArchitecture Swift Package on first open.


Running Tests

# Via Fastlane (recommended)
bundle install
bundle exec fastlane test

# Or directly with xcodebuild
xcodebuild test \
  -scheme VideoFeedEngine \
  -destination 'platform=iOS Simulator,name=iPhone 16,OS=latest' \
  | xcpretty

CI / CD

Trigger Job What it does
Every push / PR test Generates project, runs all unit tests on macos-15
Push to main (after tests pass) beta Builds release IPA and uploads to TestFlight

CI results and test artifacts are uploaded as GitHub Actions artifacts with a 7-day retention.


Key Design Decisions

Why RSS and not virtual memory for OOM detection? Virtual memory (VSZ) is inflated by lazy framework mappings and is meaningless for OOM prediction on iOS. RSS reflects the physical pages actually wired into RAM — including GPU-backed IOSurface buffers held by AVFoundation's VideoToolbox decoder. The system OOM killer acts on RSS, so monitoring RSS gives accurate early warning.

Why actor for VideoPlayerPool? Swift's actor isolation serializes all access to activePlayers without manual locking. It also integrates naturally with async/await call sites in TCA effects.

Why UIViewControllerRepresentable instead of UIViewRepresentable for the player view? dismantleUIViewController is called more reliably when SwiftUI removes a view from a scroll container. dismantleUIView has documented edge cases in LazyVStack where teardown is deferred, which would delay decoder cleanup.

Why is AVPlayer not stored in TCA state? AVPlayer is a reference type and not Equatable, which would break TCA's diffing. The TCA reducer owns intent (currentlyPlayingID); each PostCardView owns the object (AVPlayer) via .task(id: isPlaying).


License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages