forked from bluenviron/gohlslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_track_processor_fmp4.go
147 lines (118 loc) · 3.4 KB
/
client_track_processor_fmp4.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package gohlslib
import (
"context"
"fmt"
"time"
"github.com/IgorDeforges/gohlslib/pkg/codecs"
"github.com/bluenviron/mediacommon/pkg/formats/fmp4"
)
type clientTrackProcessorFMP4 struct {
track *Track
onData interface{}
timeScale uint32
timeSync *clientTimeSyncFMP4
onPartTrackProcessed func(ctx context.Context)
postProcess func(pts time.Duration, dts time.Duration, sample *fmp4.PartSample) error
// in
queue chan *fmp4.PartTrack
}
func (t *clientTrackProcessorFMP4) initialize() error {
switch t.track.Codec.(type) {
case *codecs.AV1:
var onDataCasted ClientOnDataAV1Func = func(_ time.Duration, _ [][]byte) {}
if t.onData != nil {
onDataCasted = t.onData.(ClientOnDataAV1Func)
}
t.postProcess = func(pts time.Duration, dts time.Duration, sample *fmp4.PartSample) error {
tu, err := sample.GetAV1()
if err != nil {
return err
}
onDataCasted(pts, tu)
return nil
}
case *codecs.VP9:
var onDataCasted ClientOnDataVP9Func = func(_ time.Duration, _ []byte) {}
if t.onData != nil {
onDataCasted = t.onData.(ClientOnDataVP9Func)
}
t.postProcess = func(pts time.Duration, dts time.Duration, sample *fmp4.PartSample) error {
onDataCasted(pts, sample.Payload)
return nil
}
case *codecs.H265, *codecs.H264:
var onDataCasted ClientOnDataH26xFunc = func(_ time.Duration, _ time.Duration, _ [][]byte) {}
if t.onData != nil {
onDataCasted = t.onData.(ClientOnDataH26xFunc)
}
t.postProcess = func(pts time.Duration, dts time.Duration, sample *fmp4.PartSample) error {
au, err := sample.GetH26x()
if err != nil {
return err
}
onDataCasted(pts, dts, au)
return nil
}
case *codecs.Opus:
var onDataCasted ClientOnDataOpusFunc = func(_ time.Duration, _ [][]byte) {}
if t.onData != nil {
onDataCasted = t.onData.(ClientOnDataOpusFunc)
}
t.postProcess = func(pts time.Duration, dts time.Duration, sample *fmp4.PartSample) error {
onDataCasted(pts, [][]byte{sample.Payload})
return nil
}
case *codecs.MPEG4Audio:
var onDataCasted ClientOnDataMPEG4AudioFunc = func(_ time.Duration, _ [][]byte) {}
if t.onData != nil {
onDataCasted = t.onData.(ClientOnDataMPEG4AudioFunc)
}
t.postProcess = func(pts time.Duration, dts time.Duration, sample *fmp4.PartSample) error {
onDataCasted(pts, [][]byte{sample.Payload})
return nil
}
}
t.queue = make(chan *fmp4.PartTrack)
return nil
}
func (t *clientTrackProcessorFMP4) run(ctx context.Context) error {
for {
select {
case partTrack := <-t.queue:
err := t.process(ctx, partTrack)
if err != nil {
return err
}
case <-ctx.Done():
return nil
}
}
}
func (t *clientTrackProcessorFMP4) process(ctx context.Context, partTrack *fmp4.PartTrack) error {
rawDTS := partTrack.BaseTime
for _, sample := range partTrack.Samples {
pts, dts, err := t.timeSync.convertAndSync(ctx, t.timeScale, rawDTS, sample.PTSOffset)
if err != nil {
return err
}
rawDTS += uint64(sample.Duration)
// silently discard packets prior to the first packet of the leading track
if pts < 0 {
continue
}
err = t.postProcess(pts, dts, sample)
if err != nil {
return err
}
}
t.onPartTrackProcessed(ctx)
return nil
}
func (t *clientTrackProcessorFMP4) push(ctx context.Context, partTrack *fmp4.PartTrack) error {
select {
case t.queue <- partTrack:
return nil
case <-ctx.Done():
return fmt.Errorf("terminated")
}
}