Example #1
0
        /// <summary>
        /// Get a snapshot of all transceivers associated with this PeerConnection.
        /// This PeerConnection owns the returned transceivers.
        /// </summary>
        /// <remarks>This method is thread safe</remarks>
        /// <returns></returns>
        public IReadOnlyList <RtpTransceiver> GetTransceivers()
        {
            SafetyCheck();

            PeerConnectionInterop.GetTransceivers(_handle,
                                                  out var nativeTransceiverArray,
                                                  out var nativeTransceiverArraySize);

            // When there is no transceivers,
            // The unmanaged code returns size = 0 and nativeTransceiverArray will be IntPtr.Zero,
            // therefore we ignore that.
            if (nativeTransceiverArraySize > 0)
            {
                try
                {
                    for (var i = 0; i < nativeTransceiverArraySize; i++)
                    {
                        var nativeTransceiverIntPtr = Marshal.ReadIntPtr(nativeTransceiverArray, i * IntPtr.Size);
                        if (false == _knownTransceivers.ContainsKey(nativeTransceiverIntPtr))
                        {
                            _knownTransceivers[nativeTransceiverIntPtr] = new RtpTransceiver(
                                nativeTransceiverIntPtr,
                                _signallingThread);
                        }
                    }
                }
                finally
                {
                    PeerConnectionInterop.FreeGetTransceiversResult(_handle, nativeTransceiverArray);
                }
            }
            // Return transceivers as a copy
            return(_knownTransceivers.Values.ToList());
        }
Example #2
0
        public RtpTransceiver AddTransceiver(MediaKind kind, RtpTransceiverDirection direction)
        {
            SafetyCheck();
            if (kind == MediaKind.Data)
            {
                throw new NotSupportedException();
            }

            var transceiverPtr = PeerConnectionInterop.AddTransceiver(_handle, kind == MediaKind.Audio, direction);

            if (transceiverPtr == IntPtr.Zero)
            {
                throw new AddTransceiverFailedException();
            }

            var tmp = new RtpTransceiver(transceiverPtr, _signallingThread);

            _knownTransceivers.Add(transceiverPtr, tmp);
            return(tmp);
        }