use for to wait channel

This commit is contained in:
giongto35 2019-05-20 16:00:09 +08:00
parent 0c4e851c0c
commit 2ffd03aabf
6 changed files with 14 additions and 49 deletions

View file

@ -85,13 +85,7 @@ func NewGameView(console *nes.Console, title, saveFile string, imageChannel chan
// ListenToInputChannel listen from input channel streamm, which is exposed to WebRTC session
func (view *GameView) ListenToInputChannel() {
for {
// Adding ok here make thing slowdown
// TODO: Investigate slow
keysInBinary, ok := <-view.inputChannel
if !ok {
return
}
for keysInBinary := range view.inputChannel {
for i := 0; i < NumKeys*2; i++ {
b := ((keysInBinary & 1) == 1)
view.keyPressed[i] = (view.keyPressed[i] && b) || b

View file

@ -126,15 +126,13 @@ func (v *VpxEncoder) startLooping() {
}
}()
for {
beginEncoding := time.Now()
yuv, ok := <-v.Input
if !ok || v.Done == true {
for yuv := range v.Input {
if v.Done == true {
// The first time we see IsRunning set to false, we release and return
v.Release()
return
}
beginEncoding := time.Now()
// Add Image
v.vpxCodexIter = nil

View file

@ -274,10 +274,8 @@ func (w *WebRTC) startStreaming(vp8Track *webrtc.Track, audioTrack *webrtc.DataC
}()
// TODO: Use same yuv
for w.isConnected {
yuv, ok := <-w.ImageChannel
if !ok {
log.Println("Screenshot from emulator closed")
for yuv := range w.ImageChannel {
if !w.isConnected {
return
}
if len(w.encoder.Input) < cap(w.encoder.Input) {
@ -294,13 +292,7 @@ func (w *WebRTC) startStreaming(vp8Track *webrtc.Track, audioTrack *webrtc.DataC
}
}()
for w.isConnected {
bs, ok := <-w.encoder.Output
if !ok {
log.Println("WebRTC Video sending Closed")
return
}
for bs := range w.encoder.Output {
if *config.IsMonitor {
log.Println("Encoding FPS : ", w.calculateFPS())
}
@ -316,13 +308,10 @@ func (w *WebRTC) startStreaming(vp8Track *webrtc.Track, audioTrack *webrtc.DataC
}
}()
for w.isConnected {
data, ok := <-w.AudioChannel
if !ok {
log.Println("WebRTC Audio sending Closed")
for data := range w.AudioChannel {
if !w.isConnected {
return
}
audioTrack.Send(data)
}
}()

View file

@ -165,6 +165,7 @@ func (h *Handler) RouteOverlord() {
if ok {
session.Close()
delete(h.sessions, resp.SessionID)
h.detachPeerConn(session.peerconnection)
}
return cws.EmptyPacket

View file

@ -26,13 +26,7 @@ func (r *Room) startAudio() {
var count byte = 0
// fanout Audio
for {
sample, ok := <-r.audioChannel
if !ok {
// Just for guarding
log.Println("Warn: Room ", r.ID, " audio channel closed unexpectedly")
return
}
for sample := range r.audioChannel {
if !r.IsRunning {
log.Println("Room ", r.ID, " audio channel closed")
return
@ -81,13 +75,7 @@ func (r *Room) startVideo() {
size := int(float32(config.Width*config.Height) * 1.5)
yuv := make([]byte, size, size)
// fanout Screen
for {
image, ok := <-r.imageChannel
if !ok {
// Just for guarding, should not reached
log.Println("Warn: Room ", r.ID, " video channel closed unexpectedly")
return
}
for image := range r.imageChannel {
if !r.IsRunning {
log.Println("Room ", r.ID, " video channel closed")
return

View file

@ -121,13 +121,7 @@ func (r *Room) startWebRTCSession(peerconnection *webrtc.WebRTC, playerIndex int
}()
go func() {
for {
input, ok := <-peerconnection.InputChannel
if !ok {
return
// might consider continue here
}
for input := range peerconnection.InputChannel {
if peerconnection.Done || !peerconnection.IsConnected() || !r.IsRunning {
return
}
@ -198,6 +192,7 @@ func (r *Room) Close() {
close(r.inputChannel)
close(r.Done)
// Close here is a bit wrong because this read channel
// Just dont close it, let it be gc
//close(r.imageChannel)
//close(r.audioChannel)
}