Пример #1
0
        public Scheduler(ILoggerFactory loggerFactory,
                         MediasoupOptions mediasoupOptions,
                         MediasoupServer mediasoupServer)
        {
            _loggerFactory    = loggerFactory;
            _logger           = _loggerFactory.CreateLogger <Scheduler>();
            _mediasoupOptions = mediasoupOptions;
            _mediasoupServer  = mediasoupServer;

            // 按创建 Route 时一样方式创建 RtpCodecCapabilities
            var rtpCodecCapabilities = mediasoupOptions.MediasoupSettings.RouterSettings.RtpCodecCapabilities;

            // This may throw.
            DefaultRtpCapabilities = ORTC.GenerateRouterRtpCapabilities(rtpCodecCapabilities);

            _roomsLock.Set();
        }
        private async void CallBackJoinRoom(IntPtr param1)
        {
            try
            {
                string          rtpcaps         = Marshal.PtrToStringAnsi(param1);
                RtpCapabilities rtpCapabilities = JsonConvert.DeserializeObject <RtpCapabilities>(rtpcaps);
                JoinRequest     request         = new JoinRequest();
                request.RtpCapabilities  = rtpCapabilities;
                request.SctpCapabilities = null;
                // rtpCapabilities 参数:Device.Load 后,取其 RtpCapabilities 属性。
                var result = await connection.InvokeAsync <dynamic>("Join", request);

                int i = 0;
            }
            catch (Exception ex)
            {
                messagesList.Items.Add(ex.ToString());
            }
        }
Пример #3
0
        public bool JoinPeer(int peerId, RtpCapabilities rtpCapabilities)
        {
            lock (_peerLocker)
            {
                if (!Peers.TryGetValue(peerId, out var peer))
                {
                    _logger.LogError($"JoinPeer() | Peer[{peerId}] is not exists.");
                    return(false);
                }

                if (peer.Joined)
                {
                    _logger.LogError($"JoinPeer() | Peer[{peerId}] is joined.");
                    return(false);
                }

                peer.RtpCapabilities = rtpCapabilities;
                peer.Joined          = true;
                return(true);
            }
        }
Пример #4
0
        // Validates RtpCapabilities. It may modify given data by adding missing
        // fields with default values.
        // It throws if invalid.
        public void ValidateRtpCapabilities(RtpCapabilities caps)
        {
            // Codecs is optional. If unset, fill with an empty array.
            if (caps.Codecs is null)
            {
                caps.Codecs = new RtpCodecCapability[] { }
            }
            ;
            foreach (var codec in caps.Codecs)
            {
                ValidateRtpCodecCapability(codec);
            }

            // HeaderExtensions is optional. If unset, fill with an empty array.
            if (caps.HeaderExtensions is null)
            {
                caps.HeaderExtensions = new RtpHeaderExtension[] { }
            }
            ;
            foreach (var ext in caps.HeaderExtensions)
            {
                ValidateRtpHeaderExtension(ext);
            }
        }
Пример #5
0
        public ExtendedRtpCapabilities GetExtendedRtpCapabilites(RtpCapabilities localCaps, RtpCapabilities remoteCaps)
        {
            List <ExtendedRtpCodecCapability> codecs = new();

            // Match media codecs and keep the order preferred by remoteCaps.
            foreach (var remoteCodec in remoteCaps.Codecs)
            {
                if (IsRtxCodec(remoteCodec))
                {
                    continue;
                }

                var matchingLocalCodec = localCaps.Codecs.FirstOrDefault(
                    localCodec => MatchCodecs(localCodec, remoteCodec, strict: true, modify: true));
                if (matchingLocalCodec is null)
                {
                    continue;
                }
                codecs.Add(new ExtendedRtpCodecCapability
                {
                    MimeType          = matchingLocalCodec.MimeType,
                    Kind              = matchingLocalCodec.Kind,
                    ClockRate         = matchingLocalCodec.ClockRate,
                    Channels          = matchingLocalCodec.Channels,
                    LocalPayloadType  = matchingLocalCodec.PreferredPayloadType,
                    RemotePayloadType = remoteCodec.PreferredPayloadType,
                    LocalParameters   = matchingLocalCodec.Parameters,
                    RemoteParameters  = remoteCodec.Parameters,
                    RtcpFeedback      = ReduceRtcpFeedback(matchingLocalCodec, remoteCodec)
                });
            }

            // Match RTX codecs.
            foreach (var extendedCodec in codecs)
            {
                var matchingLocalRtxCodec = localCaps.Codecs.FirstOrDefault(localCodec =>
                                                                            IsRtxCodec(localCodec) &&
                                                                            localCodec.Parameters.ContainsKey("apt") &&
                                                                            (int)localCodec.Parameters["apt"] == extendedCodec.LocalPayloadType);
                var matchingRemoteRtxCodec = remoteCaps.Codecs.FirstOrDefault(remoteCodec =>
                                                                              IsRtxCodec(remoteCodec) &&
                                                                              remoteCodec.Parameters.ContainsKey("apt") &&
                                                                              (int)remoteCodec.Parameters["apt"] == extendedCodec.RemotePayloadType);
                if (matchingLocalRtxCodec is not null && matchingRemoteRtxCodec is not null)
                {
                    extendedCodec.LocalRtxPayloadType  = matchingLocalRtxCodec.PreferredPayloadType;
                    extendedCodec.RemoteRtxPayloadType = matchingRemoteRtxCodec.PreferredPayloadType;
                }
            }

            List <ExtendedRtpHeaderExtensions> headerExtensions = new();

            // Match header extensions.
            foreach (var remoteExt in remoteCaps.HeaderExtensions)
            {
                var matchingLocalExt = localCaps.HeaderExtensions?.FirstOrDefault(localExt =>
                                                                                  MatchHeaderExtensions(localExt, remoteExt));
                if (matchingLocalExt is null)
                {
                    continue;
                }

                Direction direction = remoteExt.Direction switch
                {
                    Direction.SendRecv => Direction.SendRecv,
                    Direction.RecvOnly => Direction.SendOnly,
                    Direction.SendOnly => Direction.RecvOnly,
                    Direction.Inactive => Direction.Inactive,
                    _ => throw new NotImplementedException()
                };
                headerExtensions.Add(new ExtendedRtpHeaderExtensions
                {
                    Kind             = remoteExt.Kind,
                    Uri              = remoteExt.Uri,
                    SendId           = matchingLocalExt.PreferredId,
                    RecvId           = remoteExt.PreferredId,
                    PreferredEncrypt = matchingLocalExt.PreferredEncrypt,
                    Direction        = direction
                });
            }

            return(new ExtendedRtpCapabilities
            {
                Codecs = codecs.ToArray(),
                HeaderExtensions = headerExtensions.ToArray()
            });
        }