目录

前言

了解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/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 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.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

播放卡住的问题

笔者实际测试发现,即便在局域网内,拉流播放也会出现卡死的问题。具体原因,笔者暂未查明,后续如有解决方案会另外写一篇文章阐述。

如若遇到其它问题,欢迎留言沟通。

参考资料

macaruinaAndroid接入SRS WebRtc直播流

传说中的二货《Android使用webrtc仅拉流并且播放》

冬季穿短裤Android端WebRTC启用H264编码-sdp中无H264信息

winlinvip用SRS快速搭建WebRTC推流和播放

感谢以上博主开源奉献的无私精神!

写在最后,首先非常感谢您耐心阅读完整篇文章,坚持写原创且基于实战的文章不是件容易的事,如果本文刚好对您有点帮助,欢迎您给文章点赞评论,您的鼓励是笔者坚持不懈的动力。若文章有不对之处也欢迎指正,再次感谢。

Logo

致力于链接即构和开发者,提供实时互动和元宇宙领域的前沿洞察、技术分享和丰富的开发者活动,共建实时互动世界。

更多推荐