mirror of
https://github.com/flavioribeiro/donut.git
synced 2025-10-05 15:06:51 +08:00
provides cancelation when a component stop responding it closes all resources
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -28,59 +27,69 @@ func NewStreamingController(c *entities.Config, l *zap.Logger) *StreamingControl
|
||||
}
|
||||
}
|
||||
|
||||
func (c *StreamingController) Stream(srtConnection *astisrt.Connection, videoTrack *webrtc.TrackLocalStaticSample, metadataTrack *webrtc.DataChannel) {
|
||||
func (c *StreamingController) Stream(sp entities.StreamParameters) {
|
||||
r, w := io.Pipe()
|
||||
|
||||
defer r.Close()
|
||||
defer w.Close()
|
||||
defer srtConnection.Close()
|
||||
defer sp.SRTConnection.Close()
|
||||
defer sp.WebRTCConn.Close()
|
||||
defer sp.Cancel()
|
||||
|
||||
c.l.Sugar().Infow("start streaming")
|
||||
|
||||
// TODO: pick the proper transport? is it possible to get rtp instead?
|
||||
go c.readFromSRTIntoWriterPipe(srtConnection, w)
|
||||
go c.readFromSRTIntoWriterPipe(sp.SRTConnection, w)
|
||||
|
||||
dmx := astits.NewDemuxer(context.Background(), r)
|
||||
// reading from reader pipe into mpeg-ts demuxer
|
||||
dmx := astits.NewDemuxer(sp.Ctx, r)
|
||||
eia608Reader := eia608.NewEIA608Reader()
|
||||
h264PID := uint16(0)
|
||||
|
||||
// reading from reader pipe
|
||||
for {
|
||||
d, err := dmx.NextData()
|
||||
if err != nil {
|
||||
c.l.Sugar().Errorw("failed to demux mpeg ts",
|
||||
"error", err,
|
||||
)
|
||||
break
|
||||
}
|
||||
|
||||
if d.PMT != nil {
|
||||
h264PID = c.captureMediaInfoAndSendToWebRTC(d, metadataTrack, h264PID)
|
||||
c.captureBitrateAndSendToWebRTC(d, metadataTrack)
|
||||
}
|
||||
|
||||
if d.PID == h264PID && d.PES != nil {
|
||||
if err = videoTrack.WriteSample(media.Sample{Data: d.PES.Data, Duration: time.Second / 30}); err != nil {
|
||||
c.l.Sugar().Errorw("failed to write a sample mpeg ts to web rtc",
|
||||
"error", err,
|
||||
)
|
||||
break
|
||||
}
|
||||
captions, err := eia608Reader.Parse(d.PES)
|
||||
select {
|
||||
case <-sp.Ctx.Done():
|
||||
c.l.Sugar().Errorw("stream was cancelled")
|
||||
return
|
||||
default:
|
||||
d, err := dmx.NextData()
|
||||
if err != nil {
|
||||
c.l.Sugar().Errorw("failed to parse eia 608",
|
||||
c.l.Sugar().Errorw("failed to demux mpeg ts",
|
||||
"error", err,
|
||||
)
|
||||
break
|
||||
}
|
||||
if captions != "" {
|
||||
captionsMsg, err := eia608.BuildCaptionsMessage(d.PES.Header.OptionalHeader.PTS, captions)
|
||||
if err != nil {
|
||||
c.l.Sugar().Errorw("failed to build captions message",
|
||||
|
||||
if d.PMT != nil {
|
||||
h264PID = c.captureMediaInfoAndSendToWebRTC(d, sp.MetadataTrack, h264PID)
|
||||
c.captureBitrateAndSendToWebRTC(d, sp.MetadataTrack)
|
||||
}
|
||||
|
||||
if d.PID == h264PID && d.PES != nil {
|
||||
// writing video from mpeg-ts into webrtc
|
||||
if err = sp.VideoTrack.WriteSample(media.Sample{Data: d.PES.Data, Duration: time.Second / 30}); err != nil {
|
||||
c.l.Sugar().Errorw("failed to write a sample mpeg ts to web rtc",
|
||||
"error", err,
|
||||
)
|
||||
break
|
||||
}
|
||||
metadataTrack.SendText(captionsMsg)
|
||||
captions, err := eia608Reader.Parse(d.PES)
|
||||
if err != nil {
|
||||
c.l.Sugar().Errorw("failed to parse eia 608",
|
||||
"error", err,
|
||||
)
|
||||
break
|
||||
}
|
||||
if captions != "" {
|
||||
captionsMsg, err := eia608.BuildCaptionsMessage(d.PES.Header.OptionalHeader.PTS, captions)
|
||||
if err != nil {
|
||||
c.l.Sugar().Errorw("failed to build captions message",
|
||||
"error", err,
|
||||
)
|
||||
break
|
||||
}
|
||||
sp.MetadataTrack.SendText(captionsMsg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user