Remove webRTC in Director

This commit is contained in:
giongto35 2019-04-07 18:20:46 +08:00
parent 8c70159d5a
commit 2756a18839
3 changed files with 25 additions and 27 deletions

35
main.go
View file

@ -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
}
}

View file

@ -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()
}

View file

@ -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)
}