From 69ff25a0557a3e6419ac5917223b7ee26e15706a Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Wed, 15 Oct 2025 19:36:22 +0530 Subject: [PATCH] Use answer with mid -> trackID mapping when in single peer connection (#4005) * Use answer with mid -> trackID mapping when in single peer connection mode. * mage generate * staticcheck * handle mapped answer in tests --- go.mod | 22 +++++---- go.sum | 46 ++++++++++--------- pkg/rtc/participant.go | 11 +++-- pkg/rtc/participant_signal.go | 4 ++ pkg/rtc/signalling/interfaces.go | 1 + pkg/rtc/signalling/signalling.go | 8 ++++ pkg/rtc/signalling/signallingunimplemented.go | 4 ++ pkg/rtc/transport.go | 9 +++- pkg/rtc/transport/handler.go | 4 +- .../transport/transportfakes/fake_handler.go | 18 ++++---- pkg/rtc/transport_test.go | 8 ++-- test/client/client.go | 9 +++- 12 files changed, 93 insertions(+), 51 deletions(-) diff --git a/go.mod b/go.mod index ef1357cf..3555fff7 100644 --- a/go.mod +++ b/go.mod @@ -23,11 +23,11 @@ require ( github.com/jxskiss/base62 v1.1.0 github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731 github.com/livekit/mediatransportutil v0.0.0-20250922175932-f537f0880397 - github.com/livekit/protocol v1.42.1-0.20251012093153-c84cf2c2802b + github.com/livekit/protocol v1.42.1-0.20251015124854-bfdbffc082ab github.com/livekit/psrpc v0.7.0 github.com/mackerelio/go-osstat v0.2.6 github.com/magefile/mage v1.15.0 - github.com/maxbrunsfeld/counterfeiter/v6 v6.11.3 + github.com/maxbrunsfeld/counterfeiter/v6 v6.12.0 github.com/mitchellh/go-homedir v1.1.0 github.com/olekukonko/tablewriter v0.0.5 github.com/ory/dockertest/v3 v3.12.0 @@ -55,10 +55,10 @@ require ( go.uber.org/atomic v1.11.0 go.uber.org/multierr v1.11.0 go.uber.org/zap v1.27.0 - golang.org/x/exp v0.0.0-20250911091902-df9299821621 - golang.org/x/mod v0.28.0 + golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b + golang.org/x/mod v0.29.0 golang.org/x/sync v0.17.0 - google.golang.org/protobuf v1.36.9 + google.golang.org/protobuf v1.36.10 gopkg.in/yaml.v3 v3.0.1 ) @@ -74,7 +74,7 @@ require ( ) require ( - buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.9-20250912141014-52f32327d4b0.1 // indirect + buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.10-20250912141014-52f32327d4b0.1 // indirect buf.build/go/protovalidate v1.0.0 // indirect buf.build/go/protoyaml v0.6.0 // indirect cel.dev/expr v0.24.0 // indirect @@ -115,7 +115,7 @@ require ( github.com/moby/docker-image-spec v1.3.1 // indirect github.com/moby/term v0.5.0 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/nats-io/nats.go v1.46.0 // indirect + github.com/nats-io/nats.go v1.47.0 // indirect github.com/nats-io/nkeys v0.4.11 // indirect github.com/nats-io/nuid v1.0.1 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect @@ -145,8 +145,10 @@ require ( golang.org/x/sys v0.37.0 // indirect golang.org/x/text v0.30.0 // indirect golang.org/x/tools v0.37.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20250922171735-9219d122eba9 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20250922171735-9219d122eba9 // indirect - google.golang.org/grpc v1.75.1 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20251014184007-4626949a642f // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20251014184007-4626949a642f // indirect + google.golang.org/grpc v1.76.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) + +tool github.com/maxbrunsfeld/counterfeiter/v6 diff --git a/go.sum b/go.sum index 51500f96..f5eab17a 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.9-20250912141014-52f32327d4b0.1 h1:DQLS/rRxLHuugVzjJU5AvOwD57pdFl9he/0O7e5P294= -buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.9-20250912141014-52f32327d4b0.1/go.mod h1:aY3zbkNan5F+cGm9lITDP6oxJIwu0dn9KjJuJjWaHkg= +buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.10-20250912141014-52f32327d4b0.1 h1:31on4W/yPcV4nZHL4+UCiCvLPsMqe/vJcNg8Rci0scc= +buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.10-20250912141014-52f32327d4b0.1/go.mod h1:fUl8CEN/6ZAMk6bP8ahBJPUJw7rbp+j4x+wCcYi2IG4= buf.build/go/protovalidate v1.0.0 h1:IAG1etULddAy93fiBsFVhpj7es5zL53AfB/79CVGtyY= buf.build/go/protovalidate v1.0.0/go.mod h1:KQmEUrcQuC99hAw+juzOEAmILScQiKBP1Oc36vvCLW8= buf.build/go/protoyaml v0.6.0 h1:Nzz1lvcXF8YgNZXk+voPPwdU8FjDPTUV4ndNTXN0n2w= @@ -171,8 +171,8 @@ github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731 h1:9x+U2HGLrSw5AT github.com/livekit/mageutil v0.0.0-20250511045019-0f1ff63f7731/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ= github.com/livekit/mediatransportutil v0.0.0-20250922175932-f537f0880397 h1:Z7j2mY+bvG05UC80MpnJkitlJju8sSDWsr0Bb4dPceo= github.com/livekit/mediatransportutil v0.0.0-20250922175932-f537f0880397/go.mod h1:mSNtYzSf6iY9xM3UX42VEI+STHvMgHmrYzEHPcdhB8A= -github.com/livekit/protocol v1.42.1-0.20251012093153-c84cf2c2802b h1:yy10iTBHZV+fECKlAolZyrc/lBBf1a7mwipNOR9Dc7A= -github.com/livekit/protocol v1.42.1-0.20251012093153-c84cf2c2802b/go.mod h1:vhMS30QoEyH2p34vi6X1eWkC4EMV72ZGZwQb74ajY7A= +github.com/livekit/protocol v1.42.1-0.20251015124854-bfdbffc082ab h1:kPHpoS+vyi+yOxrBe6h/MW0/BZk4YfF3JozfEOUkz5Y= +github.com/livekit/protocol v1.42.1-0.20251015124854-bfdbffc082ab/go.mod h1:vhMS30QoEyH2p34vi6X1eWkC4EMV72ZGZwQb74ajY7A= github.com/livekit/psrpc v0.7.0 h1:rtfqfjYN06WJYloE/S0nmkJ/Y04x4pxLQLe8kQ4FVHU= github.com/livekit/psrpc v0.7.0/go.mod h1:AuDC5uOoEjQJEc69v4Li3t77Ocz0e0NdjQEuFfO+vfk= github.com/mackerelio/go-osstat v0.2.6 h1:gs4U8BZeS1tjrL08tt5VUliVvSWP26Ai2Ob8Lr7f2i0= @@ -185,8 +185,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/maxbrunsfeld/counterfeiter/v6 v6.11.3 h1:Eaq36EIyJNp7b3qDhjV7jmDVq/yPeW2v4pTqzGbOGB4= -github.com/maxbrunsfeld/counterfeiter/v6 v6.11.3/go.mod h1:6KKUoQBZBW6PDXJtNfqeEjPXMj/ITTk+cWK9t9uS5+E= +github.com/maxbrunsfeld/counterfeiter/v6 v6.12.0 h1:aOeI7xAOVdK+R6xbVsZuU9HmCZYmQVmZgPf9xJUd2Sg= +github.com/maxbrunsfeld/counterfeiter/v6 v6.12.0/go.mod h1:0hZWbtfeCYUQeAQdPLUzETiBhUSns7O6LDj9vH88xKA= github.com/mdlayher/ethtool v0.0.0-20210210192532-2b88debcdd43/go.mod h1:+t7E0lkKfbBsebllff1xdTmyJt8lH37niI6kwFk9OTo= github.com/mdlayher/genetlink v1.0.0/go.mod h1:0rJ0h4itni50A86M2kHcgS85ttZazNt7a8H2a2cw0Gc= github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA= @@ -216,8 +216,8 @@ github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0= github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= -github.com/nats-io/nats.go v1.46.0 h1:iUcX+MLT0HHXskGkz+Sg20sXrPtJLsOojMDTDzOHSb8= -github.com/nats-io/nats.go v1.46.0/go.mod h1:iRWIPokVIFbVijxuMQq4y9ttaBTMe0SFdlZfMDd+33g= +github.com/nats-io/nats.go v1.47.0 h1:YQdADw6J/UfGUd2Oy6tn4Hq6YHxCaJrVKayxxFqYrgM= +github.com/nats-io/nats.go v1.47.0/go.mod h1:iRWIPokVIFbVijxuMQq4y9ttaBTMe0SFdlZfMDd+33g= github.com/nats-io/nkeys v0.4.11 h1:q44qGV008kYd9W1b1nEBkNzvnWxtRSQ7A8BoqRrcfa0= github.com/nats-io/nkeys v0.4.11/go.mod h1:szDimtgmfOi9n25JpfIdGw12tZFYXqhGxjhVxsatHVE= github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= @@ -226,8 +226,8 @@ github.com/nyaruka/phonenumbers v1.6.5 h1:aBCaUhfpRA7hU6fsXk+p7KF1aNx4nQlq9hGeo2 github.com/nyaruka/phonenumbers v1.6.5/go.mod h1:7gjs+Lchqm49adhAKB5cdcng5ZXgt6x7Jgvi0ZorUtU= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= -github.com/onsi/gomega v1.37.0 h1:CdEG8g0S133B4OswTDC/5XPSzE1OeP29QOioj2PID2Y= -github.com/onsi/gomega v1.37.0/go.mod h1:8D9+Txp43QWKhM24yyOBEdpkzN8FvJyAwecBgsU4KU0= +github.com/onsi/gomega v1.38.2 h1:eZCjf2xjZAqe+LeWvKb5weQ+NcPwX84kqJ0cZNxok2A= +github.com/onsi/gomega v1.38.2/go.mod h1:W2MJcYxRGV63b418Ai34Ud0hEdTVXq9NW9+Sx6uXf3k= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug= @@ -358,6 +358,8 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= go.uber.org/zap/exp v0.3.0 h1:6JYzdifzYkGmTdRR59oYH+Ng7k49H9qVpWwNSsGJj3U= go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ= +go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -367,16 +369,16 @@ golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1m golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04= golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0= -golang.org/x/exp v0.0.0-20250911091902-df9299821621 h1:2id6c1/gto0kaHYyrixvknJ8tUK/Qs5IsmBtrc+FtgU= -golang.org/x/exp v0.0.0-20250911091902-df9299821621/go.mod h1:TwQYMMnGpvZyc+JpB/UAuTNIsVJifOlSkrZkhcvpVUk= +golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o= +golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/mod v0.28.0 h1:gQBtGhjxykdjY9YhZpSlZIsbnaE2+PgjfLWUQTnoZ1U= -golang.org/x/mod v0.28.0/go.mod h1:yfB/L0NOf/kmEbXjzCPOx1iK1fRutOydrCMsqRhEBxI= +golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA= +golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -479,14 +481,14 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk= gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E= -google.golang.org/genproto/googleapis/api v0.0.0-20250922171735-9219d122eba9 h1:jm6v6kMRpTYKxBRrDkYAitNJegUeO1Mf3Kt80obv0gg= -google.golang.org/genproto/googleapis/api v0.0.0-20250922171735-9219d122eba9/go.mod h1:LmwNphe5Afor5V3R5BppOULHOnt2mCIf+NxMd4XiygE= -google.golang.org/genproto/googleapis/rpc v0.0.0-20250922171735-9219d122eba9 h1:V1jCN2HBa8sySkR5vLcCSqJSTMv093Rw9EJefhQGP7M= -google.golang.org/genproto/googleapis/rpc v0.0.0-20250922171735-9219d122eba9/go.mod h1:HSkG/KdJWusxU1F6CNrwNDjBMgisKxGnc5dAZfT0mjQ= -google.golang.org/grpc v1.75.1 h1:/ODCNEuf9VghjgO3rqLcfg8fiOP0nSluljWFlDxELLI= -google.golang.org/grpc v1.75.1/go.mod h1:JtPAzKiq4v1xcAB2hydNlWI2RnF85XXcV0mhKXr2ecQ= -google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw= -google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= +google.golang.org/genproto/googleapis/api v0.0.0-20251014184007-4626949a642f h1:OiFuztEyBivVKDvguQJYWq1yDcfAHIID/FVrPR4oiI0= +google.golang.org/genproto/googleapis/api v0.0.0-20251014184007-4626949a642f/go.mod h1:kprOiu9Tr0JYyD6DORrc4Hfyk3RFXqkQ3ctHEum3ZbM= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251014184007-4626949a642f h1:1FTH6cpXFsENbPR5Bu8NQddPSaUUE6NA2XdZdDSAJK4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20251014184007-4626949a642f/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk= +google.golang.org/grpc v1.76.0 h1:UnVkv1+uMLYXoIz6o7chp59WfQUYA2ex/BXQ9rHZu7A= +google.golang.org/grpc v1.76.0/go.mod h1:Ju12QI8M6iQJtbcsV+awF5a4hfJMLi4X0JLo94ULZ6c= +google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= +google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index c77c3b8a..8e8d4092 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -1306,7 +1306,7 @@ func (p *ParticipantImpl) onPublisherSetRemoteDescription() { p.updateRidsFromSDP(parsedOffer, unmatchVideos) } -func (p *ParticipantImpl) onPublisherAnswer(answer webrtc.SessionDescription, answerId uint32) error { +func (p *ParticipantImpl) onPublisherAnswer(answer webrtc.SessionDescription, answerId uint32, midToTrackID map[string]string) error { if p.IsClosed() || p.IsDisconnected() { return nil } @@ -1318,6 +1318,11 @@ func (p *ParticipantImpl) onPublisherAnswer(answer webrtc.SessionDescription, an "answer", answer, "answerId", answerId, ) + + if p.params.UseSinglePeerConnection { + return p.sendMappedSdpAnswer(answer, answerId, midToTrackID) + } + return p.sendSdpAnswer(answer, answerId) } @@ -1948,8 +1953,8 @@ func (h PublisherTransportHandler) OnSetRemoteDescriptionOffer() { h.p.onPublisherSetRemoteDescription() } -func (h PublisherTransportHandler) OnAnswer(sd webrtc.SessionDescription, answerId uint32) error { - return h.p.onPublisherAnswer(sd, answerId) +func (h PublisherTransportHandler) OnAnswer(sd webrtc.SessionDescription, answerId uint32, midToTrackID map[string]string) error { + return h.p.onPublisherAnswer(sd, answerId, midToTrackID) } func (h PublisherTransportHandler) OnTrack(track *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) { diff --git a/pkg/rtc/participant_signal.go b/pkg/rtc/participant_signal.go index cd894ab9..b10ff7d5 100644 --- a/pkg/rtc/participant_signal.go +++ b/pkg/rtc/participant_signal.go @@ -299,6 +299,10 @@ func (p *ParticipantImpl) sendSdpAnswer(answer webrtc.SessionDescription, answer return p.signaller.WriteMessage(p.signalling.SignalSdpAnswer(protosignalling.ToProtoSessionDescription(answer, answerId))) } +func (p *ParticipantImpl) sendMappedSdpAnswer(answer webrtc.SessionDescription, answerId uint32, midToTrackID map[string]string) error { + return p.signaller.WriteMessage(p.signalling.SignalMappedSdpAnswer(protosignalling.ToProtoMappedSessionDescription(answer, answerId, midToTrackID))) +} + func (p *ParticipantImpl) sendSdpOffer(offer webrtc.SessionDescription, offerId uint32) error { return p.signaller.WriteMessage(p.signalling.SignalSdpOffer(protosignalling.ToProtoSessionDescription(offer, offerId))) } diff --git a/pkg/rtc/signalling/interfaces.go b/pkg/rtc/signalling/interfaces.go index e4983245..1ec96098 100644 --- a/pkg/rtc/signalling/interfaces.go +++ b/pkg/rtc/signalling/interfaces.go @@ -52,6 +52,7 @@ type ParticipantSignalling interface { SignalTrackSubscribed(trackSubscribed *livekit.TrackSubscribed) proto.Message SignalLeaveRequest(leave *livekit.LeaveRequest) proto.Message SignalSdpAnswer(answer *livekit.SessionDescription) proto.Message + SignalMappedSdpAnswer(mappedAnswer *livekit.MappedSessionDescription) proto.Message SignalSdpOffer(offer *livekit.SessionDescription) proto.Message SignalStreamStateUpdate(streamStateUpdate *livekit.StreamStateUpdate) proto.Message SignalSubscribedQualityUpdate(subscribedQualityUpdate *livekit.SubscribedQualityUpdate) proto.Message diff --git a/pkg/rtc/signalling/signalling.go b/pkg/rtc/signalling/signalling.go index 8b08a6d0..c8ff2d88 100644 --- a/pkg/rtc/signalling/signalling.go +++ b/pkg/rtc/signalling/signalling.go @@ -173,6 +173,14 @@ func (s *signalling) SignalLeaveRequest(leave *livekit.LeaveRequest) proto.Messa } } +func (s *signalling) SignalMappedSdpAnswer(mappedAnswer *livekit.MappedSessionDescription) proto.Message { + return &livekit.SignalResponse{ + Message: &livekit.SignalResponse_MappedAnswer{ + MappedAnswer: mappedAnswer, + }, + } +} + func (s *signalling) SignalSdpAnswer(answer *livekit.SessionDescription) proto.Message { return &livekit.SignalResponse{ Message: &livekit.SignalResponse_Answer{ diff --git a/pkg/rtc/signalling/signallingunimplemented.go b/pkg/rtc/signalling/signallingunimplemented.go index 595877b0..a1e5f507 100644 --- a/pkg/rtc/signalling/signallingunimplemented.go +++ b/pkg/rtc/signalling/signallingunimplemented.go @@ -88,6 +88,10 @@ func (u *signallingUnimplemented) SignalSdpAnswer(answer *livekit.SessionDescrip return nil } +func (u *signallingUnimplemented) SignalMappedSdpAnswer(answer *livekit.MappedSessionDescription) proto.Message { + return nil +} + func (u *signallingUnimplemented) SignalSdpOffer(offer *livekit.SessionDescription) proto.Message { return nil } diff --git a/pkg/rtc/transport.go b/pkg/rtc/transport.go index f3dbf059..ecff64b2 100644 --- a/pkg/rtc/transport.go +++ b/pkg/rtc/transport.go @@ -2640,6 +2640,13 @@ func (t *PCTransport) createAndSendAnswer() error { return errors.Wrap(err, "setting local description failed") } + midToTrackID := map[string]string{} + for _, tr := range t.pc.GetTransceivers() { + if tr.Sender() != nil && tr.Mid() != "" { + midToTrackID[tr.Mid()] = tr.Sender().Track().ID() + } + } + // // Filter after setting local description as pion expects the answer // to match between CreateAnswer and SetLocalDescription. @@ -2661,7 +2668,7 @@ func (t *PCTransport) createAndSendAnswer() error { } answerId := t.remoteOfferId.Load() - if err := t.params.Handler.OnAnswer(answer, answerId); err != nil { + if err := t.params.Handler.OnAnswer(answer, answerId, midToTrackID); err != nil { prometheus.RecordServiceOperationError("answer", "write_message") return errors.Wrap(err, "could not send answer") } diff --git a/pkg/rtc/transport/handler.go b/pkg/rtc/transport/handler.go index 8abd0fbb..f3bf2c51 100644 --- a/pkg/rtc/transport/handler.go +++ b/pkg/rtc/transport/handler.go @@ -44,7 +44,7 @@ type Handler interface { OnDataSendError(err error) OnOffer(sd webrtc.SessionDescription, offerId uint32) error OnSetRemoteDescriptionOffer() - OnAnswer(sd webrtc.SessionDescription, answerId uint32) error + OnAnswer(sd webrtc.SessionDescription, answerId uint32, midToTrackID map[string]string) error OnNegotiationStateChanged(state NegotiationState) OnNegotiationFailed() OnStreamStateChange(update *streamallocator.StreamStateUpdate) error @@ -67,7 +67,7 @@ func (h UnimplementedHandler) OnOffer(sd webrtc.SessionDescription, offerId uint return ErrNoOfferHandler } func (h UnimplementedHandler) OnSetRemoteDescriptionOffer() {} -func (h UnimplementedHandler) OnAnswer(sd webrtc.SessionDescription, answerId uint32) error { +func (h UnimplementedHandler) OnAnswer(sd webrtc.SessionDescription, answerId uint32, midToTrackID map[string]string) error { return ErrNoAnswerHandler } func (h UnimplementedHandler) OnNegotiationStateChanged(state NegotiationState) {} diff --git a/pkg/rtc/transport/transportfakes/fake_handler.go b/pkg/rtc/transport/transportfakes/fake_handler.go index 5ac5f381..23c91604 100644 --- a/pkg/rtc/transport/transportfakes/fake_handler.go +++ b/pkg/rtc/transport/transportfakes/fake_handler.go @@ -12,11 +12,12 @@ import ( ) type FakeHandler struct { - OnAnswerStub func(webrtc.SessionDescription, uint32) error + OnAnswerStub func(webrtc.SessionDescription, uint32, map[string]string) error onAnswerMutex sync.RWMutex onAnswerArgsForCall []struct { arg1 webrtc.SessionDescription arg2 uint32 + arg3 map[string]string } onAnswerReturns struct { result1 error @@ -124,19 +125,20 @@ type FakeHandler struct { invocationsMutex sync.RWMutex } -func (fake *FakeHandler) OnAnswer(arg1 webrtc.SessionDescription, arg2 uint32) error { +func (fake *FakeHandler) OnAnswer(arg1 webrtc.SessionDescription, arg2 uint32, arg3 map[string]string) error { fake.onAnswerMutex.Lock() ret, specificReturn := fake.onAnswerReturnsOnCall[len(fake.onAnswerArgsForCall)] fake.onAnswerArgsForCall = append(fake.onAnswerArgsForCall, struct { arg1 webrtc.SessionDescription arg2 uint32 - }{arg1, arg2}) + arg3 map[string]string + }{arg1, arg2, arg3}) stub := fake.OnAnswerStub fakeReturns := fake.onAnswerReturns - fake.recordInvocation("OnAnswer", []interface{}{arg1, arg2}) + fake.recordInvocation("OnAnswer", []interface{}{arg1, arg2, arg3}) fake.onAnswerMutex.Unlock() if stub != nil { - return stub(arg1, arg2) + return stub(arg1, arg2, arg3) } if specificReturn { return ret.result1 @@ -150,17 +152,17 @@ func (fake *FakeHandler) OnAnswerCallCount() int { return len(fake.onAnswerArgsForCall) } -func (fake *FakeHandler) OnAnswerCalls(stub func(webrtc.SessionDescription, uint32) error) { +func (fake *FakeHandler) OnAnswerCalls(stub func(webrtc.SessionDescription, uint32, map[string]string) error) { fake.onAnswerMutex.Lock() defer fake.onAnswerMutex.Unlock() fake.OnAnswerStub = stub } -func (fake *FakeHandler) OnAnswerArgsForCall(i int) (webrtc.SessionDescription, uint32) { +func (fake *FakeHandler) OnAnswerArgsForCall(i int) (webrtc.SessionDescription, uint32, map[string]string) { fake.onAnswerMutex.RLock() defer fake.onAnswerMutex.RUnlock() argsForCall := fake.onAnswerArgsForCall[i] - return argsForCall.arg1, argsForCall.arg2 + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3 } func (fake *FakeHandler) OnAnswerReturns(result1 error) { diff --git a/pkg/rtc/transport_test.go b/pkg/rtc/transport_test.go index 1deb43ea..e4a873f2 100644 --- a/pkg/rtc/transport_test.go +++ b/pkg/rtc/transport_test.go @@ -175,7 +175,7 @@ func TestNegotiationTiming(t *testing.T) { return true }, 10*time.Second, 10*time.Millisecond, "first offer not received yet") - handlerB.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32) error { + handlerB.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32, _midToTrackID map[string]string) error { transportA.HandleRemoteDescription(answer, answerId) return nil }) @@ -243,7 +243,7 @@ func TestFirstOfferMissedDuringICERestart(t *testing.T) { // set offer/answer with restart ICE, will negotiate twice, // first one is recover from missed offer // second one is restartICE - handlerB.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32) error { + handlerB.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32, _midToTrackID map[string]string) error { transportA.HandleRemoteDescription(answer, answerId) return nil }) @@ -303,7 +303,7 @@ func TestFirstAnswerMissedDuringICERestart(t *testing.T) { // first answer missed var firstAnswerReceived atomic.Bool - handlerB.OnAnswerCalls(func(sd webrtc.SessionDescription, answerId uint32) error { + handlerB.OnAnswerCalls(func(sd webrtc.SessionDescription, answerId uint32, _midToTrackID map[string]string) error { if firstAnswerReceived.Load() { transportA.HandleRemoteDescription(sd, answerId) } else { @@ -538,7 +538,7 @@ func handleICEExchange(t *testing.T, a, b *PCTransport, ah, bh *transportfakes.F func connectTransports(t *testing.T, offerer, answerer *PCTransport, offererHandler, answererHandler *transportfakes.FakeHandler, isICERestart bool, expectedOfferCount int32, expectedAnswerCount int32) { var offerCount atomic.Int32 var answerCount atomic.Int32 - answererHandler.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32) error { + answererHandler.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32, _midToTrackID map[string]string) error { answerCount.Inc() offerer.HandleRemoteDescription(answer, answerId) return nil diff --git a/test/client/client.go b/test/client/client.go index 5bdf98c1..6d856264 100644 --- a/test/client/client.go +++ b/test/client/client.go @@ -386,7 +386,7 @@ func NewRTCClient(conn *websocket.Conn, useSinglePeerConnection bool, opts *Opti logger.Debugw("subscriber fully established", "participant", c.localParticipant.Identity, "pID", c.localParticipant.Sid) c.subscriberFullyEstablished.Store(true) }) - subscriberHandler.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32) error { + subscriberHandler.OnAnswerCalls(func(answer webrtc.SessionDescription, answerId uint32, _midToTrackID map[string]string) error { // send remote an answer logger.Infow( "sending subscriber answer", @@ -468,6 +468,13 @@ func (c *RTCClient) handleSignalResponse(res *livekit.SignalResponse) error { "answer", msg.Answer.Sdp, ) c.handleAnswer(signalling.FromProtoSessionDescription(msg.Answer)) + case *livekit.SignalResponse_MappedAnswer: + logger.Infow( + "received mapped server answer", + "participant", c.localParticipant.Identity, + "answer", msg.MappedAnswer.SessionDescription.Sdp, + ) + c.handleAnswer(signalling.FromProtoSessionDescription(msg.MappedAnswer.SessionDescription)) case *livekit.SignalResponse_Offer: desc, offerId := signalling.FromProtoSessionDescription(msg.Offer) logger.Infow(