create the actual room object, to be used on RTC node
(ctx context.Context, createRoom *livekit.CreateRoomRequest)
| 639 | |
| 640 | // create the actual room object, to be used on RTC node |
| 641 | func (r *RoomManager) getOrCreateRoom(ctx context.Context, createRoom *livekit.CreateRoomRequest) (*rtc.Room, error) { |
| 642 | roomName := livekit.RoomName(createRoom.Name) |
| 643 | |
| 644 | r.lock.RLock() |
| 645 | lastSeenRoom := r.rooms[roomName] |
| 646 | r.lock.RUnlock() |
| 647 | |
| 648 | if lastSeenRoom != nil && lastSeenRoom.Hold() { |
| 649 | return lastSeenRoom, nil |
| 650 | } |
| 651 | |
| 652 | // create new room, get details first |
| 653 | ri, internal, created, err := r.roomAllocator.CreateRoom(ctx, createRoom, true) |
| 654 | if err != nil { |
| 655 | return nil, err |
| 656 | } |
| 657 | |
| 658 | r.lock.Lock() |
| 659 | |
| 660 | currentRoom := r.rooms[roomName] |
| 661 | for currentRoom != lastSeenRoom { |
| 662 | r.lock.Unlock() |
| 663 | if currentRoom != nil && currentRoom.Hold() { |
| 664 | return currentRoom, nil |
| 665 | } |
| 666 | |
| 667 | lastSeenRoom = currentRoom |
| 668 | r.lock.Lock() |
| 669 | currentRoom = r.rooms[roomName] |
| 670 | } |
| 671 | |
| 672 | // construct ice servers |
| 673 | newRoom := rtc.NewRoom(ri, internal, *r.rtcConfig, r.config.Room, &r.config.Audio, r.serverInfo, r.telemetry, r.agentClient, r.agentStore, r.egressLauncher) |
| 674 | |
| 675 | roomTopic := rpc.FormatRoomTopic(roomName) |
| 676 | roomServer := must.Get(rpc.NewTypedRoomServer(r, r.bus)) |
| 677 | killRoomServer := r.roomServers.Replace(roomTopic, roomServer) |
| 678 | if err := roomServer.RegisterAllRoomTopics(roomTopic); err != nil { |
| 679 | killRoomServer() |
| 680 | r.lock.Unlock() |
| 681 | return nil, err |
| 682 | } |
| 683 | agentDispatchServer := must.Get(rpc.NewTypedAgentDispatchInternalServer(r, r.bus)) |
| 684 | killDispServer := r.agentDispatchServers.Replace(roomTopic, agentDispatchServer) |
| 685 | if err := agentDispatchServer.RegisterAllRoomTopics(roomTopic); err != nil { |
| 686 | killRoomServer() |
| 687 | killDispServer() |
| 688 | r.lock.Unlock() |
| 689 | return nil, err |
| 690 | } |
| 691 | |
| 692 | newRoom.OnClose(func() { |
| 693 | killRoomServer() |
| 694 | killDispServer() |
| 695 | |
| 696 | roomInfo := newRoom.ToProto() |
| 697 | r.telemetry.RoomEnded(ctx, roomInfo) |
| 698 | prometheus.RoomEnded(time.Unix(roomInfo.CreationTime, 0)) |
no test coverage detected