实战Golang流媒体:探索开源项目lal

发表时间: 2023-09-07 07:04

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):
https://github.com/zq2599/blog_demos

关于《Golang流媒体实战》

  • 因为工作需要,开始了流媒体开发学习,于是打算选择一个Go版本的开源流媒体服务器作为学习方向
  • lal是个不错的开源项目:项目活跃、功能齐全、有详细的中文资料,因此,就选择了它
  • 仓库地址:https://github.com/q191201771/lal
  • 文档地址:https://pengrl.com/lal/#/
  • 《Golang流媒体实战》系列的目标:学习开源项目lal并编码实战,达到深入了解流媒体的目的

本篇概览

  • 作为《Golang流媒体实战》系列的开篇,本文不涉及编码,而是做一些基本操作,与大家一起了解lal,主要由以下内容组成
  1. 开发环境
  2. 下载源码
  3. 编译构建
  4. 启动lal
  5. 基础:推拉流
  6. 进阶:转推和录制

开发环境

  • 这里介绍一下我这边的开发环境供您参考
  1. 操作系统:macOS 13.2 (MacBook Por, M1 Por芯片)
  2. IDE:Visual Studio Code 1.67.2
  3. Go:1.19.3
  • 接下来开始实战,从下载源码开始

下载源码

  • 一行命令完成
git clone git@github.com:q191201771/lal.git

编译、构建、启动

  • 确保您的电脑上go已经装好,进入lal源码目录执行以下命令
sh build.sh
  • 编译完成后bin目录下会生成二进制文件,可以直接拿来用了,该目录下有很多,我们要用的是lalserver,其余的都是官方demo,暂时用不上
 lal git:(master) tree bin                                bin├── analyseflv├── benchrtmpconnect├── calcrtmpdelay├── customize_lalserver├── dispatch├── flvfile2es├── lalserver├── modflvfile├── pullhttpflv├── pullrtmp├── pullrtmp2hls├── pullrtmp2pushrtmp├── pullrtmp2pushrtsp├── pullrtsp├── pullrtsp2pushrtmp├── pullrtsp2pushrtsp└── pushrtmp0 directories, 17 files
  • 启动应用执行以下命令
./bin/lalserver -c conf/lalserver.conf.json
  • 控制台输出如下,与普通go项目无异
023/03/25 17:03:24.653855  INFO      start: 2023-03-25 17:03:24.645 - base.go:352023/03/25 17:03:24.654433  INFO         wd: /Users/will/temp/202303/25/lal - base.go:362023/03/25 17:03:24.654442  INFO       args: ./bin/lalserver -c conf/lalserver.conf.json - base.go:372023/03/25 17:03:24.654456  INFO    bininfo: GitTag=v0.33.0. GitCommitLog=a2db8795128e716a223404bd39aa215600d1f753 readme. GitStatus=cleanly. BuildTime=2023.03.25.162056. GoVersion=go version go1.19.3 darwin/arm64. runtime=darwin/arm64. - base.go:382023/03/25 17:03:24.654462  INFO    version: lal v0.33.0 (github.com/q191201771/lal) - base.go:392023/03/25 17:03:24.654467  INFO     github: https://github.com/q191201771/lal - base.go:402023/03/25 17:03:24.654472  INFO        doc: https://pengrl.com/lal - base.go:412023/03/25 17:03:24.655115  INFO start web pprof listen. addr=:8084 - server_manager__.go:1652023/03/25 17:03:24.655862  INFO add http listen for httpflv. addr=:8080, pattern=/ - server_manager__.go:1872023/03/25 17:03:24.659345  INFO add https listen for httpflv. addr=:4433, pattern=/ - server_manager__.go:1982023/03/25 17:03:24.659393  INFO add http listen for httpts. addr=:8080, pattern=/ - server_manager__.go:1872023/03/25 17:03:24.659401  INFO add https listen for httpts. addr=:4433, pattern=/ - server_manager__.go:1982023/03/25 17:03:24.659407  INFO add http listen for hls. addr=:8080, pattern=/hls/ - server_manager__.go:1872023/03/25 17:03:24.659413  INFO add https listen for hls. addr=:4433, pattern=/hls/ - server_manager__.go:1982023/03/25 17:03:24.660094  INFO start rtmp server listen. addr=:1935 - server.go:562023/03/25 17:03:24.661125  INFO start rtmps server listen. addr=:4935 - server.go:712023/03/25 17:03:24.661157  INFO start rtsp server listen. addr=:5544 - server.go:812023/03/25 17:03:24.661328  INFO start rtsps server listen. addr=:5322 - server.go:952023/03/25 17:03:24.661623  INFO start http-api server listen. addr=:8083 - http_api.go:48

推流

  • 接下来按照下图的模式操作,实现推拉流功能



  • 准备好一个mp4视频
  • 准备好FFmpeg,如果想编译成最新版本的,可以参考《Ubuntu20编译FFmpeg4(非常简单)》
  • 推流,ffmpeg文件和sample.mp4文件的位置请自行调整
