@@ -15,22 +15,25 @@ use the two most common types of timestamps:
1515
1616We know that timestamps represent the time of occurrence of an event, but these
1717concepts are pretty abstract. We need to somehow represent them in the context
18- of our framework. To represent time - durations, latencies, timestamps, - we
18+ of our framework. To represent time - durations, latencies, timestamps - we
1919use terms of type ` t:Membrane.Time.t/0 ` :
2020
2121* To create a term representing some amount of time, we use
2222 ` Membrane.Time.<unit>/0 ` and ` Membrane.Time.<unit>s/1 ` functions. For example,
23- to create a term representing three seconds, we call ` Membrane.Time.seconds(3) ` .
23+ to create a term representing three seconds, we call [ ` Membrane.Time.seconds(3) ` ] ( `Membrane.Time.seconds/1` ) .
2424* To read the amount of time represented, we can use ` Membrane.Time.as_<unit>/2 `
2525 functions. For example, to get an amount of milliseconds represented by a time,
26- we call ` Membrane.Time.as_milliseconds(some_time) `
26+ we call [ ` Membrane.Time.as_milliseconds(some_time) ` ] ( `Membrane.Time.as_milliseconds/1` ) .
27+ This function also allows for rounding the result - you can use ` :round ` mode
28+ to round the result to the nearest integer or ` :exact ` mode to get the result
29+ as a [ rational number] ( https://hexdocs.pm/ratio/Ratio.html#t:t/0 ) .
2730
2831## Carriers of timestamps
2932
30- We now have a way to represent timestamps, but to be useful, they have to refer
31- to something, an event of some sort. Media streams in
32- Membrane are packaged in [ Buffers] ( `t:Membrane.Buffer.t/0` ) when sent
33- between elements. A buffer is a struct with 4 fields:
33+ We now have a way to represent timestamps, but for them to be useful,
34+ they have to refer to something, an event of some sort. Media stream
35+ chunks in Membrane are packaged in [ Buffers] ( `t:Membrane.Buffer.t/0` )
36+ when sent between elements. A buffer is a struct with 4 fields:
3437
3538* ` :payload ` - data contained in the buffer
3639* ` :pts ` and ` :dts ` - timestamps assigned to the buffer
@@ -53,10 +56,10 @@ chunk of audio.
5356[ Realtimer] ( https://hexdocs.pm/membrane_realtimer_plugin/Membrane.Realtimer.html )
5457is an element from
5558[ membrane_realtimer_plugin] ( https://hex.pm/packages/membrane_realtimer_plugin ) .
56- It takes in a stream and limits its flow according to its PTSs. For example, if
57- it receives three buffers with ` :pts ` of 0ms, 200ms and 400ms, then it will send
58- the first buffer, the second buffer after 200ms pass, and the third one after
59- another 200ms pass.
59+ It takes in a stream and limits its flow according to its DTSs or PTSs.
60+ For example, if it receives three buffers with timestamps of 0ms, 200ms
61+ and 400ms, then it will send the first buffer, the second buffer after
62+ 200ms pass, and the third one after another 200ms pass.
6063
6164This element is useful if we have non-realtime input, and realtime output, for
6265example we want to stream the contents of a MP4 file with WebRTC. If we didn't
@@ -107,16 +110,18 @@ Dealing with timestamps can be complicated and very different depending on the
107110use case, so here is some advice on dealing with them:
108111
109112* 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.
113+ * If a filter doesn't use timestamps, it should still forward them.
114+ * If an element relies on timestamps and they are not set, it should raise a
115+ meaningful error.
112116* Sources should attach timestamps to buffers whenever they're known.
113117* Whenever possible, elements should rely on timestamps instead of
114118framerate or audio duration calculated from the stream.
115119* If an element queues buffers in its state (or uses a library that does so), it
116120should make sure that the timestamps for the output buffers are the same as for
117121the corresponding input buffers.
118122* You should ensure that calculations on timestamps don't introduce an
119- accumulating error. Prefer using rationals (Ratio library) to floats.
123+ accumulating error. Prefer using [ rationals] ( https://hexdocs.pm/ratio/Ratio.html#t:t/0 )
124+ over floats.
120125* Elements should generate deterministic output timestamps for better testability.
121126* If an element transforms N input buffers into M output buffers, each of the
122127output buffers should have either:
0 commit comments