live.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // The MIT License (MIT)
  2. //
  3. // # Copyright (c) 2021 Winlin
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files (the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. // the Software, and to permit persons to whom the Software is furnished to do so,
  10. // subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package live
  22. import (
  23. "context"
  24. "flag"
  25. "fmt"
  26. "net"
  27. "net/http"
  28. "os"
  29. "strings"
  30. "sync"
  31. "time"
  32. "github.com/ossrs/go-oryx-lib/errors"
  33. "github.com/ossrs/go-oryx-lib/logger"
  34. )
  35. var closeAfterPublished bool
  36. var pr string
  37. var streams, delay int
  38. var statListen string
  39. func Parse(ctx context.Context) {
  40. fl := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
  41. var sfu string
  42. fl.StringVar(&sfu, "sfu", "srs", "The SFU server, srs or gb28181 or janus")
  43. fl.BoolVar(&closeAfterPublished, "cap", false, "")
  44. fl.StringVar(&pr, "pr", "", "")
  45. fl.IntVar(&streams, "sn", 1, "")
  46. fl.IntVar(&delay, "delay", 10, "")
  47. fl.StringVar(&statListen, "stat", "", "")
  48. fl.Usage = func() {
  49. fmt.Println(fmt.Sprintf("Usage: %v [Options]", os.Args[0]))
  50. fmt.Println(fmt.Sprintf("Options:"))
  51. fmt.Println(fmt.Sprintf(" -sfu The target server that can be rtc, live, janus, or gb28181. Default: rtc"))
  52. fmt.Println(fmt.Sprintf(" rtc/srs: SRS WebRTC SFU server, for WebRTC/WHIP/WHEP."))
  53. fmt.Println(fmt.Sprintf(" live: SRS live streaming server, for RTMP/HTTP-FLV/HLS."))
  54. fmt.Println(fmt.Sprintf(" janus: Janus WebRTC SFU server, for janus private protocol."))
  55. fmt.Println(fmt.Sprintf(" -sn The number of streams to simulate. Variable: %%d. Default: 1"))
  56. fmt.Println(fmt.Sprintf(" -delay The start delay in ms for each client or stream to simulate. Default: 50"))
  57. fmt.Println(fmt.Sprintf(" -stat [Optional] The stat server API listen port."))
  58. fmt.Println(fmt.Sprintf("Publisher:"))
  59. fmt.Println(fmt.Sprintf(" -pr The url to publish. If sn exceed 1, auto append variable %%d."))
  60. fmt.Println(fmt.Sprintf(" -cap Whether to close connection after publish. Default: false"))
  61. fmt.Println(fmt.Sprintf("\n例如,1个推流,无媒体传输:"))
  62. fmt.Println(fmt.Sprintf(" %v -pr=rtmp://localhost/live/livestream -cap=true", os.Args[0]))
  63. fmt.Println(fmt.Sprintf("\n例如,2个推流,无媒体传输:"))
  64. fmt.Println(fmt.Sprintf(" %v -pr=rtmp://localhost/live/livestream_%%d -sn=2 -cap=true", os.Args[0]))
  65. fmt.Println()
  66. }
  67. _ = fl.Parse(os.Args[1:])
  68. showHelp := streams <= 0
  69. if pr == "" {
  70. showHelp = true
  71. }
  72. if showHelp {
  73. fl.Usage()
  74. os.Exit(-1)
  75. }
  76. if statListen != "" && !strings.Contains(statListen, ":") {
  77. statListen = ":" + statListen
  78. }
  79. summaryDesc := fmt.Sprintf("streams=%v", streams)
  80. if pr != "" {
  81. summaryDesc = fmt.Sprintf("%v, publish=(url=%v,cap=%v)",
  82. summaryDesc, pr, closeAfterPublished)
  83. }
  84. logger.Tf(ctx, "Run benchmark with %v", summaryDesc)
  85. }
  86. func Run(ctx context.Context) error {
  87. ctx, cancel := context.WithCancel(ctx)
  88. defer cancel()
  89. // Run tasks.
  90. var wg sync.WaitGroup
  91. defer wg.Wait()
  92. // Run STAT API server.
  93. wg.Add(1)
  94. go func() {
  95. defer wg.Done()
  96. if statListen == "" {
  97. return
  98. }
  99. var lc net.ListenConfig
  100. ln, err := lc.Listen(ctx, "tcp", statListen)
  101. if err != nil {
  102. logger.Ef(ctx, "stat listen err+%v", err)
  103. cancel()
  104. return
  105. }
  106. mux := http.NewServeMux()
  107. handleStat(ctx, mux, statListen)
  108. srv := &http.Server{
  109. Handler: mux,
  110. BaseContext: func(listener net.Listener) context.Context {
  111. return ctx
  112. },
  113. }
  114. go func() {
  115. <-ctx.Done()
  116. srv.Shutdown(ctx)
  117. }()
  118. logger.Tf(ctx, "Stat listen at %v", statListen)
  119. if err := srv.Serve(ln); err != nil {
  120. if ctx.Err() == nil {
  121. logger.Ef(ctx, "stat serve err+%v", err)
  122. cancel()
  123. }
  124. return
  125. }
  126. }()
  127. // Run all publishers.
  128. publisherStartedCtx, publisherStartedCancel := context.WithCancel(ctx)
  129. defer publisherStartedCancel()
  130. for i := 0; pr != "" && i < streams && ctx.Err() == nil; i++ {
  131. r_auto := pr
  132. if streams > 1 && !strings.Contains(r_auto, "%") {
  133. r_auto += "%d"
  134. }
  135. r2 := r_auto
  136. if strings.Contains(r2, "%") {
  137. r2 = fmt.Sprintf(r2, i)
  138. }
  139. gStatLive.Publishers.Expect++
  140. gStatLive.Publishers.Alive++
  141. wg.Add(1)
  142. go func(pr string) {
  143. defer wg.Done()
  144. defer func() {
  145. gStatLive.Publishers.Alive--
  146. logger.Tf(ctx, "Publisher %v done, alive=%v", pr, gStatLive.Publishers.Alive)
  147. <- publisherStartedCtx.Done()
  148. if gStatLive.Publishers.Alive == 0 {
  149. cancel()
  150. }
  151. }()
  152. if err := startPublish(ctx, pr, closeAfterPublished); err != nil {
  153. if errors.Cause(err) != context.Canceled {
  154. logger.Wf(ctx, "Run err %+v", err)
  155. }
  156. }
  157. }(r2)
  158. if delay > 0 {
  159. time.Sleep(time.Duration(delay) * time.Millisecond)
  160. }
  161. }
  162. return nil
  163. }