Example #1
0
        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            bool flag = profiles != null && combobox.SelectedIndex >= 0;

            if (flag)
            {
                StreamSetup streamSetup = new StreamSetup();
                streamSetup.Stream             = StreamType.RTPUnicast;
                streamSetup.Transport          = new Transport();
                streamSetup.Transport.Protocol = TransportProtocol.RTSP;
                MediaUri mediaUri = new MediaUri();
                mediaUri = mediaClient.GetStreamUri(streamSetup, profiles[combobox.SelectedIndex].token);
                UriBuilder uriBuilder = new UriBuilder(mediaUri.Uri);
                uriBuilder.Scheme = "rtsp";
                string[] options = new string[]
                {
                    ":rtsp-http",
                    ":rtsp-http-port=" + uriBuilder.Port.ToString(),
                    ":rtsp-user="******":rtsp-pwd=" + campassword,
                    ":network-caching=300"
                };
                control.Play(uriBuilder.Uri, options);
                stop_play.IsEnabled = true;
                playingrui          = uriBuilder.Uri;
            }
            recordcheckbox.IsEnabled = true;
        }
Example #2
0
        private Uri GetMulticastUri(Camera cam, MediaClient mediaClient, Profile mediaProfile)
        {
            if (mediaProfile?.VideoEncoderConfiguration?.Multicast != null && mediaProfile?.VideoEncoderConfiguration?.Multicast.Port != 0)
            {
                // Check for any URI supporting multicast
                foreach (TransportProtocol protocol in Enum.GetValues(typeof(TransportProtocol)))
                {
                    // Get stream URI for the requested transport/protocol and insert the User/Password if present
                    Transport transport = new Transport()
                    {
                        Protocol = protocol
                    };
                    StreamSetup ss = new StreamSetup()
                    {
                        Stream = StreamType.RTPMulticast
                    };
                    ss.Transport = transport;

                    try
                    {
                        MediaUri mu = mediaClient.GetStreamUri(ss, MediaProfile.token);
                        log.Debug(string.Format("Camera #{0} [{1}] Onvif media profile ({2}) capable of multicast [multicast URI: {3}]", cam.Number, cam.IP, mediaProfile.Name, mu.Uri));
                        return(new Uri(mu.Uri));
                    }
                    catch { } // Ignore exception and continue checking for a multicast URI
                }
            }
            else
            {
                log.Debug(string.Format("Camera #{0} [{1}] Onvif media profile ({2}) does not support multicast", cam.Number, cam.IP, mediaProfile.Name));
            }

            return(null);
        }
Example #3
0
 private void onvifVideo(string url, string user, string password)
 {
     try
     {
         AddInfo("Efetuando login em " + url);
         // http://stackoverflow.com/questions/31007828/onvif-getstreamurl-c-sharp
         // Adicionar a referencia de serviço: http://www.onvif.org/onvif/ver10/media/wsdl/media.wsdl
         // http://stackoverflow.com/questions/32779467/onvif-api-capture-image-in-c-sharp
         var messageElement = new TextMessageEncodingBindingElement();
         messageElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None);
         HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();
         httpBinding.AuthenticationScheme = AuthenticationSchemes.Basic;
         CustomBinding   bind         = new CustomBinding(messageElement, httpBinding);
         EndpointAddress mediaAddress = new EndpointAddress(url + "/onvif/Media");
         MediaClient     mediaClient  = new MediaClient(bind, mediaAddress);
         mediaClient.ClientCredentials.UserName.UserName = user;
         mediaClient.ClientCredentials.UserName.Password = password;
         Profile[]   profiles    = mediaClient.GetProfiles();
         StreamSetup streamSetup = new StreamSetup();
         streamSetup.Stream             = StreamType.RTPUnicast;
         streamSetup.Transport          = new Transport();
         streamSetup.Transport.Protocol = TransportProtocol.RTSP;
         var uri = mediaClient.GetStreamUri(streamSetup, profiles[1].token);
         AddInfo(uri.Uri);
     }
     catch (Exception ex)
     {
         AddInfo(ex);
     }
 }
