mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-21 17:18:47 +00:00
WIP
This commit is contained in:
parent
e2efc533cc
commit
2a012c0803
4 changed files with 23 additions and 6 deletions
7
main.go
7
main.go
|
|
@ -329,21 +329,23 @@ func fanoutAudio(audioChannel chan float32, roomID string) {
|
|||
return
|
||||
}
|
||||
|
||||
var count byte = 0
|
||||
|
||||
for {
|
||||
pcm[idx] = <- audioChannel
|
||||
idx ++
|
||||
|
||||
if idx >= len(pcm) {
|
||||
if idx == len(pcm) {
|
||||
data := make([]byte, 640)
|
||||
|
||||
n, err := enc.EncodeFloat32(pcm, data)
|
||||
// n := opus.EncodeFloat(enc, pcm, 120, data, 1000)
|
||||
|
||||
if err != nil {
|
||||
log.Println("[!] Failed to decode")
|
||||
continue
|
||||
}
|
||||
data = data[:n]
|
||||
// data = append(data, count)
|
||||
|
||||
|
||||
isRoomRunning := false
|
||||
|
|
@ -368,6 +370,7 @@ func fanoutAudio(audioChannel chan float32, roomID string) {
|
|||
return
|
||||
}
|
||||
idx = 0
|
||||
count = (count + 1) & 0xff
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,8 +72,13 @@ function startGame() {
|
|||
var audioStack = [];
|
||||
var nextTime = 0;
|
||||
|
||||
var isRun = false;
|
||||
|
||||
function scheduleBuffers() {
|
||||
if (isRun) return;
|
||||
console.log("daaaa");
|
||||
while ( audioStack.length) {
|
||||
isRun = true;
|
||||
var buffer = audioStack.shift();
|
||||
var source = context.createBufferSource();
|
||||
source.buffer = buffer;
|
||||
|
|
@ -83,6 +88,8 @@ function startGame() {
|
|||
source.start(nextTime);
|
||||
nextTime+=source.buffer.duration; // Make the next buffer wait the length of the last buffer before being played
|
||||
};
|
||||
console.log("diii");
|
||||
isRun = false;
|
||||
}
|
||||
|
||||
sampleRate = 16000;
|
||||
|
|
@ -93,9 +100,7 @@ function startGame() {
|
|||
pcmChunk = decoder.decode_float(opusChunk);
|
||||
myBuffer = context.createBuffer(channels, pcmChunk.length, sampleRate);
|
||||
nowBuffering = myBuffer.getChannelData(0, bitDepth, sampleRate);
|
||||
for (var i = 0; i < pcmChunk.length; i++) {
|
||||
nowBuffering[i] = pcmChunk[i];
|
||||
}
|
||||
nowBuffering.set(pcmChunk);
|
||||
return myBuffer;
|
||||
}
|
||||
|
||||
|
|
@ -105,6 +110,11 @@ function startGame() {
|
|||
ev.channel.onclose = () => log('channelX has closed');
|
||||
|
||||
ev.channel.onmessage = (e) => {
|
||||
// source = context.createBufferSource();
|
||||
// source.buffer = buf;
|
||||
// source.connect(context.destination);
|
||||
// source.start(0);
|
||||
|
||||
audioStack.push(damn(e.data));
|
||||
if ((init!=0) || (audioStack.length > 10)) { // make sure we put at least 10 chunks in the buffer before starting
|
||||
init++;
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ const (
|
|||
const NumKeys = 8
|
||||
const SampleRate = 16000
|
||||
const Channels = 1
|
||||
const TimeFrame = 40
|
||||
const TimeFrame = 60
|
||||
|
||||
type GameView struct {
|
||||
console *nes.Console
|
||||
|
|
|
|||
|
|
@ -153,6 +153,8 @@ func (w *WebRTC) StartClient(remoteSession string, width, height int) (string, e
|
|||
}
|
||||
|
||||
|
||||
// dd := false
|
||||
// audioTrack, err := w.connection.CreateDataChannel("foo2", &webrtc.DataChannelInit{Ordered: &dd})
|
||||
audioTrack, err := w.connection.CreateDataChannel("foo2", nil)
|
||||
|
||||
// WebRTC state callback
|
||||
|
|
@ -270,6 +272,8 @@ func (w *WebRTC) startStreaming(vp8Track *webrtc.Track, audioTrack *webrtc.DataC
|
|||
go func() {
|
||||
for w.isConnected {
|
||||
data := <-w.AudioChannel
|
||||
// time.Sleep()
|
||||
// time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
|
||||
audioTrack.Send(data)
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue