如何检测流媒体源是否还活着?

假设有一些类型的流媒体源,如:

RTSP://mysite.com//sources/channel1

彩信://mysite.com/channel2

有没有一种简单的方法来检测它们是否在C#中存活?

PS如果你投了票,请解释一下。 谢谢!

嗯…我面临的唯一解决方案是实现一些DLL,通过不同的方式检查所有这些URL:

(1)检查我们可以使用的http / https

 HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://content.bitsontherun.com/videos/bkaovAYt-52qL9xLP.mp4"); WebReq.Method = "GET"; HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); Console.WriteLine(WebResp.StatusCode); // Server Code 

因此,如果StatusCode OK则链接处于活动状态。

(2) 检查我们可以使用的mms

WPF Mediaelement及其活动

 public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; } void MainWindow_Loaded(object sender, RoutedEventArgs e) { try { MediaElement me = new MediaElement(); me.MediaEnded += me_MediaEnded; me.MediaFailed += me_MediaFailed; me.MediaOpened += me_MediaOpened; me.Source = new Uri("mms://95.0.159.131/TRTBELGESEL"); mainGrid.Children.Add(me); } catch (Exception ex) { } } void me_MediaOpened(object sender, RoutedEventArgs e) { Debug.WriteLine("OPENED"); // It means OK! } void me_MediaFailed(object sender, ExceptionRoutedEventArgs e) { Debug.WriteLine("FAILED"); } void me_MediaEnded(object sender, RoutedEventArgs e) { Debug.WriteLine("ENDED"); } 

(3)并检查rtsp链接我们可以使用另一个很棒的库

http://net7mma.codeplex.com/

我找到了这个:

 static void TestRtspClient() { Console.WriteLine("Test #1. Press a key to continue. Press Q to Skip"); if (Console.ReadKey().Key != ConsoleKey.Q) { //Make a client //This host uses Udp but also supports Tcp if Nat fails Rtsp.RtspClient client = new Rtsp.RtspClient("rtsp://178.218.212.102:1935/live/Stream1"); StartTest: //Assign some events (Could log each packet to a dump here) client.OnConnect += (sender, args) => { Console.WriteLine("Connected to :" + client.Location); }; client.OnRequest += (sender, request) => { Console.WriteLine("Client Requested :" + request.Location + " " + request.Method); }; client.OnResponse += (sender, request, response) => { Console.WriteLine("Client got response :" + response.StatusCode + ", for request: " + request.Location + " " + request.Method); }; client.OnPlay += (sender, args) => { //Indicate if LivePlay if (client.LivePlay) { Console.WriteLine("Playing from Live Source"); } //Indicate if StartTime is found if (client.StartTime.HasValue) { Console.WriteLine("Media Start Time:" + client.StartTime); } //Indicate if EndTime is found if (client.EndTime.HasValue) { Console.WriteLine("Media End Time:" + client.EndTime); } }; client.OnDisconnect += (sender, args) => { Console.WriteLine("Disconnected from :" + client.Location); }; try { //Try to StartListening client.StartListening(); } catch (Exception ex) { Console.BackgroundColor = ConsoleColor.Red; Console.WriteLine("Was unable to StartListening: " + ex.Message); Console.BackgroundColor = ConsoleColor.Black; } //If We are connected if (client.Connected && client.Client != null) { //Add some more events once Listening client.Client.RtpPacketReceieved += (sender, rtpPacket) => { Console.WriteLine("Got a RTP packet, SequenceNo = " + rtpPacket.SequenceNumber + " Channel = " + rtpPacket.Channel + " PayloadType = " + rtpPacket.PayloadType + " Length = " + rtpPacket.Length); }; client.Client.RtpFrameChanged += (sender, rtpFrame) => { Console.BackgroundColor = ConsoleColor.Blue; Console.WriteLine("Got a RTPFrame PacketCount = " + rtpFrame.Count + " Complete = " + rtpFrame.Complete); Console.BackgroundColor = ConsoleColor.Black; }; client.Client.RtcpPacketReceieved += (sender, rtcpPacket) => { Console.WriteLine("Got a RTCP packet Channel= " + rtcpPacket.Channel + " Type=" + rtcpPacket.PacketType + " Length=" + rtcpPacket.Length + " Bytes = " + BitConverter.ToString(rtcpPacket.Payload)); }; client.Client.RtcpPacketReceieved += (sender, rtcpPacket) => { Console.BackgroundColor = ConsoleColor.Green; Console.WriteLine("Sent a RTCP packet Channel= " + rtcpPacket.Channel + " Type=" + rtcpPacket.PacketType + " Length=" + rtcpPacket.Length + " Bytes = " + BitConverter.ToString(rtcpPacket.Payload)); Console.BackgroundColor = ConsoleColor.Black; }; Console.WriteLine("Waiting for packets... Press Q to exit"); //Ensure we recieve a bunch of packets before we say the test is good while (Console.ReadKey().Key != ConsoleKey.Q) { } try { //Send a few requests just because var one = client.SendOptions(); var two = client.SendOptions(); Console.BackgroundColor = ConsoleColor.Green; Console.WriteLine("Sending Options Success"); Console.WriteLine(string.Join(" ", one.GetHeaders())); Console.WriteLine(string.Join(" ", two.GetHeaders())); Console.BackgroundColor = ConsoleColor.Black; //Print information before disconnecting Console.BackgroundColor = ConsoleColor.Green; Console.WriteLine("RtcpBytes Sent: " + client.Client.TotalRtcpBytesSent); Console.WriteLine("Rtcp Packets Sent: " + client.Client.TotalRtcpPacketsSent); Console.WriteLine("RtcpBytes Recieved: " + client.Client.TotalRtcpBytesReceieved); Console.WriteLine("Rtcp Packets Recieved: " + client.Client.TotalRtcpPacketsReceieved); Console.WriteLine("Rtp Packets Recieved: " + client.Client.TotalRtpPacketsReceieved); Console.BackgroundColor = ConsoleColor.Black; } catch { Console.BackgroundColor = ConsoleColor.Red; Console.WriteLine("Sending Options Failed"); Console.BackgroundColor = ConsoleColor.Black; } //All done with the client client.StopListening(); } //All done Console.WriteLine("Exiting RtspClient Test"); //Perform another test if we need to if (client.Location.ToString() != "rtsp://fms.zulu.mk/zulu/a2_1") { //Do another test Console.WriteLine("Press a Key to Start Test #2 (Q to Skip)"); if (System.Console.ReadKey().Key != ConsoleKey.Q) { //Try another host (this one uses Tcp and forces the client to switch from Udp because Udp packets usually never arrive) //We will not specify Tcp we will allow the client to switch over automatically client = new Rtsp.RtspClient("rtsp://fms.zulu.mk/zulu/a2_1"); //Switch in 5 seconds rather than the default of 10 client.ProtocolSwitchSeconds = 5; Console.WriteLine("Performing 2nd Client test"); goto StartTest; } } } } 

所以我们可以使用

  if (client.Connected && client.Client != null) 

最后,我们必须将所有这些事情加在一起,就是这样。

请享用!!! 🙂

我相信你正在寻找FileWebRequest类,可以下载流的几个字节来告诉你它们是否处于活动状态。 我不认为这个类只是http,但添加对其他协议的支持应该是微不足道的。 http://msdn.microsoft.com/en-us/library/239dwksc.aspx

更新评论

那么你可以更深入地使用TcpClient类,如果服务器至少监听http,rtsp和mms端口,它可以告诉你它没有做太多工作。 http://msdn.microsoft.com/en-us/library/aa329754(v=vs.71).aspx