diff --git a/main.go b/main.go index 5569d6e6..1860fd05 100644 --- a/main.go +++ b/main.go @@ -48,8 +48,8 @@ var rooms map[string]*Room func init() { } -func startGame(path string, imageChannel chan *image.RGBA, inputChannel chan int, webRTC *webrtc.WebRTC) { - ui.Run([]string{path}, imageChannel, inputChannel, webRTC) +func startGame(path string, imageChannel chan *image.RGBA, inputChannel chan int) { + ui.Run([]string{path}, imageChannel, inputChannel) } func main() { @@ -83,18 +83,25 @@ func getWeb(w http.ResponseWriter, r *http.Request) { w.Write(bs) } +// initRoom initilize room returns roomID +func initRoom(roomID, gameName string) string { + roomID = generateRoomID() + imageChannel := make(chan *image.RGBA, 100) + inputChannel := make(chan int, 100) + rooms[roomID] = &Room{ + imageChannel: imageChannel, + inputChannel: inputChannel, + rtcSessions: []*webrtc.WebRTC{}, + } + go fanoutScreen(imageChannel, roomID) + go startGame("games/"+gameName, imageChannel, inputChannel) + + return roomID +} + func startSession(webRTC *webrtc.WebRTC, gameName string, roomID string) string { if roomID == "" { - roomID = generateRoomID() - imageChannel := make(chan *image.RGBA, 100) - inputChannel := make(chan int, 100) - rooms[roomID] = &Room{ - imageChannel: imageChannel, - inputChannel: inputChannel, - rtcSessions: []*webrtc.WebRTC{}, - } - go fanoutScreen(imageChannel, roomID) - go startGame("games/"+gameName, imageChannel, inputChannel, webRTC) + roomID = initRoom(roomID, gameName) } rooms[roomID].rtcSessions = append(rooms[roomID].rtcSessions, webRTC) @@ -230,7 +237,7 @@ func postSession(w http.ResponseWriter, r *http.Request) { rtcSessions: []*webrtc.WebRTC{}, } go fanoutScreen(imageChannel, roomID) - go startGame("games/"+postPacket.Game, imageChannel, inputChannel, webRTC) + go startGame("games/"+postPacket.Game, imageChannel, inputChannel) // fanin input channel // fanout output channel } else { @@ -281,7 +288,6 @@ func fanoutScreen(imageChannel chan *image.RGBA, roomID string) { func faninInput(inputChannel chan int, webRTC *webrtc.WebRTC) { go func() { for { - fmt.Println("spawning") // Client stopped if webRTC.IsClosed() { return @@ -290,7 +296,6 @@ func faninInput(inputChannel chan int, webRTC *webrtc.WebRTC) { // encode frame if webRTC.IsConnected() { input := <-webRTC.InputChannel - fmt.Println("received input", input) inputChannel <- input } } diff --git a/ui/director.go b/ui/director.go index e55af00a..0fb8ad7b 100644 --- a/ui/director.go +++ b/ui/director.go @@ -6,7 +6,6 @@ import ( "time" "github.com/giongto35/cloud-game/nes" - "github.com/giongto35/cloud-game/webrtc" ) type View interface { @@ -21,16 +20,14 @@ type Director struct { timestamp float64 imageChannel chan *image.RGBA inputChannel chan int - webRTC *webrtc.WebRTC } -func NewDirector(imageChannel chan *image.RGBA, inputChannel chan int, webRTC *webrtc.WebRTC) *Director { -// func NewDirector(audio *Audio, imageChannel chan *image.RGBA, inputChannel chan int) *Director { +func NewDirector(imageChannel chan *image.RGBA, inputChannel chan int) *Director { + // func NewDirector(audio *Audio, imageChannel chan *image.RGBA, inputChannel chan int) *Director { director := Director{} // director.audio = audio director.imageChannel = imageChannel director.inputChannel = inputChannel - director.webRTC = webRTC return &director } @@ -65,9 +62,7 @@ func (d *Director) Start(paths []string) { func (d *Director) Run() { for { // quit game - if d.webRTC.IsClosed() { - break - } + // TODO: Check if noone using d.Step() } diff --git a/ui/run.go b/ui/run.go index 61feafb9..65381393 100644 --- a/ui/run.go +++ b/ui/run.go @@ -5,9 +5,7 @@ import ( "image" // "log" "runtime" - // "github.com/gordonklaus/portaudio" - "github.com/giongto35/cloud-game/webrtc" ) const ( @@ -22,7 +20,7 @@ func init() { runtime.LockOSThread() } -func Run(paths []string, imageChannel chan *image.RGBA, inputChannel chan int, webRTC *webrtc.WebRTC) { +func Run(paths []string, imageChannel chan *image.RGBA, inputChannel chan int) { // TODO: stream audio // initialize audio // portaudio.Initialize() @@ -35,7 +33,7 @@ func Run(paths []string, imageChannel chan *image.RGBA, inputChannel chan int, w // defer audio.Stop() // run director - director := NewDirector(imageChannel, inputChannel, webRTC) + director := NewDirector(imageChannel, inputChannel) // director := NewDirector(audio, imageChannel, inputChannel) director.Start(paths) }