mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-28 12:36:30 +00:00
Refactor: Fix channel init order (#141)
* Fix channel order * Remove Done in encoder * Update h264 encoder done code * remove Unnecessary IsRunning
This commit is contained in:
parent
e8fbd28b2f
commit
0b9d628ec2
5 changed files with 41 additions and 78 deletions
|
|
@ -20,8 +20,6 @@ type H264Encoder struct {
|
|||
buf *bytes.Buffer
|
||||
enc *x264.Encoder
|
||||
|
||||
IsRunning bool
|
||||
Done bool
|
||||
// C
|
||||
width int
|
||||
height int
|
||||
|
|
@ -34,9 +32,6 @@ func NewH264Encoder(width, height, fps int) (encoder.Encoder, error) {
|
|||
Output: make(chan []byte, 5*chanSize),
|
||||
Input: make(chan *image.RGBA, chanSize),
|
||||
|
||||
IsRunning: true,
|
||||
Done: false,
|
||||
|
||||
buf: bytes.NewBuffer(make([]byte, 0)),
|
||||
width: width,
|
||||
height: height,
|
||||
|
|
@ -51,8 +46,6 @@ func NewH264Encoder(width, height, fps int) (encoder.Encoder, error) {
|
|||
}
|
||||
|
||||
func (v *H264Encoder) init() error {
|
||||
v.IsRunning = true
|
||||
|
||||
opts := &x264.Options{
|
||||
Width: v.width,
|
||||
Height: v.height,
|
||||
|
|
@ -79,21 +72,9 @@ func (v *H264Encoder) startLooping() {
|
|||
log.Println("Warn: Recovered panic in encoding ", r)
|
||||
log.Println(debug.Stack())
|
||||
}
|
||||
|
||||
if v.Done == true {
|
||||
// The first time we see IsRunning set to false, we release and return
|
||||
v.release()
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
for img := range v.Input {
|
||||
if v.Done == true {
|
||||
// The first time we see IsRunning set to false, we release and return
|
||||
v.release()
|
||||
return
|
||||
}
|
||||
|
||||
err := v.enc.Encode(img)
|
||||
if err != nil {
|
||||
log.Println("err encoding ", img, " using h264")
|
||||
|
|
@ -105,17 +86,13 @@ func (v *H264Encoder) startLooping() {
|
|||
|
||||
// Release release memory and stop loop
|
||||
func (v *H264Encoder) release() {
|
||||
if v.IsRunning {
|
||||
v.IsRunning = false
|
||||
log.Println("Releasing encoder")
|
||||
// TODO: Bug here, after close it will signal
|
||||
close(v.Output)
|
||||
err := v.enc.Close()
|
||||
if err != nil {
|
||||
log.Println("Failed to close H264 encoder")
|
||||
}
|
||||
log.Println("Releasing encoder")
|
||||
// TODO: Bug here, after close it will signal
|
||||
close(v.Output)
|
||||
err := v.enc.Close()
|
||||
if err != nil {
|
||||
log.Println("Failed to close H264 encoder")
|
||||
}
|
||||
// TODO: Can we merge IsRunning and Done together
|
||||
}
|
||||
|
||||
// GetInputChan returns input channel
|
||||
|
|
@ -130,6 +107,5 @@ func (v *H264Encoder) GetOutputChan() chan []byte {
|
|||
|
||||
// GetDoneChan returns done channel
|
||||
func (v *H264Encoder) Stop() {
|
||||
v.Done = true
|
||||
close(v.Input)
|
||||
v.release()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,9 +49,6 @@ type VpxEncoder struct {
|
|||
Output chan []byte // frame
|
||||
Input chan *image.RGBA // yuvI420
|
||||
|
||||
IsRunning bool
|
||||
Done bool
|
||||
|
||||
width int
|
||||
height int
|
||||
// C
|
||||
|
|
@ -70,8 +67,6 @@ func NewVpxEncoder(w, h, fps, bitrate, keyframe int) (encoder.Encoder, error) {
|
|||
Output: make(chan []byte, 5*chanSize),
|
||||
Input: make(chan *image.RGBA, chanSize),
|
||||
|
||||
IsRunning: true,
|
||||
Done: false,
|
||||
// C
|
||||
width: w,
|
||||
height: h,
|
||||
|
|
@ -116,7 +111,6 @@ func (v *VpxEncoder) init() error {
|
|||
if C.call_vpx_codec_enc_init(&v.vpxCodexCtx, encoder, &cfg) != 0 {
|
||||
return fmt.Errorf("Failed to initialize encoder")
|
||||
}
|
||||
v.IsRunning = true
|
||||
go v.startLooping()
|
||||
return nil
|
||||
}
|
||||
|
|
@ -126,24 +120,12 @@ func (v *VpxEncoder) startLooping() {
|
|||
if r := recover(); r != nil {
|
||||
log.Println("Warn: Recovered panic in encoding ", r)
|
||||
}
|
||||
|
||||
if v.Done == true {
|
||||
// The first time we see IsRunning set to false, we release and return
|
||||
v.release()
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
size := int(float32(v.width*v.height) * 1.5)
|
||||
yuv := make([]byte, size, size)
|
||||
|
||||
for img := range v.Input {
|
||||
if v.Done == true {
|
||||
// The first time we see IsRunning set to false, we release and return
|
||||
v.release()
|
||||
return
|
||||
}
|
||||
|
||||
util.RgbaToYuvInplace(img, yuv, v.width, v.height)
|
||||
|
||||
// Add Image
|
||||
|
|
@ -175,18 +157,14 @@ func (v *VpxEncoder) startLooping() {
|
|||
|
||||
// Release release memory and stop loop
|
||||
func (v *VpxEncoder) release() {
|
||||
if v.IsRunning {
|
||||
v.IsRunning = false
|
||||
log.Println("Releasing encoder")
|
||||
C.vpx_img_free(&v.vpxImage)
|
||||
C.vpx_codec_destroy(&v.vpxCodexCtx)
|
||||
// TODO: Bug here, after close it will signal
|
||||
close(v.Output)
|
||||
if v.Input != nil {
|
||||
close(v.Input)
|
||||
}
|
||||
log.Println("Releasing encoder")
|
||||
C.vpx_img_free(&v.vpxImage)
|
||||
C.vpx_codec_destroy(&v.vpxCodexCtx)
|
||||
// TODO: Bug here, after close it will signal
|
||||
close(v.Output)
|
||||
if v.Input != nil {
|
||||
close(v.Input)
|
||||
}
|
||||
// TODO: Can we merge IsRunning and Done together
|
||||
}
|
||||
|
||||
// GetInputChan returns input channel
|
||||
|
|
@ -201,5 +179,5 @@ func (v *VpxEncoder) GetOutputChan() chan []byte {
|
|||
|
||||
// GetDoneChan returns done channel
|
||||
func (v *VpxEncoder) Stop() {
|
||||
v.Done = true
|
||||
v.release()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue