Use only 2 ports #7

This commit is contained in:
GyoungSu
2024-09-08 22:22:44 +09:00
parent 4813e2768e
commit 00154ff6c9
6 changed files with 29 additions and 52 deletions

View File

@@ -60,8 +60,8 @@ The system architecture can be visualized as follows:
Start streaming by choosing from the following broadcast options: Start streaming by choosing from the following broadcast options:
### **WHIP Broadcast** ### **WHIP Broadcast (OBS >= 30.x)**
- **Server:** `http://127.0.0.1:5555/whip` - **Server:** `http://127.0.0.1:8044/whip`
- **Bearer Token:** `test` - **Bearer Token:** `test`
### **RTMP Broadcast** ### **RTMP Broadcast**
@@ -74,7 +74,7 @@ Start streaming by choosing from the following broadcast options:
- URL: `http://127.0.0.1:8044/hls/test/master.m3u8` - URL: `http://127.0.0.1:8044/hls/test/master.m3u8`
- **WHEP:** - **WHEP:**
- URL: `http://127.0.0.1:5555/` - URL: `http://127.0.0.1:8044/`
- Bearer Token: `test` - Bearer Token: `test`
- Click the **Subscribe** button. - Click the **Subscribe** button.

View File

@@ -1,11 +1,7 @@
[monitor] [service]
port = 6060 port = 8044
[whep]
port = 5555
[rtmp] [rtmp]
port = 1930 port = 1930
[hls]
port = 8044
llhls = false llhls = false
disk_ram = true disk_ram = true
[docker] [docker]

View File