Example #4
0
        public static string GetStreamURL(Structures set, int profile = 0)
        {
            try
            {
                if (set.GetMediaTokens().FirstOrDefault() == "" || !set.IsActive)
                {
                    return("");
                }
                var messageElement = new TextMessageEncodingBindingElement();
                messageElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None);
                HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();
                httpBinding.AuthenticationScheme = AuthenticationSchemes.Basic;
                CustomBinding   bind         = new CustomBinding(messageElement, httpBinding);
                EndpointAddress mediaAddress = new EndpointAddress(set.GetONVIF + "/onvif/Media");
                MediaClient     mediaClient  = new MediaClient(bind, mediaAddress);
                mediaClient.ClientCredentials.UserName.UserName = set.Login;
                mediaClient.ClientCredentials.UserName.Password = set.Password;

                ServiceReference1.StreamSetup ss = new ServiceReference1.StreamSetup();
                ss.Stream             = ServiceReference1.StreamType.RTPUnicast;
                ss.Transport          = new ServiceReference1.Transport();
                ss.Transport.Protocol = ServiceReference1.TransportProtocol.RTSP;
                MediaUri mediaUri = mediaClient.GetStreamUri(ss, set.GetMediaTokens()[profile]);
                return(mediaUri.Uri);
            }
            catch
            {
                return("");
            }
        }
Example #5
0
    void Connect()
    {
        HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();

        httpBinding.AuthenticationScheme = AuthenticationSchemes.Digest;
        var messageElement = new TextMessageEncodingBindingElement();

        // FIXME: Probe for Soap12 if Soap11 fails
        messageElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.None);
        CustomBinding bind = new CustomBinding(messageElement, httpBinding);

        media = new MediaClient(bind, new EndpointAddress(uriEntry.Text + "media_service"));
        media.ClientCredentials.UserName.UserName = @"admin";
        media.ClientCredentials.UserName.Password = @"admin";

        ptz = new PTZClient(bind, new EndpointAddress(uriEntry.Text + "ptz_service"));
        ptz.ClientCredentials.UserName.UserName = @"admin";
        ptz.ClientCredentials.UserName.Password = @"admin";

        foreach (var v in media.GetVideoSources())
        {
            Console.WriteLine(v.Resolution.Width + "x" + v.Resolution.Height);
            Console.WriteLine(v.token);
        }

        profile = media.GetProfiles().First();
        Console.WriteLine(profile.VideoSourceConfiguration.Name);
        Console.WriteLine(profile.VideoEncoderConfiguration.Resolution.Width + " " + profile.VideoEncoderConfiguration.Resolution.Height);

        Transport t = new Transport
        {
            Protocol = TransportProtocol.RTSP,
            Tunnel   = null,
        };

        Console.WriteLine(media.GetStreamUri(
                              new StreamSetup {
            Stream = StreamType.RTPUnicast, Transport = t
        },
                              profile.token).Uri);

        xmax          = profile.PTZConfiguration.PanTiltLimits.Range.XRange.Max;
        xmin          = profile.PTZConfiguration.PanTiltLimits.Range.XRange.Min;
        ymax          = profile.PTZConfiguration.PanTiltLimits.Range.YRange.Max;
        ymin          = profile.PTZConfiguration.PanTiltLimits.Range.YRange.Min;
        zmax          = profile.PTZConfiguration.ZoomLimits.Range.XRange.Max;
        zmin          = profile.PTZConfiguration.ZoomLimits.Range.XRange.Min;
        tmax          = "1";
        hbox1.Visible = true;
    }
Example #6
0
        public string GetVideoURL()
        {
            StreamSetup streamSetup = new StreamSetup();

            streamSetup.Stream             = StreamType.RTPUnicast;
            streamSetup.Transport          = new Transport();
            streamSetup.Transport.Protocol = TransportProtocol.HTTP;

            var value = CallCamera(() => mediaClient.GetStreamUri(streamSetup, CurrentProfile.Token));

            if (value == null)
            {
                return(null);
            }

            return(value.Uri);
        }
Example #7
0
        static int Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage: OnvifCamReader.exe <url> <login> <password>");
                return(1);
            }

            string url      = args[0];
            string login    = args[1];
            string password = args[2];

            var messageElement = new TextMessageEncodingBindingElement();

            messageElement.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None);
            HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();

            httpBinding.AuthenticationScheme = AuthenticationSchemes.Basic;
            CustomBinding bind = new CustomBinding(messageElement, httpBinding);

            EndpointAddress mediaAddress = new EndpointAddress(url);
            MediaClient     mediaClient  = new MediaClient(bind, mediaAddress);

            mediaClient.ClientCredentials.UserName.UserName = login;
            mediaClient.ClientCredentials.UserName.Password = password;

            Profile[] profiles     = mediaClient.GetProfiles();
            string    profileToken = profiles[0].token;
            MediaUri  mediaUri     = mediaClient.GetSnapshotUri(profileToken);

            StreamSetup streamSetup = new StreamSetup();

            streamSetup.Stream = StreamType.RTPUnicast;

            streamSetup.Transport          = new Transport();
            streamSetup.Transport.Protocol = TransportProtocol.RTSP;

            MediaUri mediaStreamUri = mediaClient.GetStreamUri(streamSetup, profileToken);

            Console.WriteLine(mediaUri.Uri.ToString());
            Console.WriteLine(mediaStreamUri.Uri.ToString());

            return(0);
        }
