-
Notifications
You must be signed in to change notification settings - Fork 27
/
client_track.go
54 lines (47 loc) · 1.01 KB
/
client_track.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
package gohlslib
import (
"context"
"fmt"
"time"
)
type clientTrack struct {
track *Track
onData clientOnDataFunc
lastAbsoluteTime *time.Time
startRTC time.Time
}
func (t *clientTrack) absoluteTime() (time.Time, bool) {
if t.lastAbsoluteTime == nil {
return zero, false
}
return *t.lastAbsoluteTime, true
}
func (t *clientTrack) handleData(
ctx context.Context,
pts int64,
dts int64,
ntp *time.Time,
data [][]byte,
) error {
// silently discard packets prior to the first packet of the leading track
if pts < 0 {
return nil
}
// synchronize time
elapsed := time.Since(t.startRTC)
dtsDuration := timestampToDuration(dts, t.track.ClockRate)
if dtsDuration > elapsed {
diff := dtsDuration - elapsed
if diff > clientMaxDTSRTCDiff {
return fmt.Errorf("difference between DTS and RTC is too big")
}
select {
case <-time.After(diff):
case <-ctx.Done():
return fmt.Errorf("terminated")
}
}
t.lastAbsoluteTime = ntp
t.onData(pts, dts, data)
return nil
}