Skip to content

Commit 3b5dc21

Browse files
authored
Merge pull request #4937 from ant-media/onvifCameraUrlEditFix
Refactor updateStreamSource method
2 parents 8609722 + 882efd8 commit 3b5dc21

File tree

2 files changed

+45
-50
lines changed

2 files changed

+45
-50
lines changed

src/main/java/io/antmedia/ipcamera/OnvifCamera.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public int connect(String address, String username, String password) {
3636
int result = CONNECT_ERROR;
3737
String camIP = "";
3838
try {
39-
39+
4040
camIP = getURL(address);
41-
41+
4242
nvt = new OnvifDevice(camIP, username, password);
4343
nvt.getSoap().setLogging(false);
4444
nvt.getDevices().getCapabilities().getDevice();
@@ -47,7 +47,7 @@ public int connect(String address, String username, String password) {
4747
profiles = nvt.getDevices().getProfiles();
4848

4949

50-
if (profiles != null)
50+
if (profiles != null)
5151
{
5252
for (Profile profile : profiles) {
5353
if (profile.getPTZConfiguration() != null) {
@@ -58,7 +58,7 @@ public int connect(String address, String username, String password) {
5858
if (profileToken == null) {
5959
profileToken = profiles.get(0).getToken();
6060
}
61-
61+
6262
result = CONNECTION_SUCCESS;
6363
}
6464
else {
@@ -71,20 +71,20 @@ public int connect(String address, String username, String password) {
7171

7272
//connection error. Let the user check ip address
7373
result = CONNECT_ERROR;
74-
}
74+
}
7575
return result;
7676
}
77-
77+
7878
@Override
7979
public String[] getProfiles() {
8080
String profilesStr[] = null;
8181
try {
8282
List<Profile> profilesLocal = nvt.getDevices().getProfiles();
8383

84-
if (profilesLocal != null)
84+
if (profilesLocal != null)
8585
{
8686
int i = 0;
87-
profilesStr = new String[profilesLocal.size()];
87+
profilesStr = new String[profilesLocal.size()];
8888
for (Profile profile : profilesLocal) {
8989
if (profile.getPTZConfiguration() != null) {
9090
profilesStr[i++] = nvt.getMedia().getRTSPStreamUri(profile.getToken());
@@ -93,7 +93,7 @@ public String[] getProfiles() {
9393
}
9494
} catch (ConnectException | SOAPException e) {
9595
// nothing to do
96-
}
96+
}
9797
return profilesStr;
9898
}
9999

@@ -125,19 +125,19 @@ public String getTCPStreamURI() {
125125

126126
} catch (ConnectException | SOAPException e) {
127127
logger.error(ExceptionUtils.getStackTrace(e));
128-
}
128+
}
129129
return rtspURL;
130130
}
131-
132-
131+
132+
133133
public boolean moveContinous(float x, float y, float zoom) {
134134
return ptzDevices.continuousMove(profileToken, x, y, zoom);
135135
}
136-
136+
137137
public boolean moveRelative(float x, float y, float zoom) {
138138
return ptzDevices.relativeMove(profileToken, x, y, zoom);
139139
}
140-
140+
141141
public boolean moveAbsolute(float x, float y, float zoom) {
142142
boolean result = false;
143143
try {
@@ -256,7 +256,7 @@ public boolean isFocusModeAuto() {
256256
public boolean setDateTime(java.sql.Date date, java.sql.Time time) {
257257
return false;
258258
}
259-
259+
260260
public String getURL (String url) {
261261

262262
String[] ipAddrParts = null;
@@ -284,4 +284,4 @@ public String getURL (String url) {
284284
return ipAddr;
285285
}
286286

287-
}
287+
}

src/main/java/io/antmedia/rest/RestServiceBase.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -438,50 +438,45 @@ private static void removeEmptyPlayListItems(Broadcast broadcast)
438438
* @return
439439
*/
440440
protected Result updateStreamSource(String streamId, Broadcast broadcast) {
441+
logger.debug("Updating camera info for stream {}", broadcast.getStreamId());
441442

442-
boolean result = false;
443-
444-
boolean resultStopStreaming = false;
445-
446-
logger.debug("update cam info for stream {}", broadcast.getStreamId());
447-
448-
if(checkStreamUrl(broadcast.getStreamUrl()))
449-
{
450-
Broadcast broadcastInDB = getDataStore().get(streamId);
451-
if (broadcastInDB != null)
452-
{
453-
resultStopStreaming = checkStopStreaming(broadcastInDB);
454-
455-
waitStopStreaming(broadcastInDB, resultStopStreaming);
456-
457-
if (AntMediaApplicationAdapter.IP_CAMERA.equals(broadcast.getType())) {
458-
String rtspURL = connectToCamera(broadcast).getMessage();
459-
460-
if (rtspURL != null) {
443+
if (!checkStreamUrl(broadcast.getStreamUrl())) {
444+
return new Result(false, "Stream URL is not valid");
445+
}
461446

462-
String authparam = broadcast.getUsername() + ":" + broadcast.getPassword() + "@";
463-
String rtspURLWithAuth = RTSP + authparam + rtspURL.substring(RTSP.length());
464-
logger.info("new RTSP URL: {}" , rtspURLWithAuth);
465-
broadcast.setStreamUrl(rtspURLWithAuth);
466-
}
467-
}
447+
Broadcast broadcastInDB = getDataStore().get(streamId);
448+
if (broadcastInDB == null) {
449+
streamId = streamId.replaceAll("[\n|\r|\t]", "_");
450+
logger.info("Broadcast with stream id: {} is null", streamId);
451+
return new Result(false, "Broadcast with streamId: " + streamId + " does not exist");
452+
}
468453

469-
result = getDataStore().updateBroadcastFields(streamId, broadcast);
454+
boolean resultStopStreaming = checkStopStreaming(broadcastInDB);
455+
waitStopStreaming(broadcastInDB, resultStopStreaming);
470456

471-
if(result) {
472-
Broadcast fetchedBroadcast = getDataStore().get(streamId);
473-
getApplication().startStreaming(fetchedBroadcast);
474-
}
475-
}
476-
else {
477-
streamId = streamId.replaceAll("[\n|\r|\t]", "_");
478-
logger.info("Broadcast with stream id: {} is null", streamId);
457+
if (AntMediaApplicationAdapter.IP_CAMERA.equals(broadcast.getType())) {
458+
Result connectionRes = connectToCamera(broadcast);
459+
if (!connectionRes.isSuccess()) {
460+
return connectionRes;
479461
}
480462

463+
String rtspURL = connectionRes.getMessage();
464+
String authparam = broadcast.getUsername() + ":" + broadcast.getPassword() + "@";
465+
String rtspURLWithAuth = RTSP + authparam + rtspURL.substring(RTSP.length());
466+
logger.info("New Stream Source URL: {}", rtspURLWithAuth);
467+
broadcast.setStreamUrl(rtspURLWithAuth);
481468
}
469+
470+
boolean result = getDataStore().updateBroadcastFields(streamId, broadcast);
471+
if (result) {
472+
Broadcast fetchedBroadcast = getDataStore().get(streamId);
473+
getApplication().startStreaming(fetchedBroadcast);
474+
}
475+
482476
return new Result(result);
483477
}
484478

479+
485480
public boolean checkStopStreaming(Broadcast broadcast)
486481
{
487482
// If broadcast status is broadcasting, this will force stop the streaming.

0 commit comments

Comments
 (0)