Example #8
0
        /// <summary>
        /// Retrieves Onvif video stream URIs from the device and stores them in the StreamUris list
        /// </summary>
        /// <param name="onvifPort">Port to connect on (normally HTTP - 80)</param>
        private void GetStreamUris(Camera cam, int onvifPort, StreamType sType, TransportProtocol tProtocol, int StreamIndex)
        {
            //StreamUris.Clear();
            MediaClient mc = OnvifServices.GetOnvifMediaClient(ServiceUris[OnvifNamespace.MEDIA], DeviceTimeOffset, cam.User, cam.Password);

            Profile[] mediaProfiles = mc.GetProfiles();

            StreamSetup ss        = new StreamSetup();
            Transport   transport = new Transport()
            {
                Protocol = tProtocol
            };
            string uri = string.Empty;

            // Only store the Profile related to the StreamIndex from the XML file
            MediaProfile = mediaProfiles[StreamIndex - 1];

            // Get stream URI for the requested transport/protocol and insert the User/Password if present
            ss.Stream    = sType;
            ss.Transport = transport;

            Uri mu = new Uri(mc.GetStreamUri(ss, MediaProfile.token).Uri);

            StreamUri = RTSP_Viewer.Classes.Utilities.InsertUriCredentials(mu, cam.User, cam.Password);

            // Get multicast uri (if available) along with requested protocol/stream type
            MulticastUri = GetMulticastUri(cam, mc, MediaProfile);  // Not being used currently
            MulticastUri = RTSP_Viewer.Classes.Utilities.InsertUriCredentials(MulticastUri, cam.User, cam.Password);

            // A PTZ may not have a PTZ configuration for a particular media profile
            // Disable PTZ access in that case
            IsPtzEnabled = IsPtz;
            if (MediaProfile.PTZConfiguration == null && IsPtz)
            {
                log.Warn(string.Format("Camera #{0} [{1}] Disabling PTZ control based on the PTZConfiguration being null for stream profile {0}", cam.Number, cam.IP, StreamUri));
                IsPtzEnabled = false;
            }
        }
