Stream webm to node.js from c# application in chunks -
i in process of learning streaming between node.js socket.io , c#.
i have code records screen ffmpeg, redirects standardoutput.basestream , stores memorybuffer, when click stop in application sends memorystream byte array node.js server storing file clients can play it. working fine , here setup that:
c#
bool ffworkerisworking = false; private void btnffmpeg_click(object sender, routedeventargs e) { backgroundworker ffworker = new backgroundworker(); ffworker.workersupportscancellation = true; ffworker.dowork += ((ffworkerobj,ffworkereventargs) => { ffworkerisworking = true; using (var ffprocess = new process()) { var processstartinfo = new processstartinfo { filename = "ffmpeg.exe", redirectstandardinput = true, redirectstandardoutput = true, useshellexecute = false, createnowindow = false, arguments = " -loglevel panic -hide_banner -y -f gdigrab -draw_mouse 1 -i desktop -threads 2 -deadline realtime -f webm -" }; ffprocess.startinfo = processstartinfo; ffprocess.start(); byte[] buffer = new byte[32768]; using (memorystream ms = new memorystream()) { while (!ffprocess.hasexited) { int read = ffprocess.standardoutput.basestream.read(buffer, 0, buffer.length); if (read <= 0) break; ms.write(buffer, 0, read); console.writeline(ms.length); if (!ffworkerisworking) { clientsocket.emit("video", ms.toarray()); ffworker.cancelasync(); break; } } } } }); ffworker.runworkerasync(); }
js (server)
socket.on('video', function(data) { fs.appendfile('public/footest.webm', data, function (err) { if (err) throw err; console.log('file uploaded'); }); });
now need change code instead of sending whole file should sends chunks of byte arrays instead of whole video, , node create file , append chunks of byte arrays received. ok sound easy enough, apparently not.
i need somehow instruct code use offset , bytes after offset , update offset.
on server side think best approach create file , append byte arrays file received.
on server side this:
js (server)
var buffer = new buffer(32768); var isbuffering = false; socket.on('video', function(data) { //concatenate buffer incoming data , broadcast.emit clients });
how able setup offset bytes sent , update offset, , how approach way of concatenating data initialized buffer?
i have tried write code reads offset end , seems working although video when added in node black:
c#
while (!ffprocess.hasexited) { int read = ffprocess.standardoutput.basestream.read(buffer, 0, buffer.length); if (read <= 0) break; int offset = (read - buffer.length > 0 ? read - buffer.length : 0); ms.write(buffer, offset, read); clientsocket.emit("videochunk", buffer.toarray()); if (!ffworkerisworking) { ffworker.cancelasync(); break; } }
node console output
js (server)
socket.on('videochunk', function(data) { if (!isbufferingdone) { buffer = buffer.concat([buffer, data]); console.log(data.length); } }); socket.on('cancelvideo', function() { isbufferingdone = true; settimeout(function() { fs.writefile("public/test.webm", buffer, function(err) { if(err) { return console.log(err); } console.log("the file saved!"); buffer = new buffer(32768); }); }, 1000); });
js (client)
socket.on('video', function(filepath) { console.log('path: ' + filepath); $('#videosource').attr('src',filepath); $('#video').play(); });
thanks!
Comments
Post a Comment