Android 音视频开发实践系列-04-Android WebRTC推流到SRS服务器实现直播功能
目錄
前言
了解WebRTC
部署SRS服務器
下載源碼并運行
可能遇到的問題
create session : create session : add publisher : publish negotiate : no found valid H.264 payload type
RTC error code=5020 : create session : stream /live/livestream busy
播放卡住的問題
參考資料
前言
最近項目用到WebRTC來替換RTMP推流功能,原因是RTMP延遲太高不滿足需求。本文意在記錄學習WebRTC的遇到的相關問題和解決方案,幫助大家避坑。
避免浪費大家時間,先上效果圖:
第一張圖是Android App使用WebRTC推流,源碼地址:https://github.com/xiangang/WebRTCTest
以上源碼fork自傳說中的二貨的《Android使用webrtc僅拉流并且播放》的demo:
??????https://github.com/Henry-6/WebRTCTest,筆者修改了對SRS服務器V4.0版本的支持,并修改了create session : create session : add publisher : publish negotiate : no found valid H.264 payload type的問題。該問題的解決方案來自于冬季穿短褲的《基于SRS服務器實現Android-Web端視頻通話(3):Android端向SRS服務器推送WebRTC流》。感謝兩位大佬!
第二張圖是SRS服務器提供的WebRTC Player的播放效果(gif太大,只能截圖看效果)。
了解WebRTC
就不多說了,如果對WebRTC技術感興趣,建議閱讀李超的《WebRTC音視頻實時互動技術--原理、實現與源碼分析》。
在微信讀書中是免費的閱讀,大大的良心!
部署SRS服務器
SRS是一個簡單高效的實時視頻服務器,支持RTMP/WebRTC/HLS/HTTP-FLV/SRT/GB28181。
官方文檔在這:https://github.com/ossrs/srs/wiki/v4_CN_Home,部署SRS服務器很簡單,按照官方WIKI來就好了。也可以參考winlinvip大佬本人寫的《用SRS快速搭建WebRTC推流和播放》,筆者也不贅述了。
下載源碼并運行
源碼在這:https://github.com/xiangang/WebRTCTest,克隆后導入AndroidStudio,建議在MainActivity類中直接修改SRS服務器的IP:
ed1.setText("webrtc://192.168.1.139/live/livestream"); ed2.setText("webrtc://192.168.1.139/live/livestream");筆者比較建議使用Linux系統來部署SRS服務器,運行推流Demo App的手機連接到和SRS服務器所在的同一個局域網內。如果沒有可用的Linux系統,建議使用Docker來部署。
App Demo安裝后點Push按鈕即可推流。Play按鈕則用于拉流播放。
另外,SRS官方也提供了Flutter的流媒體直播App:https://github.com/ossrs/flutter_live
感興趣的小伙伴可以自行下載運行體驗,需要注意的是,如果你運行遇到以下問題:
Launching lib/main.dart on LB4418 in debug mode... Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01 Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01 Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01FAILURE: Build failed with an exception.* What went wrong: Execution failed for task ':app:stripDebugDebugSymbols'. > No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi* Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.* Get more help at https://help.gradle.orgBUILD FAILED in 58s Running Gradle task 'assembleDebug'... 67.7s Exception: Gradle task assembleDebug failed with exit code 1可通過在/flutter_live/example/android/local.properties文件中配置NDK路徑來解決。
## This file must *NOT* be checked into Version Control Systems, # as it contains information specific to your local configuration. # # Location of the SDK. This is only used by Gradle. # For customization when using a Version Control System, please read the # header note. #Tue Feb 15 17:14:21 CST 2022 sdk.dir=/work/android/android-sdk-linux ndk.dir=/work/android/android-ndk-r20#配置NDK路徑 flutter.buildMode=debug flutter.versionCode=1 flutter.sdk=/home/work/flutter flutter.versionName=1.0.9注意,路徑不要搞錯了。
可能遇到的問題
create session : create session : add publisher : publish negotiate : no found valid H.264 payload type
此問題的解決方案看冬季穿短褲的《基于SRS服務器實現Android-Web端視頻通話(3):Android端向SRS服務器推送WebRTC流》
RTC error code=5020 : create session : stream /live/livestream busy
[2022-02-15 14:21:23.569][Warn][1][wz97i48f][11] RTC error code=5020 : create session : stream /live/livestream busy thread [1][wz97i48f]: do_serve_http() [src/app/srs_app_rtc_api.cpp:458][errno=11] thread [1][wz97i48f]: create_session() [src/app/srs_app_rtc_server.cpp:521][errno=11] [2022-02-15 14:21:23.577][Trace][1][wz97i48f] TCP: before dispose resource(HttpConn)(0x2c4e240), conns=1, zombies=0, ign=0, inz=0, ind=0 [2022-02-15 14:21:23.577][Warn][1][wz97i48f][104] client disconnect peer. ret=1007 [2022-02-15 14:21:23.577][Trace][1][bik1o4g8] TCP: clear zombies=1 resources, conns=1, removing=0, unsubs=0 [2022-02-15 14:21:23.577][Trace][1][wz97i48f] TCP: disposing #0 resource(HttpConn)(0x2c4e240), conns=1, disposing=1, zombies=0 [2022-02-15 14:21:23.592][Trace][1][5ftt8q4e] HTTP #0 192.168.0.8:52660 POST http://192.168.0.11:1985/rtc/v1/publish/, content-length=4089 [2022-02-15 14:21:23.593][Trace][1][5ftt8q4e] RTC publish webrtc://192.168.0.11/live/livestream, api=http://192.168.0.11:1985/rtc/v1/publish/, tid=, clientip=192.168.0.8, app=live, stream=livestream, offer=3747B, eip=, codec= [2022-02-15 14:21:23.594][Warn][1][5ftt8q4e][11] RTC error code=5020 : create session : stream /live/livestream busy thread [1][5ftt8q4e]: do_serve_http() [src/app/srs_app_rtc_api.cpp:458][errno=11] thread [1][5ftt8q4e]: create_session() [src/app/srs_app_rtc_server.cpp:521][errno=11] [2022-02-15 14:21:23.602][Trace][1][5ftt8q4e] TCP: before dispose resource(HttpConn)(0x2c4e240), conns=1, zombies=0, ign=0, inz=0, ind=0 [2022-02-15 14:21:23.602][Warn][1][5ftt8q4e][104] client disconnect peer. ret=1007 [2022-02-15 14:21:23.602][Trace][1][bik1o4g8] TCP: clear zombies=1 resources, conns=1, removing=0, unsubs=0 [2022-02-15 14:21:23.602][Trace][1][5ftt8q4e] TCP: disposing #0 resource(HttpConn)(0x2c4e240), conns=1, disposing=1, zombies=0 ^C[2022-02-15 14:21:23.606][Trace][1][823e71md] cleanup for quit signal fast=1, grace=0 [2022-02-15 14:21:23.606][Warn][1][823e71md][11] main cycle terminated, system quit normally. [2022-02-15 14:21:23.606][Warn][1][80fp0709][4] inotify ignore read failed, nn=-1 [2022-02-15 14:21:23.706][Trace][1][823e71md] srs disposed [2022-02-15 14:21:23.706][Trace][1][823e71md] srs terminated此問題有可能是SRS服務器的一個bug,建議換個流地址或重啟SRS服務器。或參考:
WebRTC: 能請求到接口返回正常,使用谷歌瀏覽器和srs直播APP都不能播放,一直在轉圈圈
排查SRS問題記錄一(RTMP推流,RTC播放卡住)
srs分發webrtc失敗,流無法播放 #1727
播放卡住的問題
筆者實際測試發現,即便在局域網內,拉流播放也會出現卡死的問題。具體原因,筆者暫未查明,后續如有解決方案會另外寫一篇文章闡述。
如若遇到其它問題,歡迎留言溝通。
參考資料
macaruina的Android接入SRS WebRtc直播流
傳說中的二貨的《Android使用webrtc僅拉流并且播放》
冬季穿短褲的Android端WebRTC啟用H264編碼-sdp中無H264信息
winlinvip的用SRS快速搭建WebRTC推流和播放
感謝以上博主開源奉獻的無私精神!
寫在最后,首先非常感謝您耐心閱讀完整篇文章,堅持寫原創且基于實戰的文章不是件容易的事,如果本文剛好對您有點幫助,歡迎您給文章點贊評論,您的鼓勵是筆者堅持不懈的動力。若文章有不對之處也歡迎指正,再次感謝。
總結
以上是生活随笔為你收集整理的Android 音视频开发实践系列-04-Android WebRTC推流到SRS服务器实现直播功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: snowboy嵌入式_编译配置SNOWB
- 下一篇: 印第安人的捕梦网 Dreamcatche