2
0

hls_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_RtmpPublish_HlsPlay_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 error
  42. defer func(ctx context.Context) {
  43. if err := filterTestError(ctx.Err(), r0, r1, r2, r3, r4); 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_HTTP_SERVER_ENABLED=on",
  55. "SRS_VHOST_HLS_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("rtmp://localhost:%v/live/%v", svr.RTMPPort(), streamID)
  66. ffmpeg := NewFFmpeg(func(v *ffmpegClient) {
  67. v.args = []string{
  68. "-stream_loop", "-1", "-re", "-i", *srsPublishAvatar, "-c", "copy", "-f", "flv", streamURL,
  69. }
  70. })
  71. wg.Add(1)
  72. go func() {
  73. defer wg.Done()
  74. <-svr.ReadyCtx().Done()
  75. r1 = ffmpeg.Run(ctx, cancel)
  76. }()
  77. // Start FFprobe to detect and verify stream.
  78. duration := time.Duration(*srsFFprobeDuration) * time.Millisecond
  79. ffprobe := NewFFprobe(func(v *ffprobeClient) {
  80. v.dvrFile = path.Join(svr.WorkDir(), "objs", fmt.Sprintf("srs-ffprobe-%v.ts", streamID))
  81. v.streamURL = fmt.Sprintf("http://localhost:%v/live/%v.m3u8", svr.HTTPPort(), streamID)
  82. v.duration, v.timeout = duration, time.Duration(*srsFFprobeTimeout)*time.Millisecond
  83. })
  84. wg.Add(1)
  85. go func() {
  86. defer wg.Done()
  87. <-svr.ReadyCtx().Done()
  88. r2 = ffprobe.Run(ctx, cancel)
  89. }()
  90. // Fast quit for probe done.
  91. select {
  92. case <-ctx.Done():
  93. case <-ffprobe.ProbeDoneCtx().Done():
  94. defer cancel()
  95. str, m := ffprobe.Result()
  96. if len(m.Streams) != 2 {
  97. r3 = errors.Errorf("invalid streams=%v, %v, %v", len(m.Streams), m.String(), str)
  98. }
  99. // Note that HLS score is low, so we only check duration. Note that only check half of duration, because we
  100. // might get only some pieces of segments.
  101. if dv := m.Duration(); dv < duration/2 {
  102. r4 = errors.Errorf("short duration=%v < %v, %v, %v", dv, duration/2, m.String(), str)
  103. }
  104. }
  105. }