Skip to content

Commit

Permalink
TEMP: Very rough split for decoder/demuxer reuse and threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
j0sh committed Aug 3, 2019
1 parent baaeb30 commit f0e6390
Show file tree
Hide file tree
Showing 4 changed files with 347 additions and 138 deletions.
45 changes: 33 additions & 12 deletions ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ const (
Amd
)

type TranscoderHandle *C.struct_transcode_thread

type TranscodeOptionsIn struct {
Fname string
Accel Acceleration
Device string
Handle TranscoderHandle
}

type TranscodeOptions struct {
Expand All @@ -41,6 +44,10 @@ type TranscodeOptions struct {
Device string
}

type TranscodeResults struct {
Handle TranscoderHandle
}

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 Expand Up @@ -119,16 +126,24 @@ func accelDeviceType(accel Acceleration) (C.enum_AVHWDeviceType, error) {
}

func Transcode2(input *TranscodeOptionsIn, ps []TranscodeOptions) error {
res, err := Transcode3(input, ps)
if err != nil {
return err
}
C.lpms_transcode_stop((**C.struct_transcode_thread)(&res.Handle))
return err
}

func Transcode3(input *TranscodeOptionsIn, ps []TranscodeOptions) (*TranscodeResults, error) {
if input == nil {
return ErrTranscoderInp
return nil, ErrTranscoderInp
}
if len(ps) <= 0 {
return nil
return nil, nil // TODO ???
}
hw_type, err := accelDeviceType(input.Accel)
if err != nil {
return err
return nil, err
}
fname := C.CString(input.Fname)
defer C.free(unsafe.Pointer(fname))
Expand All @@ -140,24 +155,24 @@ func Transcode2(input *TranscodeOptionsIn, ps []TranscodeOptions) error {
param := p.Profile
res := strings.Split(param.Resolution, "x")
if len(res) < 2 {
return ErrTranscoderRes
return nil, ErrTranscoderRes
}
w, err := strconv.Atoi(res[0])
if err != nil {
return err
return nil, err
}
h, err := strconv.Atoi(res[1])
if err != nil {
return err
return nil, err
}
br := strings.Replace(param.Bitrate, "k", "000", 1)
bitrate, err := strconv.Atoi(br)
if err != nil {
return err
return nil, err
}
encoder, scale_filter, err := configAccel(input.Accel, p.Accel, input.Device, p.Device)
if err != nil {
return err
return nil, err
}
// preserve aspect ratio along the larger dimension when rescaling
filters := fmt.Sprintf("fps=%d/%d,%s='w=if(gte(iw,ih),%d,-2):h=if(lt(iw,ih),%d,-2)'", param.Framerate, 1, scale_filter, w, h)
Expand All @@ -179,13 +194,19 @@ func Transcode2(input *TranscodeOptionsIn, ps []TranscodeOptions) error {
device = C.CString(input.Device)
defer C.free(unsafe.Pointer(device))
}
inp := &C.input_params{fname: fname, hw_type: hw_type, device: device}
ret := int(C.lpms_transcode(inp, (*C.output_params)(&params[0]), C.int(len(params))))
inp := &C.input_params{fname: fname, hw_type: hw_type, device: device,
handle: input.Handle}
results := &C.output_results{}
ret := int(C.lpms_transcode(inp, (*C.output_params)(&params[0]), results, C.int(len(params))))
if 0 != ret {
glog.Infof("Transcoder Return : %v\n", Strerror(ret))
return ErrorMap[ret]
return nil, ErrorMap[ret]
}
return nil
return &TranscodeResults{Handle: results.handle}, nil
}

func TranscodeStop(handle *TranscoderHandle) {
C.lpms_transcode_stop((**C.struct_transcode_thread)(handle))
}

func InitFFmpeg() {
Expand Down
1 change: 1 addition & 0 deletions ffmpeg/ffmpeg_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func error_map() map[int]error {
}{
{code: C.lpms_ERR_INPUT_PIXFMT, desc: "Unsupported input pixel format"},
{code: C.lpms_ERR_FILTERS, desc: "Error initializing filtergraph"},
{code: C.lpms_ERR_STOPPED, desc: "Transcode loop has stopped"},
}
for _, v := range lpmsErrors {
m[int(v.code)] = errors.New(v.desc)
Expand Down
Loading

0 comments on commit f0e6390

Please sign in to comment.