-
Notifications
You must be signed in to change notification settings - Fork 317
/
hlsSegment.go
76 lines (66 loc) · 1.81 KB
/
hlsSegment.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
package main
import (
"time"
"github.com/deepch/vdk/av"
)
//Segment struct
type Segment struct {
FPS int //Current fps
CurrentFragment *Fragment //CurrentFragment link
CurrentFragmentID int //CurrentFragment ID
Finish bool //Segment Ready
Duration time.Duration //Segment Duration
Time time.Time //Realtime EXT-X-PROGRAM-DATE-TIME
Fragment map[int]*Fragment //Fragment map
}
//NewSegment func
func (element *MuxerHLS) NewSegment() *Segment {
res := &Segment{
Fragment: make(map[int]*Fragment),
CurrentFragmentID: -1, //Default fragment -1
}
//Increase MSN
element.MSN++
element.Segments[element.MSN] = res
return res
}
//GetDuration func
func (element *Segment) GetDuration() time.Duration {
return element.Duration
}
//SetFPS func
func (element *Segment) SetFPS(fps int) {
element.FPS = fps
}
//WritePacket func
func (element *Segment) WritePacket(packet *av.Packet) {
if element.CurrentFragment == nil || element.CurrentFragment.GetDuration().Milliseconds() >= element.FragmentMS(element.FPS) {
if element.CurrentFragment != nil {
element.CurrentFragment.Close()
}
element.CurrentFragmentID++
element.CurrentFragment = element.NewFragment()
}
element.Duration += packet.Duration
element.CurrentFragment.WritePacket(packet)
}
//GetFragmentID func
func (element *Segment) GetFragmentID() int {
return element.CurrentFragmentID
}
//Close segment func
func (element *Segment) Close() {
element.Finish = true
if element.CurrentFragment != nil {
element.CurrentFragment.Close()
}
}
//FragmentMS func
func (element *Segment) FragmentMS(fps int) int64 {
for i := 6; i >= 1; i-- {
if fps%i == 0 {
return int64(float64(1000) / float64(fps) * float64(i))
}
}
return 100
}