Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
fix(decoder): fps + check keyframe
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Sep 30, 2020
1 parent 7867823 commit 5daf4b3
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pkg/elements/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Decoder struct {
Node
ctx *vpx.CodecCtx
iface *vpx.CodecIface
run bool
async bool
}

Expand All @@ -33,6 +34,16 @@ func NewDecoder(fps uint) *Decoder {

func (dec *Decoder) Write(sample *avp.Sample) error {
if sample.Type == avp.TypeVP8 {
payload := sample.Payload.([]byte)

if !dec.run {
videoKeyframe := (payload[0]&0x1 == 0)
if !videoKeyframe {
return nil
}
dec.run = true
}

if dec.iface == nil {
dec.iface = vpx.DecoderIfaceVP8()
err := vpx.Error(vpx.CodecDecInitVer(dec.ctx, dec.iface, nil, 0, vpx.DecoderABIVersion))
Expand All @@ -41,7 +52,6 @@ func (dec *Decoder) Write(sample *avp.Sample) error {
}
}

payload := sample.Payload.([]byte)
err := vpx.Error(vpx.CodecDecode(dec.ctx, string(payload), uint32(len(payload)), nil, 0))
if err != nil {
return err
Expand All @@ -56,7 +66,7 @@ func (dec *Decoder) Write(sample *avp.Sample) error {
}

func (dec *Decoder) Close() {
dec.async = false
dec.run = false
}

func (dec *Decoder) write() error {
Expand All @@ -73,9 +83,9 @@ func (dec *Decoder) write() error {
}

func (dec *Decoder) producer(fps uint) {
ticker := time.NewTicker(time.Duration(1/fps) * time.Second)
ticker := time.NewTicker(time.Duration((1/fps)*1000) * time.Millisecond)
for range ticker.C {
if !dec.async {
if !dec.run {
return
}

Expand Down

0 comments on commit 5daf4b3

Please # to comment.