1 Star 0 Fork 7

学习java/livego

forked from 暴走的蜗牛/livego 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
livego.go 2.94 KB
一键复制 编辑 原始数据 按行查看 历史
xiaoq_bj 提交于 2017-07-05 18:52 . 新增功能
package main
import (
"flag"
"github.com/gwuhaolin/livego/configure"
"github.com/gwuhaolin/livego/protocol/hls"
"github.com/gwuhaolin/livego/protocol/httpflv"
"github.com/gwuhaolin/livego/protocol/httpopera"
"github.com/gwuhaolin/livego/protocol/rtmp"
"log"
"net"
"time"
)
var (
version = "master"
rtmpAddr = flag.String("rtmp-addr", ":1935", "RTMP server listen address")
httpFlvAddr = flag.String("httpflv-addr", ":7001", "HTTP-FLV server listen address")
hlsAddr = flag.String("hls-addr", ":7002", "HLS server listen address")
operaAddr = flag.String("manage-addr", ":8090", "HTTP manage interface server listen address")
configfilename = flag.String("cfgfile", "livego.cfg", "live configure filename")
)
func init() {
log.SetFlags(log.Lshortfile | log.Ltime | log.Ldate)
flag.Parse()
}
func startHls() *hls.Server {
hlsListen, err := net.Listen("tcp", *hlsAddr)
if err != nil {
log.Fatal(err)
}
hlsServer := hls.NewServer()
go func() {
defer func() {
if r := recover(); r != nil {
log.Println("HLS server panic: ", r)
}
}()
log.Println("HLS listen On", *hlsAddr)
hlsServer.Serve(hlsListen)
}()
return hlsServer
}
func startRtmp(stream *rtmp.RtmpStream, hlsServer *hls.Server) {
rtmpListen, err := net.Listen("tcp", *rtmpAddr)
if err != nil {
log.Fatal(err)
}
var rtmpServer *rtmp.Server
if hlsServer == nil {
rtmpServer = rtmp.NewRtmpServer(stream, nil)
log.Printf("hls server disable....")
} else {
rtmpServer = rtmp.NewRtmpServer(stream, hlsServer)
log.Printf("hls server enable....")
}
defer func() {
if r := recover(); r != nil {
log.Println("RTMP server panic: ", r)
}
}()
log.Println("RTMP Listen On", *rtmpAddr)
rtmpServer.Serve(rtmpListen)
}
func startHTTPFlv(stream *rtmp.RtmpStream) {
flvListen, err := net.Listen("tcp", *httpFlvAddr)
if err != nil {
log.Fatal(err)
}
hdlServer := httpflv.NewServer(stream)
go func() {
defer func() {
if r := recover(); r != nil {
log.Println("HTTP-FLV server panic: ", r)
}
}()
log.Println("HTTP-FLV listen On", *httpFlvAddr)
hdlServer.Serve(flvListen)
}()
}
func startHTTPOpera(stream *rtmp.RtmpStream) {
if *operaAddr != "" {
opListen, err := net.Listen("tcp", *operaAddr)
if err != nil {
log.Fatal(err)
}
opServer := httpopera.NewServer(stream, *rtmpAddr)
go func() {
defer func() {
if r := recover(); r != nil {
log.Println("HTTP-Operation server panic: ", r)
}
}()
log.Println("HTTP-Operation listen On", *operaAddr)
opServer.Serve(opListen)
}()
}
}
func main() {
defer func() {
if r := recover(); r != nil {
log.Println("livego panic: ", r)
time.Sleep(1 * time.Second)
}
}()
log.Println("start livego, version", version)
err := configure.LoadConfig(*configfilename)
if err != nil {
return
}
stream := rtmp.NewRtmpStream()
hlsServer := startHls()
startHTTPFlv(stream)
startHTTPOpera(stream)
startRtmp(stream, hlsServer)
//startRtmp(stream, nil)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mmpdaye/livego.git
git@gitee.com:mmpdaye/livego.git
mmpdaye
livego
livego
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385