diff --git a/src/main/java/io/antmedia/ipcamera/IOnvifCamera.java b/src/main/java/io/antmedia/ipcamera/IOnvifCamera.java index a37c6637b..2e6bbc8a3 100755 --- a/src/main/java/io/antmedia/ipcamera/IOnvifCamera.java +++ b/src/main/java/io/antmedia/ipcamera/IOnvifCamera.java @@ -10,7 +10,7 @@ public interface IOnvifCamera { /** * - * @param address + * @param address it can be direct ip address with port or it can start with http:// or https:// * @param username * @param password * @return true if connected diff --git a/src/main/java/io/antmedia/ipcamera/OnvifCamera.java b/src/main/java/io/antmedia/ipcamera/OnvifCamera.java index b55da62e0..eee4ebc76 100755 --- a/src/main/java/io/antmedia/ipcamera/OnvifCamera.java +++ b/src/main/java/io/antmedia/ipcamera/OnvifCamera.java @@ -26,7 +26,9 @@ public class OnvifCamera implements IOnvifCamera { public static final int CONNECT_ERROR = -1; public static final int AUTHENTICATION_ERROR = -2; String profileToken; - private static final String HTTP = "http://"; + private static final String HTTP_PREFIX = "http://"; + private static final String HTTPS_PREFIX = "https://"; + protected static Logger logger = LoggerFactory.getLogger(OnvifCamera.class); @@ -37,9 +39,12 @@ public int connect(String address, String username, String password) { String camIP = ""; try { - camIP = getURL(address); + //address may include http:// or https:// so get the IPAddress directly + camIP = getIPAddress(address); - nvt = new OnvifDevice(camIP, username, password); + String protocol = getProtocol(address); + + nvt = new OnvifDevice(camIP, protocol, username, password); nvt.getSoap().setLogging(false); nvt.getDevices().getCapabilities().getDevice(); nvt.getDevices().getServices(false); @@ -75,6 +80,19 @@ public int connect(String address, String username, String password) { return result; } + public String getProtocol(String address) { + String protocol = null; + if (address.startsWith(HTTP_PREFIX)) + { + protocol = "http"; + } + else if (address.startsWith(HTTPS_PREFIX)) + { + protocol = "https"; + } + return protocol; + } + @Override public String[] getProfiles() { String profilesStr[] = null; @@ -257,22 +275,23 @@ public boolean setDateTime(java.sql.Date date, java.sql.Time time) { return false; } - public String getURL (String url) { + public String getIPAddress (String url) { String[] ipAddrParts = null; String ipAddr = url; - if(url != null && (url.startsWith(HTTP) || + if(url != null && (url.startsWith(HTTP_PREFIX) || url.startsWith("https://") || url.startsWith("rtmp://") || url.startsWith("rtmps://") || - url.startsWith("rtsp://"))) { + url.startsWith("rtsp://"))) + { ipAddrParts = url.split("//"); ipAddr = ipAddrParts[1]; } - if (ipAddr != null) { - + if (ipAddr != null) + { if (ipAddr.contains("/")){ ipAddrParts = ipAddr.split("/"); ipAddr = ipAddrParts[0]; diff --git a/src/test/java/io/antmedia/test/StreamFetcherUnitTest.java b/src/test/java/io/antmedia/test/StreamFetcherUnitTest.java index ebefb76d9..cd0fc85b9 100644 --- a/src/test/java/io/antmedia/test/StreamFetcherUnitTest.java +++ b/src/test/java/io/antmedia/test/StreamFetcherUnitTest.java @@ -441,10 +441,21 @@ public void testOnvifError() { //it should be -1 because there is a connection error assertEquals(-1, connResult); + + + //Test with protocol + newCam.setIpAddr("http://127.0.0.1:8080"); + connResult = onvif.connect(newCam.getIpAddr(), newCam.getUsername(), newCam.getPassword()); + logger.info("connResult {}", connResult); + + //it should be 0 because URL and credentials are correct + assertEquals(0, connResult); stopCameraEmulator(); } + + diff --git a/src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java b/src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java new file mode 100644 index 000000000..7f185fa6e --- /dev/null +++ b/src/test/java/io/antmedia/test/ipcamera/OnvifCameraTest.java @@ -0,0 +1,44 @@ +package io.antmedia.test.ipcamera; + +import io.antmedia.ipcamera.OnvifCamera; +import io.antmedia.ipcamera.onvif.soap.OnvifDevice; +import junit.framework.TestCase; +import org.junit.Assert; + +public class OnvifCameraTest extends TestCase { + + public void testGetIPAddress() { + + OnvifCamera onvifCamera = new OnvifCamera(); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("http://192.168.1.1:8080")); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("192.168.1.1:8080/test")); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("192.168.1.1:8080/test/test2")); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("rtmp://192.168.1.1:8080/test")); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("rtmps://192.168.1.1:8080/test")); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("http://192.168.1.1:8080/test")); + + Assert.assertEquals("192.168.1.1:8080", onvifCamera.getIPAddress("https://192.168.1.1:8080/test")); + } + + + public void testGetPortAddress() { + + OnvifCamera onvifCamera = new OnvifCamera(); + + Assert.assertEquals("http", onvifCamera.getProtocol("http://192.168.1.1:8080")); + + Assert.assertEquals("https", onvifCamera.getProtocol("https://192.168.1.1:8080/test")); + + Assert.assertNull(onvifCamera.getProtocol("192.168.1.1:8080/test/test2")); + + } + + + +} \ No newline at end of file