Skip to content

Commit

Permalink
expose lpms_is_bypass_needed function in public API
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdarkdragon committed Jun 9, 2021
1 parent f7f2491 commit c8d3486
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Binary file added data/zero-frame.ts
Binary file not shown.
39 changes: 39 additions & 0 deletions ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package ffmpeg

import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strconv"
Expand All @@ -28,6 +31,7 @@ var ErrTranscoderFmt = errors.New("TranscoderUnrecognizedFormat")
var ErrTranscoderPrf = errors.New("TranscoderUnrecognizedProfile")
var ErrTranscoderGOP = errors.New("TranscoderInvalidGOP")
var ErrTranscoderDev = errors.New("TranscoderIncompatibleDevices")
var ErrEmptyData = errors.New("EmptyData")

type Acceleration int

Expand Down Expand Up @@ -77,6 +81,41 @@ type TranscodeResults struct {
Encoded []MediaInfo
}

// HasZeroVideoFrame opens video file and returns 1 if it has video stream with 0-frame
func HasZeroVideoFrame(fname string) int {
cfname := C.CString(fname)
defer C.free(unsafe.Pointer(cfname))
return int(C.lpms_is_bypass_needed(cfname))
}

// HasZeroVideoFrameBytes opens video and returns 1 if it has video stream with 0-frame
func HasZeroVideoFrameBytes(data []byte) (int, error) {
if len(data) == 0 {
return 0, ErrEmptyData
}
or, ow, err := os.Pipe()
if err != nil {
return -1, err
}
fname := fmt.Sprintf("pipe:%d", or.Fd())
cfname := C.CString(fname)
defer C.free(unsafe.Pointer(cfname))
done := make(chan struct{})
go func() {
var err2 error
br := bytes.NewReader(data)
_, err2 = io.Copy(ow, br)
if err2 != nil {
glog.Errorf("Error sending data to lpms_is_bypass_needed function err=%v", err2)
}
ow.Close()
done <- struct{}{}
}()
bres := int(C.lpms_is_bypass_needed(cfname))
<-done
return bres, nil
}

func RTMPToHLS(localRTMPUrl string, outM3U8 string, tmpl string, seglen_secs string, seg_start int) error {
inp := C.CString(localRTMPUrl)
outp := C.CString(outM3U8)
Expand Down
33 changes: 33 additions & 0 deletions ffmpeg/ffmpeg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"testing"
)

Expand Down Expand Up @@ -1558,3 +1559,35 @@ func TestTranscoder_IgnoreUnknown(t *testing.T) {
`
run(cmd)
}

func TestTranscoder_ZeroFrame(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
fname := path.Join(wd, "..", "data", "zero-frame.ts")
res := HasZeroVideoFrame(fname)
if res != 1 {
t.Errorf("Expecting 1, got %d fname=%s", res, fname)
}
data, err := ioutil.ReadFile(fname)
if err != nil {
t.Error(err)
}
res, err = HasZeroVideoFrameBytes(data)
if err != nil {
t.Error(err)
}
if res != 1 {
t.Errorf("Expecting 1, got %d fname=%s", res, fname)
}
res, err = HasZeroVideoFrameBytes(nil)
if err != ErrEmptyData {
t.Errorf("Unexpected error %v", err)
}
fname = path.Join(wd, "..", "data", "bunny.mp4")
res = HasZeroVideoFrame(fname)
if res != 0 {
t.Errorf("Expecting 0, got %d fname=%s", res, fname)
}
}

0 comments on commit c8d3486

Please sign in to comment.