Skip to content

Commit

Permalink
example: add delay skip
Browse files Browse the repository at this point in the history
  • Loading branch information
nareix committed Jul 17, 2016
1 parent a9a34f7 commit cc5d6d4
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions examples/rtmp_server_channels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,47 @@ func init() {
type FrameDropper struct {
Interval int
n int
skipping bool
DelaySkip time.Duration
lasttime time.Time
lastpkttime time.Duration
delay time.Duration
}

func (self *FrameDropper) ModifyPacket(pkt *av.Packet, streams []av.CodecData, videoidx int, audioidx int) (drop bool, err error) {
if self.Interval != 0 && self.n >= self.Interval && pkt.Idx == int8(videoidx) && !pkt.IsKeyFrame {
drop = true
self.n = 0
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
}
}
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
}

Expand Down Expand Up @@ -65,6 +98,10 @@ func main() {
fmt.Sscanf(q, "%d", &n)
filters = append(filters, &FrameDropper{Interval: n})
}
if q := query.Get("delayskip"); q != "" {
dur, _ := time.ParseDuration(q)
filters = append(filters, &FrameDropper{DelaySkip: dur})
}
demuxer := &pktque.FilterDemuxer{
Filter: filters,
Demuxer: cursor,
Expand Down

0 comments on commit cc5d6d4

Please sign in to comment.