forked from bluenviron/gohlslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_timesync_mpegts.go
48 lines (40 loc) · 962 Bytes
/
client_timesync_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
package gohlslib
import (
"context"
"fmt"
"sync"
"time"
"github.com/bluenviron/mediacommon/pkg/formats/mpegts"
)
type clientTimeSyncMPEGTS struct {
startRTC time.Time
td *mpegts.TimeDecoder
mutex sync.Mutex
}
func newClientTimeSyncMPEGTS(startDTS int64) *clientTimeSyncMPEGTS {
return &clientTimeSyncMPEGTS{
startRTC: time.Now(),
td: mpegts.NewTimeDecoder(startDTS),
}
}
func (ts *clientTimeSyncMPEGTS) convertAndSync(ctx context.Context,
rawPTS int64, rawDTS int64,
) (time.Duration, time.Duration, error) {
ts.mutex.Lock()
pts := ts.td.Decode(rawPTS)
dts := ts.td.Decode(rawDTS)
ts.mutex.Unlock()
elapsed := time.Since(ts.startRTC)
if dts > elapsed {
diff := dts - elapsed
if diff > clientMaxDTSRTCDiff {
return 0, 0, fmt.Errorf("difference between DTS and RTC is too big")
}
select {
case <-time.After(diff):
case <-ctx.Done():
return 0, 0, fmt.Errorf("terminated")
}
}
return pts, dts, nil
}