Play Raspberry Pi h264 stream in C# app
Ok, so actually I managed to solve this:
Like I said earlier -fps 120 option is there to make the player skip what's in the buffor and play stream as soon as it receives it. PanelId is a handle of a panel in which mplayer is nested.
class Mplayer
{
Process mplayer;
public Mplayer(string path, string pipeName, int panelId)
{
String args = "";
mplayer = new Process();
mplayer.StartInfo.UseShellExecute = false;
mplayer.StartInfo.RedirectStandardInput = true;
mplayer.StartInfo.FileName = path;
args = @"\\.\pipe\" + pipeName + " -demuxer +h264es -fps 120 -nosound -cache 512";
args += " -nofs -noquiet -identify -slave ";
args += " -nomouseinput -sub-fuzziness 1 ";
args += " -vo direct3d, -ao dsound -wid ";
args += panelId;
mplayer.StartInfo.Arguments = args;
}
public void Start()
{
mplayer.Start();
}
public void End()
{
mplayer.Kill();
}
}
The background worker reading stuff from socket:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
pipeServ.WaitForConnection(); //opcjonalne?
StreamWriter sw = new StreamWriter(pipeServ);
sw.AutoFlush = true;
tcpCamera = new TcpClient();
tcpCamera.Connect(ipAddress, camPort);
NetworkStream camStream = tcpCamera.GetStream();
int read = 0;
byte[] bytes = new byte[tcpCamera.ReceiveBufferSize];
while (tcpCamera.Connected)
{
read = camStream.Read(bytes, 0, tcpCamera.ReceiveBufferSize);
if (read > 0)
pipeServ.Write(bytes, 0, read);
}
}
catch (IOException ex)
{
//Broken pipe - result of Mplayer exit
//MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The script running on the RaspberryPi. Portnumber is the number of the port rasp is listening on.
#!/bin/bash
raspivid --width 1280 --height 720 -t 9999999 --framerate 25 --output - | nc -l PORTNUMBER