<commit_before>package sfu

import (
	"sync"

	"github.com/juju/errors"
	"github.com/peer-calls/peer-calls/server/logger"
	"github.com/peer-calls/peer-calls/server/pubsub"
	"github.com/peer-calls/peer-calls/server/transport"
)

const DataChannelName = "data"

type TracksManager struct {
	log                 logger.Logger
	mu                  sync.RWMutex
	peerManagers        map[string]*PeerManager
	jitterBufferEnabled bool
}

func NewTracksManager(log logger.Logger, jitterBufferEnabled bool) *TracksManager {
	return &TracksManager{
		log:                 log.WithNamespaceAppended("tracks_manager"),
		peerManagers:        map[string]*PeerManager{},
		jitterBufferEnabled: jitterBufferEnabled,
	}
}

// Add adds a transport to the existing PeerManager. If the manager does not
// exist, it is created.
//
// NOTE: rooms are created when the peer joins the room over the WebSocket
// connection. The component in charge for this is the RoomManager.
//
// Add is called from two places:
//  - When WebRTCTransports are created and peers join the room, or
//  - When RoomManager event that a room was created: A server transport will
//    be created for each configured node.
func (m *TracksManager) Add(room string, tr transport.Transport) (<-chan pubsub.PubTrackEvent, error) {
	m.mu.Lock()
	defer m.mu.Unlock()

	log := m.log.WithCtx(logger.Ctx{
		"room_id": room,
	})

	peerManager, ok := m.peerManagers[room]
	if !ok {
		jitterHandler := NewJitterHandler(
			log,
			m.jitterBufferEnabled,
		)
		peerManager = NewPeerManager(room, m.log, jitterHandler)
		m.peerManagers[room] = peerManager<commit_msg>Use logger with room_id in tracks manager<commit_after>package sfu

import (
	"sync"

	"github.com/juju/errors"
	"github.com/peer-calls/peer-calls/server/logger"
	"github.com/peer-calls/peer-calls/server/pubsub"
	"github.com/peer-calls/peer-calls/server/transport"
)

const DataChannelName = "data"

type TracksManager struct {
	log                 logger.Logger
	mu                  sync.RWMutex
	peerManagers        map[string]*PeerManager
	jitterBufferEnabled bool
}

func NewTracksManager(log logger.Logger, jitterBufferEnabled bool) *TracksManager {
	return &TracksManager{
		log:                 log.WithNamespaceAppended("tracks_manager"),
		peerManagers:        map[string]*PeerManager{},
		jitterBufferEnabled: jitterBufferEnabled,
	}
}

// Add adds a transport to the existing PeerManager. If the manager does not
// exist, it is created.
//
// NOTE: rooms are created when the peer joins the room over the WebSocket
// connection. The component in charge for this is the RoomManager.
//
// Add is called from two places:
//  - When WebRTCTransports are created and peers join the room, or
//  - When RoomManager event that a room was created: A server transport will
//    be created for each configured node.
func (m *TracksManager) Add(room string, tr transport.Transport) (<-chan pubsub.PubTrackEvent, error) {
	m.mu.Lock()
	defer m.mu.Unlock()

	log := m.log.WithCtx(logger.Ctx{
		"room_id": room,
	})

	peerManager, ok := m.peerManagers[room]
	if !ok {
		jitterHandler := NewJitterHandler(
			log,
			m.jitterBufferEnabled,
		)
		peerManager = NewPeerManager(room, log, jitterHandler)
		m.peerManagers[room] = peerManager
{'fraction_non_alphanumeric': 0.10252463054187193, 'fraction_numerical': 0.0, 'mean_word_length': 3.5504201680672267, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '215769', 'n_tokens_mistral': 1049, 'n_tokens_neox': 1024, 'n_words': 289}