@@ -2,26 +2,16 @@ package config
// Struct to hold the configuration // Struct to hold the configuration
type Config struct { type Config struct {
Monitor Monitor `mapstructure:"monitor"`
Whep Whep `mapstructure:"whep"`
RTMP RTMP `mapstructure:"rtmp"` RTMP RTMP `mapstructure:"rtmp"`
HLS HLS `mapstructure:"hls"` Service Service `mapstructure:"service"`
Docker DockerConfig `mapstructure:"docker"` Docker DockerConfig `mapstructure:"docker"`
} }
type Monitor struct {
Port int `mapstructure:"port"`
}
type RTMP struct { type RTMP struct {
Port int `mapstructure:"port"` Port int `mapstructure:"port"`
} }
type Whep struct { type Service struct {
Port int `mapstructure:"port"`
}
type HLS struct {
Port int `mapstructure:"port"` Port int `mapstructure:"port"`
LLHLS bool `mapstructure:"llhls"` LLHLS bool `mapstructure:"llhls"`
DiskRam bool `mapstructure:"disk_ram"` DiskRam bool `mapstructure:"disk_ram"`

View File

@@ -9,7 +9,6 @@ services:
- "~/.store:/app/bin/videos" - "~/.store:/app/bin/videos"
ports: ports:
- "8044:8044" - "8044:8044"
- "5555:5555"
- "1930:1930" - "1930:1930"
- "30000-31000:30000-31000/udp" - "30000-31000:30000-31000/udp"
environment: environment:

38
main.go
View File

@@ -8,6 +8,7 @@ import (
"liveflow/media/streamer/egress/record/mp4" "liveflow/media/streamer/egress/record/mp4"
"liveflow/media/streamer/egress/record/webm" "liveflow/media/streamer/egress/record/webm"
"liveflow/media/streamer/egress/whep" "liveflow/media/streamer/egress/whep"
"liveflow/media/streamer/ingress/whip"
"net/http" "net/http"
"strconv" "strconv"
@@ -24,10 +25,9 @@ import (
"liveflow/media/hlshub" "liveflow/media/hlshub"
"liveflow/media/hub" "liveflow/media/hub"
"liveflow/media/streamer/ingress/rtmp" "liveflow/media/streamer/ingress/rtmp"
"liveflow/media/streamer/ingress/whip"
) )
// RTMP 받으면 자동으로 HLS 서비스 동작, 녹화 서비스까지~? // RTMP 받으면 자동으로 Service 서비스 동작, 녹화 서비스까지~?
func main() { func main() {
ctx := context.Background() ctx := context.Background()
viper.SetConfigName("config") // name of config file (without extension) viper.SetConfigName("config") // name of config file (without extension)
@@ -44,13 +44,6 @@ func main() {
panic(fmt.Errorf("failed to unmarshal config: %w", err)) panic(fmt.Errorf("failed to unmarshal config: %w", err))
} }
fmt.Printf("Config: %+v\n", conf) fmt.Printf("Config: %+v\n", conf)
go func() {
statsEcho := echo.New()
statsEcho.GET("/prometheus", echo.WrapHandler(promhttp.Handler()))
statsEcho.GET("/debug/pprof/*", echo.WrapHandler(http.DefaultServeMux))
statsEcho.Start(":" + strconv.Itoa(conf.Monitor.Port))
}()
log.Init() log.Init()
//log.SetCaller(ctx, true) //log.SetCaller(ctx, true)
//log.SetFormatter(ctx, &logrus.JSONFormatter{ //log.SetFormatter(ctx, &logrus.JSONFormatter{
@@ -70,13 +63,23 @@ func main() {
api.HideBanner = true api.HideBanner = true
hlsHub := hlshub.NewHLSHub() hlsHub := hlshub.NewHLSHub()
hlsHandler := httpsrv.NewHandler(hlsHub) hlsHandler := httpsrv.NewHandler(hlsHub)
api.GET("/prometheus", echo.WrapHandler(promhttp.Handler()))
api.GET("/debug/pprof/*", echo.WrapHandler(http.DefaultServeMux))
api.GET("/hls/:streamID/master.m3u8", hlsHandler.HandleMasterM3U8) api.GET("/hls/:streamID/master.m3u8", hlsHandler.HandleMasterM3U8)
api.GET("/hls/:streamID/:playlistName/stream.m3u8", hlsHandler.HandleM3U8) api.GET("/hls/:streamID/:playlistName/stream.m3u8", hlsHandler.HandleM3U8)
api.GET("/hls/:streamID/:playlistName/:resourceName", hlsHandler.HandleM3U8) api.GET("/hls/:streamID/:playlistName/:resourceName", hlsHandler.HandleM3U8)
whipServer := whip.NewWHIP(whip.WHIPArgs{
Hub: hub,
Tracks: tracks,
DockerMode: conf.Docker.Mode,
Echo: api,
})
whipServer.RegisterRoute()
go func() { go func() {
api.Start("0.0.0.0:" + strconv.Itoa(conf.HLS.Port)) fmt.Println("----------------", conf.Service.Port)
api.Start("0.0.0.0:" + strconv.Itoa(conf.Service.Port))
}() }()
// ingress 의 rtmp, whip 서비스로부터 streamID를 받아 HLS, ContainerMP4, WHEP 서비스 시작 // ingress 의 rtmp, whip 서비스로부터 streamID를 받아 Service, ContainerMP4, WHEP 서비스 시작
for source := range hub.SubscribeToStreamID() { for source := range hub.SubscribeToStreamID() {
log.Infof(ctx, "New streamID received: %s", source.StreamID()) log.Infof(ctx, "New streamID received: %s", source.StreamID())
mp4 := mp4.NewMP4(mp4.MP4Args{ mp4 := mp4.NewMP4(mp4.MP4Args{
@@ -96,9 +99,9 @@ func main() {
hls := hls.NewHLS(hls.HLSArgs{ hls := hls.NewHLS(hls.HLSArgs{
Hub: hub, Hub: hub,
HLSHub: hlsHub, HLSHub: hlsHub,
Port: conf.HLS.Port, Port: conf.Service.Port,
LLHLS: conf.HLS.LLHLS, LLHLS: conf.Service.LLHLS,
DiskRam: conf.HLS.DiskRam, DiskRam: conf.Service.DiskRam,
}) })
err := hls.Start(ctx, source) err := hls.Start(ctx, source)
if err != nil { if err != nil {
@@ -115,13 +118,6 @@ func main() {
} }
}() }()
whipServer := whip.NewWHIP(whip.WHIPArgs{
Hub: hub,
Tracks: tracks,
DockerMode: conf.Docker.Mode,
Port: conf.Whep.Port,
})
go whipServer.Serve()
rtmpServer := rtmp.NewRTMP(rtmp.RTMPArgs{ rtmpServer := rtmp.NewRTMP(rtmp.RTMPArgs{
Hub: hub, Hub: hub,
Port: conf.RTMP.Port, Port: conf.RTMP.Port,

View File

@@ -6,7 +6,6 @@ import (
"io" "io"
"liveflow/log" "liveflow/log"
"net/http" "net/http"
"strconv"
"strings" "strings"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@@ -36,14 +35,14 @@ type WHIP struct {
hub *hub.Hub hub *hub.Hub
tracks map[string][]*webrtc.TrackLocalStaticRTP tracks map[string][]*webrtc.TrackLocalStaticRTP
dockerMode bool dockerMode bool
port int echo *echo.Echo
} }
type WHIPArgs struct { type WHIPArgs struct {
Hub *hub.Hub Hub *hub.Hub
Tracks map[string][]*webrtc.TrackLocalStaticRTP Tracks map[string][]*webrtc.TrackLocalStaticRTP
DockerMode bool DockerMode bool
Port int Echo *echo.Echo
} }
func NewWHIP(args WHIPArgs) *WHIP { func NewWHIP(args WHIPArgs) *WHIP {
@@ -51,18 +50,15 @@ func NewWHIP(args WHIPArgs) *WHIP {
hub: args.Hub, hub: args.Hub,
tracks: args.Tracks, tracks: args.Tracks,
dockerMode: args.DockerMode, dockerMode: args.DockerMode,
port: args.Port, echo: args.Echo,
} }
} }
func (r *WHIP) Serve() { func (r *WHIP) RegisterRoute() {
whipServer := echo.New() whipServer := r.echo
whipServer.HideBanner = true
whipServer.Static("/", ".") whipServer.Static("/", ".")
whipServer.POST("/whip", r.whipHandler) whipServer.POST("/whip", r.whipHandler)
whipServer.POST("/whep", r.whepHandler) whipServer.POST("/whep", r.whepHandler)
//whipServer.PATCH("/whip", whipHandler)
whipServer.Start(":" + strconv.Itoa(r.port))
} }
func (r *WHIP) bearerToken(c echo.Context) (string, error) { func (r *WHIP) bearerToken(c echo.Context) (string, error) {