Showing posts with label real time streaming. Show all posts
Showing posts with label real time streaming. Show all posts

Wednesday, December 17, 2014

MPEG2TS Stream - Transport Stream




Anatomy of MPEG2TS stream, PAT, PMT, PES, Triple Play, IPTV, RTP, RTSP




For more details please look at the video here





















Each transport stream consists of a set of sub-streams (known as elementary streams), where each elementary stream can contain either MPEG-2 encoded audio, MPEG-2 encoded video, or data encapsulated in an MPEG-2 stream. Each of these elementary streams has a 'packet identifier' (usually known as a PID) that acts as a unique identifier for that stream within the transport stream.

The only restriction on the number of elementary streams in any transport stream is that each elementary stream must have a unique PID value within its containing transport stream. Since this is stored as a 13-bit value, this is not a major restriction. In practise, the number of elementary streams is limited by the total bitrate of the transport stream. Transmission issues mean that transport streams with bitrates much above 40 megabits/second can't usually be transmitted reliably.

A transport stream consists of a number of audio and video streams that are multiplexed together. First, each service in the transport stream will have its audio and video components encoded using MPEG-2 compression. The result of this process is a set of MPEG-2 elementary streams, each containing one video channel or one (mono or stereo) audio track. These streams are simply a continuous set of video frames or audio data, which is not really suitable for multiplexing. Therefore, we split these streams into packets in order to make the multiplexing process easier. The result of this is a packetized elementary stream, or PES.

To create a transport stream, each of these packetized elementary streams is packetized again and the data from the stream is stored in transport packets. Each transport packet has a length of 188 bytes, which is much smaller than a PES packet, and so a single PES packet will be split across several transport packets. This extra level of packetization allows the stream to support much more powerful error correcting techniques - PES packets are used to provide a way of multiplexing several elementary streams into one bigger stream, and are more concerned with identifying the type of data contained in the packet and the time and which it should be decoded and displayed. Transport packets, on the other hand, are almost purely concerned with providing error correction.





  







RTMP Handshake


After establishing a TCP connection, an RTMP connection is established first performing a handshake through the exchange of 3 packets from each side (also referred as Chunks in the official documentation). These are referred in the official spec as C0-2 for the client sent packets and S0-2 for the server side respectively and are not to be confused with RTMP packets that can be exchanged only after the handshake is complete. These packets have a structure of their own and C1 contains a field setting the "epoch" timestamp but since this can be set to zero, as is done in third party implementations, the packet can be simplified. The client initialises the connection by sending the C0 packet with a constant value of 0x03 representing the current protocol version. It follows straight with C1 without waiting for S0 to be received first which contains 1536 bytes, with the first 4 representing the epoch timestamp, the second 4 all being 0, and the rest being random (and which can be set to 0 in third party implementations). C2 and S2 are an echo of S1 and C1 respectively, except with the second 4 bytes being the time the respective message was received (instead of 0). After C2 and S2 are received the handshake is considered complete.