Skip to content

Commit

Permalink
Add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Thor77 committed Nov 26, 2019
1 parent 9abb3ab commit 0f85150
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func loadConfig(path string) (Config, error) {
return tmpConfig, err
}

// set default values
if tmpConfig.Addr == "" {
tmpConfig.Addr = ":1935"
}
Expand All @@ -28,5 +29,6 @@ func loadConfig(path string) (Config, error) {
if tmpConfig.MsPerSegment == 0 {
tmpConfig.MsPerSegment = 15000
}

return tmpConfig, nil
}
20 changes: 15 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ import (
)

func removeOutdatedSegments(streamLogger *log.Entry, streamName string, playlist *m3u8.MediaPlaylist) error {
// write all playlist segment URIs into map
currentSegments := make(map[string]struct{}, len(playlist.Segments))
for _, segment := range playlist.Segments {
if segment != nil {
currentSegments[segment.URI] = struct{}{}
}
}
// find (probably) segment files in current directory
segmentFiles, err := filepath.Glob(fmt.Sprintf("%s*.ts", streamName))
if err != nil {
return err
}
for _, segmentFile := range segmentFiles {
// check if file belongs to a playlist segment
if _, ok := currentSegments[segmentFile]; !ok {
if err := os.Remove(segmentFile); err != nil {
streamLogger.Errorln(err)
Expand All @@ -40,11 +43,14 @@ func removeOutdatedSegments(streamLogger *log.Entry, streamName string, playlist

func publishHandler(conn *rtmp.Conn) {
log.Debugf("Handling request %s\n", conn.URL.RequestURI())

// verify key
if conn.URL.Query().Get("key") != config.Key {
log.Errorln("Key mismatch, aborting request")
return
}

// verify stream has a name
streamName := strings.ReplaceAll(conn.URL.Path, "/", "")
if streamName == "" {
log.Errorln("Invalid stream name")
Expand Down Expand Up @@ -73,7 +79,7 @@ func publishHandler(conn *rtmp.Conn) {
clientConnected := true
var lastPacketTime time.Duration = 0
for clientConnected {
// create new segment
// create new segment file
segmentName := fmt.Sprintf("%s%04d.ts", streamName, i)
outFile, err := os.Create(segmentName)
if err != nil {
Expand All @@ -87,11 +93,12 @@ func publishHandler(conn *rtmp.Conn) {
streamLogger.Errorln(err)
return
}
// write some data

// write packets
var segmentLength time.Duration = 0
//var lastPacketTime time.Duration = 0
var packetLength time.Duration = 0
for segmentLength.Milliseconds() < config.MsPerSegment {
// read packet from source
var packet av.Packet
if packet, err = conn.ReadPacket(); err != nil {
if err == io.EOF {
Expand All @@ -102,10 +109,13 @@ func publishHandler(conn *rtmp.Conn) {
streamLogger.Errorln(err)
return
}
// write packet to destination
if err = tsMuxer.WritePacket(packet); err != nil {
streamLogger.Errorln(err)
return
}

// calculate segment length
packetLength = packet.Time - lastPacketTime
segmentLength += packetLength
lastPacketTime = packet.Time
Expand Down Expand Up @@ -133,11 +143,11 @@ func publishHandler(conn *rtmp.Conn) {
return
}

// increase counter
// increase segment index
i++
}

// remove all segments; this is probably not a good idea
// remove all segments; this is probably a bad idea
for _, segment := range playlist.Segments {
if segment != nil {
if err := os.Remove(segment.URI); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ func main() {
FullTimestamp: true,
})

// initialize server
server := &rtmp.Server{Addr: config.Addr}
server.HandlePublish = publishHandler

log.Infof("Listening on %s\n", server.Addr)
server.ListenAndServe()
}

0 comments on commit 0f85150

Please sign in to comment.