From 86bad15337650ca45dfdaad85a3e5686283868d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Thu, 3 Feb 2022 22:56:41 +0100 Subject: [PATCH] Add support for request offers to update subscriber connections MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The "sid" parameter of "requestOffer" messages is now checked to update the current Janus connection when it matches the "sid" of the subscriber rather than closing it and creating a new one. Updating a connection is needed for clients to renegotiate their received media without interrupting the current connection. For example, to stop receiving video by triggering a new offer from Janus and then setting the video transceiver direction as inactive before creating the answer. Signed-off-by: Daniel Calviño Sánchez --- mcu_janus.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/mcu_janus.go b/mcu_janus.go index fae34e9..1164736 100644 --- a/mcu_janus.go +++ b/mcu_janus.go @@ -1245,6 +1245,26 @@ retry: callback(nil, join_response.Jsep) } +func (p *mcuJanusSubscriber) update(ctx context.Context, callback func(error, map[string]interface{})) { + handle := p.handle + if handle == nil { + callback(ErrNotConnected, nil) + return + } + + configure_msg := map[string]interface{}{ + "request": "configure", + "update": true, + } + configure_response, err := handle.Message(ctx, configure_msg, nil) + if err != nil { + callback(err, nil) + return + } + + callback(nil, configure_response.Jsep) +} + func (p *mcuJanusSubscriber) SendMessage(ctx context.Context, message *MessageClientMessage, data *MessageClientMessageData, callback func(error, map[string]interface{})) { statsMcuMessagesTotal.WithLabelValues(data.Type).Inc() jsep_msg := data.Payload @@ -1256,9 +1276,11 @@ func (p *mcuJanusSubscriber) SendMessage(ctx context.Context, message *MessageCl msgctx, cancel := context.WithTimeout(context.Background(), p.mcu.mcuTimeout) defer cancel() - // TODO Only join the room if there is no sid or it does not match - // the subscriber sid; otherwise configure/update the subscriber. - p.joinRoom(msgctx, callback) + if data.Sid == "" || data.Sid != p.Sid() { + p.joinRoom(msgctx, callback) + } else { + p.update(msgctx, callback) + } } case "answer": p.deferred <- func() {