Skip to content

Commit

Permalink
Fold vpx/h264 struct options
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Aug 1, 2023
1 parent 49cb752 commit ddc841e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pkg/config/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ type Video struct {
Concurrency int
H264 struct {
Crf uint8
LogLevel int32
Preset string
Profile string
Tune string
LogLevel int
}
Vpx struct {
Bitrate uint
Expand Down
14 changes: 6 additions & 8 deletions pkg/worker/encoder/h264/x264.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package h264

/*
#include <stdint.h>
*/
// #include <stdint.h>
import "C"
import (
"fmt"
Expand All @@ -28,14 +26,14 @@ type Options struct {
// This method allows the encoder to attempt to achieve a certain output quality for the whole file
// when output file size is of less importance.
// The range of the CRF scale is 0–51, where 0 is lossless, 23 is the default, and 51 is the worst quality possible.
Crf uint8
// film, animation, grain, stillimage, psnr, ssim, fastdecode, zerolatency.
Tune string
Crf uint8
LogLevel int32
// ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo.
Preset string
// baseline, main, high, high10, high422, high444.
Profile string
LogLevel int32
Profile string
// film, animation, grain, stillimage, psnr, ssim, fastdecode, zerolatency.
Tune string
}

func NewEncoder(w, h int, opts *Options) (encoder *H264, err error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/worker/encoder/vpx/libvpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type Options struct {
// Target bandwidth to use for this stream, in kilobits per second.
Bitrate uint
// Force keyframe interval.
KeyframeInt uint
KeyframeInterval uint
}

func NewEncoder(w, h int, opts *Options) (*Vpx, error) {
Expand All @@ -101,14 +101,14 @@ func NewEncoder(w, h int, opts *Options) (*Vpx, error) {

if opts == nil {
opts = &Options{
Bitrate: 1200,
KeyframeInt: 5,
Bitrate: 1200,
KeyframeInterval: 5,
}
}

vpx := Vpx{
frameCount: C.int(0),
kfi: C.int(opts.KeyframeInt),
kfi: C.int(opts.KeyframeInterval),
}

if C.vpx_img_alloc(&vpx.image, C.VPX_IMG_FMT_I420, C.uint(w), C.uint(h), 1) == nil {
Expand Down
19 changes: 6 additions & 13 deletions pkg/worker/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,33 +124,26 @@ func (r *Room) initAudio(srcHz int, conf config.Audio) {
}

// initVideo processes videoFrames images with an encoder (codec) then pushes the result to WebRTC.
func (r *Room) initVideo(width, height int, conf config.Video) {
func (r *Room) initVideo(w, h int, conf config.Video) {
var enc encoder.Encoder
var err error

r.log.Info().Msgf("Video codec: %v", conf.Codec)
if conf.Codec == string(encoder.H264) {
r.log.Debug().Msgf("x264: build v%v", h264.LibVersion())
enc, err = h264.NewEncoder(width, height, &h264.Options{
Crf: conf.H264.Crf,
Tune: conf.H264.Tune,
Preset: conf.H264.Preset,
Profile: conf.H264.Profile,
LogLevel: int32(conf.H264.LogLevel),
})
opts := h264.Options(conf.H264)
enc, err = h264.NewEncoder(w, h, &opts)
} else {
enc, err = vpx.NewEncoder(width, height, &vpx.Options{
Bitrate: conf.Vpx.Bitrate,
KeyframeInt: conf.Vpx.KeyframeInterval,
})
opts := vpx.Options(conf.Vpx)
enc, err = vpx.NewEncoder(w, h, &opts)
}

if err != nil {
r.log.Error().Err(err).Msg("couldn't create a video encoder")
return
}

r.vEncoder = encoder.NewVideoEncoder(enc, width, height, conf.Concurrency, r.log)
r.vEncoder = encoder.NewVideoEncoder(enc, w, h, conf.Concurrency, r.log)

r.emulator.SetVideo(func(raw *emulator.GameFrame) {
data := r.vEncoder.Encode(raw.Data.RGBA)
Expand Down

0 comments on commit ddc841e

Please sign in to comment.