Search code examples
rtsphostnameonvif

ONVIF: Listing all hostname of IP cameras in same network


DiscoveryManager manager = new DiscoveryManager();
manager.setDiscoveryTimeout(10000);
manager.discover(new DiscoveryListener() {
    @Override
    public void onDiscoveryStarted() {
        System.out.println("Discovery started");
    }

    @Override
    public void onDevicesFound(List<Device> devices) {
        for (Device device : devices)
            System.out.println("Devices found: " + device.getHostName());
    }
});

I am using above code for get all host names of IP cameras, One thing I wish to mention before describe my problem. I am using IP webcam android app to make my mobile act like IP cam. With this setup above code give all host names. But this snippet didn't give the actual IP camera's hostname. thank you in advance.


Solution

  • Hello guys here is the solution for this question.

    public class OnvifMediaURL {
    
        OnvifManager manager = new OnvifManager();
    
        public void mediaURlFinder(String string) {
    
            OnvifDevice device = new OnvifDevice(string);
            manager.getServices(device, new OnvifServicesListener() {
    
                @Override
                public void onServicesReceived(@NotNull OnvifDevice onvifDevice, OnvifServices paths) {
                }
            });
    
            manager.getDeviceInformation(device, new OnvifDeviceInformationListener() {
                @Override
                public void onDeviceInformationReceived(@NotNull OnvifDevice device,
                        @NotNull OnvifDeviceInformation deviceInformation) {
                    // TODO Auto-generated method stub
                }
            });
    
            manager.getMediaProfiles(device, new OnvifMediaProfilesListener() {
    
                @Override
                public void onMediaProfilesReceived(@NotNull OnvifDevice device,
                        @NotNull List<OnvifMediaProfile> mediaProfiles) {
                    manager.getMediaStreams(device, mediaProfiles.get(0), new OnvifMediaStreamURIListener() {
    
                        @Override
                        public void onMediaStreamURIReceived(@NotNull OnvifDevice device,
                                @NotNull OnvifMediaProfile profile, @NotNull String uri) {
                            // TODO Auto-generated method stub
                            System.out.println("URL " + uri); //Printing RTSP URL
                        }
                    });
    
                }
            });
    
        }
    
    }
    

    Pass the hostname, username and password you got from OnvifDiscovery to the mediaURlFinder( ) method, and it will gie you the RTSP link and you can get some other details like manufacturer, serial number etc.. I want mention that in my case I use my mobile phone as IP camera (using IP Webcam application ) and this code gave RTSP link.