Reputation: 846
so i am having some problems with understanding how to pull this off. I know that streaming video is a topic that is hard, and that there is a lot to take account off, but anyway here we go to start of learning how to stream video.
I am using SocketIoClientDotNet as the node.js client for the c# application.
I am sending byte arrays of the video to node which are creating a temporary file and appending the buffer to it. I have tried to set the source of the video element to that file but it doesnt read it as video and are all black. I have tried to download a copy of the file since it did not work and it turns out vlc cant play it either. Ok to the code:
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 = true,
Arguments = " -loglevel panic -hide_banner -y -f gdigrab -draw_mouse 1 -i desktop -f flv -"
};
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);
clientSocket.Emit("video", ms.ToArray());
ms.Flush();
if (!ffWorkerIsWorking)
{
ffWorker.CancelAsync();
break;
}
}
}
}
});
ffWorker.RunWorkerAsync();
}
JS (server)
var buffer = new Buffer(32768);
var isBuffering = false;
var wstream;
socket.on('video', function(data) {
if(!isBuffering){
wstream = fs.createWriteStream('fooTest.flv');
isBuffering = true;
}
buffer = Buffer.concat([buffer, data]);
fs.appendFile('public/fooTest.flv', buffer, function (err) {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
});
What am i doing wrong here?
Upvotes: 0
Views: 2228
Reputation: 31209
With the OutputDataReceived
event you capture the text
output of the process stdout. That's why in the first case the server complains about the UTF-8 encoding. Your second example works because you're sending a binary stream.
You need to capture the binary base stream. See this answer on how to do it: Capturing binary output from Process.StandardOutput
I don't know how you plan to stream exactly, but if you use FLV there are already HTTP/RTMP servers you can use (eg. Nginx with the RTMP module).
Upvotes: 1