Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
8.3KB

  1. package factory
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "aoiprxkit"
  12. "github.com/jan/fm-rds-tx/internal/config"
  13. "github.com/jan/fm-rds-tx/internal/ingest"
  14. "github.com/jan/fm-rds-tx/internal/ingest/adapters/aoip"
  15. "github.com/jan/fm-rds-tx/internal/ingest/adapters/httpraw"
  16. "github.com/jan/fm-rds-tx/internal/ingest/adapters/icecast"
  17. "github.com/jan/fm-rds-tx/internal/ingest/adapters/srt"
  18. "github.com/jan/fm-rds-tx/internal/ingest/adapters/stdinpcm"
  19. )
  20. type Deps struct {
  21. Stdin io.Reader
  22. HTTP *http.Client
  23. SRTOpener aoiprxkit.SRTConnOpener
  24. AES67ReceiverFactory aoip.ReceiverFactory
  25. AES67Discover AES67DiscoverFunc
  26. }
  27. type AudioIngress interface {
  28. WritePCM16(data []byte) (int, error)
  29. }
  30. type AES67DiscoverRequest struct {
  31. StreamName string
  32. Timeout time.Duration
  33. InterfaceName string
  34. SAPGroup string
  35. SAPPort int
  36. }
  37. type AES67DiscoverFunc func(ctx context.Context, req AES67DiscoverRequest) (aoiprxkit.SAPAnnouncement, error)
  38. func BuildSource(cfg config.Config, deps Deps) (ingest.Source, AudioIngress, error) {
  39. switch normalizeIngestKind(cfg.Ingest.Kind) {
  40. case "", "none":
  41. return nil, nil, nil
  42. case "stdin", "stdin-pcm":
  43. reader := deps.Stdin
  44. if reader == nil {
  45. reader = os.Stdin
  46. }
  47. src := stdinpcm.New("stdin-main", reader, cfg.Ingest.Stdin.SampleRateHz, cfg.Ingest.Stdin.Channels, 1024)
  48. return src, nil, nil
  49. case "http-raw":
  50. src := httpraw.New("http-raw-main", cfg.Ingest.HTTPRaw.SampleRateHz, cfg.Ingest.HTTPRaw.Channels)
  51. return src, src, nil
  52. case "icecast":
  53. src := icecast.New(
  54. "icecast-main",
  55. cfg.Ingest.Icecast.URL,
  56. deps.HTTP,
  57. icecast.ReconnectConfig{
  58. Enabled: cfg.Ingest.Reconnect.Enabled,
  59. InitialBackoffMs: cfg.Ingest.Reconnect.InitialBackoffMs,
  60. MaxBackoffMs: cfg.Ingest.Reconnect.MaxBackoffMs,
  61. },
  62. icecast.WithDecoderPreference(cfg.Ingest.Icecast.Decoder),
  63. )
  64. return src, nil, nil
  65. case "srt":
  66. srtCfg := aoiprxkit.SRTConfig{
  67. URL: cfg.Ingest.SRT.URL,
  68. Mode: cfg.Ingest.SRT.Mode,
  69. SampleRateHz: cfg.Ingest.SRT.SampleRateHz,
  70. Channels: cfg.Ingest.SRT.Channels,
  71. }
  72. opts := []srt.Option{}
  73. if deps.SRTOpener != nil {
  74. opts = append(opts, srt.WithConnOpener(deps.SRTOpener))
  75. }
  76. src := srt.New("srt-main", srtCfg, opts...)
  77. return src, nil, nil
  78. case "aes67", "aoip", "aoip-rtp":
  79. aoipCfg, detail, origin, err := buildAES67Config(cfg, deps)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. opts := []aoip.Option{}
  84. if deps.AES67ReceiverFactory != nil {
  85. opts = append(opts, aoip.WithReceiverFactory(deps.AES67ReceiverFactory))
  86. }
  87. if detail != "" {
  88. opts = append(opts, aoip.WithDetail(detail))
  89. }
  90. if origin != nil {
  91. opts = append(opts, aoip.WithOrigin(*origin))
  92. }
  93. src := aoip.New("aes67-main", aoipCfg, opts...)
  94. return src, nil, nil
  95. default:
  96. return nil, nil, fmt.Errorf("unsupported ingest kind: %s", cfg.Ingest.Kind)
  97. }
  98. }
  99. func SampleRateForKind(cfg config.Config) int {
  100. switch normalizeIngestKind(cfg.Ingest.Kind) {
  101. case "stdin", "stdin-pcm":
  102. if cfg.Ingest.Stdin.SampleRateHz > 0 {
  103. return cfg.Ingest.Stdin.SampleRateHz
  104. }
  105. case "http-raw":
  106. if cfg.Ingest.HTTPRaw.SampleRateHz > 0 {
  107. return cfg.Ingest.HTTPRaw.SampleRateHz
  108. }
  109. case "icecast":
  110. return 44100
  111. case "srt":
  112. if cfg.Ingest.SRT.SampleRateHz > 0 {
  113. return cfg.Ingest.SRT.SampleRateHz
  114. }
  115. case "aes67", "aoip", "aoip-rtp":
  116. if cfg.Ingest.AES67.SampleRateHz > 0 {
  117. return cfg.Ingest.AES67.SampleRateHz
  118. }
  119. }
  120. return 44100
  121. }
  122. func normalizeIngestKind(kind string) string {
  123. return strings.ToLower(strings.TrimSpace(kind))
  124. }
  125. func buildAES67Config(cfg config.Config, deps Deps) (aoiprxkit.Config, string, *ingest.SourceOrigin, error) {
  126. base := aoiprxkit.DefaultConfig()
  127. ing := cfg.Ingest.AES67
  128. if strings.TrimSpace(ing.InterfaceName) != "" {
  129. base.InterfaceName = strings.TrimSpace(ing.InterfaceName)
  130. }
  131. if ing.PayloadType >= 0 {
  132. base.PayloadType = uint8(ing.PayloadType)
  133. }
  134. if ing.SampleRateHz > 0 {
  135. base.SampleRateHz = ing.SampleRateHz
  136. }
  137. if ing.Channels > 0 {
  138. base.Channels = ing.Channels
  139. }
  140. if strings.TrimSpace(ing.Encoding) != "" {
  141. base.Encoding = strings.ToUpper(strings.TrimSpace(ing.Encoding))
  142. }
  143. if ing.PacketTimeMs > 0 {
  144. base.PacketTime = time.Duration(ing.PacketTimeMs) * time.Millisecond
  145. }
  146. if ing.JitterDepthPackets > 0 {
  147. base.JitterDepthPackets = ing.JitterDepthPackets
  148. }
  149. if ing.ReadBufferBytes > 0 {
  150. base.ReadBufferBytes = ing.ReadBufferBytes
  151. }
  152. sdpText, discoveredStreamName, origin, err := resolveAES67SDP(ing, deps)
  153. if err != nil {
  154. return aoiprxkit.Config{}, "", nil, err
  155. }
  156. if sdpText != "" {
  157. info, err := aoiprxkit.ParseMinimalSDP(sdpText)
  158. if err != nil {
  159. return aoiprxkit.Config{}, "", nil, fmt.Errorf("parse ingest.aes67 SDP: %w", err)
  160. }
  161. parsed, err := aoiprxkit.ConfigFromSDP(base, info)
  162. if err != nil {
  163. return aoiprxkit.Config{}, "", nil, fmt.Errorf("map ingest.aes67 SDP: %w", err)
  164. }
  165. detail := ""
  166. endpoint := fmt.Sprintf("rtp://%s:%d", parsed.MulticastGroup, parsed.Port)
  167. if discoveredStreamName != "" {
  168. detail = fmt.Sprintf("rtp://%s:%d (SAP s=%s)", parsed.MulticastGroup, parsed.Port, discoveredStreamName)
  169. }
  170. if origin == nil {
  171. origin = &ingest.SourceOrigin{}
  172. }
  173. if origin.Endpoint == "" {
  174. origin.Endpoint = endpoint
  175. }
  176. return parsed, detail, origin, nil
  177. }
  178. if strings.TrimSpace(ing.MulticastGroup) != "" {
  179. base.MulticastGroup = strings.TrimSpace(ing.MulticastGroup)
  180. }
  181. if ing.Port > 0 {
  182. base.Port = ing.Port
  183. }
  184. if err := base.Validate(); err != nil {
  185. return aoiprxkit.Config{}, "", nil, err
  186. }
  187. if origin == nil {
  188. origin = &ingest.SourceOrigin{Kind: "manual"}
  189. }
  190. if origin.Endpoint == "" {
  191. origin.Endpoint = fmt.Sprintf("rtp://%s:%d", base.MulticastGroup, base.Port)
  192. }
  193. return base, "", origin, nil
  194. }
  195. func resolveAES67SDP(ing config.IngestAES67Config, deps Deps) (string, string, *ingest.SourceOrigin, error) {
  196. sdpText := strings.TrimSpace(ing.SDP)
  197. if sdpText == "" && strings.TrimSpace(ing.SDPPath) != "" {
  198. sdpPath := filepath.Clean(ing.SDPPath)
  199. data, err := os.ReadFile(sdpPath)
  200. if err != nil {
  201. return "", "", nil, fmt.Errorf("read ingest.aes67.sdpPath: %w", err)
  202. }
  203. sdpText = string(data)
  204. return sdpText, "", &ingest.SourceOrigin{
  205. Kind: "sdp-file",
  206. SDPPath: sdpPath,
  207. }, nil
  208. }
  209. if sdpText != "" {
  210. return sdpText, "", &ingest.SourceOrigin{
  211. Kind: "sdp-inline",
  212. }, nil
  213. }
  214. discoveryEnabled := ing.Discovery.Enabled || strings.TrimSpace(ing.Discovery.StreamName) != ""
  215. if !discoveryEnabled {
  216. return "", "", &ingest.SourceOrigin{
  217. Kind: "manual",
  218. }, nil
  219. }
  220. timeout := time.Duration(ing.Discovery.TimeoutMs) * time.Millisecond
  221. if timeout <= 0 {
  222. timeout = 3 * time.Second
  223. }
  224. req := AES67DiscoverRequest{
  225. StreamName: strings.TrimSpace(ing.Discovery.StreamName),
  226. Timeout: timeout,
  227. InterfaceName: strings.TrimSpace(ing.Discovery.InterfaceName),
  228. SAPGroup: strings.TrimSpace(ing.Discovery.SAPGroup),
  229. SAPPort: ing.Discovery.SAPPort,
  230. }
  231. discover := deps.AES67Discover
  232. if discover == nil {
  233. discover = discoverAES67ViaSAP
  234. }
  235. announcement, err := discover(context.Background(), req)
  236. if err != nil {
  237. return "", "", nil, fmt.Errorf("discover ingest.aes67 stream %q via SAP: %w", req.StreamName, err)
  238. }
  239. if strings.TrimSpace(announcement.SDP) == "" {
  240. return "", "", nil, fmt.Errorf("discover ingest.aes67 stream %q via SAP: empty SDP payload", req.StreamName)
  241. }
  242. return announcement.SDP, req.StreamName, &ingest.SourceOrigin{
  243. Kind: "sap-discovery",
  244. StreamName: req.StreamName,
  245. }, nil
  246. }
  247. func discoverAES67ViaSAP(ctx context.Context, req AES67DiscoverRequest) (aoiprxkit.SAPAnnouncement, error) {
  248. if req.StreamName == "" {
  249. return aoiprxkit.SAPAnnouncement{}, fmt.Errorf("stream name must not be empty")
  250. }
  251. listenerCfg := aoiprxkit.DefaultSAPListenerConfig()
  252. if req.InterfaceName != "" {
  253. listenerCfg.InterfaceName = req.InterfaceName
  254. }
  255. if req.SAPGroup != "" {
  256. listenerCfg.Group = req.SAPGroup
  257. }
  258. if req.SAPPort > 0 {
  259. listenerCfg.Port = req.SAPPort
  260. }
  261. sf, err := aoiprxkit.NewStreamFinder(listenerCfg)
  262. if err != nil {
  263. return aoiprxkit.SAPAnnouncement{}, err
  264. }
  265. if err := sf.Start(ctx); err != nil {
  266. return aoiprxkit.SAPAnnouncement{}, err
  267. }
  268. defer sf.Stop()
  269. waitCtx, cancel := context.WithTimeout(ctx, req.Timeout)
  270. defer cancel()
  271. return sf.WaitForStreamName(waitCtx, req.StreamName)
  272. }