Commit 3d51d240 authored by Yoann HOUPERT's avatar Yoann HOUPERT
Browse files

refactoring disconnect

parent 3fd2e9f5
...@@ -17,23 +17,19 @@ robotController = { ...@@ -17,23 +17,19 @@ robotController = {
return easyrtc.myEasyrtcid; return easyrtc.myEasyrtcid;
}, },
getParticipants: () => {
return easyrtc.getRoomOccupantsAsArray(room);
},
getRemoteStream: participant => { getRemoteStream: participant => {
return easyrtc.getRemoteStream(participant); return easyrtc.getRemoteStream(participant);
}, },
getRemoteStreams: () => { getParticipants: () => {
const participants = robotController.getParticipants(); const participants = easyrtc.getRoomOccupantsAsArray(room);
const res = {}; const res = [];
for (let i = 0; i < participants.length; i++) { for (let i = 0; i < participants.length; i++) {
const participant = participants[i]; const participant = participants[i];
try { try {
const mediaStream = robotController.getRemoteStream(participant); const mediaStream = robotController.getRemoteStream(participant);
if (mediaStream !== null) { if (mediaStream !== null) {
res[participant] = mediaStream; res.push(participant);
} }
} catch (err) { } catch (err) {
console.error('could not get remote stream for %s', participant); console.error('could not get remote stream for %s', participant);
......
...@@ -20,9 +20,19 @@ const angular = { ...@@ -20,9 +20,19 @@ const angular = {
}) })
}; };
const easyRTCMock = participants => ({ const easyRTCMock = (participantsWithStream, participantsWithoutStream = []) => ({
getRoomOccupantsAsArray: () => participants, getRoomOccupantsAsArray: () => participantsWithStream
getRemoteStream: participant => ({origin: participant}) .concat(participantsWithoutStream),
getRemoteStream: participant => {
// Participant must be in list of participants AND not be in the list
// of participants without stream.
if (participantsWithStream.indexOf(participant) !== -1) {
return {
origin: participant
};
}
return null;
}
}); });
const document = { const document = {
...@@ -50,18 +60,18 @@ describe('client/controller', () => { ...@@ -50,18 +60,18 @@ describe('client/controller', () => {
expect(participants).toHaveLength(3); expect(participants).toHaveLength(3);
}); });
test('should not return participants without stream', () => {
global.easyrtc = easyRTCMock(['p1', 'p2', 'p3'], ['nostream']);
const participants = global.robotController.getParticipants();
expect(participants).not.toEqual(expect.arrayContaining(['nostream']));
});
test('should return the Streams of the participant', () => { test('should return the Streams of the participant', () => {
const stream = global.robotController.getRemoteStream('p2'); const stream = global.robotController.getRemoteStream('p2');
expect(stream.origin).toBe('p2'); expect(stream.origin).toBe('p2');
}); });
test('should return the Streams of all the participants', () => {
const streams = global.robotController.getRemoteStreams();
expect(streams).toHaveProperty('p1');
expect(streams).toHaveProperty('p2');
expect(streams).toHaveProperty('p3');
});
test('should call listener on push events', () => { test('should call listener on push events', () => {
let eventFired = false; let eventFired = false;
......
...@@ -83,32 +83,13 @@ robot = { ...@@ -83,32 +83,13 @@ robot = {
robot.getUserStream(easyrtcid); robot.getUserStream(easyrtcid);
}, },
getParticipantNumber() {
const participants = robotController.getParticipants();
let participantsNumber = participants.length;
for (let i = 0; i < participants.length; i++) {
const participant = participants[i];
try {
const mediaStream = robotController.getRemoteStream(participant);
if (mediaStream === null) {
participantsNumber--;
}
} catch (err) {
console.error('could not get remote stream for %s', participant);
console.error(err);
}
}
return participantsNumber;
},
stopRecordParticipant(easyrtcid) { stopRecordParticipant(easyrtcid) {
robot.participantsMediaRecorders[easyrtcid].stop(); robot.participantsMediaRecorders[easyrtcid].stop();
robot.recordedParticipantsWS[easyrtcid].close(); robot.recordedParticipantsWS[easyrtcid].close();
}, },
disconnectRobot() { checkDisconnect() {
if (robot.getParticipantNumber() === 1) { if (robotController.getParticipantNumber().length === 1) {
robotController.disconnect(); robotController.disconnect();
} }
}, },
...@@ -124,7 +105,7 @@ robot = { ...@@ -124,7 +105,7 @@ robot = {
robotController.onAttendeeRemove = (e, data) => { robotController.onAttendeeRemove = (e, data) => {
robot.stopRecordParticipant(data.easyrtcid); robot.stopRecordParticipant(data.easyrtcid);
robot.disconnectRobot(); robot.checkDisconnect();
}; };
function recoStartRetry() { function recoStartRetry() {
...@@ -150,7 +131,7 @@ robot = { ...@@ -150,7 +131,7 @@ robot = {
} }
// Wait 5 minute before leaving a room if alone // Wait 5 minute before leaving a room if alone
setTimeout(robot.disconnectRobot(), 300000); setTimeout(robot.checkDisconnect(), 300000);
} }
}; };
......
...@@ -51,7 +51,9 @@ describe('client/robot', () => { ...@@ -51,7 +51,9 @@ describe('client/robot', () => {
'someid1', 'someid1',
'someid2' 'someid2'
], ],
getRemoteStream: id => ({type: 'RemoteStream', id}) getRemoteStream: id => ({type: 'RemoteStream', id}),
getParticipantNumber: () => 3,
disconnect: () => {}
}; };
global.robotLib = { global.robotLib = {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment