Ejemplo n.º 1
0
        public bool Write(RtpPacket packet)
        {
            if (_IPEndPoint == null)
            {
                if (IPAddress == null || Port == 0)
                {
                    Console.Error.WriteLine("IP address and port not set yet. Discarding packet.");
                    return(false);
                }
                _IPEndPoint = new IPEndPoint(System.Net.IPAddress.Parse(IPAddress), Port);
            }

            var header = new RtpPacketHeader
            {
                Marker                = packet.Marker,
                Timestamp             = packet.Timestamp,
                SequenceNumber        = packet.SequenceNumber,
                PayloadType           = packet.PayloadType,
                SynchronizationSource = packet.SynchronizationSource
            };
            var headerLength = header.CalculateHeaderLength();

            header.WriteTo(_Buffer, 0);
            _Buffer.Write(packet.Payload, headerLength);

            _Client.Send(_Buffer.Data, headerLength + packet.Payload.Length, _IPEndPoint);

            return(true);
        }
Ejemplo n.º 2
0
        private async Task Loop()
        {
            var dispatchQueue = new DispatchQueue <UdpReceiveResult>((result) =>
            {
                var buffer = DataBuffer.Wrap(result.Buffer);
                var header = RtpPacketHeader.ReadFrom(buffer);
                try
                {
                    OnPacket?.Invoke(new RtpPacket(buffer.Subset(header.CalculateHeaderLength()), header.SequenceNumber, header.Timestamp, header.Marker)
                    {
                        PayloadType           = header.PayloadType,
                        SynchronizationSource = header.SynchronizationSource
                    });
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine($"Unexpected exception raising packet. {ex}");
                }
            });

            while (_LoopActive)
            {
                try
                {
                    dispatchQueue.Enqueue(await _Server.ReceiveAsync());
                }
                catch (ObjectDisposedException)
                {
                    break;
                }
            }

            dispatchQueue.WaitForDrain();
            dispatchQueue.Destroy();
        }
        protected override void DoProcessFrame(AudioFrame frame, AudioBuffer inputBuffer)
        {
            var rtpHeaders  = inputBuffer.RtpHeaders;
            var rtpPayloads = inputBuffer.DataBuffers;

            for (var i = 0; i < rtpHeaders.Length; i++)
            {
                var rtpHeader = rtpHeaders[i];
                if (rtpHeader == null)
                {
                    rtpHeader = new RtpPacketHeader();
                }
                if (rtpHeader.SequenceNumber == -1)
                {
                    rtpHeader.SequenceNumber = (int)(_SequenceNumber++ % (ushort.MaxValue + 1));
                }
                if (rtpHeader.Timestamp == -1)
                {
                    rtpHeader.Timestamp = frame.Timestamp % ((long)uint.MaxValue + 1);
                }
                _Writer.Write(new RtpPacket(rtpPayloads[i], rtpHeader.SequenceNumber, rtpHeader.Timestamp, rtpHeader.Marker)
                {
                    PayloadType           = PayloadType,
                    SynchronizationSource = SynchronizationSource
                });
            }
        }
Ejemplo n.º 4
0
        protected override void DoProcessFrame(VideoFrame frame, VideoBuffer inputBuffer)
        {
            if (frame.Timestamp != _LastTimestamp)
            {
                _KeyFrameIntervalCounter++;
                _LastTimestamp = frame.Timestamp;
            }

            if (_KeyFrameIntervalCounter == KeyFrameInterval)
            {
                Console.Error.WriteLine("Raising keyframe request.");
                RaiseControlFrame(new FirControlFrame(new FirEntry(GetCcmSequenceNumber())));
                _KeyFrameIntervalCounter = 0;
            }

            if (!WaitForKeyFrame)
            {
                _Ready = true;
            }

            if (!_Ready)
            {
                // clone to check keyframe since H.264 keyframe test on
                // packetized data buffers is destructive in 1.9.x
                var inputBufferClone = inputBuffer.Clone();
                var dataBufferClones = new DataBuffer[inputBuffer.DataBuffers.Length];
                for (var i = 0; i < dataBufferClones.Length; i++)
                {
                    dataBufferClones[i] = DataBuffer.Wrap(inputBuffer.DataBuffers[i].ToArray());
                }
                inputBufferClone.DataBuffers = dataBufferClones;
                if (inputBufferClone.IsKeyFrame)
                {
                    _Ready = true;
                }
            }

            if (_Ready)
            {
                var rtpHeaders  = inputBuffer.RtpHeaders;
                var rtpPayloads = inputBuffer.DataBuffers;
                for (var i = 0; i < rtpHeaders.Length; i++)
                {
                    var rtpHeader = rtpHeaders[i];
                    if (rtpHeader == null)
                    {
                        rtpHeader = new RtpPacketHeader();
                    }
                    if (rtpHeader.SequenceNumber == -1)
                    {
                        rtpHeader.SequenceNumber = (int)(_SequenceNumber++ % (ushort.MaxValue + 1));
                    }
                    if (rtpHeader.Timestamp == -1)
                    {
                        rtpHeader.Timestamp = frame.Timestamp % ((long)uint.MaxValue + 1);
                    }
                    _Writer.Write(new RtpPacket(rtpPayloads[i], rtpHeader.SequenceNumber, rtpHeader.Timestamp, rtpHeader.Marker)
                    {
                        PayloadType           = PayloadType,
                        SynchronizationSource = SynchronizationSource
                    });
                }
            }
        }