在纯 PHP 中实现 WebRTC 协议的完整实现!后端不需要 Node.js 或 JavaScript 即可使用。但是,您需要启用 FFI。
目标是让使用纯 PHP 构建基于 WebRTC 的应用程序变得容易,包括媒体服务器、视频会议 Web 应用程序、SFU 和点对点应用程序。
“地址:https://github.com/PHP-WebRTC
该软件包提供了完全用 PHP 编写的完整 WebRTC 实现,包括对 ICE、DTLS、SRTP、SCTP、RTP 和数据通道的支持。它专为实时音频、视频和数据通信而设计,无需依赖外部 WebRTC 库即可实现点对点连接。使用模块化组件构建,并使用 ReactPHP 完全异步。
创建名为 install-deps.sh
的文件:
#!/bin/bash
set -e
echo"🔧 Updating system and installing base dependencies..."
apt-get update && apt-get install -y \
apt-transport-https \
ca-certificates \
gnupg \
autoconf \
automake \
build-essential \
cmake \
git \
libtool \
pkg-config \
yasm \
nasm \
wget \
unzip \
curl \
libzip-dev \
libgmp-dev \
libssl-dev \
libsrtp2-dev \
libx264-dev \
libffi-dev \
libprotobuf-dev \
php-dev \
php-pear \
php-gmp \
php-zip \
php-ffi \
php-cli \
php-mbstring \
php-curl \
php-xml \
php-bcmath \
php-tokenizer \
php-dom \
php-pcov \
php-sqlite3 \
php-pdo \
php-json \
php-opcache \
php-readline \
php-soap \
php-intl \
php-exif \
php-fileinfo \
php-phar \
php-fpm \
php-common \
php-iconv \
php-posix \
php-sockets \
php-pcntl \
python3 \
&& rm -rf /var/lib/apt/lists/*
echo"✅ Base system dependencies installed."
echo"📦 Installing PHP Protobuf extension..."
pecl install protobuf
echo"extension=protobuf.so" > /etc/php/8.4/cli/conf.d/30-protobuf.ini
echo"✅ Protobuf extension installed and enabled."
echo"⚙️ Enabling PHP FFI..."
echo"ffi.enable=true" > /etc/php/8.4/cli/conf.d/30-ffi.ini
echo"✅ FFI enabled."
echo"🎬 Building and installing FFmpeg 7.1.1..."
cd /tmp
wget https://ffmpeg.org/releases/ffmpeg-7.1.1.tar.bz2
tar xjf ffmpeg-7.1.1.tar.bz2
cd ffmpeg-7.1.1
./configure --enable-shared --enable-gpl --enable-libx264 --enable-libopus --enable-libvpx
make -j$(nproc)
make install
ldconfig
cd /tmp && rm -rf ffmpeg-7.1.1*
echo"✅ FFmpeg 7.1.1 installed."
echo"🎧 Building and installing libopus 1.4..."
cd /tmp
wget https://github.com/xiph/opus/releases/download/v1.4/opus-1.4.tar.gz
tar xzvf opus-1.4.tar.gz
cd opus-1.4
./configure --prefix=/usr/local --enable-shared
make -j$(nproc)
make install
ldconfig
cd /tmp && rm -rf opus-1.3.1*
echo"✅ libopus 1.3.1 installed."
echo"🎥 Building and installing libvpx 1.15.0..."
cd /tmp
wget https://github.com/webmproject/libvpx/archive/refs/tags/v1.15.0.tar.gz
tar xzvf v1.15.0.tar.gz
cd libvpx-1.15.0
./configure --prefix=/usr/local --enable-shared --disable-examples
make -j$(nproc)
make install
ldconfig
cd /tmp && rm -rf libvpx-1.15.0*
echo"✅ libvpx 1.15.0 installed."
echo"🎉 All dependencies installed successfully!"
授予脚本执行权限并运行它:
chmod +x install-deps.sh
./install-deps.sh"
通过 Composer 安装 PHP WebRTC
composer require quasarstream/webrtc
RTCConfiguration
类提供了配置 RTCPeerConnection
的选项。这包括 STUN/TURN 服务器和可选的 TLS 证书和私钥路径,确保安全 DTLS 连接。
use Webrtc\Webrtc\RTCConfiguration;
useWebrtc\ICE\RTCIceServer;
$stunServer = new RTCIceServer();
$stunServer->setUrls(['stun:stun.l.google.com:19302']);
$turnServer = new RTCIceServer();
$turnServer->setUrls(['turn:turn.example.com']);
$turnServer->setUsername('user');
$turnServer->setCredential('pass');
$turnServer->setCredentialType('password');
$config = new RTCConfiguration(
iceServers: [$stunServer, $turnServer],
certificatePath: '/etc/ssl/certs/rtc-cert.pem',
privateKeyPath: '/etc/ssl/private/rtc-key.pem'
);
您还可以构建默认配置(使用 Google 的公共 STUN)
$configuration = new RTCConfiguration();
$pc = new RTCPeerConnection($configuration);
//or don't pass any variable
$pc = new RTCPeerConnection();
有一个选项可以将关联数组传递给 config 作为 blow
use Webrtc\Webrtc\RTCConfiguration;
$pc = new RTCPeerConnection([
'iceServers' => [
[
'urls' => ['stun:stun.l.google.com:19302']
],
[
'urls' => ['turn:turn.example.com'],
'username' => 'user',
'credential' => 'pass',
'credentialType' => 'password'
]
],
'certificatePath' => '/etc/ssl/certs/rtc-cert.pem',
'privateKeyPath' => '/etc/ssl/private/rtc-key.pem'
]);