live-sfu-demo/internal/sfu/cdn/provider.go

53 lines
1.5 KiB
Go

package cdn
import (
"context"
"fmt"
"strings"
)
// Provider implements third-party CDN distribution via RTMP/SRT forward.
type Provider struct {
rtmpURL string
name string
}
// NewProvider creates a CDN provider. rtmpURL is the base RTMP URL, e.g. rtmp://cdn.example.com/live
func NewProvider(rtmpURL, name string) *Provider {
if name == "" {
name = "CDN"
}
return &Provider{rtmpURL: rtmpURL, name: name}
}
func (p *Provider) Kind() string { return "cdn" }
func (p *Provider) Name() string { return p.name }
func (p *Provider) Configured() bool { return strings.TrimSpace(p.rtmpURL) != "" }
func (p *Provider) RTMPURL() string { return p.rtmpURL }
// Forward returns the RTMP push URL for the given stream.
// In the current implementation this is a stub that just composes the URL and returns ready status.
// Future: actually trigger SRS forward API or external pusher.
func (p *Provider) ApplyConfig(rtmpURL, name string) {
p.rtmpURL = rtmpURL
if name != "" {
p.name = name
}
}
func (p *Provider) Forward(ctx context.Context, room, stream string) (*ForwardResult, error) {
if !p.Configured() {
return nil, fmt.Errorf("cdn not configured: CDN_RTMP_URL empty")
}
base := strings.TrimRight(p.rtmpURL, "/")
url := base + "/" + stream
return &ForwardResult{Kind: "cdn", URL: url, Status: "ready"}, nil
}
// ForwardResult mirrors sfu.ForwardResult to avoid import cycle
type ForwardResult struct {
Kind string
URL string
Status string
}