前言
了解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服务器实现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/01 FAILURE: 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 prefi: 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.org
BUILD 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推流和播放
感谢以上博主开源奉献的无私精神!
写在最后,首先非常感谢您耐心阅读完整篇文章,坚持写原创且基于实战的文章不是件容易的事,如果本文刚好对您有点帮助,欢迎您给文章点赞评论,您的鼓励是笔者坚持不懈的动力。若文章有不对之处也欢迎指正,再次感谢。