-
Notifications
You must be signed in to change notification settings - Fork 3
/
filters.go
48 lines (36 loc) · 1.09 KB
/
filters.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 main
import (
"log"
"time"
"github.com/nareix/joy4/av"
)
// CalcBitrate calculates bitrate and stores it in a Restream
type CalcBitrate struct {
Bitrate float64
bitCnt int64
lastTime int64
Restream *Restream
Endpoint *Endpoint
}
func (self *CalcBitrate) ModifyPacket(pkt *av.Packet, streams []av.CodecData, videoidx int, audioidx int) (drop bool, err error) {
// Sample abt. every half second
if self.lastTime+(5e8) < time.Now().UnixNano() {
timeDelta := ((float64(time.Now().UnixNano()) - float64(self.lastTime)) * 1e-9)
self.Bitrate = float64(self.bitCnt/1000) / timeDelta
if self.Restream != nil {
self.Restream.Stats.Bitrate = self.Bitrate
}
if self.Endpoint != nil {
log.Fatal("Endpoint bitrate calculation not yet implemented")
self.Endpoint.Stats.Bitrate = self.Bitrate
}
log.Printf("%s bitrate %.3f kb/s\n", self.Restream.Name, self.Bitrate)
self.lastTime = time.Now().UnixNano()
self.bitCnt = 0
}
// TODO: Pretty sure this is somewhat incorrect
// values seem to be off by about 700 kb/s
self.bitCnt += int64(len(pkt.Data))
drop = false
return
}