Skip to content

Commit 008d1d8

Browse files
authored
Update emulator.js
Newer version of test code shows live stream on player 2 device
1 parent c5828d9 commit 008d1d8

1 file changed

Lines changed: 88 additions & 50 deletions

File tree

WebRTC/emulator.js

Lines changed: 88 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Test 72
21
class EmulatorJS {
32
getCores() {
43
let rv = {
@@ -4626,7 +4625,8 @@ createNetplayMenu() {
46264625

46274626
const joined = this.createElement("div");
46284627
const title2 = this.createElement("strong");
4629-
title2.innerText = "{roomname}";
4628+
// Placeholder, will be set dynamically in netplayRoomJoined
4629+
title2.innerText = "";
46304630
const password = this.createElement("div");
46314631
password.innerText = "Password: ";
46324632
const table2 = this.createElement("table");
@@ -4671,6 +4671,8 @@ createNetplayMenu() {
46714671
this.openNetplayMenu = function() {
46724672
console.log("Opening Netplay menu");
46734673
this.netplayMenu.style.display = "";
4674+
// Initialize netplay object if not already done
4675+
this.netplay = this.netplay || {};
46744676
this.netplay.peerConnections = this.netplay.peerConnections || {};
46754677
this.netplay.localStream = this.netplay.localStream || null;
46764678
this.netplay.table = tbody;
@@ -4681,7 +4683,14 @@ createNetplayMenu() {
46814683
this.netplay.tabs = [rooms, joined];
46824684
this.netplay.videoContainer = videoContainer;
46834685
this.netplay.video = video;
4684-
this.defineNetplayFunctions();
4686+
// Ensure defineNetplayFunctions is called safely
4687+
try {
4688+
this.defineNetplayFunctions();
4689+
} catch (error) {
4690+
console.error("Error defining netplay functions:", error);
4691+
this.displayMessage("Failed to initialize netplay functions", 5000);
4692+
return;
4693+
}
46854694

46864695
if (!this.netplay.name) {
46874696
console.log("Initializing netplay object, player name not set yet");
@@ -4737,7 +4746,7 @@ createNetplayMenu() {
47374746
}.bind(this);
47384747
}
47394748

4740-
// Section 2
4749+
// NEW Section 2
47414750
defineNetplayFunctions() {
47424751
function guidGenerator() {
47434752
const S4 = function() {
@@ -4899,7 +4908,7 @@ defineNetplayFunctions() {
48994908
this.netplay.localStream = stream;
49004909
};
49014910

4902-
//Test Coturn Server
4911+
//Test Coturn Server
49034912
this.netplayCreatePeerConnection = function(peerId) {
49044913
const pc = new RTCPeerConnection({
49054914
iceServers: [
@@ -5120,12 +5129,12 @@ defineNetplayFunctions() {
51205129
canvas.style.zIndex = "1"; // Ensure canvas is visible
51215130
console.log("Canvas styles:", canvas.style.cssText);
51225131

5123-
// Initial canvas setup with fallback dimensions
5124-
const initialWidth = 700;
5125-
const initialHeight = 720;
5126-
canvas.width = initialWidth;
5127-
canvas.height = initialHeight;
5128-
console.log("Initial canvas dimensions set to:", { width: canvas.width, height: canvas.height });
5132+
// Match canvas dimensions to video dimensions
5133+
const videoWidth = videoElement.videoWidth || 700;
5134+
const videoHeight = videoElement.videoHeight || 720;
5135+
canvas.width = videoWidth;
5136+
canvas.height = videoHeight;
5137+
console.log("Canvas dimensions set to match video:", { width: canvas.width, height: canvas.height });
51295138

51305139
// Temporarily set a background color to confirm visibility
51315140
ctx.fillStyle = "red";
@@ -5146,41 +5155,30 @@ defineNetplayFunctions() {
51465155
}
51475156
}
51485157
}
5149-
// Wait for valid dimensions
5150-
const checkDimensions = () => new Promise(resolve => {
5151-
const interval = setInterval(() => {
5152-
if (videoElement.videoWidth > 0 && videoElement.videoHeight > 0) {
5153-
clearInterval(interval);
5154-
console.log("Video dimensions now valid:", { videoWidth: videoElement.videoWidth, videoHeight: videoElement.videoHeight });
5155-
resolve();
5156-
} else {
5157-
console.log("Waiting for video dimensions, current:", { videoWidth: videoElement.videoWidth, videoHeight: videoElement.videoHeight });
5158-
}
5159-
}, 100);
5160-
});
5161-
await checkDimensions();
5162-
// Update canvas dimensions to match video
5163-
canvas.width = videoElement.videoWidth;
5164-
canvas.height = videoElement.videoHeight;
5165-
console.log("Canvas dimensions updated to match video:", { width: canvas.width, height: canvas.height });
5158+
// Check video element content
5159+
if (videoElement.videoWidth === 0 || videoElement.videoHeight === 0) {
5160+
console.warn("Video element has zero dimensions, likely no valid frame:", { videoWidth: videoElement.videoWidth, videoHeight: videoElement.videoHeight });
5161+
} else {
5162+
console.log("Video dimensions:", { videoWidth: videoElement.videoWidth, videoHeight: videoElement.videoHeight });
5163+
}
51665164
};
51675165

51685166
const drawFrame = () => {
51695167
if (!this.isNetplay || this.netplay.owner) {
51705168
console.log("Stopping drawFrame: Not in netplay or is owner");
51715169
return;
51725170
}
5173-
if (videoElement.readyState >= videoElement.HAVE_CURRENT_DATA && videoElement.videoWidth > 0 && videoElement.videoHeight > 0) {
5171+
if (videoElement.readyState >= videoElement.HAVE_CURRENT_DATA) {
51745172
// Clear the canvas before drawing
51755173
ctx.clearRect(0, 0, canvas.width, canvas.height);
51765174
// Draw the video frame
5177-
const drawSuccess = ctx.drawImage(videoElement, 0, 0, canvas.width, canvas.height);
5178-
console.log("Drawing frame to canvas at time:", videoElement.currentTime, "Video readyState:", videoElement.readyState, "Draw success:", !!drawSuccess);
5175+
ctx.drawImage(videoElement, 0, 0, canvas.width, canvas.height);
5176+
console.log("Drawing frame to canvas at time:", videoElement.currentTime, "Video readyState:", videoElement.readyState);
51795177
// Log canvas content for debugging
51805178
const imageData = ctx.getImageData(0, 0, 1, 1).data;
51815179
console.log("Pixel at (0,0):", imageData, "All zeros?", imageData.every(v => v === 0));
51825180
} else {
5183-
console.log("Video not ready to draw, readyState:", videoElement.readyState, "Dimensions:", { videoWidth: videoElement.videoWidth, videoHeight: videoElement.videoHeight });
5181+
console.log("Video not ready to draw, readyState:", videoElement.readyState);
51845182
}
51855183
requestAnimationFrame(drawFrame);
51865184
};
@@ -5530,7 +5528,7 @@ defineNetplayFunctions() {
55305528
};
55315529
}
55325530

5533-
// Section 3
5531+
// NEW Section 3
55345532
netplayRoomJoined(isOwner, roomName, password, roomId) {
55355533
this.isNetplay = true;
55365534
this.netplay.inputs = {};
@@ -5550,24 +5548,14 @@ netplayRoomJoined(isOwner, roomName, password, roomId) {
55505548
if (!this.netplay.owner) {
55515549
this.canvas.style.display = "none";
55525550
this.netplayCanvas.style.display = "";
5553-
this.netplay.oldStyles = [
5554-
this.elements.bottomBar.cheat[0].style.display
5555-
];
5551+
this.netplay.oldStyles = [this.elements.bottomBar.cheat[0].style.display];
55565552
this.elements.bottomBar.cheat[0].style.display = "none";
55575553
this.gameManager.resetCheat();
55585554
console.log("Player 2 joined, awaiting WebRTC stream...");
5559-
// Log to confirm input event listeners
5560-
console.log("Game input listeners active:", this.gameManager.functions.simulateInput !== undefined);
5561-
// Ensure the parent element has focus to capture inputs
5555+
this.netplayCanvas.style.zIndex = "10";
55625556
this.elements.parent.focus();
5563-
// Prevent default browser behavior (e.g., scrolling)
5564-
e.preventDefault();
5565-
// Send input to netplay.simulateInput
5566-
const playerIndex = this.netplayGetUserIndex();
5567-
this.netplay.simulateInput(playerIndex, index, isKeyDown ? 1 : 0);
5568-
};
5569-
// Attach input handlers to the parent element
5570-
this.addEventListener(this.elements.parent, "keydown keyup", handleKeyEvent);
5557+
// Rely on EJS's existing input system
5558+
console.log("Game input listeners active:", this.gameManager.functions.simulateInput !== undefined);
55715559
setTimeout(() => {
55725560
if (!this.netplay.webRtcReady) {
55735561
console.error("WebRTC connection not established after timeout");
@@ -5584,6 +5572,36 @@ netplayRoomJoined(isOwner, roomName, password, roomId) {
55845572
}
55855573
}
55865574

5575+
netplayLeaveRoom() {
5576+
this.isNetplay = false;
5577+
this.netplay.tabs[0].style.display = "";
5578+
this.netplay.tabs[1].style.display = "none";
5579+
this.netplay.extra = null;
5580+
this.netplay.playerID = null;
5581+
this.netplay.createButton.innerText = this.localization("Create a Room");
5582+
if (this.netplay.socket) {
5583+
this.netplay.socket.disconnect();
5584+
this.netplay.socket = null;
5585+
}
5586+
this.elements.bottomBar.cheat[0].style.display = this.netplay.oldStyles[0];
5587+
this.canvas.style.display = "";
5588+
this.netplayCanvas.style.display = "none";
5589+
this.netplay.videoContainer.style.display = "none";
5590+
this.netplayMenu.style.display = ""; // Restore menu visibility
5591+
// Clean up video element
5592+
if (this.netplay.video) {
5593+
this.netplay.video.srcObject = null;
5594+
if (this.netplay.video.parentElement) {
5595+
this.netplay.video.parentElement.removeChild(this.netplay.video);
5596+
}
5597+
}
5598+
Object.values(this.netplay.peerConnections).forEach(pcData => pcData.pc.close());
5599+
this.netplay.peerConnections = {};
5600+
this.netplay.localStream = null;
5601+
this.netplay.webRtcReady = false;
5602+
console.log("Left netplay room, state cleaned up");
5603+
}
5604+
55875605
// Section 4
55885606
netplayDataMessage(data) {
55895607
if (data["sync-control"]) {
@@ -5603,11 +5621,26 @@ netplayDataMessage(data) {
56035621
}
56045622
});
56055623
}
5624+
if (data.frameData) {
5625+
console.log("Received frame data on Player 2:", data.frameData);
5626+
const ctx = this.canvas.getContext('2d');
5627+
if (data.frameData.pixelSample.every(v => v === 0)) {
5628+
console.warn("Frame data indicates black screen, attempting reconstruction");
5629+
this.reconstructFrame(data.frameData.inputs);
5630+
} else {
5631+
console.log("Frame data indicates content, relying on WebRTC stream");
5632+
}
5633+
}
56065634
}
56075635

5608-
// Section 5
5636+
// NEW Section 5
56095637
netplaySendMessage(data) {
5610-
if (this.netplay.socket) this.netplay.socket.emit("data-message", data);
5638+
if (this.netplay.socket && this.netplay.socket.connected) {
5639+
this.netplay.socket.emit("data-message", data);
5640+
console.log("Sent data message:", data);
5641+
} else {
5642+
console.error("Cannot send message: Socket is not connected");
5643+
}
56115644
}
56125645

56135646
netplayReset() {
@@ -5647,7 +5680,12 @@ netplayUpdateListStart() {
56475680
netplayUpdateListStop() {
56485681
clearInterval(this.netplay.updateListInterval);
56495682
}
5650-
// END New Netplay
5683+
5684+
// Helper method to get user index (assuming simple indexing for now)
5685+
netplayGetUserIndex() {
5686+
return Object.keys(this.netplay.players).indexOf(this.netplay.playerID);
5687+
}
5688+
// NEW END scoding-23
56515689

56525690
createCheatsMenu() {
56535691
const body = this.createPopup("Cheats", {

0 commit comments

Comments
 (0)