forked from bluenviron/gohlslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_track_processor_mpegts.go
97 lines (78 loc) · 2.06 KB
/
client_track_processor_mpegts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package gohlslib
import (
"context"
"fmt"
"time"
"github.com/IgorDeforges/gohlslib/pkg/codecs"
)
type mpegtsSample struct {
pts int64
dts int64
data [][]byte
}
type clientTrackProcessorMPEGTS struct {
track *Track
onData interface{}
timeSync *clientTimeSyncMPEGTS
onPartProcessorDone func(ctx context.Context)
postProcess func(pts time.Duration, dts time.Duration, data [][]byte)
queue chan *mpegtsSample
}
func (t *clientTrackProcessorMPEGTS) initialize() {
switch t.track.Codec.(type) {
case *codecs.H264:
var onDataCasted ClientOnDataH26xFunc = func(pts time.Duration, dts time.Duration, au [][]byte) {}
if t.onData != nil {
onDataCasted = t.onData.(ClientOnDataH26xFunc)
}
t.postProcess = func(pts time.Duration, dts time.Duration, data [][]byte) {
onDataCasted(pts, dts, data)
}
case *codecs.MPEG4Audio:
var onDataCasted ClientOnDataMPEG4AudioFunc = func(pts time.Duration, aus [][]byte) {}
if t.onData != nil {
onDataCasted = t.onData.(ClientOnDataMPEG4AudioFunc)
}
t.postProcess = func(pts time.Duration, dts time.Duration, data [][]byte) {
onDataCasted(pts, data)
}
}
t.queue = make(chan *mpegtsSample, clientMPEGTSSampleQueueSize)
}
func (t *clientTrackProcessorMPEGTS) run(ctx context.Context) error {
for {
select {
case sample := <-t.queue:
err := t.process(ctx, sample)
if err != nil {
return err
}
case <-ctx.Done():
return nil
}
}
}
func (t *clientTrackProcessorMPEGTS) process(ctx context.Context, sample *mpegtsSample) error {
if sample == nil {
t.onPartProcessorDone(ctx)
return nil
}
pts, dts, err := t.timeSync.convertAndSync(ctx, sample.pts, sample.dts)
if err != nil {
return err
}
// silently discard packets prior to the first packet of the leading track
if pts < 0 {
return nil
}
t.postProcess(pts, dts, sample.data)
return nil
}
func (t *clientTrackProcessorMPEGTS) push(ctx context.Context, sample *mpegtsSample) error {
select {
case t.queue <- sample:
return nil
case <-ctx.Done():
return fmt.Errorf("terminated")
}
}