dvr_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_DvrFlv_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 error
  42. defer func(ctx context.Context) {
  43. if err := filterTestError(ctx.Err(), r0, r1, r2, r3, r4, r5, r6); 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 hooks service.
  52. hooks := NewHooksService()
  53. wg.Add(1)
  54. go func() {
  55. defer wg.Done()
  56. r6 = hooks.Run(ctx, cancel)
  57. }()
  58. // Start SRS server and wait for it to be ready.
  59. svr := NewSRSServer(func(v *srsServer) {
  60. v.envs = []string{
  61. "SRS_VHOST_DVR_ENABLED=on",
  62. "SRS_VHOST_DVR_DVR_PLAN=session",
  63. "SRS_VHOST_DVR_DVR_PATH=./objs/nginx/html/[app]/[stream].[timestamp].flv",
  64. fmt.Sprintf("SRS_VHOST_DVR_DVR_DURATION=%v", *srsFFprobeDuration),
  65. "SRS_VHOST_HTTP_HOOKS_ENABLED=on",
  66. fmt.Sprintf("SRS_VHOST_HTTP_HOOKS_ON_DVR=http://localhost:%v/api/v1/dvrs", hooks.HooksAPI()),
  67. }
  68. })
  69. wg.Add(1)
  70. go func() {
  71. defer wg.Done()
  72. <-hooks.ReadyCtx().Done()
  73. r0 = svr.Run(ctx, cancel)
  74. }()
  75. // Start FFmpeg to publish stream.
  76. duration := time.Duration(*srsFFprobeDuration) * time.Millisecond
  77. streamID := fmt.Sprintf("stream-%v-%v", os.Getpid(), rand.Int())
  78. streamURL := fmt.Sprintf("rtmp://localhost:%v/live/%v", svr.RTMPPort(), streamID)
  79. ffmpeg := NewFFmpeg(func(v *ffmpegClient) {
  80. // When process quit, still keep case to run.
  81. v.cancelCaseWhenQuit, v.ffmpegDuration = false, duration
  82. v.args = []string{
  83. "-stream_loop", "-1", "-re", "-i", *srsPublishAvatar, "-c", "copy", "-f", "flv", streamURL,
  84. }
  85. })
  86. wg.Add(1)
  87. go func() {
  88. defer wg.Done()
  89. <-svr.ReadyCtx().Done()
  90. r1 = ffmpeg.Run(ctx, cancel)
  91. }()
  92. // Start FFprobe to detect and verify stream.
  93. ffprobe := NewFFprobe(func(v *ffprobeClient) {
  94. v.dvrByFFmpeg, v.streamURL = false, streamURL
  95. v.duration, v.timeout = duration, time.Duration(*srsFFprobeTimeout)*time.Millisecond
  96. wg.Add(1)
  97. go func() {
  98. defer wg.Done()
  99. for evt := range hooks.HooksEvents() {
  100. if onDvrEvt, ok := evt.(*HooksEventOnDvr); ok {
  101. fp := path.Join(svr.WorkDir(), onDvrEvt.File)
  102. logger.Tf(ctx, "FFprobe: Set the dvrFile=%v from callback", fp)
  103. v.dvrFile = fp
  104. }
  105. }
  106. }()
  107. })
  108. wg.Add(1)
  109. go func() {
  110. defer wg.Done()
  111. <-svr.ReadyCtx().Done()
  112. r2 = ffprobe.Run(ctx, cancel)
  113. }()
  114. // Fast quit for probe done.
  115. select {
  116. case <-ctx.Done():
  117. case <-ffprobe.ProbeDoneCtx().Done():
  118. defer cancel()
  119. str, m := ffprobe.Result()
  120. if len(m.Streams) != 2 {
  121. r3 = errors.Errorf("invalid streams=%v, %v, %v", len(m.Streams), m.String(), str)
  122. }
  123. if ts := 90; m.Format.ProbeScore < ts {
  124. r4 = errors.Errorf("low score=%v < %v, %v, %v", m.Format.ProbeScore, ts, m.String(), str)
  125. }
  126. if dv := m.Duration(); dv < duration/2 {
  127. r5 = errors.Errorf("short duration=%v < %v, %v, %v", dv, duration/2, m.String(), str)
  128. }
  129. }
  130. }