This commit is contained in:
Raja Subramanian
2025-08-03 04:00:38 +05:30
committed by GitHub
parent db4bc127e8
commit 5751692a2f
8 changed files with 90 additions and 87 deletions
+9 -9
View File
@@ -73,7 +73,7 @@ type RoomManager struct {
router routing.Router
roomAllocator RoomAllocator
roomManagerServer rpc.TypedRoomManagerServer
rtcRestServer rpc.RTCRestServer[livekit.NodeID]
whipServer rpc.WHIPServer[livekit.NodeID]
roomStore ObjectStore
telemetry telemetry.TelemetryService
recorder observability.Reporter
@@ -91,7 +91,7 @@ type RoomManager struct {
agentDispatchServers utils.MultitonService[rpc.RoomTopic]
participantServers utils.MultitonService[rpc.ParticipantTopic]
httpSignalParticipantServers utils.MultitonService[rpc.ParticipantTopic]
rtcRestParticipantServers utils.MultitonService[rpc.ParticipantTopic]
whipParticipantServers utils.MultitonService[rpc.ParticipantTopic]
iceConfigCache *sutils.IceConfigCache[iceConfigCacheKey]
@@ -157,11 +157,11 @@ func NewLocalRoomManager(
return nil, err
}
r.rtcRestServer, err = rpc.NewRTCRestServer[livekit.NodeID](rtcRestService{r}, bus, rpc.WithDefaultServerOptions(conf.PSRPC, logger.GetLogger()))
r.whipServer, err = rpc.NewWHIPServer[livekit.NodeID](whipService{r}, bus, rpc.WithDefaultServerOptions(conf.PSRPC, logger.GetLogger()))
if err != nil {
return nil, err
}
if err := r.rtcRestServer.RegisterAllCommonTopics(currentNode.NodeID()); err != nil {
if err := r.whipServer.RegisterAllCommonTopics(currentNode.NodeID()); err != nil {
return nil, err
}
@@ -236,12 +236,12 @@ func (r *RoomManager) Stop() {
}
r.roomManagerServer.Kill()
r.rtcRestServer.Kill()
r.whipServer.Kill()
r.roomServers.Kill()
r.agentDispatchServers.Kill()
r.participantServers.Kill()
r.httpSignalParticipantServers.Kill()
r.rtcRestParticipantServers.Kill()
r.whipParticipantServers.Kill()
if r.rtcConfig != nil {
if r.rtcConfig.UDPMux != nil {
@@ -512,9 +512,9 @@ func (r *RoomManager) StartSession(
}
if useOneShotSignallingMode {
rtcRestParticipantServer := must.Get(rpc.NewTypedRTCRestParticipantServer(rtcRestParticipantService{r}, r.bus))
participantServerClosers = append(participantServerClosers, utils.CloseFunc(r.rtcRestParticipantServers.Replace(participantTopic, rtcRestParticipantServer)))
if err := rtcRestParticipantServer.RegisterAllCommonTopics(participantTopic); err != nil {
whipParticipantServer := must.Get(rpc.NewTypedWHIPParticipantServer(whipParticipantService{r}, r.bus))
participantServerClosers = append(participantServerClosers, utils.CloseFunc(r.whipParticipantServers.Replace(participantTopic, whipParticipantServer)))
if err := whipParticipantServer.RegisterAllCommonTopics(participantTopic); err != nil {
participantServerClosers.Close()
pLogger.Errorw("could not join register participant topic for rtc rest participant server", err)
_ = participant.Close(true, types.ParticipantCloseReasonMessageBusFailed, false)
+21 -18
View File
@@ -17,14 +17,14 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
)
type rtcRestService struct {
type whipService struct {
*RoomManager
}
func (s rtcRestService) Create(ctx context.Context, req *rpc.RTCRestCreateRequest) (*rpc.RTCRestCreateResponse, error) {
func (s whipService) Create(ctx context.Context, req *rpc.WHIPCreateRequest) (*rpc.WHIPCreateResponse, error) {
pi, err := routing.ParticipantInitFromStartSession(req.StartSession, s.RoomManager.currentNode.Region())
if err != nil {
logger.Errorw("rtcRest service: could not create participant init", err)
logger.Errorw("whip service: could not create participant init", err)
return nil, err
}
@@ -37,28 +37,31 @@ func (s rtcRestService) Create(ctx context.Context, req *rpc.RTCRestCreateReques
routing.NewNullMessageSink(livekit.ConnectionID(req.StartSession.ConnectionId)), // no responseSink
true, // useOneShotSignallingMode
); err != nil {
logger.Errorw("rtcRest service: could not start session", err)
logger.Errorw("whip service: could not start session", err)
return nil, err
}
room := s.RoomManager.GetRoom(ctx, livekit.RoomName(req.StartSession.RoomName))
if room == nil {
logger.Errorw("rtcRest service: could not find room", nil, "room", req.StartSession.RoomName)
logger.Errorw("whip service: could not find room", nil, "room", req.StartSession.RoomName)
return nil, ErrRoomNotFound
}
lp := room.GetParticipant(pi.Identity)
if lp == nil {
room.Logger().Errorw("rtcRest service: could not find local participant", nil, "participant", pi.Identity)
room.Logger().Errorw("whip service: could not find local participant", nil, "participant", pi.Identity)
return nil, ErrParticipantNotFound
}
if err := lp.HandleOffer(webrtc.SessionDescription{Type: webrtc.SDPTypeOffer, SDP: req.OfferSdp}, 0); err != nil {
lp.GetLogger().Errorw("rtcRest service: could not handle offer", err)
lp.GetLogger().Errorw("whip service: could not handle offer", err)
return nil, err
}
// wait for subscriptions to resolve
// NOTE: this is outside the WHIP spec, but added as a convenience for clients doing
// one-shot signalling (i. e. send an offer and get an answer once) to publish and subscribe to
// well-known tracks (i. e. remote participant identity and track names are well known)
eg, _ := errgroup.WithContext(ctx)
for publisherIdentity, trackList := range req.SubscribedParticipantTracks {
for _, trackName := range trackList.TrackNames {
@@ -74,19 +77,19 @@ func (s rtcRestService) Create(ctx context.Context, req *rpc.RTCRestCreateReques
}
err = eg.Wait()
if err != nil {
lp.GetLogger().Errorw("rtcRest service: could not subscribe to tracks", err)
lp.GetLogger().Errorw("whip service: could not subscribe to tracks", err)
return nil, err
}
answer, _, err := lp.GetAnswer()
if err != nil {
lp.GetLogger().Errorw("rtcRest service: could not get answer", err)
lp.GetLogger().Errorw("whip service: could not get answer", err)
return nil, err
}
iceSessionID, err := lp.GetPublisherICESessionUfrag()
if err != nil {
lp.GetLogger().Errorw("rtcRest service: could not get ICE session ID", err)
lp.GetLogger().Errorw("whip service: could not get ICE session ID", err)
return nil, err
}
@@ -99,7 +102,7 @@ func (s rtcRestService) Create(ctx context.Context, req *rpc.RTCRestCreateReques
false,
)
}
return &rpc.RTCRestCreateResponse{
return &rpc.WHIPCreateResponse{
AnswerSdp: answer.SDP,
ParticipantId: string(lp.ID()),
IceServers: iceServers,
@@ -109,11 +112,11 @@ func (s rtcRestService) Create(ctx context.Context, req *rpc.RTCRestCreateReques
// -------------------------------------------
type rtcRestParticipantService struct {
type whipParticipantService struct {
*RoomManager
}
func (r rtcRestParticipantService) ICETrickle(ctx context.Context, req *rpc.RTCRestParticipantICETrickleRequest) (*emptypb.Empty, error) {
func (r whipParticipantService) ICETrickle(ctx context.Context, req *rpc.WHIPParticipantICETrickleRequest) (*emptypb.Empty, error) {
room := r.RoomManager.GetRoom(ctx, livekit.RoomName(req.Room))
if room == nil {
return nil, ErrRoomNotFound
@@ -126,7 +129,7 @@ func (r rtcRestParticipantService) ICETrickle(ctx context.Context, req *rpc.RTCR
iceSessionID, err := lp.GetPublisherICESessionUfrag()
if err != nil {
lp.GetLogger().Warnw("rtcRestParticipant service ice-trickle: could not get ICE session ufrag", err)
lp.GetLogger().Warnw("whipParticipant service ice-trickle: could not get ICE session ufrag", err)
return nil, psrpc.NewError(psrpc.Internal, err)
}
@@ -144,7 +147,7 @@ func (r rtcRestParticipantService) ICETrickle(ctx context.Context, req *rpc.RTCR
return &emptypb.Empty{}, nil
}
func (r rtcRestParticipantService) ICERestart(ctx context.Context, req *rpc.RTCRestParticipantICERestartRequest) (*rpc.RTCRestParticipantICERestartResponse, error) {
func (r whipParticipantService) ICERestart(ctx context.Context, req *rpc.WHIPParticipantICERestartRequest) (*rpc.WHIPParticipantICERestartResponse, error) {
room := r.RoomManager.GetRoom(ctx, livekit.RoomName(req.Room))
if room == nil {
return nil, ErrRoomNotFound
@@ -162,17 +165,17 @@ func (r rtcRestParticipantService) ICERestart(ctx context.Context, req *rpc.RTCR
iceSessionID, err := lp.GetPublisherICESessionUfrag()
if err != nil {
lp.GetLogger().Warnw("rtcRestParticipant service ice-restart: could not get ICE session ufrag", err)
lp.GetLogger().Warnw("whipParticipant service ice-restart: could not get ICE session ufrag", err)
return nil, psrpc.NewError(psrpc.Internal, err)
}
return &rpc.RTCRestParticipantICERestartResponse{
return &rpc.WHIPParticipantICERestartResponse{
IceSessionId: iceSessionID,
SdpFragment: sdpFragment,
}, nil
}
func (r rtcRestParticipantService) DeleteSession(ctx context.Context, req *rpc.RTCRestParticipantDeleteSessionRequest) (*emptypb.Empty, error) {
func (r whipParticipantService) DeleteSession(ctx context.Context, req *rpc.WHIPParticipantDeleteSessionRequest) (*emptypb.Empty, error) {
room := r.RoomManager.GetRoom(ctx, livekit.RoomName(req.Room))
if room == nil {
return nil, ErrRoomNotFound
+4 -4
View File
@@ -50,7 +50,7 @@ type LivekitServer struct {
ioService *IOInfoService
rtcService *RTCService
rtcv2Service *RTCv2Service
rtcRestService *RTCRestService
whipService *WHIPService
agentService *AgentService
httpServer *http.Server
promServer *http.Server
@@ -74,7 +74,7 @@ func NewLivekitServer(conf *config.Config,
ioService *IOInfoService,
rtcService *RTCService,
rtcv2Service *RTCv2Service,
rtcRestService *RTCRestService,
whipService *WHIPService,
agentService *AgentService,
keyProvider auth.KeyProvider,
router routing.Router,
@@ -89,7 +89,7 @@ func NewLivekitServer(conf *config.Config,
ioService: ioService,
rtcService: rtcService,
rtcv2Service: rtcv2Service,
rtcRestService: rtcRestService,
whipService: whipService,
agentService: agentService,
router: router,
roomManager: roomManager,
@@ -151,7 +151,7 @@ func NewLivekitServer(conf *config.Config,
xtwirp.RegisterServer(mux, sipServer)
rtcService.SetupRoutes(mux)
rtcv2Service.SetupRoutes(mux)
rtcRestService.SetupRoutes(mux)
whipService.SetupRoutes(mux)
mux.Handle("/agent", agentService)
mux.HandleFunc("/", s.defaultHandler)
@@ -42,31 +42,31 @@ const (
cParticipantIDPath = "/whip/v1/{participant_id}"
)
type RTCRestService struct {
type WHIPService struct {
http.Handler
config *config.Config
router routing.Router
roomAllocator RoomAllocator
client rpc.RTCRestClient[livekit.NodeID]
client rpc.WHIPClient[livekit.NodeID]
topicFormatter rpc.TopicFormatter
participantClient rpc.TypedRTCRestParticipantClient
participantClient rpc.TypedWHIPParticipantClient
}
func NewRTCRestService(
func NewWHIPService(
config *config.Config,
router routing.Router,
roomAllocator RoomAllocator,
clientParams rpc.ClientParams,
topicFormatter rpc.TopicFormatter,
participantClient rpc.TypedRTCRestParticipantClient,
) (*RTCRestService, error) {
client, err := rpc.NewRTCRestClient[livekit.NodeID](clientParams.Args())
participantClient rpc.TypedWHIPParticipantClient,
) (*WHIPService, error) {
client, err := rpc.NewWHIPClient[livekit.NodeID](clientParams.Args())
if err != nil {
return nil, err
}
return &RTCRestService{
return &WHIPService{
config: config,
router: router,
roomAllocator: roomAllocator,
@@ -76,7 +76,7 @@ func NewRTCRestService(
}, nil
}
func (s *RTCRestService) SetupRoutes(mux *http.ServeMux) {
func (s *WHIPService) SetupRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET "+cParticipantPath, s.handleGet)
mux.HandleFunc("OPTIONS "+cParticipantPath, s.handleOptions)
mux.HandleFunc("POST "+cParticipantPath, s.handleCreate)
@@ -85,12 +85,12 @@ func (s *RTCRestService) SetupRoutes(mux *http.ServeMux) {
mux.HandleFunc("DELETE "+cParticipantIDPath, s.handleParticipantDelete)
}
func (s *RTCRestService) handleGet(w http.ResponseWriter, r *http.Request) {
func (s *WHIPService) handleGet(w http.ResponseWriter, r *http.Request) {
// https:/www.rfc-editor.org/rfc/rfc9725.html#name-http-usage
w.WriteHeader(http.StatusNoContent)
}
func (s *RTCRestService) handleOptions(w http.ResponseWriter, r *http.Request) {
func (s *WHIPService) handleOptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Methods", "PATCH, OPTIONS, GET, POST, DELETE")
@@ -117,7 +117,7 @@ type createRequest struct {
SubscribedParticipantTrackNames map[string][]string
}
func (s *RTCRestService) validateCreate(r *http.Request) (*createRequest, int, error) {
func (s *WHIPService) validateCreate(r *http.Request) (*createRequest, int, error) {
claims := GetGrants(r.Context())
if claims == nil || claims.Video == nil {
return nil, http.StatusUnauthorized, rtc.ErrPermissionDenied
@@ -199,7 +199,7 @@ func (s *RTCRestService) validateCreate(r *http.Request) (*createRequest, int, e
}, http.StatusOK, nil
}
func (s *RTCRestService) handleCreate(w http.ResponseWriter, r *http.Request) {
func (s *WHIPService) handleCreate(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Content-type") != "application/sdp" {
s.handleError("Create", w, r, http.StatusBadRequest, fmt.Errorf("unsupported content-type: %s", r.Header.Get("Content-type")))
return
@@ -231,14 +231,14 @@ func (s *RTCRestService) handleCreate(w http.ResponseWriter, r *http.Request) {
return
}
subscribedParticipantTracks := map[string]*rpc.RTCRestCreateRequest_TrackList{}
subscribedParticipantTracks := map[string]*rpc.WHIPCreateRequest_TrackList{}
for identity, trackNames := range req.SubscribedParticipantTrackNames {
subscribedParticipantTracks[identity] = &rpc.RTCRestCreateRequest_TrackList{
subscribedParticipantTracks[identity] = &rpc.WHIPCreateRequest_TrackList{
TrackNames: trackNames,
}
}
res, err := s.client.Create(r.Context(), livekit.NodeID(rtcNode.Id), &rpc.RTCRestCreateRequest{
res, err := s.client.Create(r.Context(), livekit.NodeID(rtcNode.Id), &rpc.WHIPCreateRequest{
OfferSdp: req.OfferSDP,
StartSession: starSession,
SubscribedParticipantTracks: subscribedParticipantTracks,
@@ -291,7 +291,7 @@ func (s *RTCRestService) handleCreate(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(res.AnswerSdp))
sutils.GetLogger(r.Context()).Infow(
"API RTCRest.Create",
"API WHIP.Create",
"connID", connID,
"participant", req.ParticipantInit.Identity,
"room", req.RoomName,
@@ -301,12 +301,12 @@ func (s *RTCRestService) handleCreate(w http.ResponseWriter, r *http.Request) {
return
}
func (s *RTCRestService) handleParticipantGet(w http.ResponseWriter, r *http.Request) {
func (s *WHIPService) handleParticipantGet(w http.ResponseWriter, r *http.Request) {
// https:/www.rfc-editor.org/rfc/rfc9725.html#name-http-usage
w.WriteHeader(http.StatusNoContent)
}
func (s *RTCRestService) iceTrickle(
func (s *WHIPService) iceTrickle(
w http.ResponseWriter,
r *http.Request,
roomName livekit.RoomName,
@@ -318,7 +318,7 @@ func (s *RTCRestService) iceTrickle(
_, err := s.participantClient.ICETrickle(
r.Context(),
s.topicFormatter.ParticipantTopic(r.Context(), roomName, participantIdentity),
&rpc.RTCRestParticipantICETrickleRequest{
&rpc.WHIPParticipantICETrickleRequest{
Room: string(roomName),
ParticipantIdentity: string(participantIdentity),
ParticipantId: string(pID),
@@ -349,7 +349,7 @@ func (s *RTCRestService) iceTrickle(
return
}
sutils.GetLogger(r.Context()).Infow(
"API RTCRest.Patch",
"API WHIP.Patch",
"method", "ice-trickle",
"room", roomName,
"participant", participantIdentity,
@@ -360,7 +360,7 @@ func (s *RTCRestService) iceTrickle(
w.WriteHeader(http.StatusNoContent)
}
func (s *RTCRestService) iceRestart(
func (s *WHIPService) iceRestart(
w http.ResponseWriter,
r *http.Request,
roomName livekit.RoomName,
@@ -371,7 +371,7 @@ func (s *RTCRestService) iceRestart(
res, err := s.participantClient.ICERestart(
r.Context(),
s.topicFormatter.ParticipantTopic(r.Context(), roomName, participantIdentity),
&rpc.RTCRestParticipantICERestartRequest{
&rpc.WHIPParticipantICERestartRequest{
Room: string(roomName),
ParticipantIdentity: string(participantIdentity),
ParticipantId: string(pID),
@@ -401,7 +401,7 @@ func (s *RTCRestService) iceRestart(
return
}
sutils.GetLogger(r.Context()).Infow(
"API RTCRest.Patch",
"API WHIP.Patch",
"method", "ice-restart",
"room", roomName,
"participant", participantIdentity,
@@ -417,7 +417,7 @@ func (s *RTCRestService) iceRestart(
w.Write([]byte(res.SdpFragment))
}
func (s *RTCRestService) handleParticipantPatch(w http.ResponseWriter, r *http.Request) {
func (s *WHIPService) handleParticipantPatch(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Content-type") != "application/trickle-ice-sdpfrag" {
s.handleError("Patch", w, r, http.StatusBadRequest, fmt.Errorf("unsupported content-type: %s", r.Header.Get("Content-type")))
return
@@ -470,7 +470,7 @@ func (s *RTCRestService) handleParticipantPatch(w http.ResponseWriter, r *http.R
}
}
func (s *RTCRestService) handleParticipantDelete(w http.ResponseWriter, r *http.Request) {
func (s *WHIPService) handleParticipantDelete(w http.ResponseWriter, r *http.Request) {
claims := GetGrants(r.Context())
if claims == nil || claims.Video == nil {
s.handleError("Delete", w, r, http.StatusUnauthorized, rtc.ErrPermissionDenied)
@@ -494,7 +494,7 @@ func (s *RTCRestService) handleParticipantDelete(w http.ResponseWriter, r *http.
_, err = s.participantClient.DeleteSession(
r.Context(),
s.topicFormatter.ParticipantTopic(r.Context(), roomName, livekit.ParticipantIdentity(claims.Identity)),
&rpc.RTCRestParticipantDeleteSessionRequest{
&rpc.WHIPParticipantDeleteSessionRequest{
Room: string(roomName),
ParticipantIdentity: claims.Identity,
ParticipantId: r.PathValue("participant_id"),
@@ -506,7 +506,7 @@ func (s *RTCRestService) handleParticipantDelete(w http.ResponseWriter, r *http.
}
sutils.GetLogger(r.Context()).Infow(
"API RTCRest.Delete",
"API WHIP.Delete",
"participant", claims.Identity,
"pID", r.PathValue("participant_id"),
"room", roomName,
@@ -515,9 +515,9 @@ func (s *RTCRestService) handleParticipantDelete(w http.ResponseWriter, r *http.
w.WriteHeader(http.StatusOK)
}
func (s *RTCRestService) handleError(method string, w http.ResponseWriter, r *http.Request, status int, err error) {
func (s *WHIPService) handleError(method string, w http.ResponseWriter, r *http.Request, status int, err error) {
sutils.GetLogger(r.Context()).Warnw(
fmt.Sprintf("API RTCRest.%s", method), err,
fmt.Sprintf("API WHIP.%s", method), err,
"status", status,
)
w.WriteHeader(status)
+2 -2
View File
@@ -78,7 +78,7 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
NewRoomService,
NewRTCService,
NewRTCv2Service,
NewRTCRestService,
NewWHIPService,
NewAgentService,
NewAgentDispatchService,
agent.NewAgentClient,
@@ -96,7 +96,7 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
rpc.NewTypedRoomClient,
rpc.NewTypedParticipantClient,
rpc.NewTypedSignalv2ParticipantClient,
rpc.NewTypedRTCRestParticipantClient,
rpc.NewTypedWHIPParticipantClient,
rpc.NewTypedAgentDispatchInternalClient,
NewLocalRoomManager,
NewTURNAuthHandler,
+3 -3
View File
@@ -125,11 +125,11 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
return nil, err
}
rtCv2Service := NewRTCv2Service(conf, roomAllocator, router, topicFormatter, signalv2ParticipantClient)
rtcRestParticipantClient, err := rpc.NewTypedRTCRestParticipantClient(clientParams)
whipParticipantClient, err := rpc.NewTypedWHIPParticipantClient(clientParams)
if err != nil {
return nil, err
}
serviceRTCRestService, err := NewRTCRestService(conf, router, roomAllocator, clientParams, topicFormatter, rtcRestParticipantClient)
serviceWHIPService, err := NewWHIPService(conf, router, roomAllocator, clientParams, topicFormatter, whipParticipantClient)
if err != nil {
return nil, err
}
@@ -162,7 +162,7 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
if err != nil {
return nil, err
}
livekitServer, err := NewLivekitServer(conf, roomService, agentDispatchService, egressService, ingressService, sipService, ioInfoService, rtcService, rtCv2Service, serviceRTCRestService, agentService, keyProvider, router, roomManager, signalServer, signalv2Server, server, currentNode)
livekitServer, err := NewLivekitServer(conf, roomService, agentDispatchService, egressService, ingressService, sipService, ioInfoService, rtcService, rtCv2Service, serviceWHIPService, agentService, keyProvider, router, roomManager, signalServer, signalv2Server, server, currentNode)
if err != nil {
return nil, err
}