Android audio and video development practice series-04-Android WebRTC push stream to SRS server to realize live broadcast function

Table of contents

foreword

Learn about WebRTC

Deploy the SRS server

Download the source code and run

possible problems

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

Play stuck problem

References


foreword

Recently, the project uses WebRTC to replace the RTMP streaming function, because the RTMP delay is too high to meet the demand. This article intends to record the related problems and solutions encountered in learning WebRTC, and help everyone avoid pitfalls.

To avoid wasting everyone's time, first upload the renderings:

The first picture is an Android App using WebRTC to push streams, the source code address: https://github.com/xiangang/WebRTCTest

The above source code fork is from the demo of the legendary Erhuo 's "Android uses webrtc to only pull streams and play" :

​​​​​​https://github.com/Henry-6/WebRTCTest , the author modified the support for the SRS server version V4.0, and modified the create session: create session: add publisher: publish negotiate: no found Valid H.264 payload type problem. The solution to this problem comes from "Android-Web Video Calling Based on SRS Server (3): Android Pushes WebRTC Stream to SRS Server" by Wearing Shorts in Winter . Thanks to the two big guys!

The second picture is the playback effect of the WebRTC Player provided by the SRS server (the gif is too large, so you can only see the effect by taking a screenshot).

Learn about WebRTC

Not much to say, if you are interested in WebRTC technology, it is recommended to read "WebRTC Audio and Video Real-time Interactive Technology--Principle, Implementation and Source Code Analysis" by Li Chao .

It is free to read in WeChat reading, great conscience!

Deploy the SRS server

SRS is a simple and efficient real-time video server that supports RTMP/WebRTC/HLS/HTTP-FLV/SRT/GB28181.

The official document is here: https://github.com/ossrs/srs/wiki/v4_CN_Home , deploying the SRS server is very simple, just follow the official WIKI. You can also refer to "Using SRS to Quickly Build WebRTC Streaming and Playback" written by winlinvip himself , and the author will not go into details.

Download the source code and run

The source code is here: https://github.com/xiangang/WebRTCTest , import it into AndroidStudio after cloning, it is recommended to directly modify the IP of the SRS server in the MainActivity class:

ed1.setText("webrtc://192.168.1.139/live/livestream");
ed2.setText("webrtc://192.168.1.139/live/livestream");

The author recommends using the Linux system to deploy the SRS server, and the mobile phone running the streaming demo app is connected to the same local area network as the SRS server. If there is no Linux system available, it is recommended to use Docker to deploy.

After the App Demo is installed, click the Push button to push the stream. The Play button is used for streaming playback.

In addition, SRS officially provides Flutter's live streaming app: https://github.com/ossrs/flutter_live

Interested partners can download the running experience by themselves. It should be noted that if you run into the following problems:

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

It can be solved by configuring the NDK path in the /flutter_live/example/android/local.properties file.

## 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

Be careful not to get the path wrong.

possible problems

create session : create session : add publisher : publish negotiate : no found valid H.264 payload type

For the solution to this problem, see "Android-Web Video Calling Based on SRS Server (3): Android Pushes WebRTC Stream to SRS Server" by Wearing Shorts in Winter

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

This problem may be a bug of the SRS server. It is recommended to change the stream address or restart the SRS server. or refer to:

WebRTC: Can request the interface to return to normal, use Google browser and srs live APP can not play, has been circling

Troubleshoot SRS problem record 1 (RTMP streaming, RTC playback stuck)

srs fails to distribute webrtc, the stream cannot be played #1727

Play stuck problem

The author's actual test found that even in the local area network, the streaming playback will still have the problem of freezing. The author has not yet identified the specific reason, and if there is a solution in the future, I will write another article to explain it.

If you encounter other problems, please leave a message to communicate.

References

Macaruina 's Android access to SRS WebRtc live stream

The legendary Erhuo's " Android uses webrtc to only pull and play"

WebRTC on Android side wearing shorts in winter enables H264 encoding - no H264 information in sdp

Winlinvip uses SRS to quickly build WebRTC streaming and playback

Thanks to the above bloggers for their selfless dedication to open source!

Written at the end, first of all, thank you very much for your patience in reading the entire article. It is not easy to insist on writing original and practical articles. If this article happens to be helpful to you, you are welcome to like and comment on the article. Your encouragement is the author's insistence Unrelenting drive. If there are any mistakes in the article, please correct me, thank you again.

Guess you like

Origin blog.csdn.net/xiangang12202/article/details/122994219