diff --git a/internal/hls/primaryplaylist.go b/internal/hls/primaryplaylist.go index 6633c1b1..01316fa2 100644 --- a/internal/hls/primaryplaylist.go +++ b/internal/hls/primaryplaylist.go @@ -14,7 +14,7 @@ type primaryPlaylist struct { audioTrack *gortsplib.Track h264Conf *gortsplib.TrackConfigH264 - breader *bytes.Reader + cnt []byte } func newPrimaryPlaylist( @@ -38,15 +38,13 @@ func newPrimaryPlaylist( codecs = append(codecs, "mp4a.40.2") } - cnt := "#EXTM3U\n" - cnt += "#EXT-X-STREAM-INF:BANDWIDTH=200000,CODECS=\"" + strings.Join(codecs, ",") + "\"\n" - cnt += "stream.m3u8\n" - - p.breader = bytes.NewReader([]byte(cnt)) + p.cnt = []byte("#EXTM3U\n" + + "#EXT-X-STREAM-INF:BANDWIDTH=200000,CODECS=\"" + strings.Join(codecs, ",") + "\"\n" + + "stream.m3u8\n") return p } func (p *primaryPlaylist) reader() io.Reader { - return p.breader + return bytes.NewReader(p.cnt) } diff --git a/internal/hls/streamplaylist.go b/internal/hls/streamplaylist.go index 8de36819..9fc3a9c3 100644 --- a/internal/hls/streamplaylist.go +++ b/internal/hls/streamplaylist.go @@ -9,15 +9,14 @@ import ( "sync" ) -type readerFunc struct { - wrapped func() []byte - reader *bytes.Reader +type asyncReader struct { + generator func() []byte + reader *bytes.Reader } -func (r *readerFunc) Read(buf []byte) (int, error) { +func (r *asyncReader) Read(buf []byte) (int, error) { if r.reader == nil { - cnt := r.wrapped() - r.reader = bytes.NewReader(cnt) + r.reader = bytes.NewReader(r.generator()) } return r.reader.Read(buf) } @@ -53,7 +52,7 @@ func (p *streamPlaylist) close() { } func (p *streamPlaylist) reader() io.Reader { - return &readerFunc{wrapped: func() []byte { + return &asyncReader{generator: func() []byte { p.mutex.Lock() defer p.mutex.Unlock()