gb28181.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // The MIT License (MIT)
  2. //
  3. // # Copyright (c) 2022-2024 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 gb28181
  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. "io"
  29. "os"
  30. "strings"
  31. "time"
  32. )
  33. type gbMainConfig struct {
  34. sipConfig SIPConfig
  35. psConfig PSConfig
  36. }
  37. func Parse(ctx context.Context) interface{} {
  38. fl := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
  39. var sfu string
  40. fl.StringVar(&sfu, "sfu", "srs", "The SFU server, srs or gb28181 or janus")
  41. c := &gbMainConfig{}
  42. fl.StringVar(&c.sipConfig.addr, "pr", "", "")
  43. fl.StringVar(&c.sipConfig.user, "user", "", "")
  44. fl.StringVar(&c.sipConfig.server, "server", "", "")
  45. fl.StringVar(&c.sipConfig.domain, "domain", "", "")
  46. fl.IntVar(&c.sipConfig.random, "random", 0, "")
  47. fl.StringVar(&c.psConfig.video, "sv", "", "")
  48. fl.StringVar(&c.psConfig.audio, "sa", "", "")
  49. fl.IntVar(&c.psConfig.fps, "fps", 0, "")
  50. fl.Usage = func() {
  51. fmt.Println(fmt.Sprintf("Usage: %v [Options]", os.Args[0]))
  52. fmt.Println(fmt.Sprintf("Options:"))
  53. fmt.Println(fmt.Sprintf(" -sfu The target server that can be rtc, live, janus, or gb28181. Default: rtc"))
  54. fmt.Println(fmt.Sprintf(" rtc/srs: SRS WebRTC SFU server, for WebRTC/WHIP/WHEP."))
  55. fmt.Println(fmt.Sprintf(" live: SRS live streaming server, for RTMP/HTTP-FLV/HLS."))
  56. fmt.Println(fmt.Sprintf(" janus: Janus WebRTC SFU server, for janus private protocol."))
  57. fmt.Println(fmt.Sprintf(" gb28181: GB media server, for GB protocol."))
  58. fmt.Println(fmt.Sprintf("SIP:"))
  59. fmt.Println(fmt.Sprintf(" -user The SIP username, ID of device."))
  60. fmt.Println(fmt.Sprintf(" -random Append N number to user as random device ID, like 1320000001."))
  61. fmt.Println(fmt.Sprintf(" -server The SIP server ID, ID of server."))
  62. fmt.Println(fmt.Sprintf(" -domain The SIP domain, domain of server and device."))
  63. fmt.Println(fmt.Sprintf("Publisher:"))
  64. fmt.Println(fmt.Sprintf(" -pr The SIP server address, format is tcp://ip:port over TCP."))
  65. fmt.Println(fmt.Sprintf(" -fps [Optional] The fps of .h264 source file."))
  66. fmt.Println(fmt.Sprintf(" -sa [Optional] The file path to read audio, ignore if empty."))
  67. fmt.Println(fmt.Sprintf(" -sv [Optional] The file path to read video, ignore if empty."))
  68. fmt.Println(fmt.Sprintf("\n例如,1个推流:"))
  69. fmt.Println(fmt.Sprintf(" %v -sfu gb28181 -pr tcp://127.0.0.1:5060 -user 34020000001320000001 -server 34020000002000000001 -domain 3402000000", os.Args[0]))
  70. fmt.Println(fmt.Sprintf(" %v -sfu gb28181 -pr tcp://127.0.0.1:5060 -user 3402000000 -random 10 -server 34020000002000000001 -domain 3402000000", os.Args[0]))
  71. fmt.Println(fmt.Sprintf(" %v -sfu gb28181 -pr tcp://127.0.0.1:5060 -user 3402000000 -random 10 -server 34020000002000000001 -domain 3402000000 -sa avatar.aac -sv avatar.h264 -fps 25", os.Args[0]))
  72. fmt.Println(fmt.Sprintf(" %v -sfu gb28181 -pr tcp://127.0.0.1:5060 -user livestream -server srs -domain ossrs.io -sa avatar.aac -sv avatar.h264 -fps 25", os.Args[0]))
  73. fmt.Println()
  74. }
  75. if err := fl.Parse(os.Args[1:]); err == flag.ErrHelp {
  76. os.Exit(0)
  77. }
  78. showHelp := c.sipConfig.String() == ""
  79. if showHelp {
  80. fl.Usage()
  81. os.Exit(-1)
  82. }
  83. summaryDesc := ""
  84. if c.sipConfig.addr != "" {
  85. pubString := strings.Join([]string{c.sipConfig.String(), c.psConfig.String()}, ",")
  86. summaryDesc = fmt.Sprintf("%v, publish(%v)", summaryDesc, pubString)
  87. }
  88. logger.Tf(ctx, "Run benchmark with %v", summaryDesc)
  89. return c
  90. }
  91. func Run(ctx context.Context, r0 interface{}) (err error) {
  92. conf := r0.(*gbMainConfig)
  93. ctx, cancel := context.WithCancel(ctx)
  94. session := NewGBSession(&GBSessionConfig{
  95. regTimeout: 3 * time.Hour, inviteTimeout: 3 * time.Hour,
  96. }, &conf.sipConfig)
  97. defer session.Close()
  98. if err := session.Connect(ctx); err != nil {
  99. return errors.Wrapf(err, "connect %v", conf.sipConfig)
  100. }
  101. if err := session.Register(ctx); err != nil {
  102. return errors.Wrapf(err, "register %v", conf.sipConfig)
  103. }
  104. if err := session.Invite(ctx); err != nil {
  105. return errors.Wrapf(err, "invite %v", conf.sipConfig)
  106. }
  107. if conf.psConfig.video == "" || conf.psConfig.audio == "" {
  108. cancel()
  109. return nil
  110. }
  111. ingester := NewPSIngester(&IngesterConfig{
  112. psConfig: conf.psConfig,
  113. ssrc: uint32(session.out.ssrc),
  114. clockRate: session.out.clockRate,
  115. payloadType: uint8(session.out.payloadType),
  116. })
  117. defer ingester.Close()
  118. if ingester.conf.serverAddr, err = utilBuildMediaAddr(session.sip.conf.addr, session.out.mediaPort); err != nil {
  119. return err
  120. }
  121. if err := ingester.Ingest(ctx); err != nil {
  122. if errors.Cause(err) == io.EOF {
  123. logger.Tf(ctx, "EOF, video=%v, audio=%v", conf.psConfig.video, conf.psConfig.audio)
  124. return nil
  125. }
  126. return errors.Wrap(err, "ingest")
  127. }
  128. return nil
  129. }