./ffmpeg \-re \-stream_loop -1 \-i ../videos/sample.mp4 \-c copy \-f flv \'rtmp://127.0.0.1:1935/live/test110'
  • 推流成功时控制台持续输出类似一下内容
Output #0, flv, to 'rtmp://127.0.0.1:1935/live/test110':  Metadata:    major_brand     : isom    minor_version   : 512    compatible_brands: isomiso2avc1mp41    encoder         : Lavf60.3.100  Stream #0:0(und): Video: h264 (Main) ([7][0][0][0] / 0x0007), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 1086 kb/s, 25 fps, 25 tbr, 1k tbn (default)    Metadata:      creation_time   : 1970-01-01T00:00:00.000000Z      handler_name    : VideoHandler      vendor_id       : [0][0][0][0]  Stream #0:1(und): Audio: aac (LC) ([10][0][0][0] / 0x000A), 48000 Hz, 5.1, fltp, 383 kb/s (default)    Metadata:      creation_time   : 1970-01-01T00:00:00.000000Z      handler_name    : SoundHandler      vendor_id       : [0][0][0][0]Stream mapping:  Stream #0:0 -> #0:0 (copy)  Stream #0:1 -> #0:1 (copy)Press [q] to stop, [?] for helpframe=  342 fps= 25 q=-1.0 size=    2089kB time=00:00:13.69 bitrate=1249.5kbits/s speed=   1x
  • 于此同时,lal控制台也有日志输出,证明流已经到了lal
2023/03/25 17:22:17.394118  INFO accept a rtmp connection. remoteAddr=127.0.0.1:56148 - server.go:952023/03/25 17:22:17.394486 DEBUG [NAZACONN1] lifecycle new connection. net.Conn=0x140002c0008, naza.Connection=0x140002ce000 - connection.go:1922023/03/25 17:22:17.395248  INFO [RTMPPUBSUB1] lifecycle new rtmp ServerSession. session=0x1400029cd00, remote addr=127.0.0.1:56148 - server_session.go:1122023/03/25 17:22:17.395315 DEBUG handshake complex mode. - handshake.go:2482023/03/25 17:22:17.395684  INFO [RTMPPUBSUB1] < R Handshake C0+C1. - server_session.go:1962023/03/25 17:22:17.395699  INFO [RTMPPUBSUB1] > W Handshake S0+S1+S2. - server_session.go:1982023/03/25 17:22:17.395927  INFO [RTMPPUBSUB1] < R Handshake C2. - server_session.go:2062023/03/25 17:22:17.396586  INFO [RTMPPUBSUB1] < R connect('live'). tcUrl=rtmp://127.0.0.1:1935/live - server_session.go:4032023/03/25 17:22:17.397251  INFO [RTMPPUBSUB1] > W Window Acknowledgement Size 5000000. - server_session.go:4072023/03/25 17:22:17.397376  INFO [RTMPPUBSUB1] > W Set Peer Bandwidth. - server_session.go:4122023/03/25 17:22:17.397421  INFO [RTMPPUBSUB1] > W SetChunkSize 4096. - server_session.go:4172023/03/25 17:22:17.397465  INFO [RTMPPUBSUB1] > W _result('NetConnection.Connect.Success'). - server_session.go:4222023/03/25 17:22:17.397618 DEBUG [RTMPPUBSUB1] read command message, ignore it. cmd=releaseStream, header={Csid:3 MsgLen:36 MsgTypeId:20 MsgStreamId:0 TimestampAbs:0}, b=len(core)=128, rpos=25, wpos=36, hex=00000000  05 02 00 07 74 65 73 74  31 31 30                 |....test110| - server_session.go:3562023/03/25 17:22:17.397654 DEBUG [RTMPPUBSUB1] read command message, ignore it. cmd=FCPublish, header={Csid:3 MsgLen:32 MsgTypeId:20 MsgStreamId:0 TimestampAbs:0}, b=len(core)=128, rpos=21, wpos=32, hex=00000000  05 02 00 07 74 65 73 74  31 31 30                 |....test110| - server_session.go:356

拉流

  • 由于lal支持多种协议,因此拉流方式也有多种
  • 播放器我选择使用VLC,虽然ffplay也可以,但是VLC能更加方便的查看媒体流信息
  • 先试试rtmp协议的拉流,地址是
rtmp://127.0.0.1:1935/live/test110
  • 操作如下图,填入上述地址即可


  • 播放效果如下图左侧,使用快捷键cmd+i,可以打开右侧的信息窗口,看到媒体流的编码详情



  • 再试试http-flv、hls、rtsp、ts协议的,地址分别如下,都能顺利播放
http://127.0.0.1:8080/live/test110.flvhttp://127.0.0.1:8080/hls/test110.m3u8rtsp://localhost:5544/live/test110http://127.0.0.1:8080/live/test110.ts
  • 另外hls格式的流也能用Safari浏览器直接打开(不能用chrome,会变成下载m3u8文件)



  • 至此,《Golang流媒体实战》系列的开篇就算完成了,通过动手操作咱们算是对lal有了初步的了解,接下来的章节,咱们会逐渐深入,学习更多流媒体开发的知识点

欢迎关注头条号:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...