Skip to content

Commit

Permalink
Merge pull request #372 from boostcampwm-2024/feat/#358/master-exit-r…
Browse files Browse the repository at this point in the history
…oom-end

[Feat] 발표자가 탭 닫기로 접속 종료 시에도 방폭되게 함
  • Loading branch information
simeunseo authored Dec 5, 2024
2 parents 231c3a7 + dd97ebb commit 1ee54f7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 28 deletions.
4 changes: 2 additions & 2 deletions apps/media/src/mediasoup/mediasoup.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class MediasoupService implements OnModuleInit {
return worker;
}

async createRoom(roomId: string) {
async createRoom(roomId: string, masterSocketId: string) {
const isExistRoom = this.roomService.existRoom(roomId);
if (isExistRoom) {
return roomId;
Expand All @@ -59,7 +59,7 @@ export class MediasoupService implements OnModuleInit {
mediaCodecs: this.mediasoupConfig.router.mediaCodecs,
});

return this.roomService.createRoom(roomId, router);
return this.roomService.createRoom(roomId, router, masterSocketId);
}

joinRoom(roomId: string, socketId: string, nickname: string) {
Expand Down
6 changes: 1 addition & 5 deletions apps/media/src/record/record.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ export class RecordService {
return recordInfo;
}

getRecordInfo(roomId: string) {
return this.recordInfos.get(roomId);
}

private async addPlainTransport(recordInfo: RecordInfo, router: types.Router) {
const plainTransport = await this.mediasoupService.createPlainTransport(router);
recordInfo.setPlainTransport(plainTransport);
Expand Down Expand Up @@ -117,7 +113,7 @@ export class RecordService {
return;
}
this.releasePort(recordInfo.port);
recordInfo.stopRecordProcess();
recordInfo.clearStream();
this.recordInfos.delete(roomId);
}

Expand Down
3 changes: 2 additions & 1 deletion apps/media/src/record/recordInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class RecordInfo {
this.recordConsumer.resume();
}

stopRecordProcess() {
clearStream() {
if (this.recordConsumer) {
this.recordConsumer.close();
this.recordConsumer = null;
Expand Down Expand Up @@ -84,6 +84,7 @@ export class RecordInfo {
this.ncpService.uploadFile(filePath, remoteFileName, roomId);
unlinkSync(sdpFilePath);
this.ffmpegProcess = null;
this.clearStream();
})
.save(filePath);

Expand Down
37 changes: 27 additions & 10 deletions apps/media/src/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export class RoomService {

constructor() {}

createRoom(roomId: string, router: Router) {
const room = new Room(roomId, router);
createRoom(roomId: string, router: Router, masterSocketId: string) {
const room = new Room(roomId, router, masterSocketId);
this.rooms.set(roomId, room);
return roomId;
}
Expand All @@ -31,14 +31,7 @@ export class RoomService {

deletePeer(socketId: string) {
for (const [roomId, room] of this.rooms) {
if (!room.removePeer(socketId)) continue;

if (room.peers.size === 0) {
room.close();
this.rooms.delete(roomId);
}

return roomId;
if (room.removePeer(socketId)) return roomId;
}
}

Expand All @@ -48,4 +41,28 @@ export class RoomService {
this.rooms.delete(roomId);
return roomId;
}

checkIsMaster(roomId: string, socketId: string) {
const room = this.rooms.get(roomId);
if (!room) {
return false;
}
return room.masterSocketId === socketId;
}

checkRoomIsOpen(roomId: string) {
const room = this.rooms.get(roomId);
if (!room) {
return false;
}
return room.isOpen;
}

setRoomIsOpen(roomId: string, isOpen: boolean) {
const room = this.rooms.get(roomId);
if (!room) {
return;
}
room.isOpen = isOpen;
}
}
6 changes: 5 additions & 1 deletion apps/media/src/room/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import { Peer } from './peer';

export class Room {
id: string;
masterSocketId: string;
router: Router;
peers: Map<string, Peer>;
isOpen: boolean;

constructor(roomId: string, router: Router) {
constructor(roomId: string, router: Router, masterSocketId: string) {
this.id = roomId;
this.router = router;
this.masterSocketId = masterSocketId;
this.peers = new Map();
this.isOpen = true;
}

getRouter() {
Expand Down
24 changes: 16 additions & 8 deletions apps/media/src/signaling/signaling.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,29 @@ import type { client, server } from '@repo/mediasoup';

import { MediasoupService } from '@/mediasoup/mediasoup.service';
import { RecordService } from '@/record/record.service';
import { RoomService } from '@/room/room.service';
import { WSExceptionFilter } from '@/wsException.filter';

@WebSocketGateway()
@UseFilters(WSExceptionFilter)
export class SignalingGateway implements OnGatewayDisconnect {
constructor(
private mediasoupService: MediasoupService,
private recordService: RecordService
private recordService: RecordService,
private roomService: RoomService
) {}

@SubscribeMessage(SOCKET_EVENTS.createRoom)
async handleCreateRoom(@MessageBody('roomId') roomId: string) {
await this.mediasoupService.createRoom(roomId);
async handleCreateRoom(@ConnectedSocket() client: Socket, @MessageBody('roomId') roomId: string) {
await this.mediasoupService.createRoom(roomId, client.id);
return { roomId };
}

@SubscribeMessage(SOCKET_EVENTS.joinRoom)
joinRoom(@ConnectedSocket() client: Socket, @MessageBody() joinRoomDto: server.JoinRoomDto) {
const { roomId, nickname } = joinRoomDto;
client.join(roomId);
const rtpCapabilities = this.mediasoupService.joinRoom(roomId, client.id, nickname);
client.join(roomId);
client.to(roomId).emit(SOCKET_EVENTS.newPeer, { peerId: client.id, nickname });
return { rtpCapabilities };
}
Expand Down Expand Up @@ -104,12 +106,18 @@ export class SignalingGateway implements OnGatewayDisconnect {

handleDisconnect(@ConnectedSocket() client: Socket) {
const roomId = this.mediasoupService.disconnect(client.id);
const recordInfo = this.recordService.getRecordInfo(roomId);
if (recordInfo && recordInfo.socketId === client.id) {
const isMaster = this.roomService.checkIsMaster(roomId, client.id);
if (isMaster) {
client.to(roomId).emit(SOCKET_EVENTS.roomClosed);
this.recordService.stopRecord(roomId);
this.mediasoupService.closeRoom(roomId);
return;
}

client.to(roomId).emit(SOCKET_EVENTS.peerLeft, { peerId: client.id });
const isOpen = this.roomService.checkRoomIsOpen(roomId);
if (isOpen) {
client.to(roomId).emit(SOCKET_EVENTS.peerLeft, { peerId: client.id });
}
}

@SubscribeMessage(SOCKET_EVENTS.closeProducer)
Expand Down Expand Up @@ -188,7 +196,7 @@ export class SignalingGateway implements OnGatewayDisconnect {
@SubscribeMessage(SOCKET_EVENTS.closeRoom)
closeMeetingRoom(@ConnectedSocket() client: Socket, @MessageBody('roomId') roomId: string) {
client.to(roomId).emit(SOCKET_EVENTS.roomClosed);
this.mediasoupService.closeRoom(roomId);
this.roomService.setRoomIsOpen(roomId, false);
}

@SubscribeMessage(SOCKET_EVENTS.startRecord)
Expand Down
3 changes: 2 additions & 1 deletion apps/media/src/signaling/signaling.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { Module } from '@nestjs/common';

import { MediasoupModule } from '@/mediasoup/mediasoup.module';
import { RecordModule } from '@/record/record.module';
import { RoomModule } from '@/room/room.module';

import { SignalingGateway } from './signaling.gateway';

@Module({
imports: [MediasoupModule, RecordModule],
imports: [MediasoupModule, RecordModule, RoomModule],
providers: [SignalingGateway],
exports: [SignalingGateway],
})
Expand Down

0 comments on commit 1ee54f7

Please sign in to comment.