Skip to content

Commit 8731777

Browse files
committed
Added participant list in voice room using async
1 parent a99ea7e commit 8731777

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

src/main/java/org/spacehub/service/VoiceRoom/JanusService.java

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,34 +112,53 @@ public void createAudioRoom(String sessionId, String handleId, int roomId) {
112112
}
113113

114114
public void joinAudioRoom(String sessionId, String handleId, int roomId, String displayName) {
115-
Map<String, Object> body = Map.of(
116-
"request", "join", "room", roomId, "display", displayName
117-
);
118-
Map<String, Object> request = Map.of(
119-
"janus", "message", "transaction", UUID.randomUUID().toString(), "body", body
120-
);
115+
Map<String, Object> body = Map.of("request", "join", "room", roomId, "display", displayName);
116+
Map<String, Object> request = Map.of("janus", "message", "transaction", UUID.randomUUID().toString(), "body", body);
121117
String handleUrl = String.format("%s/%s/%s", janusUrl, sessionId, handleId);
122118

123119
ResponseEntity<JsonNode> response = restTemplate.postForEntity(handleUrl, request, JsonNode.class);
124-
logger.info("Janus join response: {}", response.getBody());
120+
JsonNode respBody = response.getBody();
121+
logger.info("Janus join response: {}", respBody);
122+
123+
JsonNode participantsNode = respBody != null
124+
? respBody.path("plugindata").path("data").path("participants")
125+
: null;
126+
if (participantsNode != null && !participantsNode.isMissingNode() && participantsNode.isArray()) {
127+
return;
128+
}
129+
130+
JsonNode listResp;
131+
int attempts = 3;
132+
for (int i = 0; i < attempts; i++) {
133+
try {
134+
listResp = listParticipants(sessionId, handleId, roomId);
135+
if (listResp != null) {
136+
JsonNode listParticipantsNode = listResp.path("plugindata").path("data").path("participants");
137+
if (listParticipantsNode != null && listParticipantsNode.isArray() && !listParticipantsNode.isEmpty()) {
138+
logger.info("Found participants via listparticipants on attempt {}", i + 1);
139+
return;
140+
}
141+
}
142+
} catch (Exception ex) {
143+
logger.warn("listparticipants attempt {} failed: {}", i + 1, ex.getMessage());
144+
}
145+
try {
146+
Thread.sleep(200L);
147+
} catch (InterruptedException ie) {
148+
Thread.currentThread().interrupt();
149+
break;
150+
}
151+
}
152+
logger.info("Returning join response without participants.");
125153
}
126154

127155
public JsonNode listParticipants(String sessionId, String handleId, int roomId) {
128-
Map<String, Object> body = Map.of(
129-
"request", "listparticipants", "room", roomId
130-
);
131-
Map<String, Object> request = Map.of(
132-
"janus", "message", "transaction", UUID.randomUUID().toString(), "body", body
133-
);
134156
String handleUrl = String.format("%s/%s/%s", janusUrl, sessionId, handleId);
135-
157+
Map<String, Object> body = Map.of("request", "listparticipants", "room", roomId);
158+
Map<String, Object> request = Map.of("janus", "message", "transaction", UUID.randomUUID().toString(), "body", body);
136159
ResponseEntity<JsonNode> response = restTemplate.postForEntity(handleUrl, request, JsonNode.class);
137-
138-
if (response.getBody() != null) {
139-
return response.getBody().path("plugindata").path("data").path("list");
140-
}
141-
142-
throw new RuntimeException("Failed to get participant list from Janus");
160+
logger.info("Janus listparticipants response: {}", response.getBody());
161+
return response.getBody();
143162
}
144163

145164
public void sendOffer(String sessionId, String handleId, String sdpOffer,

0 commit comments

Comments
 (0)