Skip to content

Streaming Frames From Server To Client #305

@The-Box1

Description

@The-Box1

I've been looking through #288 for help with server side, but everything there is geared for just one screenshot. I'm trying to stream 60 FPS over socket.io but the speed I was hoping for didn't show up. I morphed the getFrameFromMemory or whatever it is into something that manually updates the canvas since for some reason when I try to access the pixels of the canvas from the canvas library all I get is white. Here's the function, I'm just running this then using canvas.toDataURL() and sending that string to the client to render. Is there something I'm missing?

`
async function UpdateCanvas() {

  const frameInProgressVideoOutputLocation = await WasmBoy._getWasmConstant('FRAME_LOCATION');
  const frameInProgressMemory = await WasmBoy._getWasmMemorySection(
    frameInProgressVideoOutputLocation,
    frameInProgressVideoOutputLocation + GAMEBOY_CAMERA_HEIGHT * GAMEBOY_CAMERA_WIDTH * 3 + 1
  );

  for (let y = 0; y < GAMEBOY_CAMERA_HEIGHT; y++) {
    for (let x = 0; x < GAMEBOY_CAMERA_WIDTH; x++) {

      let pixelStart = (y * GAMEBOY_CAMERA_WIDTH + x) * 3;

      var id = ctx.createImageData(1, 1);
      var d = id.data;
      d[0] = frameInProgressMemory[pixelStart + 0];
      d[1] = frameInProgressMemory[pixelStart + 1];
      d[2] = frameInProgressMemory[pixelStart + 2];
      d[3] = 255;
      ctx.putImageData(id, x, y);
    }
  }
}

WasmBoy.config(WasmBoyOptions, canvas).then(async () => {
  WasmBoy.loadROM(new Uint8Array(fs.readFileSync('/home/thebox/Desktop/Website/files/roms/pokemon.gb'))).then(async () => {
    WasmBoy.play();

    setInterval(() => {
      UpdateCanvas();
      io.emit('frame', canvas.toDataURL());
    }, frameTransferRate);
  })
});

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions