iOS的音视频框架有 AVKit 和 AVFoundation,他们的关系如图:

avkit

可以看出,avkit 位于相对上层,使用起来也更加容易,我们先讲解 avkit。

AVKit

苹果官方文档在这里:https://developer.apple.com/documentation/avkit/playing-video-content-in-a-standard-user-interface

架构

+------------------------------------------------------------------+
|                        Application Layer                         |
|  (Your App / SwiftUI / UIKit)                                    |
|                                                                  |
|  - Custom Video Player UI                                        |
|  - Playback Controls, Seek Bar, Buffer Indicator                 |
|  - Business Logic (e.g., DRM, Analytics, Playlist)                |
+-------------------------------+----------------------------------+
                                |
                                | Uses
                                \/
+------------------------------------------------------------------+
|                     High-Level AV Frameworks                     |
|  (Built on top of AVFoundation)                                  |
|                                                                  |
|  +----------------+    +------------------+    +--------------+  |
|  | AVKit          |    | ReplayKit        |    | Photos       |  |
|  | (AVPlayerView, |    | (Screen/Video    |    | (PHImage     |  |
|  |  AVRoutePicker)|    |  Recording)      |    |  Manager)    |  |
|  +----------------+    +------------------+    +--------------+  |
|                                                                  |
|  * Provides ready-to-use UI components                           |
|  * Simplifies common media tasks                                 |
+-------------------------------+----------------------------------+
                                |
                                | Built upon
                                \/
+------------------------------------------------------------------+
|                       AVFoundation Core                          |
|  (The main multimedia framework on Apple platforms)              |
|                                                                  |
|  +------------------------------------------------------------+  |
|  | AVPlayer / AVQueuePlayer                                   |  |
|  | - Controls playback timeline                                |  |
|  +------------------------------------------------------------+  |
|                                                                  |
|  +------------------------------------------------------------+  |
|  | AVPlayerItem                                               |  |
|  | - Represents a single media asset (video/audio)            |  |
|  | - Manages loading, buffering, tracks, metadata             |  |
|  +------------------------------------------------------------+  |
|                                                                  |
|  +------------------------------------------------------------+  |
|  | AVAsset / AVURLAsset / AVComposition                       |  |
|  | - Describes media structure (tracks, duration, format)     |  |
|  | - Does NOT load data until needed (lazy)                   |  |
|  +------------------------------------------------------------+  |
|                                                                  |
|  +------------------------------------------------------------+  |
|  | AVMetadata, AVTimedMetadataGroup                           |  |
|  | - Handles subtitles, ID3 tags, chapter info                |  |
|  +------------------------------------------------------------+  |
|                                                                  |
|  +------------------------------------------------------------+  |
|  | AVCaptureSession / AVCaptureDevice                         |  |
|  | - Camera & microphone input pipeline                       |  |
|  +------------------------------------------------------------+  |
|                                                                  |
|  +------------------------------------------------------------+  |
|  | AVAssetExportSession / AVAssetReader/Writer                |  |
|  | - Transcoding, trimming, mixing                            |  |
|  +------------------------------------------------------------+  |
+-------------------------------+----------------------------------+
                                |
                                | Relies on
                                \/
+------------------------------------------------------------------+
|                    Lower-Level C Frameworks                      |
|  (Private or semi-private; mostly closed-source)                 |
|                                                                  |
|  +----------------+    +----------------+    +----------------+  |
|  | Core Media     |    | Core Video     |    | Core Audio     |  |
|  | (CMTime,       |    | (CVPixelBuffer,|    | (AudioStream,  |  |
|  |  CMSampleBuffer)|   |  CVImageBuffer)|    |  AudioQueue)   |  |
|  +----------------+    +----------------+    +----------------+  |
|                                                                  |
|  +----------------+    +----------------+    +----------------+  |
|  | Video Toolbox  |    | Audio Toolbox  |    | MediaToolbox   |  |
|  | (HW encoding/  |    | (Audio codecs) |    | (Playback      |  |
|  |  decoding)     |    |                |    |  engine)       |  |
|  +----------------+    +----------------+    +----------------+  |
|                                                                  |
|  +------------------------------------------------------------+  |
|  | Network Stack (NSURLSession, HTTP Live Streaming parser)   |  |
|  +------------------------------------------------------------+  |
+-------------------------------+----------------------------------+
                                |
                                | Runs on
                                \/
