0

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?

Dan-Levi Tømta
  • 796
  • 3
  • 14
  • 29

1 Answers1

1

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).

Community
  • 1
  • 1
aergistal
  • 29,947
  • 5
  • 70
  • 92
  • Thanks, looking into this right away. About the format, im not sure what is best, as i im very fresh on this subject as of now. i will return here when i have learned more and share. – Dan-Levi Tømta Aug 07 '15 at 09:56
  • I have read into capturing the binary output of the process standaroutput and now the output is sent to node as a byte array. I have updated the question with the new code. I am still having problems getting this to work but i think i am closer to the solution for my question. Care to take a look? – Dan-Levi Tømta Aug 07 '15 at 14:40
  • Test the `ffmpeg` command first by writing to a `flv` file, just to make sure it works. On Linux I managed to send it over a socket to VLC and it works. – aergistal Aug 07 '15 at 15:10
  • Ill mark this as the answer, the video is successfully transfered. – Dan-Levi Tømta Aug 07 '15 at 17:34