0

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

http://mysite.com/sources/1.flv

rtsp://mysite.com//sources/channel1

http://mysite.com//sources/1.mp4

彩信://mysite.com/channel2

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

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

4

2 回答 2

2

嗯......我面临的唯一一个解决方案是实现一些 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

所以如果StatusCodeOK链接是活的。

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

最后,我们必须将所有这些东西结合在一起,仅此而已。

享受!!!:)

于 2013-02-14T15:43:20.757 回答
1

我相信您正在寻找 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

于 2013-02-14T14:30:09.547 回答