mirror of
https://github.com/flavioribeiro/donut.git
synced 2025-10-06 15:36:50 +08:00
use sugar log directly
This commit is contained in:
@@ -11,14 +11,14 @@ import (
|
||||
|
||||
type SRTController struct {
|
||||
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
|
||||
astisrt.SetLogLevel(astisrt.LogLevel(astisrt.LogLevelNotice))
|
||||
astisrt.SetLogHandler(func(ll astisrt.LogLevel, file, area, msg string, line int) {
|
||||
l.Sugar().Infow("SRT",
|
||||
l.Infow("SRT",
|
||||
"ll", ll,
|
||||
"msg", msg,
|
||||
)
|
||||
@@ -26,7 +26,7 @@ func NewSRTController(c *entities.Config, l *zap.Logger, lc fx.Lifecycle) (*SRTC
|
||||
|
||||
// Startup srt
|
||||
if err := astisrt.Startup(); err != nil {
|
||||
l.Sugar().Errorw("failed to start up srt",
|
||||
l.Errorw("failed to start up srt",
|
||||
"error", 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 {
|
||||
// Clean up
|
||||
if err := astisrt.CleanUp(); err != nil {
|
||||
l.Sugar().Errorw("failed to clean up srt",
|
||||
l.Errorw("failed to clean up srt",
|
||||
"error", 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) {
|
||||
c.l.Sugar().Infow("trying to connect srt")
|
||||
c.l.Infow("trying to connect srt")
|
||||
|
||||
if err := params.Valid(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.l.Sugar().Infow("Connecting to SRT ",
|
||||
c.l.Infow("Connecting to SRT ",
|
||||
"offer", params.String(),
|
||||
)
|
||||
|
||||
@@ -71,7 +71,7 @@ func (c *SRTController) Connect(cancel context.CancelFunc, params entities.Reque
|
||||
},
|
||||
|
||||
OnDisconnect: func(conn *astisrt.Connection, err error) {
|
||||
c.l.Sugar().Infow("Canceling SRT",
|
||||
c.l.Infow("Canceling SRT",
|
||||
"error", err,
|
||||
)
|
||||
cancel()
|
||||
@@ -81,11 +81,11 @@ func (c *SRTController) Connect(cancel context.CancelFunc, params entities.Reque
|
||||
Port: params.SRTPort,
|
||||
})
|
||||
if err != nil {
|
||||
c.l.Sugar().Errorw("failed to connect srt",
|
||||
c.l.Errorw("failed to connect srt",
|
||||
"error", err,
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
c.l.Sugar().Infow("Connected to SRT")
|
||||
c.l.Infow("Connected to SRT")
|
||||
return conn, nil
|
||||
}
|
||||
|
@@ -16,10 +16,10 @@ import (
|
||||
|
||||
type StreamingController struct {
|
||||
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{
|
||||
c: c,
|
||||
l: l,
|
||||
@@ -43,18 +43,18 @@ func (c *StreamingController) Stream(sp entities.StreamParameters) {
|
||||
eia608Reader := NewEIA608Reader()
|
||||
h264PID := uint16(0)
|
||||
|
||||
c.l.Sugar().Infow("streaming has started")
|
||||
c.l.Infow("streaming has started")
|
||||
for {
|
||||
select {
|
||||
case <-sp.Ctx.Done():
|
||||
c.l.Sugar().Errorw("streaming has stopped")
|
||||
c.l.Errorw("streaming has stopped")
|
||||
return
|
||||
default:
|
||||
// fetching mpeg-ts data
|
||||
// ref https://tsduck.io/download/docs/mpegts-introduction.pdf
|
||||
mpegTSDemuxData, err := mpegTSDemuxer.NextData()
|
||||
if err != nil {
|
||||
c.l.Sugar().Errorw("failed to demux mpeg-ts",
|
||||
c.l.Errorw("failed to demux mpeg-ts",
|
||||
"error", err,
|
||||
)
|
||||
return
|
||||
@@ -69,7 +69,7 @@ func (c *StreamingController) Stream(sp entities.StreamParameters) {
|
||||
// writing mpeg-ts video/captions to webrtc channels
|
||||
err = c.writeMpegtsToWebRTC(mpegTSDemuxData, h264PID, err, sp, eia608Reader)
|
||||
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,
|
||||
)
|
||||
return
|
||||
@@ -139,14 +139,14 @@ func (c *StreamingController) readFromSRTIntoWriterPipe(srtConnection *astisrt.C
|
||||
for {
|
||||
n, err := srtConnection.Read(inboundMpegTsPacket)
|
||||
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,
|
||||
)
|
||||
break
|
||||
}
|
||||
|
||||
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,
|
||||
)
|
||||
break
|
||||
|
@@ -12,13 +12,13 @@ import (
|
||||
|
||||
type WebRTCController struct {
|
||||
c *entities.Config
|
||||
l *zap.Logger
|
||||
l *zap.SugaredLogger
|
||||
api *webrtc.API
|
||||
}
|
||||
|
||||
func NewWebRTCController(
|
||||
c *entities.Config,
|
||||
l *zap.Logger,
|
||||
l *zap.SugaredLogger,
|
||||
api *webrtc.API,
|
||||
) *WebRTCController {
|
||||
return &WebRTCController{
|
||||
@@ -29,7 +29,7 @@ func NewWebRTCController(
|
||||
}
|
||||
|
||||
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{}
|
||||
if !c.c.EnableICEMux {
|
||||
@@ -42,7 +42,7 @@ func (c *WebRTCController) CreatePeerConnection(cancel context.CancelFunc) (*web
|
||||
|
||||
peerConnection, err := c.api.NewPeerConnection(peerConnectionConfiguration)
|
||||
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,
|
||||
)
|
||||
return nil, err
|
||||
@@ -55,13 +55,13 @@ func (c *WebRTCController) CreatePeerConnection(cancel context.CancelFunc) (*web
|
||||
connectionState == webrtc.ICEConnectionStateFailed
|
||||
|
||||
if finished {
|
||||
c.l.Sugar().Infow("Canceling webrtc",
|
||||
c.l.Infow("Canceling webrtc",
|
||||
"status", connectionState.String(),
|
||||
)
|
||||
cancel()
|
||||
}
|
||||
|
||||
c.l.Sugar().Infow("OnICEConnectionStateChange",
|
||||
c.l.Infow("OnICEConnectionStateChange",
|
||||
"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) {
|
||||
|
||||
c.l.Sugar().Infow("Gathering WebRTC Candidates")
|
||||
c.l.Infow("Gathering WebRTC Candidates")
|
||||
gatherComplete := webrtc.GatheringCompletePromise(peer)
|
||||
answer, err := peer.CreateAnswer(nil)
|
||||
if err != nil {
|
||||
@@ -110,7 +110,7 @@ func (c *WebRTCController) GatheringWebRTC(peer *webrtc.PeerConnection) (*webrtc
|
||||
}
|
||||
|
||||
<-gatherComplete
|
||||
c.l.Sugar().Infow("Gathering WebRTC Candidates Complete")
|
||||
c.l.Infow("Gathering WebRTC Candidates Complete")
|
||||
|
||||
return peer.LocalDescription(), nil
|
||||
}
|
||||
|
@@ -12,7 +12,7 @@ import (
|
||||
|
||||
type SignalingHandler struct {
|
||||
c *entities.Config
|
||||
l *zap.Logger
|
||||
l *zap.SugaredLogger
|
||||
webRTCController *controllers.WebRTCController
|
||||
srtController *controllers.SRTController
|
||||
streamingController *controllers.StreamingController
|
||||
@@ -20,7 +20,7 @@ type SignalingHandler struct {
|
||||
|
||||
func NewSignalingHandler(
|
||||
c *entities.Config,
|
||||
log *zap.Logger,
|
||||
log *zap.SugaredLogger,
|
||||
webRTCController *controllers.WebRTCController,
|
||||
srtController *controllers.SRTController,
|
||||
streamingController *controllers.StreamingController,
|
||||
@@ -36,19 +36,19 @@ func NewSignalingHandler(
|
||||
|
||||
func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
|
||||
if r.Method != http.MethodPost {
|
||||
h.l.Sugar().Errorw("unexpected method")
|
||||
h.l.Errorw("unexpected method")
|
||||
return entities.ErrHTTPPostOnly
|
||||
}
|
||||
|
||||
params := entities.RequestParams{}
|
||||
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,
|
||||
)
|
||||
return err
|
||||
}
|
||||
if err := params.Valid(); err != nil {
|
||||
h.l.Sugar().Errorw("invalid params",
|
||||
h.l.Errorw("invalid params",
|
||||
"error", err,
|
||||
)
|
||||
return err
|
||||
@@ -58,7 +58,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) err
|
||||
|
||||
peer, err := h.webRTCController.CreatePeerConnection(cancel)
|
||||
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,
|
||||
)
|
||||
return err
|
||||
@@ -73,7 +73,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) err
|
||||
}, "video", params.SRTStreamID,
|
||||
)
|
||||
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,
|
||||
)
|
||||
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)
|
||||
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,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
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,
|
||||
)
|
||||
return err
|
||||
@@ -96,7 +96,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) err
|
||||
|
||||
localDescription, err := h.webRTCController.GatheringWebRTC(peer)
|
||||
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,
|
||||
)
|
||||
return err
|
||||
@@ -104,7 +104,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) err
|
||||
|
||||
srtConnection, err := h.srtController.Connect(cancel, params)
|
||||
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,
|
||||
)
|
||||
return err
|
||||
@@ -124,7 +124,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) err
|
||||
|
||||
err = json.NewEncoder(w).Encode(*localDescription)
|
||||
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,
|
||||
)
|
||||
return err
|
||||
|
@@ -14,7 +14,7 @@ type ErrorHTTPHandler interface {
|
||||
func NewServeMux(
|
||||
index *handlers.IndexHandler,
|
||||
signaling *handlers.SignalingHandler,
|
||||
l *zap.Logger,
|
||||
l *zap.SugaredLogger,
|
||||
) *http.ServeMux {
|
||||
|
||||
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) {
|
||||
err := next.ServeHTTP(w, r)
|
||||
if err != nil {
|
||||
l.Sugar().Errorw("error on handler",
|
||||
l.Errorw("error on handler",
|
||||
"err", err,
|
||||
)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
@@ -14,7 +14,7 @@ import (
|
||||
func NewHTTPServer(
|
||||
c *entities.Config,
|
||||
mux *http.ServeMux,
|
||||
log *zap.Logger,
|
||||
log *zap.SugaredLogger,
|
||||
lc fx.Lifecycle,
|
||||
) *http.Server {
|
||||
srv := &http.Server{
|
||||
@@ -27,7 +27,7 @@ func NewHTTPServer(
|
||||
if err != nil {
|
||||
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,
|
||||
)
|
||||
go srv.Serve(ln)
|
||||
|
7
main.go
7
main.go
@@ -54,8 +54,11 @@ func main() {
|
||||
fx.Provide(controllers.NewWebRTCMediaEngine),
|
||||
fx.Provide(controllers.NewWebRTCAPI),
|
||||
|
||||
// Logging, Config
|
||||
fx.Provide(zap.NewProduction),
|
||||
// Logging, Config constructors
|
||||
fx.Provide(func() *zap.SugaredLogger {
|
||||
logger, _ := zap.NewProduction()
|
||||
return logger.Sugar()
|
||||
}),
|
||||
fx.Provide(func() *entities.Config {
|
||||
return &c
|
||||
}),
|
||||
|
Reference in New Issue
Block a user