mp3_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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_RtmpPlay_CodecMP3_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()
  53. wg.Add(1)
  54. go func() {
  55. defer wg.Done()
  56. r0 = svr.Run(ctx, cancel)
  57. }()
  58. // Start FFmpeg to publish stream.
  59. streamID := fmt.Sprintf("stream-%v-%v", os.Getpid(), rand.Int())
  60. streamURL := fmt.Sprintf("rtmp://localhost:%v/live/%v", svr.RTMPPort(), streamID)
  61. ffmpeg := NewFFmpeg(func(v *ffmpegClient) {
  62. v.args = []string{
  63. "-stream_loop", "-1", "-re", "-i", *srsPublishAvatar, "-vcodec", "copy", "-acodec", "libmp3lame", "-f", "flv", streamURL,
  64. }
  65. })
  66. wg.Add(1)
  67. go func() {
  68. defer wg.Done()
  69. <-svr.ReadyCtx().Done()
  70. r1 = ffmpeg.Run(ctx, cancel)
  71. }()
  72. // Start FFprobe to detect and verify stream.
  73. duration := time.Duration(*srsFFprobeDuration) * time.Millisecond
  74. ffprobe := NewFFprobe(func(v *ffprobeClient) {
  75. v.dvrFile = path.Join(svr.WorkDir(), "objs", fmt.Sprintf("srs-ffprobe-%v.flv", streamID))
  76. v.streamURL = fmt.Sprintf("rtmp://localhost:%v/live/%v", svr.RTMPPort(), streamID)
  77. v.duration, v.timeout = duration, time.Duration(*srsFFprobeTimeout)*time.Millisecond
  78. })
  79. wg.Add(1)
  80. go func() {
  81. defer wg.Done()
  82. <-svr.ReadyCtx().Done()
  83. r2 = ffprobe.Run(ctx, cancel)
  84. }()
  85. // Fast quit for probe done.
  86. select {
  87. case <-ctx.Done():
  88. case <-ffprobe.ProbeDoneCtx().Done():
  89. defer cancel()
  90. str, m := ffprobe.Result()
  91. if len(m.Streams) != 2 {
  92. r3 = errors.Errorf("invalid streams=%v, %v, %v", len(m.Streams), m.String(), str)
  93. }
  94. if ts := 90; m.Format.ProbeScore < ts {
  95. r4 = errors.Errorf("low score=%v < %v, %v, %v", m.Format.ProbeScore, ts, m.String(), str)
  96. }
  97. if dv := m.Duration(); dv < duration {
  98. r5 = errors.Errorf("short duration=%v < %v, %v, %v", dv, duration, m.String(), str)
  99. }
  100. if a := m.Audio(); a == nil {
  101. r6 = errors.Errorf("no audio, %v, %v", m.String(), str)
  102. } else if a.CodecName != "mp3" {
  103. r7 = errors.Errorf("invalid audio codec=%v, %v, %v", a.CodecName, m.String(), str)
  104. }
  105. }
  106. }
  107. func TestFast_RtmpPublish_HttpFlvPlay_CodecMP3_Basic(t *testing.T) {
  108. // This case is run in parallel.
  109. t.Parallel()
  110. // Setup the max timeout for this case.
  111. ctx, cancel := context.WithTimeout(logger.WithContext(context.Background()), time.Duration(*srsTimeout)*time.Millisecond)
  112. defer cancel()
  113. // Check a set of errors.
  114. var r0, r1, r2, r3, r4, r5, r6, r7 error
  115. defer func(ctx context.Context) {
  116. if err := filterTestError(ctx.Err(), r0, r1, r2, r3, r4, r5, r6, r7); err != nil {
  117. t.Errorf("Fail for err %+v", err)
  118. } else {
  119. logger.Tf(ctx, "test done with err %+v", err)
  120. }
  121. }(ctx)
  122. var wg sync.WaitGroup
  123. defer wg.Wait()
  124. // Start SRS server and wait for it to be ready.
  125. svr := NewSRSServer(func(v *srsServer) {
  126. v.envs = []string{
  127. "SRS_HTTP_SERVER_ENABLED=on",
  128. "SRS_VHOST_HTTP_REMUX_ENABLED=on",
  129. // If guessing, we might got no audio because transcoding might be delay for sending audio packets.
  130. "SRS_VHOST_HTTP_REMUX_GUESS_HAS_AV=off",
  131. }
  132. })
  133. wg.Add(1)
  134. go func() {
  135. defer wg.Done()
  136. r0 = svr.Run(ctx, cancel)
  137. }()
  138. // Start FFmpeg to publish stream.
  139. streamID := fmt.Sprintf("stream-%v-%v", os.Getpid(), rand.Int())
  140. streamURL := fmt.Sprintf("rtmp://localhost:%v/live/%v", svr.RTMPPort(), streamID)
  141. ffmpeg := NewFFmpeg(func(v *ffmpegClient) {
  142. v.args = []string{
  143. "-stream_loop", "-1", "-re", "-i", *srsPublishAvatar, "-vcodec", "copy", "-acodec", "libmp3lame", "-f", "flv", streamURL,
  144. }
  145. })
  146. wg.Add(1)
  147. go func() {
  148. defer wg.Done()
  149. <-svr.ReadyCtx().Done()
  150. r1 = ffmpeg.Run(ctx, cancel)
  151. }()
  152. // Start FFprobe to detect and verify stream.
  153. duration := time.Duration(*srsFFprobeDuration) * time.Millisecond
  154. ffprobe := NewFFprobe(func(v *ffprobeClient) {
  155. v.dvrFile = path.Join(svr.WorkDir(), "objs", fmt.Sprintf("srs-ffprobe-%v.flv", streamID))
  156. v.streamURL = fmt.Sprintf("http://localhost:%v/live/%v.flv", svr.HTTPPort(), streamID)
  157. v.duration, v.timeout = duration, time.Duration(*srsFFprobeTimeout)*time.Millisecond
  158. })
  159. wg.Add(1)
  160. go func() {
  161. defer wg.Done()
  162. <-svr.ReadyCtx().Done()
  163. r2 = ffprobe.Run(ctx, cancel)
  164. }()
  165. // Fast quit for probe done.
  166. select {
  167. case <-ctx.Done():
  168. case <-ffprobe.ProbeDoneCtx().Done():
  169. defer cancel()
  170. str, m := ffprobe.Result()
  171. if len(m.Streams) != 2 {
  172. r3 = errors.Errorf("invalid streams=%v, %v, %v", len(m.Streams), m.String(), str)
  173. }
  174. if ts := 90; m.Format.ProbeScore < ts {
  175. r4 = errors.Errorf("low score=%v < %v, %v, %v", m.Format.ProbeScore, ts, m.String(), str)
  176. }
  177. if dv := m.Duration(); dv < duration {
  178. r5 = errors.Errorf("short duration=%v < %v, %v, %v", dv, duration, m.String(), str)
  179. }
  180. if a := m.Audio(); a == nil {
  181. r6 = errors.Errorf("no audio, %v, %v", m.String(), str)
  182. } else if a.CodecName != "mp3" {
  183. r7 = errors.Errorf("invalid audio codec=%v, %v, %v", a.CodecName, m.String(), str)
  184. }
  185. }
  186. }
  187. func TestFast_RtmpPublish_HttpMp3Play_CodecMP3_Basic(t *testing.T) {
  188. // This case is run in parallel.
  189. t.Parallel()
  190. // Setup the max timeout for this case.
  191. ctx, cancel := context.WithTimeout(logger.WithContext(context.Background()), time.Duration(*srsTimeout)*time.Millisecond)
  192. defer cancel()
  193. // Check a set of errors.
  194. var r0, r1, r2, r3, r4, r5, r6, r7 error
  195. defer func(ctx context.Context) {
  196. if err := filterTestError(ctx.Err(), r0, r1, r2, r3, r4, r5, r6, r7); err != nil {
  197. t.Errorf("Fail for err %+v", err)
  198. } else {
  199. logger.Tf(ctx, "test done with err %+v", err)
  200. }
  201. }(ctx)
  202. var wg sync.WaitGroup
  203. defer wg.Wait()
  204. // Start SRS server and wait for it to be ready.
  205. svr := NewSRSServer(func(v *srsServer) {
  206. v.envs = []string{
  207. "SRS_HTTP_SERVER_ENABLED=on",
  208. "SRS_VHOST_HTTP_REMUX_ENABLED=on",
  209. "SRS_VHOST_HTTP_REMUX_MOUNT=[vhost]/[app]/[stream].mp3",
  210. }
  211. })
  212. wg.Add(1)
  213. go func() {
  214. defer wg.Done()
  215. r0 = svr.Run(ctx, cancel)
  216. }()
  217. // Start FFmpeg to publish stream.
  218. streamID := fmt.Sprintf("stream-%v-%v", os.Getpid(), rand.Int())
  219. streamURL := fmt.Sprintf("rtmp://localhost:%v/live/%v", svr.RTMPPort(), streamID)
  220. ffmpeg := NewFFmpeg(func(v *ffmpegClient) {
  221. v.args = []string{
  222. "-stream_loop", "-1", "-re", "-i", *srsPublishAvatar, "-vn", "-acodec", "libmp3lame", "-f", "flv", streamURL,
  223. }
  224. })
  225. wg.Add(1)
  226. go func() {
  227. defer wg.Done()
  228. <-svr.ReadyCtx().Done()
  229. r1 = ffmpeg.Run(ctx, cancel)
  230. }()
  231. // Start FFprobe to detect and verify stream.
  232. duration := time.Duration(*srsFFprobeDuration) * time.Millisecond
  233. ffprobe := NewFFprobe(func(v *ffprobeClient) {
  234. v.dvrFile = path.Join(svr.WorkDir(), "objs", fmt.Sprintf("srs-ffprobe-%v.mp3", streamID))
  235. v.streamURL = fmt.Sprintf("http://localhost:%v/live/%v.mp3", svr.HTTPPort(), streamID)
  236. v.duration, v.timeout = duration, time.Duration(*srsFFprobeTimeout)*time.Millisecond
  237. })
  238. wg.Add(1)
  239. go func() {
  240. defer wg.Done()
  241. <-svr.ReadyCtx().Done()
  242. r2 = ffprobe.Run(ctx, cancel)
  243. }()
  244. // Fast quit for probe done.
  245. select {
  246. case <-ctx.Done():
  247. case <-ffprobe.ProbeDoneCtx().Done():
  248. defer cancel()
  249. str, m := ffprobe.Result()
  250. if len(m.Streams) != 1 {
  251. r3 = errors.Errorf("invalid streams=%v, %v, %v", len(m.Streams), m.String(), str)
  252. }
  253. // Note that HTTP-MP3 score is low, so we only check duration.
  254. if dv := m.Duration(); dv < duration {
  255. r5 = errors.Errorf("short duration=%v < %v, %v, %v", dv, duration, m.String(), str)
  256. }
  257. if a := m.Audio(); a == nil {
  258. r6 = errors.Errorf("no audio, %v, %v", m.String(), str)
  259. } else if a.CodecName != "mp3" {
  260. r7 = errors.Errorf("invalid audio codec=%v, %v, %v", a.CodecName, m.String(), str)
  261. }
  262. }
  263. }
  264. func TestFast_RtmpPublish_HttpTsPlay_CodecMP3_Basic(t *testing.T) {
  265. // This case is run in parallel.
  266. t.Parallel()
  267. // Setup the max timeout for this case.
  268. ctx, cancel := context.WithTimeout(logger.WithContext(context.Background()), time.Duration(*srsTimeout)*time.Millisecond)
  269. defer cancel()
  270. // Check a set of errors.
  271. var r0, r1, r2, r3, r4, r5, r6, r7 error
  272. defer func(ctx context.Context) {
  273. if err := filterTestError(ctx.Err(), r0, r1, r2, r3, r4, r5, r6, r7); err != nil {
  274. t.Errorf("Fail for err %+v", err)
  275. } else {
  276. logger.Tf(ctx, "test done with err %+v", err)
  277. }
  278. }(ctx)
  279. var wg sync.WaitGroup
  280. defer wg.Wait()
  281. // Start SRS server and wait for it to be ready.
  282. svr := NewSRSServer(func(v *srsServer) {
  283. v.envs = []string{
  284. "SRS_HTTP_SERVER_ENABLED=on",
  285. "SRS_VHOST_HTTP_REMUX_ENABLED=on",
  286. "SRS_VHOST_HTTP_REMUX_MOUNT=[vhost]/[app]/[stream].ts",
  287. }
  288. })
  289. wg.Add(1)
  290. go func() {
  291. defer wg.Done()
  292. r0 = svr.Run(ctx, cancel)
  293. }()
  294. // Start FFmpeg to publish stream.
  295. streamID := fmt.Sprintf("stream-%v-%v", os.Getpid(), rand.Int())
  296. streamURL := fmt.Sprintf("rtmp://localhost:%v/live/%v", svr.RTMPPort(), streamID)
  297. ffmpeg := NewFFmpeg(func(v *ffmpegClient) {
  298. v.args = []string{
  299. "-stream_loop", "-1", "-re", "-i", *srsPublishAvatar, "-vcodec", "copy", "-acodec", "libmp3lame", "-f", "flv", streamURL,
  300. }
  301. })
  302. wg.Add(1)
  303. go func() {
  304. defer wg.Done()
  305. <-svr.ReadyCtx().Done()
  306. r1 = ffmpeg.Run(ctx, cancel)
  307. }()
  308. // Start FFprobe to detect and verify stream.
  309. duration := time.Duration(*srsFFprobeDuration) * time.Millisecond
  310. ffprobe := NewFFprobe(func(v *ffprobeClient) {
  311. v.dvrFile = path.Join(svr.WorkDir(), "objs", fmt.Sprintf("srs-ffprobe-%v.ts", streamID))
  312. v.streamURL = fmt.Sprintf("http://localhost:%v/live/%v.ts", svr.HTTPPort(), streamID)
  313. v.duration, v.timeout = duration, time.Duration(*srsFFprobeTimeout)*time.Millisecond
  314. })
  315. wg.Add(1)
  316. go func() {
  317. defer wg.Done()
  318. <-svr.ReadyCtx().Done()
  319. r2 = ffprobe.Run(ctx, cancel)
  320. }()
  321. // Fast quit for probe done.
  322. select {
  323. case <-ctx.Done():
  324. case <-ffprobe.ProbeDoneCtx().Done():
  325. defer cancel()
  326. str, m := ffprobe.Result()
  327. if len(m.Streams) != 2 {
  328. r3 = errors.Errorf("invalid streams=%v, %v, %v", len(m.Streams), m.String(), str)
  329. }
  330. // Note that HTTP-TS score is low, so we only check duration.
  331. if dv := m.Duration(); dv < duration {
  332. r5 = errors.Errorf("short duration=%v < %v, %v, %v", dv, duration, m.String(), str)
  333. }
  334. if a := m.Audio(); a == nil {
  335. r6 = errors.Errorf("no audio, %v, %v", m.String(), str)
  336. } else if a.CodecName != "mp3" {
  337. r7 = errors.Errorf("invalid audio codec=%v, %v, %v", a.CodecName, m.String(), str)
  338. }
  339. }
  340. }
  341. func TestFast_RtmpPublish_HlsPlay_CodecMP3_Basic(t *testing.T) {
  342. // This case is run in parallel.
  343. t.Parallel()
  344. // Setup the max timeout for this case.
  345. ctx, cancel := context.WithTimeout(logger.WithContext(context.Background()), time.Duration(*srsTimeout)*time.Millisecond)
  346. defer cancel()
  347. // Check a set of errors.
  348. var r0, r1, r2, r3, r4, r5, r6 error
  349. defer func(ctx context.Context) {
  350. if err := filterTestError(ctx.Err(), r0, r1, r2, r3, r4, r5, r6); err != nil {
  351. t.Errorf("Fail for err %+v", err)
  352. } else {
  353. logger.Tf(ctx, "test done with err %+v", err)
  354. }
  355. }(ctx)
  356. var wg sync.WaitGroup
  357. defer wg.Wait()
  358. // Start SRS server and wait for it to be ready.
  359. svr := NewSRSServer(func(v *srsServer) {
  360. v.envs = []string{
  361. "SRS_HTTP_SERVER_ENABLED=on",
  362. "SRS_VHOST_HLS_ENABLED=on",
  363. }
  364. })
  365. wg.Add(1)
  366. go func() {
  367. defer wg.Done()
  368. r0 = svr.Run(ctx, cancel)
  369. }()
  370. // Start FFmpeg to publish stream.
  371. streamID := fmt.Sprintf("stream-%v-%v", os.Getpid(), rand.Int())
  372. streamURL := fmt.Sprintf("rtmp://localhost:%v/live/%v", svr.RTMPPort(), streamID)
  373. ffmpeg := NewFFmpeg(func(v *ffmpegClient) {
  374. v.args = []string{
  375. "-stream_loop", "-1", "-re", "-i", *srsPublishAvatar, "-vcodec", "copy", "-acodec", "libmp3lame", "-f", "flv", streamURL,
  376. }
  377. })
  378. wg.Add(1)
  379. go func() {
  380. defer wg.Done()
  381. <-svr.ReadyCtx().Done()
  382. r1 = ffmpeg.Run(ctx, cancel)
  383. }()
  384. // Start FFprobe to detect and verify stream.
  385. duration := time.Duration(*srsFFprobeDuration) * time.Millisecond
  386. ffprobe := NewFFprobe(func(v *ffprobeClient) {
  387. v.dvrFile = path.Join(svr.WorkDir(), "objs", fmt.Sprintf("srs-ffprobe-%v.ts", streamID))
  388. v.streamURL = fmt.Sprintf("http://localhost:%v/live/%v.m3u8", svr.HTTPPort(), streamID)
  389. v.duration, v.timeout = duration, time.Duration(*srsFFprobeTimeout)*time.Millisecond
  390. })
  391. wg.Add(1)
  392. go func() {
  393. defer wg.Done()
  394. <-svr.ReadyCtx().Done()
  395. r2 = ffprobe.Run(ctx, cancel)
  396. }()
  397. // Fast quit for probe done.
  398. select {
  399. case <-ctx.Done():
  400. case <-ffprobe.ProbeDoneCtx().Done():
  401. defer cancel()
  402. str, m := ffprobe.Result()
  403. if len(m.Streams) != 2 {
  404. r3 = errors.Errorf("invalid streams=%v, %v, %v", len(m.Streams), m.String(), str)
  405. }
  406. // Note that HLS score is low, so we only check duration. Note that only check half of duration, because we
  407. // might get only some pieces of segments.
  408. if dv := m.Duration(); dv < duration/2 {
  409. r4 = errors.Errorf("short duration=%v < %v, %v, %v", dv, duration/2, m.String(), str)
  410. }
  411. if a := m.Audio(); a == nil {
  412. r5 = errors.Errorf("no audio, %v, %v", m.String(), str)
  413. } else if a.CodecName != "mp3" {
  414. r6 = errors.Errorf("invalid audio codec=%v, %v, %v", a.CodecName, m.String(), str)
  415. }
  416. }
  417. }