Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem switching videos #424

Open
TXMengXue opened this issue Feb 21, 2024 · 2 comments
Open

Problem switching videos #424

TXMengXue opened this issue Feb 21, 2024 · 2 comments

Comments

@TXMengXue
Copy link

const canvas = document.getElementById('videoCanvas');

if (player) { 
    player.destroy();
}
player = new JSMpeg.Player(url, {canvas: canvas}); 

image

@TXMengXue
Copy link
Author

var elementCanvas = document.getElementById('videoCanvas');
if (player) {
    player.destroy();
    var ctx = elementCanvas.getContext("2d");
    ctx.fillStyle = "black"; 
    ctx.fillRect(0, 0, elementCanvas.width, elementCanvas.height); 
}
player = new JSMpeg.Player('ws://localhost:8083/', {canvas: elementCanvas}); 

image

@phoboslab
Copy link
Owner

Works for me™

Tested with

html:

<canvas id="video-canvas"></canvas>
<script type="text/javascript" src="jsmpeg.min.js"></script>
<script type="text/javascript">
	let player = null;
	let port = '8082';
	let canvas = document.getElementById('video-canvas');
	let toggleStreams = function() {
		port = port === '8082' ? '8084' : '8082';
		if (player) {
			player.destroy();
		}
		let url = 'ws://'+document.location.hostname+':'+port;
		player = new JSMpeg.Player(url, {canvas: canvas});
	}
	toggleStreams();
</script>
<button onclick="toggleStreams()">switch</button>

Terminal 1:

node websocket-relay.js zomg 8081 8082

Terminal 2:

ffmpeg -stream_loop -1 -re -i video.mp4 -f mpegts -codec:v mpeg1video -codec:a mp2 -b 0 http://127.0.0.1:8081/zomg

Terminal 3:

node websocket-relay.js zomg 8083 8084

Terminal 4:

# same input video but different size (`-s wxh`)
ffmpeg -stream_loop -1 -re -i video.mp4 -f mpegts -codec:v mpeg1video -codec:a mp2 -s 320x180 -b 0 http://127.0.0.1:8083/zomg
Video_2024-02-24_21-57-03.mp4

It's curious that you were able to do canvas.getContext("2d");, because the default canvas context will be WebGL, if disableGl:true is not specified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants