srt_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // The MIT License (MIT)
  2. //
  3. // # Copyright (c) 2023 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 blackbox
  22. import (
  23. "context"
  24. "fmt"
  25. "github.com/ossrs/go-oryx-lib/errors"
  26. "github.com/ossrs/go-oryx-lib/logger"
  27. "math/rand"
  28. "os"
  29. "path"
  30. "sync"
  31. "testing"
  32. "time"
  33. )
  34. func TestFast_SrtPublish_SrtPlay_Basic(t *testing.T) {
  35. // This case is run in parallel.
  36. t.Parallel()
  37. // Setup the max timeout for this case.
  38. ctx, cancel := context.WithTimeout(logger.WithContext(context.Background()), time.Duration(*srsTimeout)*time.Millisecond)
  39. defer cancel()
  40. // Check a set of errors.
  41. var r0, r1, r2, r3, r4, r5, r6, r7 error
  42. defer func(ctx context.Context) {
  43. if err := filterTestError(ctx.Err(), r0, r1, r2, r3, r4, r5, r6, r7); err != nil {
  44. t.Errorf("Fail for err %+v", err)
  45. } else {
  46. logger.Tf(ctx, "test done with err %+v", err)
  47. }
  48. }(ctx)
  49. var wg sync.WaitGroup
  50. defer wg.Wait()
  51. // Start SRS server and wait for it to be ready.
  52. svr := NewSRSServer(func(v *srsServer) {
  53. v.envs = []string{
  54. "SRS_SRT_SERVER_ENABLED=on",
  55. "SRS_VHOST_SRT_ENABLED=on",
  56. }
  57. })
  58. wg.Add(1)
  59. go func() {
  60. defer wg.Done()
  61. r0 = svr.Run(ctx, cancel)
  62. }()
  63. // Start FFmpeg to publish stream.
  64. streamID := fmt.Sprintf("stream-%v-%v", os.Getpid(), rand.Int())
  65. streamURL := fmt.Sprintf("srt://localhost:%v?streamid=#!::r=live/%v,m=publish", svr.SRTPort(), streamID)
  66. ffmpeg := NewFFmpeg(func(v *ffmpegClient) {
  67. v.args = []string{
  68. "-stream_loop", "-1", "-re", "-i", *srsPublishAvatar, "-c", "copy",
  69. "-pes_payload_size", "0", "-f", "mpegts", streamURL,
  70. }
  71. })
  72. wg.Add(1)
  73. go func() {
  74. defer wg.Done()
  75. <-svr.ReadyCtx().Done()
  76. r1 = ffmpeg.Run(ctx, cancel)
  77. }()
  78. // Start FFprobe to detect and verify stream.
  79. duration := time.Duration(*srsFFprobeDuration) * time.Millisecond
  80. ffprobe := NewFFprobe(func(v *ffprobeClient) {
  81. v.dvrFile = path.Join(svr.WorkDir(), "objs", fmt.Sprintf("srs-ffprobe-%v.ts", streamID))
  82. v.streamURL = fmt.Sprintf("srt://localhost:%v?streamid=#!::r=live/%v,m=request", svr.SRTPort(), streamID)
  83. v.duration, v.timeout = duration, time.Duration(*srsFFprobeTimeout)*time.Millisecond
  84. })
  85. wg.Add(1)
  86. go func() {
  87. defer wg.Done()
  88. <-svr.ReadyCtx().Done()
  89. r2 = ffprobe.Run(ctx, cancel)
  90. }()
  91. // Fast quit for probe done.
  92. select {
  93. case <-ctx.Done():
  94. case <-ffprobe.ProbeDoneCtx().Done():
  95. defer cancel()
  96. str, m := ffprobe.Result()
  97. if len(m.Streams) != 2 {
  98. r3 = errors.Errorf("invalid streams=%v, %v, %v", len(m.Streams), m.String(), str)
  99. }
  100. // Note that SRT score is low, so we only check duration.
  101. if dv := m.Duration(); dv < duration {
  102. r5 = errors.Errorf("short duration=%v < %v, %v, %v", dv, duration, m.String(), str)
  103. }
  104. }
  105. }