Example #9
0
        private void Connect()
        {
            while (true)
            {
                try
                {
                    try
                    {
                        var profiles    = MediaClient.GetProfiles().ToArray();
                        var streamSetup = new StreamSetup
                        {
                            Stream    = StreamType.RTPUnicast,
                            Transport = new Transport {
                                Protocol = TransportProtocol.RTSP
                            }
                        };
                        List <MediaEndpoint> uris = new List <MediaEndpoint>();

                        foreach (var p in profiles)
                        {
                            var l = MediaClient.GetStreamUri(streamSetup, p.token);
                            //make sure using correct ip address (for external access)
                            var u = new UriBuilder(l.Uri)
                            {
                                Host = URL.Host
                            };
                            if (RtspPort > 0)
                            {
                                u.Port = RtspPort;
                            }

                            l.Uri = u.ToString();
                            if (!string.IsNullOrEmpty(Username))
                            {
                                l.Uri = l.Uri.ReplaceFirst("://", "://" + Uri.EscapeDataString(Username) + ":" + Uri.EscapeDataString(Password) + "@");
                            }

                            var s = p?.VideoEncoderConfiguration;
                            if (s != null)
                            {
                                uris.Add(new MediaEndpoint(l, s));
                            }
                            else
                            {
                                var e = p?.VideoSourceConfiguration;
                                if (e != null)
                                {
                                    uris.Add(new MediaEndpoint(l, e));
                                }
                            }
                        }
                        _mediaEndpoints = uris.ToArray();
                        _profiles       = profiles;
                    }
                    catch (ProtocolException pex)
                    {
                        var wex = pex.InnerException as WebException;
                        if (wex != null)
                        {
                            throw wex;
                        }
                    }

                    break;
                }
                catch (Exception ex)
                {
                    _mediaClient = null;
                    if (!(ex is TimeoutException))
                    {
                        var wex = ex as WebException;
                        var wwwAuthenticateHeader = wex?.Response?.Headers["WWW-Authenticate"];
                        if (!string.IsNullOrEmpty(wwwAuthenticateHeader) && _auth != OnvifAuthMode.Digest)
                        {
                            try
                            {
                                realm  = GetDigestHeaderAttribute("realm", wwwAuthenticateHeader);
                                nonce  = GetDigestHeaderAttribute("nonce", wwwAuthenticateHeader);
                                qop    = GetDigestHeaderAttribute("qop", wwwAuthenticateHeader);
                                opaque = GetDigestHeaderAttribute("opaque", wwwAuthenticateHeader);
                                cnonce = new Random().Next(123400, 9999999).ToString();
                                if (!string.IsNullOrEmpty(nonce))
                                {
                                    _auth = OnvifAuthMode.Digest;
                                    continue;
                                }
                            }
                            catch (ApplicationException aex)
                            {
                                //not a digest request
                            }
                        }

                        switch (_auth)
                        {
                        case OnvifAuthMode.None:
                            _auth = OnvifAuthMode.Basic;
                            continue;

                        case OnvifAuthMode.Basic:
                            _auth = OnvifAuthMode.UsernameToken;
                            continue;

                        case OnvifAuthMode.UsernameToken:
                            if (!string.IsNullOrEmpty(realm))
                            {
                                _auth = OnvifAuthMode.Digest;
                                continue;
                            }

                            break;
                        }
                    }

                    _auth = OnvifAuthMode.None;
                    Logger.LogException(ex, "Onvif Auth");
                    break;
                }
            }
        }
Example #10
0
        private List <string> GetMediaProfileUris(MediaClient mclient, Profile p)
        {
            List <string> uris = new List <string>();
            StreamSetup   ss   = new StreamSetup();

            // Unicast options
            ss.Stream = StreamType.RTPUnicast;

            ss.Transport = new Transport()
            {
                Protocol = TransportProtocol.HTTP
            };

            try
            {
                MediaUri mu = mclient.GetStreamUri(ss, p.token);
                uris.Add(ss.Transport.Protocol.ToString() + "\t" + mu.Uri + " (" + ss.Stream.ToString() + ")");
            }
            catch { };

            ss.Transport = new Transport()
            {
                Protocol = TransportProtocol.RTSP
            };
            try
            {
                MediaUri mu = mclient.GetStreamUri(ss, p.token);
                uris.Add(ss.Transport.Protocol.ToString() + "\t" + mu.Uri + " (" + ss.Stream.ToString() + ")");
            }
            catch { };

            ss.Transport = new Transport()
            {
                Protocol = TransportProtocol.TCP
            };
            try
            {
                MediaUri mu = mclient.GetStreamUri(ss, p.token);
                uris.Add(ss.Transport.Protocol.ToString() + "\t" + mu.Uri + " (" + ss.Stream.ToString() + ")");
            }
            catch { };

            ss.Transport = new Transport()
            {
                Protocol = TransportProtocol.UDP
            };
            try
            {
                MediaUri mu = mclient.GetStreamUri(ss, p.token);
                uris.Add(ss.Transport.Protocol.ToString() + "\t" + mu.Uri + " (" + ss.Stream.ToString() + ")");
            }
            catch { };

            // Multicast options
            ss.Stream = StreamType.RTPMulticast;

            ss.Transport = new Transport()
            {
                Protocol = TransportProtocol.HTTP
            };

            try
            {
                MediaUri mu = mclient.GetStreamUri(ss, p.token);
                uris.Add(ss.Transport.Protocol.ToString() + "\t" + mu.Uri + " (" + ss.Stream.ToString() + ")");
            }
            catch { };

            ss.Transport = new Transport()
            {
                Protocol = TransportProtocol.RTSP
            };
            try
            {
                MediaUri mu = mclient.GetStreamUri(ss, p.token);
                uris.Add(ss.Transport.Protocol.ToString() + "\t" + mu.Uri + " (" + ss.Stream.ToString() + ")");
            }
            catch { };

            ss.Transport = new Transport()
            {
                Protocol = TransportProtocol.TCP
            };
            try
            {
                MediaUri mu = mclient.GetStreamUri(ss, p.token);
                uris.Add(ss.Transport.Protocol.ToString() + "\t" + mu.Uri + " (" + ss.Stream.ToString() + ")");
            }
            catch { };

            ss.Transport = new Transport()
            {
                Protocol = TransportProtocol.UDP
            };
            try
            {
                MediaUri mu = mclient.GetStreamUri(ss, p.token);
                uris.Add(ss.Transport.Protocol.ToString() + "\t" + mu.Uri + " (" + ss.Stream.ToString() + ")");
            }
            catch { };

            return(uris);
        }
