ffmpeg 支持 webrtc 推流

1 前言

ffmpeg 是音視頻業界最著名的開源之一,支持多種流媒體協議的推拉流,支持多種媒體格式,支持多種編解碼器,但是一直沒有對 webrtc 協議的推拉流進行支持。

在 whip 協議寫入 rfc 的草案後,webrtc 協議的推流協議就有了規範。

whip 協議,全名: WebRTC-HTTP ingestion protocol.

rfc 地址: 

https://www.ietf.org/archive/id/draft-ietf-wish-whip-16.txt

在 SRS 開源創始人 winlin 親自下場,開發了 ffmpeg 支持 webrtc 推流的功能,在 ffmpeg 裏支持了 whip 協議。

源碼地址:

https://github.com/ossrs/ffmpeg-webrtc

本文介紹如何使用 ffmpeg 進行 webrtc 推流。

2 whip 簡介

whip 協議的流程圖如下:

 +-------------+    +---------------+ +--------------+ +---------------+
 | WHIP client |    | WHIP endpoint | | Media Server | | WHIP session  |
 +--+----------+    +---------+-----+ +------+-------+ +--------|------+
    |                         |              |                  |
    |                         |              |                  |
    |HTTP POST (SDP Offer)    |              |                  |
    +------------------------>+              |                  |
    |201 Created (SDP answer) |              |                  |
    +<------------------------+              |                  |
    |          ICE REQUEST                   |                  |
    +--------------------------------------->+                  |
    |          ICE RESPONSE                  |                  |
    |<---------------------------------------+                  |
    |          DTLS SETUP                    |                  |
    |<======================================>|                  |
    |          RTP/RTCP FLOW                 |                  |
    +<-------------------------------------->+                  |
    | HTTP DELETE                                               |
    +---------------------------------------------------------->+
    | 200 OK                                                    |
    <-----------------------------------------------------------x

3 ffmpeg webrtc 模塊編譯

ffmpeg 的編譯過程對於小白是比較痛苦的。本節介紹如何編譯 ffmpeg,讓其支持 webrtc 推流。

首先支持 webrtc 的推薦編碼格式爲: 

同時需要支持 openssl,因爲 https post 是需要 openssl。

最後需要 whip muxer 模塊,大神 winlin 開發的 webrtc 推流模塊。

3.1 x264 編譯

下載地址:

https://code.videolan.org/videolan/x264/-/archive/master/x264-master.tar.bz2

x264 編譯:

./configure --enable-shared --disable-asm
make -j 2
make install

如果需要支持 asm 的,可以去下載 yasm 後再編譯

3.2 libopus 編譯

下載地址:

https://opus-codec.org/downloads/

libopus 編譯:

./configure --enable-shared
make -j 2
make install

3.3 ffmpeg 編譯

源碼地址:

git@github.com:ossrs/ffmpeg-webrtc.git
分支: patch/whip/v1

./configure 中需要使能:

編譯:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/ && ./configure --disable-avdevice \
--disable-doc --disable-devices \
--enable-openssl --enable-muxer=whip \
--enable-libopus --enable-libx264 \
--enable-nonfree --enable-gpl \
--extra-ldflags="-lpthread -lssl -lcrypto" \
--disable-x86asm
make -j 2
make install

3.4 ffmpeg 的 webrtc 推流

推流需要注意的:

http://192.168.1.100:1985/rtc/v1/whip/?app=live&stream=livestream

其中 ip 爲 srs 服務的對外地址,1985 爲 srs 的 httpapi 端口號。

"/rtc/v1/whip/" 爲 http 的路徑名。

"app=live" 和 "stream=livestream",表明在 srs 中 webrtc 推流後,自動轉成 rtmp 時,rtmp 的 app 爲 live,stream 爲 livestream。

ffmpeg 推流命令行:

ffmpeg -re -i source.flv \
-c:v libx264 -profile:v baseline \
-c:a libopus -ar 48000 -ac 2 -ab 32k \
-f whip "http://192.168.1.100:1985/rtc/v1/whip/?app=live&stream=livestream"

3.4.1 srs 編譯

srs 是國內流媒體最牛逼的開源之一,它的易用性和文檔完整性是業內最好的。

github 地址:

https://github.com/ossrs/srs

編譯與運行

./configure
make -j 2
./objs/srs -c conf/rtc2rtmp.conf

直接使用 rtc2rtmp.conf,這個配置支持自動把 rtc 流轉成 rtmp.

在 ffmpeg 推流後:

ffmpeg -re -i source.flv \
-c:v libx264 -profile:v baseline \
-c:a libopus -ar 48000 -ac 2 -ab 32k \
-f whip "http://192.168.1.100:1985/rtc/v1/whip/?app=live&stream=livestream"

因爲 srs 自動把 rtc 轉成 rtmp 流,所以可以用 vlc 直接觀看地址:

rtmp://192.168.1.100/live/livestream

公衆號後面的文章還會繼續介紹:

通過對代碼的學習,讓你能掌握整個 webrtc 的推流過程,對整個 webrtc 協議族有深入的認識。

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/urFWlNYcOFs5OwCrre9WGA