Skip to content

Commit da69a20

Browse files
Merge branch 'development' of https://github.com/somiljain2006/SpaceHub-Backend into development
# Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit.
2 parents 5b6a8fd + 8731777 commit da69a20

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

src/main/java/org/spacehub/controller/voiceRoom/VoiceRoomController.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414
import com.fasterxml.jackson.databind.JsonNode;
15+
1516
import java.util.ArrayList;
17+
import java.util.HashMap;
1618
import java.util.List;
1719
import java.util.Map;
1820
import java.util.UUID;
@@ -62,11 +64,10 @@ public ResponseEntity<?> joinVoiceRoom(
6264
String sessionId = janusService.createSession();
6365
String handleId = janusService.attachAudioBridgePlugin(sessionId);
6466

65-
JsonNode janusResponse = janusService.joinAudioRoom(sessionId, handleId, janusRoomId, displayName);
66-
67-
JsonNode participantsNode = janusResponse.path("plugindata").path("data").path("participants");
67+
janusService.joinAudioRoom(sessionId, handleId, janusRoomId, displayName);
68+
JsonNode participantsNode = janusService.listParticipants(sessionId, handleId, janusRoomId);
6869

69-
Map<String, Object> responseData = new java.util.HashMap<>();
70+
Map<String, Object> responseData = new HashMap<>();
7071
responseData.put("message", "Joined voice room successfully");
7172
responseData.put("janusRoomId", janusRoomId);
7273
responseData.put("sessionId", sessionId);

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

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,53 @@ public void createAudioRoom(String sessionId, String handleId, int roomId) {
111111
restTemplate.postForEntity(handleUrl, request, JsonNode.class);
112112
}
113113

114-
public JsonNode 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-
);
114+
public void joinAudioRoom(String sessionId, String handleId, int roomId, String displayName) {
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.");
153+
}
154+
155+
public JsonNode listParticipants(String sessionId, String handleId, int roomId) {
156+
String handleUrl = String.format("%s/%s/%s", janusUrl, sessionId, handleId);
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);
159+
ResponseEntity<JsonNode> response = restTemplate.postForEntity(handleUrl, request, JsonNode.class);
160+
logger.info("Janus listparticipants response: {}", response.getBody());
125161
return response.getBody();
126162
}
127163

0 commit comments

Comments
 (0)