11# Timestamps
22
3- In a nutshell, timestamps determine when a given event occurred in time. For
4- example, when you take a photo with your phone, the exact time and date the photo
5- is taken is recorded - it's a timestamp. When dealing with media we also need a
6- way to tell when different things need to happen. In Membrane we use the two most
7- common types of timestamps:
3+ In a nutshell, timestamps determine when a given event occurred (or should occur)
4+ in time. For example, when you take a photo with your phone, the exact time and
5+ date the photo is taken is recorded - it's a timestamp. When dealing with media
6+ we also need a way to tell when different things need to happen. In Membrane we
7+ use the two most common types of timestamps:
88
99* PTS (Presentation Time Stamp) - determines when the media should be
1010 displayed.
@@ -42,7 +42,7 @@ These units finally can have timestamps assigned to them - and in
4242most cases they do. For example, a PTS assigned to a buffer containing a
4343raw video frame determines when the frame should be displayed.
4444
45- ## Presentation Time Stamps
45+ ## Presentation Time Stamps (PTS)
4646
4747As previously mentioned, PTSs are used to tell when a piece of media should be
4848presented to the user. It can mean either displaying a video frame, or playing a
@@ -64,7 +64,7 @@ use Realtimer, then we would read the contents of the file as fast as possible a
6464send them over as fast as possible, which is not something we want. We want the receiver
6565to get the stream in realtime, so that they can display it as it comes.
6666
67- ## Decoding Time Stamps
67+ ## Decode Time Stamps (DTS)
6868
6969The purpose of DTSs is to tell a decoder when a frame should be
7070decoded. In a lot of codecs the media can be decoded as it comes, but in some
@@ -91,10 +91,47 @@ consisting of four frames. Frames 1 and 4 are I-frames, frame 2 is a P-frame
9191depending on frame 1, and frame 3 is a B-frame depending on frames 2 and 4.
9292If a decoder receives these frames with the following timestamps:
9393
94- 1 ) pts: 0ms, dts: 0ms
95- 2 ) pts: 200ms, dts: 200ms
96- 3 ) pts: 400ms , dts: 600ms
97- 4 ) pts: 600ms , dts: 400ms
94+ * [ 1 ] pts: 0ms, dts: 0ms
95+ * [ 2 ] pts: 200ms, dts: 200ms
96+ * [ 4 ] pts: 600ms , dts: 400ms
97+ * [ 3 ] pts: 400ms , dts: 600ms
9898
9999It will first decode the frames in order (1, 2, 4, 3), according to their DTSs.
100100If it hadn't decoded frames 2 and 4 first, it couldn't have decoded frame 3.
101+ It's also important to note that DTSs should always be monotonic, while PTSs
102+ for streams with B-frames can be non-monotonic.
103+
104+ ## Tips and guidelines
105+
106+ Dealing with timestamps can be complicated and very different depending on the
107+ use case, so here is some advice on dealing with them:
108+
109+ * Filters should always forward timestamps.
110+ * If a filter doesn't use timestamps, it should forward them, but not rely on
111+ them being set.
112+ * Sources should attach timestamps to buffers whenever they're known.
113+ * Whenever possible, elements should rely on timestamps instead of
114+ framerate or audio duration calculated from the stream.
115+ * If an element queues buffers in its state (or uses a library that does so), it
116+ should make sure that the timestamps for the output buffers are the same as for
117+ the corresponding input buffers.
118+ * You should ensure that calculations on timestamps don't introduce an
119+ accumulating error. Prefer using rationals (Ratio library) to floats.
120+ * Elements should generate deterministic output timestamps for better testability.
121+ * If an element transforms N input buffers into M output buffers, each of the
122+ output buffers should have either:
123+ * the timestamp of the first of the input buffers (even if only a part of it
124+ was used to construct the output buffers).
125+ * more precise timestamps, if it's possible to calculate them.
126+ * Timestamps are harder than they seem and are the source of many bugs, including:
127+ * Audio/video desynchronization
128+ * Stream hanging (due to waiting indefinitely to process/play a buffer because
129+ of a wrong timestamp)
130+ * Stream stalls (e.g. due to processing a real-time stream slightly faster
131+ than real-time)
132+ * Memory leaks (e.g. due to processing a real-time stream slightly slower
133+ than real-time and indefinite buffering)
134+ * Video flickering (due to incorrect handling of B-frames)
135+ * Audio cracking
136+
137+ Therefore, operations on timestamps should be given a lot of care and be well-tested.
0 commit comments