Example #11
0
        public bool Initialise(string cameraAddress, string userName, string password)
        {
            bool result = false;

            cam_ip = cameraAddress;
            cam_id = userName;
            cam_pw = password;

            //ConnectCam();
            try
            {
                var messageElement = new TextMessageEncodingBindingElement()
                {
                    MessageVersion = MessageVersion.CreateVersion(
                        EnvelopeVersion.Soap12, AddressingVersion.None)
                };
                HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();
                httpBinding.AuthenticationScheme = System.Net.AuthenticationSchemes.Digest;
                CustomBinding bind = new CustomBinding(messageElement, httpBinding);
                //mediaClient = new Media2Client(bind,
                //  new EndpointAddress($"http://{cameraAddress}/onvif/device_service"));
                //mediaClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel =
                //  System.Security.Principal.TokenImpersonationLevel.Impersonation;
                //mediaClient.ClientCredentials.HttpDigest.ClientCredential.UserName = userName;
                //mediaClient.ClientCredentials.HttpDigest.ClientCredential.Password = password;
                //ptzClient = new PTZClient(bind,
                //  new EndpointAddress($"http://{cameraAddress}/onvif/device_service"));
                //ptzClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel =
                //  System.Security.Principal.TokenImpersonationLevel.Impersonation;
                //ptzClient.ClientCredentials.HttpDigest.ClientCredential.UserName = userName;
                //ptzClient.ClientCredentials.HttpDigest.ClientCredential.Password = password;

                deviceclient = new DeviceClient(bind,
                                                new EndpointAddress($"http://{cameraAddress}/onvif/device_service"));
                deviceclient.ClientCredentials.HttpDigest.AllowedImpersonationLevel =
                    System.Security.Principal.TokenImpersonationLevel.Impersonation;
                deviceclient.ClientCredentials.HttpDigest.ClientCredential.UserName = userName;
                deviceclient.ClientCredentials.HttpDigest.ClientCredential.Password = password;

                mediaClient10 = new MediaClient(bind,
                                                new EndpointAddress($"http://{cameraAddress}/onvif/media_service"));
                mediaClient10.ClientCredentials.HttpDigest.AllowedImpersonationLevel =
                    System.Security.Principal.TokenImpersonationLevel.Impersonation;
                mediaClient10.ClientCredentials.HttpDigest.ClientCredential.UserName = userName;
                mediaClient10.ClientCredentials.HttpDigest.ClientCredential.Password = password;



                //onvif ver 1.0
                var profs = mediaClient10.GetProfiles();

                profile10 = mediaClient10.GetProfile(profs[0].token);

                ONVIF_MediaProfilDashboard.Media10.StreamSetup streamSetup = new ONVIF_MediaProfilDashboard.Media10.StreamSetup()
                {
                };

                streamSetup.Stream             = StreamType.RTPUnicast;
                streamSetup.Transport          = new Transport();
                streamSetup.Transport.Protocol = TransportProtocol.RTSP;



                MediaUri mediaUri = mediaClient10.GetStreamUri(streamSetup, profs[0].token);

                //string string2222 = deviceclient.GetWsdlUrl();



                var configs = ptzClient.GetConfigurations();

                options = ptzClient.GetConfigurationOptions(configs[0].token);

                velocity = new OnvifPTZService.PTZSpeed()
                {
                    PanTilt = new OnvifPTZService.Vector2D()
                    {
                        x     = 0,
                        y     = 0,
                        space = options.Spaces.ContinuousPanTiltVelocitySpace[0].URI,
                    },
                    Zoom = new OnvifPTZService.Vector1D()
                    {
                        x     = 0,
                        space = options.Spaces.ContinuousZoomVelocitySpace[0].URI,
                    }
                };

                ErrorMessage = "";
                result       = initialised = true;
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
            }
            return(result);
        }