Skip to content

Commit

Permalink
Split segments by time instead of packets
Browse files Browse the repository at this point in the history
  • Loading branch information
Thor77 committed Nov 24, 2019
1 parent 131efb7 commit b25c1a3
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"strings"
"time"

"github.com/grafov/m3u8"
"github.com/nareix/joy4/av"
Expand All @@ -15,7 +16,7 @@ import (

const addr = ":1935"
const key = "test"
const packetsPerSegment = 100
const msPerSegment = 15000

// TODO: replace failln with println / switch to logrus to include stream name in msg

Expand Down Expand Up @@ -48,6 +49,7 @@ func main() {

i := 0
clientConnected := true
var lastPacketTime time.Duration = 0
for clientConnected {
// create new segment
segmentName := fmt.Sprintf("%s%04d.ts", streamName, i)
Expand All @@ -62,8 +64,10 @@ func main() {
log.Fatalln(err)
}
// write some data
packetCount := packetsPerSegment
for packetCount > 0 {
var segmentLength time.Duration = 0
//var lastPacketTime time.Duration = 0
var packetLength time.Duration = 0
for segmentLength.Milliseconds() < msPerSegment {
var packet av.Packet
if packet, err = conn.ReadPacket(); err != nil {
if err == io.EOF {
Expand All @@ -76,10 +80,9 @@ func main() {
if err = tsMuxer.WritePacket(packet); err != nil {
log.Fatalln(err)
}
if packet.IsKeyFrame {
fmt.Println("packet is keyframe")
packetCount--
}
packetLength = packet.Time - lastPacketTime
segmentLength += packetLength
lastPacketTime = packet.Time
}
// write trailer
if err := tsMuxer.WriteTrailer(); err != nil {
Expand All @@ -88,7 +91,7 @@ func main() {
log.Printf("Successfully wrote segment %s\n", segmentName)

// update playlist
playlist.Append(segmentName, 1.0, "")
playlist.Append(segmentName, segmentLength.Seconds(), "")
playlistFile, err := os.Create(fmt.Sprintf("%s.m3u8", streamName))
if err != nil {
log.Fatalln(err)
Expand Down

0 comments on commit b25c1a3

Please sign in to comment.