Example #1
0
 public void Disconnect()
 {
     _client.Stop();
     _keepaliveTimer   = null;
     _videoDataChannel = 0;
     _decoder          = null;
     _h264Payload      = new H264Payload();
 }
Example #2
0
        private void DataReceived(object sender, RtspChunkEventArgs e)
        {
            RtspData data_received = e.Message as RtspData;

            if (data_received.Channel == _videoDataChannel)
            {
                // Received some Video or Audio Data on the correct channel.

                // RTP Packet Header
                // 0 - Version, P, X, CC, M, PT and Sequence Number
                //32 - Timestamp
                //64 - SSRC
                //96 - CSRCs (optional)
                //nn - Extension ID and Length
                //nn - Extension header

                int  rtp_version         = (e.Message.Data[0] >> 6);
                int  rtp_padding         = (e.Message.Data[0] >> 5) & 0x01;
                int  rtp_extension       = (e.Message.Data[0] >> 4) & 0x01;
                int  rtp_csrc_count      = (e.Message.Data[0] >> 0) & 0x0F;
                int  rtp_marker          = (e.Message.Data[1] >> 7) & 0x01;
                int  rtp_payload_type    = (e.Message.Data[1] >> 0) & 0x7F;
                uint rtp_sequence_number = ((uint)e.Message.Data[2] << 8) + (uint)(e.Message.Data[3]);
                uint rtp_timestamp       = ((uint)e.Message.Data[4] << 24) + (uint)(e.Message.Data[5] << 16) + (uint)(e.Message.Data[6] << 8) + (uint)(e.Message.Data[7]);
                uint rtp_ssrc            = ((uint)e.Message.Data[8] << 24) + (uint)(e.Message.Data[9] << 16) + (uint)(e.Message.Data[10] << 8) + (uint)(e.Message.Data[11]);

                int rtp_payload_start = 4                       // V,P,M,SEQ
                                        + 4                     // time stamp
                                        + 4                     // ssrc
                                        + (4 * rtp_csrc_count); // zero or more csrcs

                uint rtp_extension_id   = 0;
                uint rtp_extension_size = 0;
                if (rtp_extension == 1)
                {
                    rtp_extension_id   = ((uint)e.Message.Data[rtp_payload_start + 0] << 8) + (uint)(e.Message.Data[rtp_payload_start + 1] << 0);
                    rtp_extension_size = ((uint)e.Message.Data[rtp_payload_start + 2] << 8) + (uint)(e.Message.Data[rtp_payload_start + 3] << 0) * 4; // units of extension_size is 4-bytes
                    rtp_payload_start += 4 + (int)rtp_extension_size;                                                                                 // extension header and extension payload
                }


                if (data_received.Channel == _videoDataChannel && rtp_payload_type == 96)
                {
                    byte[] rtp_payload = new byte[e.Message.Data.Length - rtp_payload_start];
                    Array.Copy(e.Message.Data, rtp_payload_start, rtp_payload, 0, rtp_payload.Length);

                    List <byte[]> nal_units = _h264Payload.Process_H264_RTP_Packet(rtp_payload, rtp_marker);

                    if (nal_units != null)
                    {
                        // we have passed in enough RTP packets to make a Frame of video
                        try
                        {
                            if (_decoder == null)
                            {
                                _decoder = new H264Decoder(nal_units);
                            }

                            _currentBitmap = _decoder.Update(nal_units);

                            NewBitmapAvailable.Set();
                        }
                        catch (Exception error)
                        {
                            Console.WriteLine(error.Message);
                        }
                    }
                }
            }
        }