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); } }
public static Profile[] GetProfiles(BaseOnvifTest test, MediaClient client) { Profile[] profiles = null; RunStep(test, () => { profiles = client.GetProfiles(); }, "Get Media profiles"); DoRequestDelay(test); return(profiles); }
public static Profile[] GetProfiles(string url, string login, string pass) { try { MediaClient mediaClient = GetMediaBase(url, login, pass); return(mediaClient.GetProfiles()); } catch { return(new Profile[] { new Profile() }); } }
protected Profile[] GetProfiles() { MediaClient client = MediaClient; if (client == null) { return(null); } Profile[] profiles = null; RunStep(() => { profiles = client.GetProfiles(); }, "Get Profiles"); DoRequestDelay(); return(profiles); }
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; }
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); }
public static string GetPTZToken(Structures set, int profile = 0) { try { 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; Profile[] profiles = mediaClient.GetProfiles(); return(profiles[profile].PTZConfiguration.token); } catch { return(""); } }
private Bitmap onvifPicture(string url, string user, string password) { try { if (mediaClient == null) { AddInfo("Efetuando login em: " + url); // 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 = new MediaClient(bind, mediaAddress); mediaClient.ClientCredentials.UserName.UserName = user; mediaClient.ClientCredentials.UserName.Password = password; } else { AddInfo("Obtem imagem: " + url); } Profile[] profiles = mediaClient.GetProfiles(); string profileToken = profiles[1].token; AddInfo("profileToken: " + profileToken); MediaUri mediaUri = mediaClient.GetSnapshotUri(profileToken); WebRequest requestPic = WebRequest.Create(mediaUri.Uri); WebResponse responsePic = requestPic.GetResponse(); return((Bitmap)Bitmap.FromStream(responsePic.GetResponseStream())); } catch (Exception ex) { AddInfo(ex); return(null); } }
/// <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; } }
public IEnumerable <IMediaProfile> GetProfiles() { var value = CallCamera(() => mediaClient.GetProfiles()); if (value == null) { return(new List <IMediaProfile>()); } return(value.Select(p => new MediaProfile { Name = p.Name, Token = p.token, AudioEncoding = (Services.Media.AudioEncoding)Enum.Parse(typeof(Services.Media.AudioEncoding), p.AudioEncoderConfiguration.Encoding.ToString()), AudioBitrate = p.AudioEncoderConfiguration.Bitrate, VideoEncoding = (Services.Media.VideoEncoding)Enum.Parse(typeof(Services.Media.VideoEncoding), p.VideoEncoderConfiguration.Encoding.ToString()), VideoBitrate = p.VideoEncoderConfiguration.RateControl.BitrateLimit, VideoFrameRate = p.VideoEncoderConfiguration.RateControl.FrameRateLimit, VideoHeight = p.VideoEncoderConfiguration.Resolution.Height, VideoWidth = p.VideoEncoderConfiguration.Resolution.Width })); }
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; } } }
public bool Initialise(string cameraAddress, string userName, string password) { bool result = false; try { var messageElement = new TextMessageEncodingBindingElement() { MessageVersion = MessageVersion.CreateVersion( EnvelopeVersion.Soap12, AddressingVersion.None) }; HttpTransportBindingElement httpBinding = new HttpTransportBindingElement() { AuthenticationScheme = AuthenticationSchemes.Digest }; CustomBinding bind = new CustomBinding(messageElement, httpBinding); mediaClient = new MediaClient(bind, new EndpointAddress($"http://{cameraAddress}/onvif/Media")); 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/PTZ")); ptzClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation; ptzClient.ClientCredentials.HttpDigest.ClientCredential.UserName = userName; ptzClient.ClientCredentials.HttpDigest.ClientCredential.Password = password; var profs = mediaClient.GetProfiles(); profile = mediaClient.GetProfile(profs[0].token); 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, } }; if (relative) { timer = new Timer(TimerInterval); timer.Elapsed += Timer_Elapsed; velocity.PanTilt.space = options.Spaces.RelativePanTiltTranslationSpace[0].URI; panDistance = (options.Spaces.RelativePanTiltTranslationSpace[0].XRange.Max - options.Spaces.RelativePanTiltTranslationSpace[0].XRange.Min) / PanIncrements; tiltDistance = (options.Spaces.RelativePanTiltTranslationSpace[0].YRange.Max - options.Spaces.RelativePanTiltTranslationSpace[0].YRange.Min) / TiltIncrements; } vector = new PTZVector() { PanTilt = new OnvifPTZService.Vector2D() { x = 0, y = 0, space = options.Spaces.RelativePanTiltTranslationSpace[0].URI } }; ErrorMessage = ""; result = initialised = true; } catch (Exception ex) { ErrorMessage = ex.Message; } return(result); }
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); }
private Profile[] GetProfiles(MediaClient mediaClient) { return(mediaClient.GetProfiles()); }