-
Notifications
You must be signed in to change notification settings - Fork 28
/
client_time_conv_fmp4.go
69 lines (55 loc) · 1.33 KB
/
client_time_conv_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
package gohlslib
import (
"context"
"sync"
"time"
)
type clientTimeConvFMP4 struct {
leadingTimeScale int64
leadingBaseTime int64
mutex sync.Mutex
ntpAvailable bool
ntpValue time.Time
ntpTimestamp int64
ntpClockRate int
chLeadingNTPReceived chan struct{}
}
func (ts *clientTimeConvFMP4) initialize() {
ts.chLeadingNTPReceived = make(chan struct{})
}
func (ts *clientTimeConvFMP4) convert(v int64, clockRate int) int64 {
return v - multiplyAndDivide(ts.leadingBaseTime, int64(clockRate), ts.leadingTimeScale)
}
func (ts *clientTimeConvFMP4) setNTP(value time.Time, timestamp int64, clockRate int) {
ts.mutex.Lock()
defer ts.mutex.Unlock()
ts.ntpAvailable = true
ts.ntpValue = value
ts.ntpTimestamp = timestamp
ts.ntpClockRate = clockRate
}
func (ts *clientTimeConvFMP4) setLeadingNTPReceived() {
select {
case <-ts.chLeadingNTPReceived:
return
default:
}
close(ts.chLeadingNTPReceived)
}
func (ts *clientTimeConvFMP4) getNTP(ctx context.Context, timestamp int64, clockRate int) *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-multiplyAndDivide(ts.ntpTimestamp, int64(clockRate), int64(ts.ntpClockRate)),
clockRate))
return &v
}