2
0

dvr_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. }
  131. func TestFast_RtmpPublish_DvrMp4_Basic(t *testing.T) {
  132. // This case is run in parallel.
  133. t.Parallel()
  134. // Setup the max timeout for this case.
  135. ctx, cancel := context.WithTimeout(logger.WithContext(context.Background()), time.Duration(*srsTimeout)*time.Millisecond)
  136. defer cancel()
  137. // Check a set of errors.
  138. var r0, r1, r2, r3, r4, r5, r6 error
  139. defer func(ctx context.Context) {
  140. if err := filterTestError(ctx.Err(), r0, r1, r2, r3, r4, r5, r6); err != nil {
  141. t.Errorf("Fail for err %+v", err)
  142. } else {
  143. logger.Tf(ctx, "test done with err %+v", err)
  144. }
  145. }(ctx)
  146. var wg sync.WaitGroup
  147. defer wg.Wait()
  148. // Start hooks service.
  149. hooks := NewHooksService()
  150. wg.Add(1)
  151. go func() {
  152. defer wg.Done()
  153. r6 = hooks.Run(ctx, cancel)
  154. }()
  155. // Start SRS server and wait for it to be ready.
  156. svr := NewSRSServer(func(v *srsServer) {
  157. v.envs = []string{
  158. "SRS_VHOST_DVR_ENABLED=on",
  159. "SRS_VHOST_DVR_DVR_PLAN=session",
  160. "SRS_VHOST_DVR_DVR_PATH=./objs/nginx/html/[app]/[stream].[timestamp].mp4",
  161. fmt.Sprintf("SRS_VHOST_DVR_DVR_DURATION=%v", *srsFFprobeDuration),
  162. "SRS_VHOST_HTTP_HOOKS_ENABLED=on",
  163. fmt.Sprintf("SRS_VHOST_HTTP_HOOKS_ON_DVR=http://localhost:%v/api/v1/dvrs", hooks.HooksAPI()),
  164. }
  165. })
  166. wg.Add(1)
  167. go func() {
  168. defer wg.Done()
  169. <-hooks.ReadyCtx().Done()
  170. r0 = svr.Run(ctx, cancel)
  171. }()
  172. // Start FFmpeg to publish stream.
  173. duration := time.Duration(*srsFFprobeDuration) * time.Millisecond
  174. streamID := fmt.Sprintf("stream-%v-%v", os.Getpid(), rand.Int())
  175. streamURL := fmt.Sprintf("rtmp://localhost:%v/live/%v", svr.RTMPPort(), streamID)
  176. ffmpeg := NewFFmpeg(func(v *ffmpegClient) {
  177. // When process quit, still keep case to run.
  178. v.cancelCaseWhenQuit, v.ffmpegDuration = false, duration
  179. v.args = []string{
  180. "-stream_loop", "-1", "-re", "-i", *srsPublishAvatar, "-c", "copy", "-f", "flv", streamURL,
  181. }
  182. })
  183. wg.Add(1)
  184. go func() {
  185. defer wg.Done()
  186. <-svr.ReadyCtx().Done()
  187. r1 = ffmpeg.Run(ctx, cancel)
  188. }()
  189. // Start FFprobe to detect and verify stream.
  190. ffprobe := NewFFprobe(func(v *ffprobeClient) {
  191. v.dvrByFFmpeg, v.streamURL = false, streamURL
  192. v.duration, v.timeout = duration, time.Duration(*srsFFprobeTimeout)*time.Millisecond
  193. wg.Add(1)
  194. go func() {
  195. defer wg.Done()
  196. for evt := range hooks.HooksEvents() {
  197. if onDvrEvt, ok := evt.(*HooksEventOnDvr); ok {
  198. fp := path.Join(svr.WorkDir(), onDvrEvt.File)
  199. logger.Tf(ctx, "FFprobe: Set the dvrFile=%v from callback", fp)
  200. v.dvrFile = fp
  201. }
  202. }
  203. }()
  204. })
  205. wg.Add(1)
  206. go func() {
  207. defer wg.Done()
  208. <-svr.ReadyCtx().Done()
  209. r2 = ffprobe.Run(ctx, cancel)
  210. }()
  211. // Fast quit for probe done.
  212. select {
  213. case <-ctx.Done():
  214. case <-ffprobe.ProbeDoneCtx().Done():
  215. defer cancel()
  216. str, m := ffprobe.Result()
  217. if len(m.Streams) != 2 {
  218. r3 = errors.Errorf("invalid streams=%v, %v, %v", len(m.Streams), m.String(), str)
  219. }
  220. if ts := 90; m.Format.ProbeScore < ts {
  221. r4 = errors.Errorf("low score=%v < %v, %v, %v", m.Format.ProbeScore, ts, m.String(), str)
  222. }
  223. if dv := m.Duration(); dv < duration/2 {
  224. r5 = errors.Errorf("short duration=%v < %v, %v, %v", dv, duration/2, m.String(), str)
  225. }
  226. }
  227. }