+------------------------------------------------------------------+
|                        Hardware & OS Kernel                      |
|                                                                  |
|  - GPU (for video decode/encode via Metal/VDA)                   |
|  - Audio I/O (via HAL - Hardware Abstraction Layer)              |
|  - File System / Network Interface                               |
|  - Secure Enclave (for FairPlay DRM)                             |
+------------------------------------------------------------------+

应用层开发,我们一般接触到的:

+--------------------------------------------------+
|                 Your App                         |
|                                                  |
|  +----------------+     +---------------------+  |
|  | Custom Player  |     | Game / Tutorial App |  |
|  | (Uses AVFoun-  |     | (Uses ReplayKit to  |  |
|  |  dation directly)|   |  record screen)     |  |
|  +----------------+     +----------+----------+  |
|                                    |             |
+------------------+-----------------+-------------+
                   |                 |
                   | Uses          | Uses
                   \/                \/
        +----------+------+  +-------+--------+
        |    AVKit        |  |   ReplayKit    |
        | (For standard   |  | (For recording |
        |  playback UI)   |  |  and broadcast)|
        +--------+--------+  +-------+--------+
                 |                   |
                 | Both rely on      | Relies on
                 \/                 \/
        +--------------------------------------+
        |           AVFoundation               |
        | - AVPlayer (playback)                |
        | - AVCaptureSession (audio input)     |
        | - AVAssetWriter (recording output)   |
        | - AVAsset (media representation)     |
        +--------------------------------------+
                         |
                         | Uses
                        \/
               +---------------------+
               | Core Media / Video  |
               | Toolbox / Core Audio|
               +---------------------+

总结

大多数 App 会混合使用:

用 AVFoundation 做核心播放逻辑
用 AVKit 做全屏播放页
用 ReplayKit 做用户生成内容(UGC)录制

ijkplayer

avfoundation 是 iOS 自带,免去了编译操作,但是定制性差一些,下面聊聊比较火的 ijkplayer。他的架构图:

+------------------------------------------------------------------+
|                        Application Layer                         |
|  (Your App / Demo: IJKMediaDemo, Custom Player UI)              |
+------------------------------------------------------------------+
                              │
                              \/
+------------------------------------------------------------------+
|                     IJKMediaPlayback API Layer                   |
|  - Protocol: IJKMediaPlayback (Obj-C)                            |
|  - Main Class: IJKFFMoviePlayerController                        |
|  - View: IJKFFMovieView (Metal/OpenGL)                           |
+------------------------------------------------------------------+
                              │
                              \/
+------------------------------------------------------------------+
|                    FFmpeg Wrapper Layer (C)                      |
|  - ijkavformat.c / ijkio.c / ijkplayer.c                         |
|  - 封装 FFmpeg 核心功能:demux, decode, audio output, clock sync |
|  - 提供 C 接口:ijkmp_* (e.g., ijkmp_prepare_async)              |
+------------------------------------------------------------------+
                              │
           ┌──────────────────┼──────────────────┐
          \/                  \/                  \/
+------------------+ +------------------+ +------------------+
|   libavformat    | |   libavcodec     | |   libswscale     |
|   (Demuxing)     | |   (Decoding)     | |   (Scaling/YUV)  |
+------------------+ +------------------+ +------------------+
           │                  │                  │
           \/                 \/                 \/
+---------------------------------------------------------------+
|                        libavutil (Utils)                      |
|  - dict, log, thread, time, frame, hwaccel (VideoToolbox)     |
+---------------------------------------------------------------+
                              │
                              \/
+------------------------------------------------------------------+
|                       Optional Dependencies                      |
|  - OpenSSL (libcrypto/libssl): for HTTPS/RTMPS                   |
|  - zlib: for HTTP gzip                                           |
|  - VideoToolbox: for iOS hardware decoding (H.264/H.265)         |
+------------------------------------------------------------------+

行动

下载编译运行都可以在这里找到:ijkplayer iOS 编译完全指南(Xcode 26.0.1 下,修复部分问题)

