Skip to content

Commit

Permalink
update mediacommon; replace Client.OnData with OnDataH26x, OnDataMPEG…
Browse files Browse the repository at this point in the history
…4Audio, OnDataOpus
  • Loading branch information
aler9 committed Jul 27, 2023
1 parent a0c63e1 commit 07ac215
Show file tree
Hide file tree
Showing 23 changed files with 513 additions and 412 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ General features:
* [Examples](#examples)
* [API Documentation](#api-documentation)
* [Standards](#standards)
* [Links](#links)
* [Related projects](#related-projects)

## Examples

Expand All @@ -72,8 +72,8 @@ https://pkg.go.dev/github.com/bluenviron/gohlslib#pkg-index
* [Codec standards](https://github.com/bluenviron/mediacommon#standards)
* [Golang project layout](https://github.com/golang-standards/project-layout)

## Links

Related projects
## Related projects

* [MediaMTX](https://github.com/bluenviron/mediamtx)
* [gortsplib](https://github.com/bluenviron/gortsplib)
* [mediacommon](https://github.com/bluenviron/mediacommon)
30 changes: 23 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ import (
const (
clientMPEGTSEntryQueueSize = 100
clientFMP4MaxPartTracksPerSegment = 200
clientLiveStartingInvPosition = 3
clientLiveMaxInvPosition = 5
clientLiveInitialDistance = 3
clientLiveMaxDistanceFromEnd = 5
clientMaxDTSRTCDiff = 10 * time.Second
)

type onDataH26xFunc func(pts time.Duration, dts time.Duration, au [][]byte)

type onDataMPEG4AudioFunc func(pts time.Duration, dts time.Duration, aus [][]byte)

type onDataOpusFunc func(pts time.Duration, dts time.Duration, packets [][]byte)

func clientAbsoluteURL(base *url.URL, relative string) (*url.URL, error) {
u, err := url.Parse(relative)
if err != nil {
Expand Down Expand Up @@ -50,7 +56,7 @@ type Client struct {
ctx context.Context
ctxCancel func()
onTracks func([]*Track) error
onData map[*Track]func(time.Duration, interface{})
onData map[*Track]interface{}
playlistURL *url.URL

// out
Expand All @@ -74,7 +80,7 @@ func (c *Client) Start() error {

c.ctx, c.ctxCancel = context.WithCancel(context.Background())

c.onData = make(map[*Track]func(time.Duration, interface{}))
c.onData = make(map[*Track]interface{})
c.outErr = make(chan error, 1)

go c.run()
Expand All @@ -97,9 +103,19 @@ func (c *Client) OnTracks(cb func([]*Track) error) {
c.onTracks = cb
}

// OnData sets a callback that is called when data arrives.
func (c *Client) OnData(forma *Track, cb func(time.Duration, interface{})) {
c.onData[forma] = cb
// OnDataH26x sets a callback that is called when data from an H26x track is received.
func (c *Client) OnDataH26x(forma *Track, onData onDataH26xFunc) {
c.onData[forma] = onData
}

// OnDataMPEG4Audio sets a callback that is called when data from a MPEG-4 Audio track is received.
func (c *Client) OnDataMPEG4Audio(forma *Track, onData onDataMPEG4AudioFunc) {
c.onData[forma] = onData
}

// OnDataOpus sets a callback that is called when data from an Opus track is received.
func (c *Client) OnDataOpus(forma *Track, onData onDataOpusFunc) {
c.onData[forma] = onData
}

func (c *Client) run() {
Expand Down
5 changes: 2 additions & 3 deletions client_downloader_primary.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"net/url"
"strings"
"time"

"github.com/bluenviron/gohlslib/pkg/playlist"
)
Expand Down Expand Up @@ -104,7 +103,7 @@ type clientDownloaderPrimary struct {
httpClient *http.Client
log LogFunc
onTracks func([]*Track) error
onData map[*Track]func(time.Duration, interface{})
onData map[*Track]interface{}
rp *clientRoutinePool

leadingTimeSync clientTimeSync
Expand All @@ -123,7 +122,7 @@ func newClientDownloaderPrimary(
log LogFunc,
rp *clientRoutinePool,
onTracks func([]*Track) error,
onData map[*Track]func(time.Duration, interface{}),
onData map[*Track]interface{},
) *clientDownloaderPrimary {
return &clientDownloaderPrimary{
primaryPlaylistURL: primaryPlaylistURL,
Expand Down
15 changes: 7 additions & 8 deletions client_downloader_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"net/url"
"strconv"
"time"

"github.com/bluenviron/gohlslib/pkg/playlist"
)
Expand Down Expand Up @@ -40,7 +39,7 @@ type clientDownloaderStream struct {
onStreamTracks func(context.Context, []*Track) bool
onSetLeadingTimeSync func(clientTimeSync)
onGetLeadingTimeSync func(context.Context) (clientTimeSync, bool)
onData map[*Track]func(time.Duration, interface{})
onData map[*Track]interface{}

curSegmentID *int
}
Expand All @@ -55,7 +54,7 @@ func newClientDownloaderStream(
onStreamTracks func(context.Context, []*Track) bool,
onSetLeadingTimeSync func(clientTimeSync),
onGetLeadingTimeSync func(context.Context) (clientTimeSync, bool),
onData map[*Track]func(time.Duration, interface{}),
onData map[*Track]interface{},
) *clientDownloaderStream {
return &clientDownloaderStream{
isLeading: isLeading,
Expand Down Expand Up @@ -216,8 +215,8 @@ func (d *clientDownloaderStream) fillSegmentQueue(
var segPos int

if d.curSegmentID == nil {
if !pl.Endlist { // live stream: start from clientLiveStartingInvPosition
seg, segPos = findSegmentWithInvPosition(pl.Segments, clientLiveStartingInvPosition)
if !pl.Endlist { // live stream: start from clientLiveInitialDistance
seg, segPos = findSegmentWithInvPosition(pl.Segments, clientLiveInitialDistance)
if seg == nil {
return fmt.Errorf("there aren't enough segments to fill the buffer")
}
Expand All @@ -231,12 +230,12 @@ func (d *clientDownloaderStream) fillSegmentQueue(
var invPos int
seg, segPos, invPos = findSegmentWithID(pl.MediaSequence, pl.Segments, *d.curSegmentID+1)
if seg == nil {
return fmt.Errorf("following segment not found or not ready yet")
return fmt.Errorf("next segment not found or not ready yet")
}

d.log(LogLevelDebug, "segment inverse position: %d", invPos)
d.log(LogLevelDebug, "distance of next segment from end of playlist: %d", invPos)

if !pl.Endlist && invPos > clientLiveMaxInvPosition {
if !pl.Endlist && invPos > clientLiveMaxDistanceFromEnd {
return fmt.Errorf("playback is too late")
}
}
Expand Down
Loading

0 comments on commit 07ac215

Please sign in to comment.