janus.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 janus
  22. import (
  23. "context"
  24. "flag"
  25. "fmt"
  26. "github.com/ossrs/go-oryx-lib/errors"
  27. "github.com/ossrs/go-oryx-lib/logger"
  28. "os"
  29. "strings"
  30. "sync"
  31. "time"
  32. )
  33. var sr string
  34. var pli int
  35. var pr, sourceAudio, sourceVideo string
  36. var fps int
  37. var audioLevel, videoTWCC bool
  38. var clients, streams, delay int
  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.StringVar(&sr, "sr", "", "")
  44. fl.IntVar(&pli, "pli", 10, "")
  45. fl.StringVar(&pr, "pr", "", "")
  46. fl.StringVar(&sourceAudio, "sa", "", "")
  47. fl.StringVar(&sourceVideo, "sv", "", "")
  48. fl.IntVar(&fps, "fps", 0, "")
  49. fl.BoolVar(&audioLevel, "al", true, "")
  50. fl.BoolVar(&videoTWCC, "twcc", true, "")
  51. fl.IntVar(&clients, "nn", 1, "")
  52. fl.IntVar(&streams, "sn", 1, "")
  53. fl.IntVar(&delay, "delay", 50, "")
  54. fl.Usage = func() {
  55. fmt.Println(fmt.Sprintf("Usage: %v [Options]", os.Args[0]))
  56. fmt.Println(fmt.Sprintf("Options:"))
  57. fmt.Println(fmt.Sprintf(" -sfu The target server that can be rtc, live, janus, or gb28181. Default: rtc"))
  58. fmt.Println(fmt.Sprintf(" rtc/srs: SRS WebRTC SFU server, for WebRTC/WHIP/WHEP."))
  59. fmt.Println(fmt.Sprintf(" live: SRS live streaming server, for RTMP/HTTP-FLV/HLS."))
  60. fmt.Println(fmt.Sprintf(" janus: Janus WebRTC SFU server, for janus private protocol."))
  61. fmt.Println(fmt.Sprintf(" gb28181: GB media server, for GB protocol."))
  62. fmt.Println(fmt.Sprintf(" -nn The number of clients to simulate. Default: 1"))
  63. fmt.Println(fmt.Sprintf(" -sn The number of streams to simulate. Variable: %%d. Default: 1"))
  64. fmt.Println(fmt.Sprintf(" -delay The start delay in ms for each client or stream to simulate. Default: 50"))
  65. fmt.Println(fmt.Sprintf(" -al [Optional] Whether enable audio-level. Default: true"))
  66. fmt.Println(fmt.Sprintf(" -twcc [Optional] Whether enable vdieo-twcc. Default: true"))
  67. fmt.Println(fmt.Sprintf("Player or Subscriber:"))
  68. fmt.Println(fmt.Sprintf(" -sr The url to play/subscribe. If sn exceed 1, auto append variable %%d."))
  69. fmt.Println(fmt.Sprintf(" -pli [Optional] PLI request interval in seconds. Default: 10"))
  70. fmt.Println(fmt.Sprintf("Publisher:"))
  71. fmt.Println(fmt.Sprintf(" -pr The url to publish. If sn exceed 1, auto append variable %%d."))
  72. fmt.Println(fmt.Sprintf(" -fps [Optional] The fps of .h264 source file."))
  73. fmt.Println(fmt.Sprintf(" -sa [Optional] The file path to read audio, ignore if empty."))
  74. fmt.Println(fmt.Sprintf(" -sv [Optional] The file path to read video, ignore if empty."))
  75. fmt.Println(fmt.Sprintf("\n例如,1个播放,1个推流:"))
  76. fmt.Println(fmt.Sprintf(" %v -sfu janus -sr webrtc://localhost:8080/2345/livestream", os.Args[0]))
  77. fmt.Println(fmt.Sprintf(" %v -sfu janus -pr webrtc://localhost:8080/2345/livestream -sa avatar.ogg -sv avatar.h264 -fps 25", os.Args[0]))
  78. fmt.Println(fmt.Sprintf("\n例如,1个流,3个播放,共3个客户端:"))
  79. fmt.Println(fmt.Sprintf(" %v -sfu janus -sr webrtc://localhost:8080/2345/livestream -nn 3", os.Args[0]))
  80. fmt.Println(fmt.Sprintf(" %v -sfu janus -pr webrtc://localhost:8080/2345/livestream -sa avatar.ogg -sv avatar.h264 -fps 25", os.Args[0]))
  81. fmt.Println(fmt.Sprintf("\n例如,2个流,每个流3个播放,共6个客户端:"))
  82. fmt.Println(fmt.Sprintf(" %v -sfu janus -sr webrtc://localhost:8080/2345/livestream_%%d -sn 2 -nn 3", os.Args[0]))
  83. fmt.Println(fmt.Sprintf(" %v -sfu janus -pr webrtc://localhost:8080/2345/livestream_%%d -sn 2 -sa avatar.ogg -sv avatar.h264 -fps 25", os.Args[0]))
  84. fmt.Println(fmt.Sprintf("\n例如,2个推流:"))
  85. fmt.Println(fmt.Sprintf(" %v -sfu janus -pr webrtc://localhost:8080/2345/livestream_%%d -sn 2 -sa avatar.ogg -sv avatar.h264 -fps 25", os.Args[0]))
  86. }
  87. if err := fl.Parse(os.Args[1:]); err == flag.ErrHelp {
  88. os.Exit(0)
  89. }
  90. showHelp := (clients <= 0 || streams <= 0)
  91. if sr == "" && pr == "" {
  92. showHelp = true
  93. }
  94. if pr != "" && (sourceAudio == "" && sourceVideo == "") {
  95. showHelp = true
  96. }
  97. if showHelp {
  98. fl.Usage()
  99. os.Exit(-1)
  100. }
  101. summaryDesc := fmt.Sprintf("delay=%v, al=%v, twcc=%v", delay, audioLevel, videoTWCC)
  102. if sr != "" {
  103. summaryDesc = fmt.Sprintf("%v, play(url=%v, pli=%v)", summaryDesc, sr, pli)
  104. }
  105. if pr != "" {
  106. summaryDesc = fmt.Sprintf("%v, publish(url=%v, sa=%v, sv=%v, fps=%v)",
  107. summaryDesc, pr, sourceAudio, sourceVideo, fps)
  108. }
  109. logger.Tf(ctx, "Run benchmark with %v", summaryDesc)
  110. checkFlags := func() error {
  111. if sourceVideo != "" && !strings.HasSuffix(sourceVideo, ".h264") {
  112. return errors.Errorf("Should be .264, actual %v", sourceVideo)
  113. }
  114. if sourceVideo != "" && strings.HasSuffix(sourceVideo, ".h264") && fps <= 0 {
  115. return errors.Errorf("Video fps should >0, actual %v", fps)
  116. }
  117. return nil
  118. }
  119. if err := checkFlags(); err != nil {
  120. logger.Ef(ctx, "Check faile err %+v", err)
  121. os.Exit(-1)
  122. }
  123. }
  124. func Run(ctx context.Context) error {
  125. // Run tasks.
  126. var wg sync.WaitGroup
  127. // Run all subscribers or players.
  128. for i := 0; sr != "" && i < streams && ctx.Err() == nil; i++ {
  129. r_auto := sr
  130. if streams > 1 && !strings.Contains(r_auto, "%") {
  131. r_auto += "%d"
  132. }
  133. r2 := r_auto
  134. if strings.Contains(r2, "%") {
  135. r2 = fmt.Sprintf(r2, i)
  136. }
  137. for j := 0; sr != "" && j < clients && ctx.Err() == nil; j++ {
  138. wg.Add(1)
  139. go func(sr string) {
  140. defer wg.Done()
  141. if err := startPlay(ctx, sr, audioLevel, videoTWCC, pli); err != nil {
  142. if errors.Cause(err) != context.Canceled {
  143. logger.Wf(ctx, "Run err %+v", err)
  144. }
  145. }
  146. }(r2)
  147. time.Sleep(time.Duration(delay) * time.Millisecond)
  148. }
  149. }
  150. // Run all publishers.
  151. for i := 0; pr != "" && i < streams && ctx.Err() == nil; i++ {
  152. r_auto := pr
  153. if streams > 1 && !strings.Contains(r_auto, "%") {
  154. r_auto += "%d"
  155. }
  156. r2 := r_auto
  157. if strings.Contains(r2, "%") {
  158. r2 = fmt.Sprintf(r2, i)
  159. }
  160. wg.Add(1)
  161. go func(pr string) {
  162. defer wg.Done()
  163. if err := startPublish(ctx, pr, sourceAudio, sourceVideo, fps, audioLevel, videoTWCC); err != nil {
  164. if errors.Cause(err) != context.Canceled {
  165. logger.Wf(ctx, "Run err %+v", err)
  166. }
  167. }
  168. }(r2)
  169. time.Sleep(time.Duration(delay) * time.Millisecond)
  170. }
  171. wg.Wait()
  172. return nil
  173. }