forked from nareix/joy4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (152 loc) · 3.83 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"fmt"
"github.com/ikmak/joy4/av"
"github.com/ikmak/joy4/av/avutil"
"github.com/ikmak/joy4/av/pktque"
"github.com/ikmak/joy4/av/pubsub"
"github.com/ikmak/joy4/format"
"github.com/ikmak/joy4/format/rtmp"
"sync"
"time"
)
func init() {
format.RegisterAll()
}
type FrameDropper struct {
Interval int
n int
skipping bool
DelaySkip time.Duration
lasttime time.Time
lastpkttime time.Duration
delay time.Duration
SkipInterval int
}
func (self *FrameDropper) ModifyPacket(pkt *av.Packet, streams []av.CodecData, videoidx int, audioidx int) (drop bool, err error) {
if self.DelaySkip != 0 && pkt.Idx == int8(videoidx) {
now := time.Now()
if !self.lasttime.IsZero() {
realdiff := now.Sub(self.lasttime)
pktdiff := pkt.Time - self.lastpkttime
self.delay += realdiff - pktdiff
}
self.lasttime = time.Now()
self.lastpkttime = pkt.Time
if !self.skipping {
if self.delay > self.DelaySkip {
self.skipping = true
self.delay = 0
}
} else {
if pkt.IsKeyFrame {
self.skipping = false
}
}
if self.skipping {
drop = true
}
if self.SkipInterval != 0 && pkt.IsKeyFrame {
if self.n == self.SkipInterval {
self.n = 0
self.skipping = true
}
self.n++
}
}
if self.Interval != 0 {
if self.n >= self.Interval && pkt.Idx == int8(videoidx) && !pkt.IsKeyFrame {
drop = true
self.n = 0
}
self.n++
}
return
}
func main() {
server := &rtmp.Server{}
l := &sync.RWMutex{}
type Channel struct {
que *pubsub.Queue
}
channels := map[string]*Channel{}
server.HandlePlay = func(conn *rtmp.Conn) {
l.RLock()
ch := channels[conn.URL.Path]
l.RUnlock()
if ch != nil {
cursor := ch.que.Latest()
query := conn.URL.Query()
if q := query.Get("delaygop"); q != "" {
n := 0
fmt.Sscanf(q, "%d", &n)
cursor = ch.que.DelayedGopCount(n)
} else if q := query.Get("delaytime"); q != "" {
dur, _ := time.ParseDuration(q)
cursor = ch.que.DelayedTime(dur)
}
filters := pktque.Filters{}
if q := query.Get("waitkey"); q != "" {
filters = append(filters, &pktque.WaitKeyFrame{})
}
filters = append(filters, &pktque.FixTime{StartFromZero: true, MakeIncrement: true})
if q := query.Get("framedrop"); q != "" {
n := 0
fmt.Sscanf(q, "%d", &n)
filters = append(filters, &FrameDropper{Interval: n})
}
if q := query.Get("delayskip"); q != "" {
dur, _ := time.ParseDuration(q)
skipper := &FrameDropper{DelaySkip: dur}
if q := query.Get("skipinterval"); q != "" {
n := 0
fmt.Sscanf(q, "%d", &n)
skipper.SkipInterval = n
}
filters = append(filters, skipper)
}
demuxer := &pktque.FilterDemuxer{
Filter: filters,
Demuxer: cursor,
}
avutil.CopyFile(conn, demuxer)
}
}
server.HandlePublish = func(conn *rtmp.Conn) {
l.Lock()
ch := channels[conn.URL.Path]
if ch == nil {
ch = &Channel{}
ch.que = pubsub.NewQueue()
query := conn.URL.Query()
if q := query.Get("cachegop"); q != "" {
var n int
fmt.Sscanf(q, "%d", &n)
ch.que.SetMaxGopCount(n)
}
channels[conn.URL.Path] = ch
} else {
ch = nil
}
l.Unlock()
if ch == nil {
return
}
avutil.CopyFile(ch.que, conn)
l.Lock()
delete(channels, conn.URL.Path)
l.Unlock()
ch.que.Close()
}
server.ListenAndServe()
// ffmpeg -re -i movie.flv -c copy -f flv rtmp://localhost/movie
// ffmpeg -f avfoundation -i "0:0" .... -f flv rtmp://localhost/screen
// with cache size options
// ffplay rtmp://localhost/movie
// ffplay rtmp://localhost/screen
// ffplay rtmp://localhost/movie?delaytime=5s
// ffplay rtmp://localhost/movie?delaytime=10s&waitkey=true
// ffplay rtmp://localhost/movie?delaytime=20s
// ffmpeg -re -i movie.flv -c copy -f flv rtmp://localhost/movie?cachegop=2
// ffmpeg -re -i movie.flv -c copy -f flv rtmp://localhost/movie?cachegop=1
}