日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android 音视频开发实践系列-04-Android WebRTC推流到SRS服务器实现直播功能

發(fā)布時(shí)間:2024/3/12 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 音视频开发实践系列-04-Android WebRTC推流到SRS服务器实现直播功能 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

前言

了解WebRTC

部署SRS服務(wù)器

下載源碼并運(yùn)行

可能遇到的問題

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

播放卡住的問題

參考資料


前言

最近項(xiàng)目用到WebRTC來(lái)替換RTMP推流功能,原因是RTMP延遲太高不滿足需求。本文意在記錄學(xué)習(xí)WebRTC的遇到的相關(guān)問題和解決方案,幫助大家避坑。

避免浪費(fèi)大家時(shí)間,先上效果圖:

第一張圖是Android App使用WebRTC推流,源碼地址:https://github.com/xiangang/WebRTCTest

以上源碼fork自傳說(shuō)中的二貨的《Android使用webrtc僅拉流并且播放》的demo:

??????https://github.com/Henry-6/WebRTCTest,筆者修改了對(duì)SRS服務(wù)器V4.0版本的支持,并修改了create session : create session : add publisher : publish negotiate : no found valid H.264 payload type的問題。該問題的解決方案來(lái)自于冬季穿短褲的《基于SRS服務(wù)器實(shí)現(xiàn)Android-Web端視頻通話(3):Android端向SRS服務(wù)器推送WebRTC流》。感謝兩位大佬!

第二張圖是SRS服務(wù)器提供的WebRTC Player的播放效果(gif太大,只能截圖看效果)。

了解WebRTC

就不多說(shuō)了,如果對(duì)WebRTC技術(shù)感興趣,建議閱讀李超的《WebRTC音視頻實(shí)時(shí)互動(dòng)技術(shù)--原理、實(shí)現(xiàn)與源碼分析》。

在微信讀書中是免費(fèi)的閱讀,大大的良心!

部署SRS服務(wù)器

SRS是一個(gè)簡(jiǎn)單高效的實(shí)時(shí)視頻服務(wù)器,支持RTMP/WebRTC/HLS/HTTP-FLV/SRT/GB28181。

官方文檔在這:https://github.com/ossrs/srs/wiki/v4_CN_Home,部署SRS服務(wù)器很簡(jiǎn)單,按照官方WIKI來(lái)就好了。也可以參考winlinvip大佬本人寫的《用SRS快速搭建WebRTC推流和播放》,筆者也不贅述了。

下載源碼并運(yùn)行

源碼在這:https://github.com/xiangang/WebRTCTest,克隆后導(dǎo)入AndroidStudio,建議在MainActivity類中直接修改SRS服務(wù)器的IP:

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

筆者比較建議使用Linux系統(tǒng)來(lái)部署SRS服務(wù)器,運(yùn)行推流Demo App的手機(jī)連接到和SRS服務(wù)器所在的同一個(gè)局域網(wǎng)內(nèi)。如果沒有可用的Linux系統(tǒng),建議使用Docker來(lái)部署。

App Demo安裝后點(diǎn)Push按鈕即可推流。Play按鈕則用于拉流播放。

另外,SRS官方也提供了Flutter的流媒體直播App:https://github.com/ossrs/flutter_live

感興趣的小伙伴可以自行下載運(yùn)行體驗(yàn),需要注意的是,如果你運(yùn)行遇到以下問題:

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路徑來(lái)解決。

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

注意,路徑不要搞錯(cuò)了。

可能遇到的問題

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

此問題的解決方案看冬季穿短褲的《基于SRS服務(wù)器實(shí)現(xiàn)Android-Web端視頻通話(3):Android端向SRS服務(wù)器推送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服務(wù)器的一個(gè)bug,建議換個(gè)流地址或重啟SRS服務(wù)器。或參考:

WebRTC: 能請(qǐng)求到接口返回正常,使用谷歌瀏覽器和srs直播APP都不能播放,一直在轉(zhuǎn)圈圈

排查SRS問題記錄一(RTMP推流,RTC播放卡住)

srs分發(fā)webrtc失敗,流無(wú)法播放 #1727

播放卡住的問題

筆者實(shí)際測(cè)試發(fā)現(xiàn),即便在局域網(wǎng)內(nèi),拉流播放也會(huì)出現(xiàn)卡死的問題。具體原因,筆者暫未查明,后續(xù)如有解決方案會(huì)另外寫一篇文章闡述。

如若遇到其它問題,歡迎留言溝通。

參考資料

macaruina的Android接入SRS WebRtc直播流

傳說(shuō)中的二貨的《Android使用webrtc僅拉流并且播放》

冬季穿短褲的Android端WebRTC啟用H264編碼-sdp中無(wú)H264信息

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

感謝以上博主開源奉獻(xiàn)的無(wú)私精神!

寫在最后,首先非常感謝您耐心閱讀完整篇文章,堅(jiān)持寫原創(chuàng)且基于實(shí)戰(zhàn)的文章不是件容易的事,如果本文剛好對(duì)您有點(diǎn)幫助,歡迎您給文章點(diǎn)贊評(píng)論,您的鼓勵(lì)是筆者堅(jiān)持不懈的動(dòng)力。若文章有不對(duì)之處也歡迎指正,再次感謝。

總結(jié)

以上是生活随笔為你收集整理的Android 音视频开发实践系列-04-Android WebRTC推流到SRS服务器实现直播功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。