I am trying to write a proxy server where I will convert a video file to live stream. A vod file consists for multiple child manifest files of different bit rates. each child manifest consist of multiple ts segments say 4 second each.for simplicity I have created a dummy map of 2 child manifests containing 4 ts segments each. My task is to create an live stream infinitely in for loop. so I am sending each ts segment after 4 seconds in go routine and writing to channel and I have one more go routine where I am reading from this go routine and writing to output string. once all segments are over I am updating to loopEnd
channel and start again. There are 2 APIs for different bit rates that gives me new ts after each 4 seconds.
package main
import (
"fmt"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"sync"
"time"
)
func main() {
vodToLiveMap := convertVodToLive()
engine := gin.Default()
engine.Use(gzip.Gzip(gzip.DefaultCompression))
engine.GET("/134200", func(gctx *gin.Context) {
prepareResponse(gctx, "134200", vodToLiveMap)
})
engine.GET("/290400", func(gctx *gin.Context) {
prepareResponse(gctx, "290400", vodToLiveMap)
})
engine.Run() // listen and serve on 0.0.0.0:8080
}
// this represents one ulr for http live stream
type hlsEntry struct {
timeCode int // timecode in microseconds
body string // string including transport segment URL, timecode
}
type VodToLive struct {
SequenceNumber int
Out chan string
CurrentStream string
}
func convertVodToLive() sync.Map {
var hlsEntriesBitrate1 = []hlsEntry{
{
timeCode: 4000000,
body: "#EXTINF:4.00000,
/master_Layer1_001.ts
",
},
{
timeCode: 4000000,
body: "#EXTINF:4.00000,
/master_Layer1_002.ts
",
},
{
timeCode: 4000000,
body: "#EXTINF:4.00000,
/master_Layer1_003.ts
",
},
{
timeCode: 4000000,
body: "#EXTINF:4.00000,
/master_Layer1_004.ts
",
},
}
var hlsEntriesBitrate2 = []hlsEntry{
{
timeCode: 4000000,
body: "#EXTINF:4.00000,
/master_Layer2_001.ts
",
},
{
timeCode: 4000000,
body: "#EXTINF:4.00000,
/master_Layer2_002.ts
",
},
{
timeCode: 4000000,
body: "#EXTINF:4.00000,
/master_Layer2_003.ts
",
},
{
timeCode: 4000000,
body: "#EXTINF:4.00000,
/master_Layer2_004.ts
",
},
}
fmt.Println(hlsEntriesBitrate2)
var hlsEntriesMap = make(map[string][]hlsEntry)
hlsEntriesMap["134200"] = hlsEntriesBitrate1
hlsEntriesMap["290400"] = hlsEntriesBitrate2
outputString := ""
vodToLiveMap := sync.Map{}
i := 0
//Iterate for all child manifest files of different bit rates
for childURLSlug := range hlsEntriesMap {
vodToLive := &VodToLive{}
vodToLive.SequenceNumber = 1
vodToLive.Out = make(chan string)
vodToLiveMap.Store(childURLSlug, vodToLive)
loopEnd := make(chan bool)
//Writing to vodToLive for a childURLSlug
go func(childURLSlug string) {
for {
for _, entry := range hlsEntriesMap[childURLSlug] {
select {
case <-time.After(time.Duration(entry.timeCode) * time.Microsecond):
vodToLive.SequenceNumber++
vodToLive.Out <- entry.body
}
}
loopEnd <- true
fmt.Println(childURLSlug)
}
}(childURLSlug)
//Reading from vodToLive's Out channel for a childURLSlug to update CurrentStream
go func(childURLSlug string) {
for {
for {
select {
//If video reaches end of stream then restart stream from Sequence #1
case <-loopEnd:
vodToLive.SequenceNumber = 1
//time.Sleep(10 * time.Second)
vodToLive.CurrentStream = ""
break
default:
outputString = <-vodToLive.Out
fmt.Println(outputString)
vodToLive.CurrentStream = vodToLive.CurrentStream + outputString
}
}
}
}(childURLSlug)
i++
}
return vodToLiveMap
}
func prepareResponse(gctx *gin.Context, childURLSlug string, vodToLiveMap sync.Map) {
vodToLive, _ := vodToLiveMap.Load(childURLSlug)
gctx.Header("Content-Type", "application/x-mpegURL")
response := `#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:4
#EXT-X-MEDIA-SEQUENCE:` + "1" + "
" + vodToLive.(*VodToLive).CurrentStream
size := len(response)
if size > 0 && response[size-1] == '
' {
response = response[:size-1]
}
gctx.String(200, response)
}
//curl localhost:8080/134200
//curl localhost:8080/290400
But problem here is that I am not getting all ts segments in curl request and sometimes even ts are not updated after the end of loop. I shows all 4 ts segments in each call but these should be reset after each endLoop.
This is fixed just by replacing default with a select on a channel(vodToLive.Out
) and adding 4 seconds wait in case <-loopEnd
go func(childURLSlug string) {
for {
for {
select {
//If video reaches end of stream then restart stream from Sequence #1
case <-loopEnd:
vodToLive.SequenceNumber = 1
// so this will ensure that next stream starts only when first is complete.
time.Sleep(4 * time.Second) //Usually a ts is 4 seconds length.
vodToLive.CurrentStream = ""
break
case res := <-vodToLive.Out:
outputString := res
fmt.Println(outputString)
vodToLive.CurrentStream = vodToLive.CurrentStream + outputString
}
}
}
}(childURLSlug)