Created
July 19, 2021 14:09
-
-
Save chenxuuu/cc44b579bfb00eda9544f0d735c77268 to your computer and use it in GitHub Desktop.
ffmpeg tcp broadcast server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading; | |
namespace tcpBroadcast | |
{ | |
class Program | |
{ | |
//用来存客户端 | |
private static List<TcpClient> clients = new List<TcpClient>(); | |
static void Main(string[] args) | |
{ | |
Thread cs = new Thread(() => | |
{ | |
TcpListener server = null; | |
try | |
{ | |
// Set the TcpListener on port | |
Int32 port = 23456; | |
IPAddress localAddr = IPAddress.Parse("0.0.0.0"); | |
// TcpListener server = new TcpListener(port); | |
server = new TcpListener(localAddr, port); | |
// Start listening for client requests. | |
server.Start(); | |
Console.WriteLine("wait connections..."); | |
// Enter the listening loop. | |
while (true) | |
{ | |
// Perform a blocking call to accept requests. | |
// You could also use server.AcceptSocket() here. | |
TcpClient client = server.AcceptTcpClient(); | |
Console.WriteLine("New connection!"); | |
// Get a stream object for reading and writing | |
NetworkStream stream = client.GetStream(); | |
client.SendTimeout = 500; | |
lock (clients) | |
clients.Add(client); | |
} | |
} | |
catch (SocketException e) | |
{ | |
Console.WriteLine("SocketException: {0}", e); | |
} | |
finally | |
{ | |
// Stop listening for new clients. | |
server.Stop(); | |
Environment.Exit(1); | |
} | |
}); | |
Thread ss = new Thread(() => | |
{ | |
TcpListener server = null; | |
try | |
{ | |
// Set the TcpListener on port | |
Int32 port = 23455; | |
IPAddress localAddr = IPAddress.Parse("127.0.0.1"); | |
// TcpListener server = new TcpListener(port); | |
server = new TcpListener(localAddr, port); | |
// Start listening for client requests. | |
server.Start(); | |
Console.WriteLine("wait ffmpeg..."); | |
// Enter the listening loop. | |
while (true) | |
{ | |
// Perform a blocking call to accept requests. | |
// You could also use server.AcceptSocket() here. | |
TcpClient client = server.AcceptTcpClient(); | |
Console.WriteLine("ffmpeg connected!"); | |
// Get a stream object for reading and writing | |
NetworkStream stream = client.GetStream(); | |
int i; | |
Byte[] bytes = new Byte[2048]; | |
// Loop to receive all the data sent by the client. | |
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) | |
{ | |
lock (clients) | |
for (int c = clients.Count - 1; c >= 0; c--) | |
{ | |
try | |
{ | |
clients[c].GetStream().Write(bytes, 0, i); | |
} | |
catch | |
{ | |
clients.RemoveAt(c); | |
Console.WriteLine("disconnected"); | |
} | |
} | |
} | |
Console.WriteLine("ffmpeg disconnected!"); | |
} | |
} | |
catch (SocketException e) | |
{ | |
Console.WriteLine("SocketException: {0}", e); | |
} | |
finally | |
{ | |
// Stop listening for new clients. | |
server.Stop(); | |
Environment.Exit(1); | |
} | |
}); | |
Thread ff = new Thread(() => | |
{ | |
while(true) | |
{ | |
try | |
{ | |
var process = new Process() | |
{ | |
StartInfo = new ProcessStartInfo | |
{ | |
FileName = "/bin/bash", | |
Arguments = "-c \"ffmpeg -re -i 'http://ngcdn001.cnr.cn/live/zgzs/index.m3u8' -f mp3 tcp://127.0.0.1:23455\"", | |
RedirectStandardOutput = true, | |
UseShellExecute = false, | |
CreateNoWindow = true, | |
} | |
}; | |
process.Start(); | |
string result = process.StandardOutput.ReadToEnd(); | |
process.WaitForExit(); | |
} | |
catch(Exception e) | |
{ | |
Console.WriteLine(e); | |
} | |
Thread.Sleep(5000); | |
} | |
}); | |
cs.Start(); | |
ss.Start(); | |
ff.Start(); | |
cs.Join(); | |
ss.Join(); | |
Console.WriteLine("exit!"); | |
Environment.Exit(3); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment