main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package main
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "net"
  6. "path"
  7. "runtime"
  8. "time"
  9. "github.com/gwuhaolin/livego/configure"
  10. "github.com/gwuhaolin/livego/protocol/api"
  11. "github.com/gwuhaolin/livego/protocol/hls"
  12. "github.com/gwuhaolin/livego/protocol/httpflv"
  13. "github.com/gwuhaolin/livego/protocol/rtmp"
  14. log "github.com/sirupsen/logrus"
  15. )
  16. var VERSION = "master"
  17. func startHls() *hls.Server {
  18. hlsAddr := configure.Config.GetString("hls_addr")
  19. hlsListen, err := net.Listen("tcp", hlsAddr)
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. hlsServer := hls.NewServer()
  24. go func() {
  25. defer func() {
  26. if r := recover(); r != nil {
  27. log.Error("HLS server panic: ", r)
  28. }
  29. }()
  30. log.Info("HLS listen On ", hlsAddr)
  31. hlsServer.Serve(hlsListen)
  32. }()
  33. return hlsServer
  34. }
  35. func startRtmp(stream *rtmp.RtmpStream, hlsServer *hls.Server) {
  36. rtmpAddr := configure.Config.GetString("rtmp_addr")
  37. isRtmps := configure.Config.GetBool("enable_rtmps")
  38. var rtmpListen net.Listener
  39. if isRtmps {
  40. certPath := configure.Config.GetString("rtmps_cert")
  41. keyPath := configure.Config.GetString("rtmps_key")
  42. cert, err := tls.LoadX509KeyPair(certPath, keyPath)
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. rtmpListen, err = tls.Listen("tcp", rtmpAddr, &tls.Config{
  47. Certificates: []tls.Certificate{cert},
  48. })
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. } else {
  53. var err error
  54. rtmpListen, err = net.Listen("tcp", rtmpAddr)
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. }
  59. var rtmpServer *rtmp.Server
  60. if hlsServer == nil {
  61. rtmpServer = rtmp.NewRtmpServer(stream, nil)
  62. log.Info("HLS server disable....")
  63. } else {
  64. rtmpServer = rtmp.NewRtmpServer(stream, hlsServer)
  65. log.Info("HLS server enable....")
  66. }
  67. defer func() {
  68. if r := recover(); r != nil {
  69. log.Error("RTMP server panic: ", r)
  70. }
  71. }()
  72. if isRtmps {
  73. log.Info("RTMPS Listen On ", rtmpAddr)
  74. } else {
  75. log.Info("RTMP Listen On ", rtmpAddr)
  76. }
  77. rtmpServer.Serve(rtmpListen)
  78. }
  79. func startHTTPFlv(stream *rtmp.RtmpStream) {
  80. httpflvAddr := configure.Config.GetString("httpflv_addr")
  81. flvListen, err := net.Listen("tcp", httpflvAddr)
  82. if err != nil {
  83. log.Fatal(err)
  84. }
  85. hdlServer := httpflv.NewServer(stream)
  86. go func() {
  87. defer func() {
  88. if r := recover(); r != nil {
  89. log.Error("HTTP-FLV server panic: ", r)
  90. }
  91. }()
  92. log.Info("HTTP-FLV listen On ", httpflvAddr)
  93. hdlServer.Serve(flvListen)
  94. }()
  95. }
  96. func startAPI(stream *rtmp.RtmpStream) {
  97. apiAddr := configure.Config.GetString("api_addr")
  98. rtmpAddr := configure.Config.GetString("rtmp_addr")
  99. if apiAddr != "" {
  100. opListen, err := net.Listen("tcp", apiAddr)
  101. if err != nil {
  102. log.Fatal(err)
  103. }
  104. opServer := api.NewServer(stream, rtmpAddr)
  105. go func() {
  106. defer func() {
  107. if r := recover(); r != nil {
  108. log.Error("HTTP-API server panic: ", r)
  109. }
  110. }()
  111. log.Info("HTTP-API listen On ", apiAddr)
  112. opServer.Serve(opListen)
  113. }()
  114. }
  115. }
  116. func init() {
  117. log.SetFormatter(&log.TextFormatter{
  118. FullTimestamp: true,
  119. CallerPrettyfier: func(f *runtime.Frame) (string, string) {
  120. filename := path.Base(f.File)
  121. return fmt.Sprintf("%s()", f.Function), fmt.Sprintf(" %s:%d", filename, f.Line)
  122. },
  123. })
  124. }
  125. func main() {
  126. defer func() {
  127. if r := recover(); r != nil {
  128. log.Error("livego panic: ", r)
  129. time.Sleep(1 * time.Second)
  130. }
  131. }()
  132. log.Infof(`
  133. _ _ ____
  134. | | (_)_ _____ / ___| ___
  135. | | | \ \ / / _ \ | _ / _ \
  136. | |___| |\ V / __/ |_| | (_) |
  137. |_____|_| \_/ \___|\____|\___/
  138. version: %s
  139. `, VERSION)
  140. apps := configure.Applications{}
  141. configure.Config.UnmarshalKey("server", &apps)
  142. for _, app := range apps {
  143. stream := rtmp.NewRtmpStream()
  144. var hlsServer *hls.Server
  145. if app.Hls {
  146. hlsServer = startHls()
  147. }
  148. if app.Flv {
  149. startHTTPFlv(stream)
  150. }
  151. if app.Api {
  152. startAPI(stream)
  153. }
  154. startRtmp(stream, hlsServer)
  155. }
  156. }