对比

优劣势:


+------------------+----------------+-------------------+------------------------+-----------+--------------+
| 特性             | ijkplayer      | AVPlayer (iOS)    | ExoPlayer (Android)    | WebRTC    | GStreamer    |
+------------------+----------------+-------------------+------------------------+-----------+--------------+
| RTMP 支持        | ✅ 原生         | ❌ 不支持          | ❌ 需扩展               | ❌ 不支持  | ✅ 插件支持   |
+------------------+----------------+-------------------+------------------------+-----------+--------------+
| HTTP-FLV 支持    | ✅ 原生         | ❌                | ❌                     | ❌        | ✅           |
+------------------+----------------+-------------------+------------------------+-----------+--------------+
| HLS 延迟         | ~3~8s(可优化)| 6~15s(固定)     | ~3~8s(可优化)        | ❌        | ~2~5s        |
+------------------+----------------+-------------------+------------------------+-----------+--------------+
| 最低延迟         | 1~3s           | ≥6s               | 2~5s                   | <500ms    | 1~3s         |
+------------------+----------------+-------------------+------------------------+-----------+--------------+
| 硬解支持         | ✅ (VideoToolbox)| ✅               | ✅                     | ✅        | ✅ (需配置)  |
+------------------+----------------+-------------------+------------------------+-----------+--------------+
| 自定义协议       | ✅ (C 层 IO)    | ❌                | ✅ (DataSource)        | ❌        | ✅           |
+------------------+----------------+-------------------+------------------------+-----------+--------------+
| 包体积增量       | +8~12 MB       | 0                 | +5~8 MB                | +10~15 MB | +15~20 MB    |
+------------------+----------------+-------------------+------------------------+-----------+--------------+
| 开发难度         | 中(需编译)   | 低                | 中                     | 高        | 高           |
+------------------+----------------+-------------------+------------------------+-----------+--------------+
| 适用场景         | 移动端直播     | 点播/HLS          | Android 直播           | 超低延迟  | 嵌入式/专业  |
|                  | (RTMP/FLV)     |                   |                        | 互动      |              |
+------------------+----------------+-------------------+------------------------+-----------+--------------+

ijkplayer 直播模块介绍

架构

[RTMP Server / HTTP-FLV CDN]
          │
          ▼
   Custom I/O (ijkio.c) ←──┐
          │                │ 支持自定义协议、重连、超时
   libavformat (demux)     │
          │                │ 绕过 FFmpeg 默认缓冲
   AVPacket → PacketQueue ─┘
          │
   libavcodec (decode)
          │
   AVFrame → FrameQueue
          │
   VideoToolbox (硬解) or libswscale (软解)
          │
   Metal / OpenGL → IJKFFMovieView
          │
   AudioQueue → Speaker

编译配置

# module.sh
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-protocol=rtp"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-protocol=rtmp"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_FLAGS --extra-cflags=-I${FFMPEG_DEP_ROOT}/include"

初始化

NSURL *url = [NSURL URLWithString:@"rtmp://live.example.com/app/stream"];

IJKFFOptions *options = [IJKFFOptions optionsByDefault];

// 核心:低延迟配置
[options setPlayerOptionIntValue:1 forKey:@"videotoolbox"];           // 硬解
[options setPlayerOptionIntValue:1 forKey:@"start_on_prepared"];      // 准备好就播
[options setPlayerOptionIntValue:1 forKey:@"overlay-format"];         // GPU 渲染

// 减少缓冲(直播关键!)
[options setFormatOptionStringValue:@"0" forKey:@"reconnect"];
[options setFormatOptionStringValue:@"5000000" forKey:@"timeout"];    // 5s 超时
[options setFormatOptionStringValue:@"nobuffer" forKey:@"fflags"];
[options setFormatOptionStringValue:@"1" forKey:@"flush_packets"];
[options setCodecOptionStringValue:@"low_delay" forKey:@"flags"];
[options setPlayerOptionIntValue:1 forKey:@"framedrop"];              // 丢帧保流畅

IJKFFMoviePlayerController *player = [[IJKFFMoviePlayerController alloc] initWithContentURL:url options:options];