type.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package test
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "sync"
  6. "testing"
  7. "time"
  8. "go.uber.org/zap"
  9. . "m7s.live/engine/v4"
  10. "m7s.live/engine/v4/config"
  11. "m7s.live/engine/v4/track"
  12. "m7s.live/engine/v4/util"
  13. )
  14. var conf UnitTestConfig
  15. var unitTestPlugin = InstallPlugin(&conf)
  16. var spsppsbase64 = "AAABJ2QANKwTGqAoALWgHixLcAAAAAEozgVywA=="
  17. var spspps, _ = base64.RawStdEncoding.DecodeString(spsppsbase64)
  18. var EngineChan = make(chan int, 10)
  19. var WaitEngine sync.WaitGroup
  20. func UseEngine() {
  21. EngineChan <- 1
  22. WaitEngine.Wait()
  23. }
  24. func FreeEngine() {
  25. EngineChan <- -1
  26. }
  27. func init() {
  28. WaitEngine.Add(1)
  29. go func() {
  30. var use = 0
  31. bg := context.Background()
  32. var ctx context.Context
  33. var cancel context.CancelFunc
  34. for {
  35. select {
  36. case delta := <-EngineChan:
  37. use += delta
  38. switch use {
  39. case 1:
  40. ctx, cancel = context.WithTimeout(bg, time.Second*20)
  41. go Run(ctx, "config.yaml")
  42. case 0:
  43. cancel()
  44. WaitEngine.Add(1)
  45. }
  46. }
  47. }
  48. }()
  49. }
  50. type UnitTestConfig struct {
  51. config.Subscribe
  52. config.Publish
  53. }
  54. func (t *UnitTestConfig) OnEvent(event any) {
  55. switch event.(type) {
  56. case FirstConfig:
  57. WaitEngine.Done()
  58. }
  59. }
  60. type UnitTestPublisher struct {
  61. tb testing.TB
  62. Publisher
  63. }
  64. type UnitTestSubsciber struct {
  65. tb testing.TB
  66. Subscriber
  67. }
  68. func (s *UnitTestSubsciber) OnEvent(event any) {
  69. switch v := event.(type) {
  70. case AudioFrame:
  71. case VideoFrame:
  72. b := v.AUList.ToBytes()
  73. seq := (uint16(b[1]) << 8) | uint16(b[2])
  74. // s.Trace("sequence", zap.Uint32("sequence", v.Sequence), zap.Uint16("seq", seq), zap.Int("len", len(b)))
  75. if v.Sequence != uint32(seq) {
  76. s.tb.Fatal("sequence error", v.Sequence, seq)
  77. }
  78. default:
  79. s.Subscriber.OnEvent(event)
  80. }
  81. }
  82. func (pub *UnitTestPublisher) OnEvent(event any) {
  83. switch event.(type) {
  84. case IPublisher:
  85. pub.VideoTrack = track.NewH264(pub.Stream)
  86. pub.VideoTrack.WriteAnnexB(0, 0, spspps)
  87. pub.AudioTrack = track.NewAAC(pub.Stream)
  88. go pub.WriteAudio()
  89. go pub.WriteVideo()
  90. }
  91. }
  92. func (pub *UnitTestPublisher) WriteAudio() {
  93. for i := uint32(0); pub.Err() == nil; i++ {
  94. time.Sleep(40 * time.Millisecond)
  95. elapse := time.Since(pub.StartTime)
  96. pts := uint32(elapse.Milliseconds() * 90)
  97. pub.AudioTrack.WriteADTS(pts, util.Buffer([]byte{0xFF, 0xE1, 0x20, 0x00, 0x29, 0xA7, 0xF0, byte(i >> 8), byte(i), 0}))
  98. }
  99. }
  100. func (pub *UnitTestPublisher) WriteVideo() {
  101. for i := uint32(0); pub.Err() == nil; i++ {
  102. time.Sleep(40 * time.Millisecond)
  103. elapse := time.Since(pub.StartTime)
  104. pts := uint32(elapse.Milliseconds() * 90)
  105. var naluType byte = 0x61
  106. if elapse%8 == 0 {
  107. naluType = 0x65
  108. }
  109. data := []byte{naluType, byte(i >> 8), byte(i)}
  110. pub.Trace("data", zap.Uint32("i", i))
  111. pub.VideoTrack.WriteNalu(pts, pts, data)
  112. }
  113. }