如何检测,如果流媒体来源是活着吗? [英] How to detect if the streaming media source is alive?
问题描述
让我们说,有一些类型的,如流媒体来源:
Let's say there are some types of the streaming media sources like:
http://mysite.com/sources/1.flv
RTSP://mysite.com//sources/channel1
rtsp://mysite.com//sources/channel1
http://mysite.com//sources/1.mp4
彩信://mysite.com/channel2
mms://mysite.com/channel2
有没有一种简单的方法来检测,如果他们是活在C#?
Is there a simple way to detect if they are alive in C#?
P.S。如果否决,请解释一下。谢谢!
P.S. If you voted down please explain it. Thanks!
推荐答案
嗯......我现在面临的唯一一种解决方案是实现一些DLL,它通过不同的方式检查所有这些网址:
Well... The only one solution I am facing is implement some DLL that check all those URLs via different ways:
(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
因此,如果状态code
是确定
的链接是活的。
(2)以检查彩信,我们可以使用
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链接,我们可以使用另外一个伟大的图书馆
我发现这一点:
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;
}
}
}
}
因此,我们可以使用
So we can use
if (client.Connected && client.Client != null)
最后,我们必须将所有这些东西放在一起,仅此而已。
Finally we have to join all those things together and that's it.
享受! :)
这篇关于如何检测,如果流媒体来源是活着吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!