when alwaysAvailableFile points to a file with a H265 or H264 track, server is started, an online stream is published and then closed, video was getting corrupted since the online video was overriding the parameters of the offline video.
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package stream
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
func multiplyAndDivide2(v, m, d time.Duration) time.Duration {
|
|
secs := v / d
|
|
dec := v % d
|
|
return (secs*m + dec*m/d)
|
|
}
|
|
|
|
type offlineSubStream struct {
|
|
stream *Stream
|
|
|
|
subStream *SubStream
|
|
ctx context.Context
|
|
ctxCancel func()
|
|
wg sync.WaitGroup
|
|
tracks []*offlineSubStreamTrack
|
|
}
|
|
|
|
func (o *offlineSubStream) initialize() error {
|
|
o.subStream = &SubStream{
|
|
Stream: o.stream,
|
|
CurDesc: o.stream.offlineDesc,
|
|
UseRTPPackets: false,
|
|
}
|
|
err := o.subStream.Initialize()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
o.ctx, o.ctxCancel = context.WithCancel(context.Background())
|
|
|
|
pos := 0
|
|
o.tracks = make([]*offlineSubStreamTrack, len(o.subStream.CurDesc.Medias))
|
|
|
|
for _, media := range o.subStream.CurDesc.Medias {
|
|
for _, forma := range media.Formats {
|
|
t := &offlineSubStreamTrack{
|
|
wg: &o.wg,
|
|
file: o.stream.AlwaysAvailableFile,
|
|
pos: pos,
|
|
ctx: o.ctx,
|
|
subStream: o.subStream,
|
|
media: media,
|
|
format: forma,
|
|
}
|
|
t.initialize()
|
|
o.tracks[pos] = t
|
|
pos++
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (o *offlineSubStream) close(waitLastSample bool) {
|
|
for _, track := range o.tracks {
|
|
track.waitLastSample = waitLastSample
|
|
}
|
|
|
|
o.ctxCancel()
|
|
o.wg.Wait()
|
|
}
|