-
Notifications
You must be signed in to change notification settings - Fork 28
/
client_time_conv_mpegts.go
71 lines (55 loc) · 1.28 KB
/
client_time_conv_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
package gohlslib
import (
"context"
"sync"
"time"
"github.com/bluenviron/mediacommon/pkg/formats/mpegts"
)
type clientTimeConvMPEGTS struct {
startDTS int64
mutex sync.Mutex
td *mpegts.TimeDecoder2
ntpAvailable bool
ntpValue time.Time
ntpTimestamp int64
chLeadingNTPReceived chan struct{}
}
func (ts *clientTimeConvMPEGTS) initialize() {
ts.td = mpegts.NewTimeDecoder2()
ts.td.Decode(ts.startDTS)
ts.chLeadingNTPReceived = make(chan struct{})
}
func (ts *clientTimeConvMPEGTS) convert(v int64) int64 {
ts.mutex.Lock()
defer ts.mutex.Unlock()
return ts.td.Decode(v)
}
func (ts *clientTimeConvMPEGTS) setNTP(value time.Time, timestamp int64) {
ts.mutex.Lock()
defer ts.mutex.Unlock()
ts.ntpAvailable = true
ts.ntpValue = value
ts.ntpTimestamp = timestamp
}
func (ts *clientTimeConvMPEGTS) setLeadingNTPReceived() {
select {
case <-ts.chLeadingNTPReceived:
return
default:
}
close(ts.chLeadingNTPReceived)
}
func (ts *clientTimeConvMPEGTS) getNTP(ctx context.Context, timestamp int64) *time.Time {
select {
case <-ts.chLeadingNTPReceived:
case <-ctx.Done():
return nil
}
ts.mutex.Lock()
defer ts.mutex.Unlock()
if !ts.ntpAvailable {
return nil
}
v := ts.ntpValue.Add(timestampToDuration(timestamp-ts.ntpTimestamp, 90000))
return &v
}