mirror of
https://github.com/flavioribeiro/donut.git
synced 2025-10-07 07:50:54 +08:00
use sugar log directly
This commit is contained in:
@@ -11,14 +11,14 @@ import (
|
|||||||
|
|
||||||
type SRTController struct {
|
type SRTController struct {
|
||||||
c *entities.Config
|
c *entities.Config
|
||||||
l *zap.Logger
|
l *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSRTController(c *entities.Config, l *zap.Logger, lc fx.Lifecycle) (*SRTController, error) {
|
func NewSRTController(c *entities.Config, l *zap.SugaredLogger, lc fx.Lifecycle) (*SRTController, error) {
|
||||||
// Handle logs
|
// Handle logs
|
||||||
astisrt.SetLogLevel(astisrt.LogLevel(astisrt.LogLevelNotice))
|
astisrt.SetLogLevel(astisrt.LogLevel(astisrt.LogLevelNotice))
|
||||||
astisrt.SetLogHandler(func(ll astisrt.LogLevel, file, area, msg string, line int) {
|
astisrt.SetLogHandler(func(ll astisrt.LogLevel, file, area, msg string, line int) {
|
||||||
l.Sugar().Infow("SRT",
|
l.Infow("SRT",
|
||||||
"ll", ll,
|
"ll", ll,
|
||||||
"msg", msg,
|
"msg", msg,
|
||||||
)
|
)
|
||||||
@@ -26,7 +26,7 @@ func NewSRTController(c *entities.Config, l *zap.Logger, lc fx.Lifecycle) (*SRTC
|
|||||||
|
|
||||||
// Startup srt
|
// Startup srt
|
||||||
if err := astisrt.Startup(); err != nil {
|
if err := astisrt.Startup(); err != nil {
|
||||||
l.Sugar().Errorw("failed to start up srt",
|
l.Errorw("failed to start up srt",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -36,7 +36,7 @@ func NewSRTController(c *entities.Config, l *zap.Logger, lc fx.Lifecycle) (*SRTC
|
|||||||
OnStop: func(ctx context.Context) error {
|
OnStop: func(ctx context.Context) error {
|
||||||
// Clean up
|
// Clean up
|
||||||
if err := astisrt.CleanUp(); err != nil {
|
if err := astisrt.CleanUp(); err != nil {
|
||||||
l.Sugar().Errorw("failed to clean up srt",
|
l.Errorw("failed to clean up srt",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
@@ -52,13 +52,13 @@ func NewSRTController(c *entities.Config, l *zap.Logger, lc fx.Lifecycle) (*SRTC
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *SRTController) Connect(cancel context.CancelFunc, params entities.RequestParams) (*astisrt.Connection, error) {
|
func (c *SRTController) Connect(cancel context.CancelFunc, params entities.RequestParams) (*astisrt.Connection, error) {
|
||||||
c.l.Sugar().Infow("trying to connect srt")
|
c.l.Infow("trying to connect srt")
|
||||||
|
|
||||||
if err := params.Valid(); err != nil {
|
if err := params.Valid(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.l.Sugar().Infow("Connecting to SRT ",
|
c.l.Infow("Connecting to SRT ",
|
||||||
"offer", params.String(),
|
"offer", params.String(),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ func (c *SRTController) Connect(cancel context.CancelFunc, params entities.Reque
|
|||||||
},
|
},
|
||||||
|
|
||||||
OnDisconnect: func(conn *astisrt.Connection, err error) {
|
OnDisconnect: func(conn *astisrt.Connection, err error) {
|
||||||
c.l.Sugar().Infow("Canceling SRT",
|
c.l.Infow("Canceling SRT",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
cancel()
|
cancel()
|
||||||
@@ -81,11 +81,11 @@ func (c *SRTController) Connect(cancel context.CancelFunc, params entities.Reque
|
|||||||
Port: params.SRTPort,
|
Port: params.SRTPort,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.l.Sugar().Errorw("failed to connect srt",
|
c.l.Errorw("failed to connect srt",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c.l.Sugar().Infow("Connected to SRT")
|
c.l.Infow("Connected to SRT")
|
||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
@@ -16,10 +16,10 @@ import (
|
|||||||
|
|
||||||
type StreamingController struct {
|
type StreamingController struct {
|
||||||
c *entities.Config
|
c *entities.Config
|
||||||
l *zap.Logger
|
l *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStreamingController(c *entities.Config, l *zap.Logger) *StreamingController {
|
func NewStreamingController(c *entities.Config, l *zap.SugaredLogger) *StreamingController {
|
||||||
return &StreamingController{
|
return &StreamingController{
|
||||||
c: c,
|
c: c,
|
||||||
l: l,
|
l: l,
|
||||||
@@ -43,18 +43,18 @@ func (c *StreamingController) Stream(sp entities.StreamParameters) {
|
|||||||
eia608Reader := NewEIA608Reader()
|
eia608Reader := NewEIA608Reader()
|
||||||
h264PID := uint16(0)
|
h264PID := uint16(0)
|
||||||
|
|
||||||
c.l.Sugar().Infow("streaming has started")
|
c.l.Infow("streaming has started")
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-sp.Ctx.Done():
|
case <-sp.Ctx.Done():
|
||||||
c.l.Sugar().Errorw("streaming has stopped")
|
c.l.Errorw("streaming has stopped")
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
// fetching mpeg-ts data
|
// fetching mpeg-ts data
|
||||||
// ref https://tsduck.io/download/docs/mpegts-introduction.pdf
|
// ref https://tsduck.io/download/docs/mpegts-introduction.pdf
|
||||||
mpegTSDemuxData, err := mpegTSDemuxer.NextData()
|
mpegTSDemuxData, err := mpegTSDemuxer.NextData()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.l.Sugar().Errorw("failed to demux mpeg-ts",
|
c.l.Errorw("failed to demux mpeg-ts",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
@@ -69,7 +69,7 @@ func (c *StreamingController) Stream(sp entities.StreamParameters) {
|
|||||||
// writing mpeg-ts video/captions to webrtc channels
|
// writing mpeg-ts video/captions to webrtc channels
|
||||||
err = c.writeMpegtsToWebRTC(mpegTSDemuxData, h264PID, err, sp, eia608Reader)
|
err = c.writeMpegtsToWebRTC(mpegTSDemuxData, h264PID, err, sp, eia608Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.l.Sugar().Errorw("failed to write an mpeg-ts to web rtc",
|
c.l.Errorw("failed to write an mpeg-ts to web rtc",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
@@ -139,14 +139,14 @@ func (c *StreamingController) readFromSRTIntoWriterPipe(srtConnection *astisrt.C
|
|||||||
for {
|
for {
|
||||||
n, err := srtConnection.Read(inboundMpegTsPacket)
|
n, err := srtConnection.Read(inboundMpegTsPacket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.l.Sugar().Errorw("str conn failed to write data to buffer",
|
c.l.Errorw("str conn failed to write data to buffer",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := w.Write(inboundMpegTsPacket[:n]); err != nil {
|
if _, err := w.Write(inboundMpegTsPacket[:n]); err != nil {
|
||||||
c.l.Sugar().Errorw("failed to write mpeg-ts into the pipe",
|
c.l.Errorw("failed to write mpeg-ts into the pipe",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
break
|
break
|
||||||
|
@@ -12,13 +12,13 @@ import (
|
|||||||
|
|
||||||
type WebRTCController struct {
|
type WebRTCController struct {
|
||||||
c *entities.Config
|
c *entities.Config
|
||||||
l *zap.Logger
|
l *zap.SugaredLogger
|
||||||
api *webrtc.API
|
api *webrtc.API
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWebRTCController(
|
func NewWebRTCController(
|
||||||
c *entities.Config,
|
c *entities.Config,
|
||||||
l *zap.Logger,
|
l *zap.SugaredLogger,
|
||||||
api *webrtc.API,
|
api *webrtc.API,
|
||||||
) *WebRTCController {
|
) *WebRTCController {
|
||||||
return &WebRTCController{
|
return &WebRTCController{
|
||||||
@@ -29,7 +29,7 @@ func NewWebRTCController(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *WebRTCController) CreatePeerConnection(cancel context.CancelFunc) (*webrtc.PeerConnection, error) {
|
func (c *WebRTCController) CreatePeerConnection(cancel context.CancelFunc) (*webrtc.PeerConnection, error) {
|
||||||
c.l.Sugar().Infow("trying to set up web rtc conn")
|
c.l.Infow("trying to set up web rtc conn")
|
||||||
|
|
||||||
peerConnectionConfiguration := webrtc.Configuration{}
|
peerConnectionConfiguration := webrtc.Configuration{}
|
||||||
if !c.c.EnableICEMux {
|
if !c.c.EnableICEMux {
|
||||||
@@ -42,7 +42,7 @@ func (c *WebRTCController) CreatePeerConnection(cancel context.CancelFunc) (*web
|
|||||||
|
|
||||||
peerConnection, err := c.api.NewPeerConnection(peerConnectionConfiguration)
|
peerConnection, err := c.api.NewPeerConnection(peerConnectionConfiguration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.l.Sugar().Errorw("error while creating a new peer connection",
|
c.l.Errorw("error while creating a new peer connection",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -55,13 +55,13 @@ func (c *WebRTCController) CreatePeerConnection(cancel context.CancelFunc) (*web
|
|||||||
connectionState == webrtc.ICEConnectionStateFailed
|
connectionState == webrtc.ICEConnectionStateFailed
|
||||||
|
|
||||||
if finished {
|
if finished {
|
||||||
c.l.Sugar().Infow("Canceling webrtc",
|
c.l.Infow("Canceling webrtc",
|
||||||
"status", connectionState.String(),
|
"status", connectionState.String(),
|
||||||
)
|
)
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
c.l.Sugar().Infow("OnICEConnectionStateChange",
|
c.l.Infow("OnICEConnectionStateChange",
|
||||||
"status", connectionState.String(),
|
"status", connectionState.String(),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -100,7 +100,7 @@ func (c *WebRTCController) SetRemoteDescription(peer *webrtc.PeerConnection, des
|
|||||||
|
|
||||||
func (c *WebRTCController) GatheringWebRTC(peer *webrtc.PeerConnection) (*webrtc.SessionDescription, error) {
|
func (c *WebRTCController) GatheringWebRTC(peer *webrtc.PeerConnection) (*webrtc.SessionDescription, error) {
|
||||||
|
|
||||||
c.l.Sugar().Infow("Gathering WebRTC Candidates")
|
c.l.Infow("Gathering WebRTC Candidates")
|
||||||
gatherComplete := webrtc.GatheringCompletePromise(peer)
|
gatherComplete := webrtc.GatheringCompletePromise(peer)
|
||||||
answer, err := peer.CreateAnswer(nil)
|
answer, err := peer.CreateAnswer(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -110,7 +110,7 @@ func (c *WebRTCController) GatheringWebRTC(peer *webrtc.PeerConnection) (*webrtc
|
|||||||
}
|
}
|
||||||
|
|
||||||
<-gatherComplete
|
<-gatherComplete
|
||||||
c.l.Sugar().Infow("Gathering WebRTC Candidates Complete")
|
c.l.Infow("Gathering WebRTC Candidates Complete")
|
||||||
|
|
||||||
return peer.LocalDescription(), nil
|
return peer.LocalDescription(), nil
|
||||||
}
|
}
|
||||||
|
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
type SignalingHandler struct {
|
type SignalingHandler struct {
|
||||||
c *entities.Config
|
c *entities.Config
|
||||||
l *zap.Logger
|
l *zap.SugaredLogger
|
||||||
webRTCController *controllers.WebRTCController
|
webRTCController *controllers.WebRTCController
|
||||||
srtController *controllers.SRTController
|
srtController *controllers.SRTController
|
||||||
streamingController *controllers.StreamingController
|
streamingController *controllers.StreamingController
|
||||||
@@ -20,7 +20,7 @@ type SignalingHandler struct {
|
|||||||
|
|
||||||
func NewSignalingHandler(
|
func NewSignalingHandler(
|
||||||
c *entities.Config,
|
c *entities.Config,
|
||||||
log *zap.Logger,
|
log *zap.SugaredLogger,
|
||||||
webRTCController *controllers.WebRTCController,
|
webRTCController *controllers.WebRTCController,
|
||||||
srtController *controllers.SRTController,
|
srtController *controllers.SRTController,
|
||||||
streamingController *controllers.StreamingController,
|
streamingController *controllers.StreamingController,
|
||||||
@@ -36,19 +36,19 @@ func NewSignalingHandler(
|
|||||||
|
|
||||||
func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
|
func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
h.l.Sugar().Errorw("unexpected method")
|
h.l.Errorw("unexpected method")
|
||||||
return entities.ErrHTTPPostOnly
|
return entities.ErrHTTPPostOnly
|
||||||
}
|
}
|
||||||
|
|
||||||
params := entities.RequestParams{}
|
params := entities.RequestParams{}
|
||||||
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
|
||||||
h.l.Sugar().Errorw("error while decoding request params json",
|
h.l.Errorw("error while decoding request params json",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := params.Valid(); err != nil {
|
if err := params.Valid(); err != nil {
|
||||||
h.l.Sugar().Errorw("invalid params",
|
h.l.Errorw("invalid params",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
@@ -58,7 +58,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) err
|
|||||||
|
|
||||||
peer, err := h.webRTCController.CreatePeerConnection(cancel)
|
peer, err := h.webRTCController.CreatePeerConnection(cancel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.l.Sugar().Errorw("error while setting up web rtc connection",
|
h.l.Errorw("error while setting up web rtc connection",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
@@ -73,7 +73,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) err
|
|||||||
}, "video", params.SRTStreamID,
|
}, "video", params.SRTStreamID,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.l.Sugar().Errorw("error while creating a web rtc track",
|
h.l.Errorw("error while creating a web rtc track",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
@@ -81,14 +81,14 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) err
|
|||||||
|
|
||||||
metadataSender, err := h.webRTCController.CreateDataChannel(peer, entities.MetadataChannelID)
|
metadataSender, err := h.webRTCController.CreateDataChannel(peer, entities.MetadataChannelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.l.Sugar().Errorw("error while createing a web rtc data channel",
|
h.l.Errorw("error while createing a web rtc data channel",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = h.webRTCController.SetRemoteDescription(peer, params.Offer); err != nil {
|
if err = h.webRTCController.SetRemoteDescription(peer, params.Offer); err != nil {
|
||||||
h.l.Sugar().Errorw("error while setting a remote web rtc description",
|
h.l.Errorw("error while setting a remote web rtc description",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
@@ -96,7 +96,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) err
|
|||||||
|
|
||||||
localDescription, err := h.webRTCController.GatheringWebRTC(peer)
|
localDescription, err := h.webRTCController.GatheringWebRTC(peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.l.Sugar().Errorw("error while preparing a local web rtc description",
|
h.l.Errorw("error while preparing a local web rtc description",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
@@ -104,7 +104,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) err
|
|||||||
|
|
||||||
srtConnection, err := h.srtController.Connect(cancel, params)
|
srtConnection, err := h.srtController.Connect(cancel, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.l.Sugar().Errorw("error while connecting to an srt server",
|
h.l.Errorw("error while connecting to an srt server",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
@@ -124,7 +124,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) err
|
|||||||
|
|
||||||
err = json.NewEncoder(w).Encode(*localDescription)
|
err = json.NewEncoder(w).Encode(*localDescription)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.l.Sugar().Errorw("error while encoding a local web rtc description",
|
h.l.Errorw("error while encoding a local web rtc description",
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
|
@@ -14,7 +14,7 @@ type ErrorHTTPHandler interface {
|
|||||||
func NewServeMux(
|
func NewServeMux(
|
||||||
index *handlers.IndexHandler,
|
index *handlers.IndexHandler,
|
||||||
signaling *handlers.SignalingHandler,
|
signaling *handlers.SignalingHandler,
|
||||||
l *zap.Logger,
|
l *zap.SugaredLogger,
|
||||||
) *http.ServeMux {
|
) *http.ServeMux {
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
@@ -39,11 +39,11 @@ func setCors(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorHandler(l *zap.Logger, next ErrorHTTPHandler) http.Handler {
|
func errorHandler(l *zap.SugaredLogger, next ErrorHTTPHandler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := next.ServeHTTP(w, r)
|
err := next.ServeHTTP(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Sugar().Errorw("error on handler",
|
l.Errorw("error on handler",
|
||||||
"err", err,
|
"err", err,
|
||||||
)
|
)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@@ -14,7 +14,7 @@ import (
|
|||||||
func NewHTTPServer(
|
func NewHTTPServer(
|
||||||
c *entities.Config,
|
c *entities.Config,
|
||||||
mux *http.ServeMux,
|
mux *http.ServeMux,
|
||||||
log *zap.Logger,
|
log *zap.SugaredLogger,
|
||||||
lc fx.Lifecycle,
|
lc fx.Lifecycle,
|
||||||
) *http.Server {
|
) *http.Server {
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
@@ -27,7 +27,7 @@ func NewHTTPServer(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Sugar().Infow(fmt.Sprintf("Starting HTTP server. Open http://%s to access the demo", srv.Addr),
|
log.Infow(fmt.Sprintf("Starting HTTP server. Open http://%s to access the demo", srv.Addr),
|
||||||
"addr", srv.Addr,
|
"addr", srv.Addr,
|
||||||
)
|
)
|
||||||
go srv.Serve(ln)
|
go srv.Serve(ln)
|
||||||
|
7
main.go
7
main.go
@@ -54,8 +54,11 @@ func main() {
|
|||||||
fx.Provide(controllers.NewWebRTCMediaEngine),
|
fx.Provide(controllers.NewWebRTCMediaEngine),
|
||||||
fx.Provide(controllers.NewWebRTCAPI),
|
fx.Provide(controllers.NewWebRTCAPI),
|
||||||
|
|
||||||
// Logging, Config
|
// Logging, Config constructors
|
||||||
fx.Provide(zap.NewProduction),
|
fx.Provide(func() *zap.SugaredLogger {
|
||||||
|
logger, _ := zap.NewProduction()
|
||||||
|
return logger.Sugar()
|
||||||
|
}),
|
||||||
fx.Provide(func() *entities.Config {
|
fx.Provide(func() *entities.Config {
|
||||||
return &c
|
return &c
|
||||||
}),
|
}),
|
||||||
|
Reference in New Issue
Block a user