-
Notifications
You must be signed in to change notification settings - Fork 27
/
client_track_processor_fmp4.go
113 lines (89 loc) · 2.26 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
package gohlslib
import (
"context"
"fmt"
"time"
"github.com/bluenviron/gohlslib/v2/pkg/codecs"
"github.com/bluenviron/mediacommon/pkg/formats/fmp4"
)
type procEntryFMP4 struct {
partTrack *fmp4.PartTrack
dts int64
ntp *time.Time
}
type clientTrackProcessorFMP4 struct {
track *clientTrack
onPartTrackProcessed func(ctx context.Context)
decodePayload func(sample *fmp4.PartSample) ([][]byte, error)
// in
queue chan *procEntryFMP4
}
func (t *clientTrackProcessorFMP4) initialize() error {
switch t.track.track.Codec.(type) {
case *codecs.AV1:
t.decodePayload = func(sample *fmp4.PartSample) ([][]byte, error) {
return sample.GetAV1()
}
case *codecs.VP9:
t.decodePayload = func(sample *fmp4.PartSample) ([][]byte, error) {
return [][]byte{sample.Payload}, nil
}
case *codecs.H265, *codecs.H264:
t.decodePayload = func(sample *fmp4.PartSample) ([][]byte, error) {
return sample.GetH26x()
}
case *codecs.Opus:
t.decodePayload = func(sample *fmp4.PartSample) ([][]byte, error) {
return [][]byte{sample.Payload}, nil
}
case *codecs.MPEG4Audio:
t.decodePayload = func(sample *fmp4.PartSample) ([][]byte, error) {
return [][]byte{sample.Payload}, nil
}
}
t.queue = make(chan *procEntryFMP4)
return nil
}
func (t *clientTrackProcessorFMP4) run(ctx context.Context) error {
for {
select {
case entry := <-t.queue:
err := t.process(ctx, entry)
if err != nil {
return err
}
case <-ctx.Done():
return nil
}
}
}
func (t *clientTrackProcessorFMP4) process(ctx context.Context, entry *procEntryFMP4) error {
dts := entry.dts
for _, sample := range entry.partTrack.Samples {
data, err := t.decodePayload(sample)
if err != nil {
return err
}
pts := dts + int64(sample.PTSOffset)
var ntp *time.Time
if entry.ntp != nil {
v := entry.ntp.Add(timestampToDuration(dts-entry.dts, t.track.track.ClockRate))
ntp = &v
}
err = t.track.handleData(ctx, pts, dts, ntp, data)
if err != nil {
return err
}
dts += int64(sample.Duration)
}
t.onPartTrackProcessed(ctx)
return nil
}
func (t *clientTrackProcessorFMP4) push(ctx context.Context, entry *procEntryFMP4) error {
select {
case t.queue <- entry:
return nil
case <-ctx.Done():
return fmt.Errorf("terminated")
}
}