Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

199 satır
4.9KB

  1. package factory
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/jan/fm-rds-tx/internal/config"
  6. )
  7. func TestBuildSourceNone(t *testing.T) {
  8. cfg := config.Default()
  9. cfg.Ingest.Kind = "none"
  10. src, ingress, err := BuildSource(cfg, Deps{})
  11. if err != nil {
  12. t.Fatalf("build source: %v", err)
  13. }
  14. if src != nil || ingress != nil {
  15. t.Fatalf("expected nil source and ingress for kind=none")
  16. }
  17. }
  18. func TestBuildSourceHTTPRawProvidesIngress(t *testing.T) {
  19. cfg := config.Default()
  20. cfg.Ingest.Kind = "http-raw"
  21. src, ingress, err := BuildSource(cfg, Deps{})
  22. if err != nil {
  23. t.Fatalf("build source: %v", err)
  24. }
  25. if src == nil {
  26. t.Fatalf("expected source")
  27. }
  28. if ingress == nil {
  29. t.Fatalf("expected ingress for http-raw")
  30. }
  31. }
  32. func TestBuildSourceKindIsNormalized(t *testing.T) {
  33. cfg := config.Default()
  34. cfg.Ingest.Kind = " HTTP-RAW "
  35. src, ingress, err := BuildSource(cfg, Deps{})
  36. if err != nil {
  37. t.Fatalf("build source: %v", err)
  38. }
  39. if src == nil || ingress == nil {
  40. t.Fatalf("expected source and ingress for normalized http-raw kind")
  41. }
  42. if got := src.Descriptor().Kind; got != "http-raw" {
  43. t.Fatalf("source kind=%q want http-raw", got)
  44. }
  45. }
  46. func TestBuildSourceStdin(t *testing.T) {
  47. cfg := config.Default()
  48. cfg.Ingest.Kind = "stdin"
  49. src, ingress, err := BuildSource(cfg, Deps{Stdin: bytes.NewReader(nil)})
  50. if err != nil {
  51. t.Fatalf("build source: %v", err)
  52. }
  53. if src == nil {
  54. t.Fatalf("expected source")
  55. }
  56. if ingress != nil {
  57. t.Fatalf("expected no ingress for stdin")
  58. }
  59. if got := src.Descriptor().Kind; got != "stdin-pcm" {
  60. t.Fatalf("source kind=%s", got)
  61. }
  62. }
  63. func TestBuildSourceIcecastUsesDecoderPreference(t *testing.T) {
  64. cfg := config.Default()
  65. cfg.Ingest.Kind = "icecast"
  66. cfg.Ingest.Icecast.URL = "http://localhost:8000/stream"
  67. cfg.Ingest.Icecast.Decoder = "ffmpeg"
  68. src, ingress, err := BuildSource(cfg, Deps{})
  69. if err != nil {
  70. t.Fatalf("build source: %v", err)
  71. }
  72. if src == nil {
  73. t.Fatalf("expected source")
  74. }
  75. if ingress != nil {
  76. t.Fatalf("expected no ingress for icecast")
  77. }
  78. if got := src.Descriptor().Codec; got != "ffmpeg" {
  79. t.Fatalf("codec=%s want ffmpeg", got)
  80. }
  81. }
  82. func TestBuildSourceSRT(t *testing.T) {
  83. cfg := config.Default()
  84. cfg.Ingest.Kind = "srt"
  85. cfg.Ingest.SRT.URL = "srt://127.0.0.1:9000?mode=listener"
  86. cfg.Ingest.SRT.Mode = "listener"
  87. cfg.Ingest.SRT.SampleRateHz = 48000
  88. cfg.Ingest.SRT.Channels = 2
  89. src, ingress, err := BuildSource(cfg, Deps{})
  90. if err != nil {
  91. t.Fatalf("build source: %v", err)
  92. }
  93. if src == nil {
  94. t.Fatalf("expected source")
  95. }
  96. if ingress != nil {
  97. t.Fatalf("expected no ingress for srt")
  98. }
  99. if got := src.Descriptor().Kind; got != "srt" {
  100. t.Fatalf("source kind=%s", got)
  101. }
  102. }
  103. func TestBuildSourceAES67(t *testing.T) {
  104. cfg := config.Default()
  105. cfg.Ingest.Kind = "aes67"
  106. cfg.Ingest.AES67.MulticastGroup = "239.69.10.20"
  107. cfg.Ingest.AES67.Port = 5008
  108. cfg.Ingest.AES67.PayloadType = 98
  109. cfg.Ingest.AES67.SampleRateHz = 48000
  110. cfg.Ingest.AES67.Channels = 2
  111. cfg.Ingest.AES67.Encoding = "L24"
  112. cfg.Ingest.AES67.PacketTimeMs = 1
  113. cfg.Ingest.AES67.JitterDepthPackets = 6
  114. src, ingress, err := BuildSource(cfg, Deps{})
  115. if err != nil {
  116. t.Fatalf("build source: %v", err)
  117. }
  118. if src == nil {
  119. t.Fatalf("expected source")
  120. }
  121. if ingress != nil {
  122. t.Fatalf("expected no ingress for aes67")
  123. }
  124. if got := src.Descriptor().Kind; got != "aes67" {
  125. t.Fatalf("source kind=%s", got)
  126. }
  127. }
  128. func TestBuildSourceAES67FromInlineSDP(t *testing.T) {
  129. cfg := config.Default()
  130. cfg.Ingest.Kind = "aes67"
  131. cfg.Ingest.AES67.MulticastGroup = ""
  132. cfg.Ingest.AES67.SDP = "v=0\r\ns=demo\r\nc=IN IP4 239.10.20.30\r\nm=audio 5004 RTP/AVP 97\r\na=rtpmap:97 L24/48000/2\r\na=ptime:1\r\n"
  133. src, _, err := BuildSource(cfg, Deps{})
  134. if err != nil {
  135. t.Fatalf("build source: %v", err)
  136. }
  137. desc := src.Descriptor()
  138. if desc.Transport != "rtp" {
  139. t.Fatalf("transport=%q want rtp", desc.Transport)
  140. }
  141. if desc.SampleRateHz != 48000 || desc.Channels != 2 {
  142. t.Fatalf("shape=%d/%d", desc.SampleRateHz, desc.Channels)
  143. }
  144. }
  145. func TestBuildSourceUnsupportedKind(t *testing.T) {
  146. cfg := config.Default()
  147. cfg.Ingest.Kind = "nope"
  148. _, _, err := BuildSource(cfg, Deps{})
  149. if err == nil {
  150. t.Fatalf("expected error")
  151. }
  152. }
  153. func TestSampleRateForKind(t *testing.T) {
  154. cfg := config.Default()
  155. cfg.Ingest.Kind = "stdin"
  156. cfg.Ingest.Stdin.SampleRateHz = 48000
  157. if got := SampleRateForKind(cfg); got != 48000 {
  158. t.Fatalf("stdin sample rate=%d", got)
  159. }
  160. cfg.Ingest.Kind = "http-raw"
  161. cfg.Ingest.HTTPRaw.SampleRateHz = 32000
  162. if got := SampleRateForKind(cfg); got != 32000 {
  163. t.Fatalf("http-raw sample rate=%d", got)
  164. }
  165. cfg.Ingest.Kind = "icecast"
  166. if got := SampleRateForKind(cfg); got != 44100 {
  167. t.Fatalf("icecast sample rate=%d", got)
  168. }
  169. cfg.Ingest.Kind = "srt"
  170. cfg.Ingest.SRT.SampleRateHz = 48000
  171. if got := SampleRateForKind(cfg); got != 48000 {
  172. t.Fatalf("srt sample rate=%d", got)
  173. }
  174. cfg.Ingest.Kind = "aes67"
  175. cfg.Ingest.AES67.SampleRateHz = 32000
  176. if got := SampleRateForKind(cfg); got != 32000 {
  177. t.Fatalf("aes67 sample rate=%d", got)
  178. }
  179. }