revid: restructured revid.reset code to make more flexible for testing purposes.

This commit is contained in:
Saxon 2019-03-16 15:16:06 +10:30
parent 4d7f2d7b32
commit aa888ef115
1 changed files with 35 additions and 8 deletions

View File

@ -168,19 +168,21 @@ func (r *Revid) Bitrate() int {
return r.bitrate return r.bitrate
} }
// reset swaps the current config of a Revid with the passed func (r *Revid) setConfig(config Config) error {
// configuration; checking validity and returning errors if not valid.
func (r *Revid) reset(config Config) error {
r.config.Logger = config.Logger r.config.Logger = config.Logger
err := config.Validate(r) err := config.Validate(r)
if err != nil { if err != nil {
return errors.New("Config struct is bad: " + err.Error()) return errors.New("Config struct is bad: " + err.Error())
} }
r.config = config r.config = config
return nil
}
func (r *Revid) setupPipeline(mtsEnc func(io.Writer, int) stream.Encoder, flvEnc func(io.Writer, int) (stream.Encoder, error)) error {
r.buffer = (*buffer)(ring.NewBuffer(ringBufferSize, ringBufferElementSize, writeTimeout)) r.buffer = (*buffer)(ring.NewBuffer(ringBufferSize, ringBufferElementSize, writeTimeout))
r.encoder = make([]stream.Encoder, 0) r.encoder = make([]stream.Encoder, 0)
// mtsSenders will hold the senders the require MPEGTS encoding, and flvSenders // mtsSenders will hold the senders the require MPEGTS encoding, and flvSenders
// will hold senders that require FLV encoding. // will hold senders that require FLV encoding.
var mtsSenders, flvSenders []loadSender var mtsSenders, flvSenders []loadSender
@ -220,10 +222,7 @@ func (r *Revid) reset(config Config) error {
// as a destination. // as a destination.
if len(mtsSenders) != 0 { if len(mtsSenders) != 0 {
ms := multiSender(mtsSenders) ms := multiSender(mtsSenders)
if err != nil { e := mtsEnc(ms, int(r.config.FrameRate))
return err
}
e := mts.NewEncoder(ms, float64(r.config.FrameRate))
r.encoder = append(r.encoder, e) r.encoder = append(r.encoder, e)
} }
@ -232,7 +231,7 @@ func (r *Revid) reset(config Config) error {
// as a destination. // as a destination.
if len(flvSenders) != 0 { if len(flvSenders) != 0 {
ms := multiSender(flvSenders) ms := multiSender(flvSenders)
e, err := flv.NewEncoder(ms, true, true, int(r.config.FrameRate)) e, err := flvEnc(ms, int(r.config.FrameRate))
if err != nil { if err != nil {
return err return err
} }
@ -256,6 +255,34 @@ func (r *Revid) reset(config Config) error {
r.config.Logger.Log(logger.Info, pkg+"using MJPEG lexer") r.config.Logger.Log(logger.Info, pkg+"using MJPEG lexer")
r.lexTo = lex.MJPEG r.lexTo = lex.MJPEG
} }
return nil
}
func newMtsEncoder(dst io.Writer, fps int) stream.Encoder {
e := mts.NewEncoder(dst, float64(fps))
return e
}
func newFlvEncoder(dst io.Writer, fps int) (stream.Encoder, error) {
e, err := flv.NewEncoder(dst, true, true, fps)
if err != nil {
return nil, err
}
return e, nil
}
// reset swaps the current config of a Revid with the passed
// configuration; checking validity and returning errors if not valid.
func (r *Revid) reset(config Config) error {
err := r.setConfig(config)
if err != nil {
return err
}
err = r.setupPipeline(newMtsEncoder, newFlvEncoder)
if err != nil {
return err
}
return nil return nil
} }