Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
units.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_packet/units.h
10 //! @brief Various units used in packets.
11 
12 #ifndef ROC_PACKET_UNITS_H_
13 #define ROC_PACKET_UNITS_H_
14 
15 #include "roc_core/stddefs.h"
16 #include "roc_core/time.h"
17 
18 namespace roc {
19 namespace packet {
20 
21 //! Packet stream identifier.
22 //! @remarks
23 //! Identifies packet stream within session.
24 //! Unique only within one session.
25 //! For example, audio packet stream and repair (FEC) packet stream
26 //! usually have different source identifiers.
27 typedef uint32_t stream_source_t;
28 
29 //! Packet stream timestamp.
30 //! @remarks
31 //! Defines position of packet contents (e.g. audio chunk) within stream.
32 //! Starts from unspecified value and can wrap.
33 //! Measured in sender's clock domain and clock rate.
34 //! For PCM audio, stream timestamp is incremented by one every N samples,
35 //! where N is the number of channels.
36 typedef uint32_t stream_timestamp_t;
37 
38 //! Packet stream timestamp delta.
39 //! @remarks
40 //! Signed version of stream_timestamp_t.
41 typedef int32_t stream_timestamp_diff_t;
42 
43 //! Compute difference between two timestamps.
45  const stream_timestamp_t b) {
46  return stream_timestamp_diff_t(a - b);
47 }
48 
49 //! Check if `a` is before `b`, taking possible wrap into account.
51  return stream_timestamp_diff(a, b) < 0;
52 }
53 
54 //! Check if `a` is before or equal to `b`, taking possible wrap into account.
56  return stream_timestamp_diff(a, b) <= 0;
57 }
58 
59 //! Convert nanoseconds to stream timestamp.
61 
62 //! Convert stream timestamp.to nanoseconds.
64 
65 //! Convert nanoseconds to stream timestamp delta.
67  size_t sample_rate);
68 
69 //! Convert stream timestamp.delta to nanoseconds.
71  size_t sample_rate);
72 
73 //! Packet sequence number.
74 //! @remarks
75 //! Defines position of packet within stream.
76 //! Starts from unspecified value and can wrap.
77 //! Incremented by one each packet.
78 typedef uint16_t seqnum_t;
79 
80 //! Packet sequence number delta.
81 //! @remarks
82 //! Signed version of seqnum_t.
83 typedef int16_t seqnum_diff_t;
84 
85 //! Compute difference between two seqnums.
86 inline seqnum_diff_t seqnum_diff(const seqnum_t a, const seqnum_t b) {
87  return seqnum_diff_t(a - b);
88 }
89 
90 //! Check if `a` is before `b`, taking possible wrap into account.
91 inline bool seqnum_lt(const seqnum_t a, const seqnum_t b) {
92  return seqnum_diff(a, b) < 0;
93 }
94 
95 //! Check if `a` is before or equal to `b`, taking possible wrap into account.
96 inline bool seqnum_le(const seqnum_t a, const seqnum_t b) {
97  return seqnum_diff(a, b) <= 0;
98 }
99 
100 //! Extended sequence number.
101 //! @remarks
102 //! Sequence number extended to 32 bits.
103 typedef uint32_t ext_seqnum_t;
104 
105 //! Extended sequence number delta.
106 //! @remarks
107 //! Signed version of ext_seqnum_t.
108 typedef int32_t ext_seqnum_diff_t;
109 
110 //! Compute difference between two extended seqnums.
112  return ext_seqnum_diff_t(a - b);
113 }
114 
115 //! Check if `a` is before `b`, taking possible wrap into account.
116 inline bool ext_seqnum_lt(const ext_seqnum_t a, const ext_seqnum_t b) {
117  return ext_seqnum_diff(a, b) < 0;
118 }
119 
120 //! Check if `a` is before or equal to `b`, taking possible wrap into account.
121 inline bool ext_seqnum_le(const ext_seqnum_t a, const ext_seqnum_t b) {
122  return ext_seqnum_diff(a, b) <= 0;
123 }
124 
125 //! FEC packet block number.
126 //! @remarks
127 //! Defines position of FEC packet block within stream.
128 //! Starts from unspecified value and can wrap.
129 //! Incremented by one each block.
130 typedef uint16_t blknum_t;
131 
132 //! FEC packet block number delta.
133 //! @remarks
134 //! Signed version of blknum_t.
135 typedef int16_t blknum_diff_t;
136 
137 //! Compute difference between two FEC packet block numbers.
138 inline blknum_diff_t blknum_diff(const blknum_t a, const blknum_t b) {
139  return blknum_diff_t(a - b);
140 }
141 
142 //! Check if `a` is before `b`, taking possible wrap into account.
143 inline bool blknum_lt(const blknum_t a, const blknum_t b) {
144  return blknum_diff(a, b) < 0;
145 }
146 
147 //! Check if `a` is before or equal to `b`, taking possible wrap into account.
148 inline bool blknum_le(const blknum_t a, const blknum_t b) {
149  return blknum_diff(a, b) <= 0;
150 }
151 
152 } // namespace packet
153 } // namespace roc
154 
155 #endif // ROC_PACKET_UNITS_H_
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
seqnum_diff_t seqnum_diff(const seqnum_t a, const seqnum_t b)
Compute difference between two seqnums.
Definition: units.h:86
ext_seqnum_diff_t ext_seqnum_diff(const ext_seqnum_t a, const ext_seqnum_t b)
Compute difference between two extended seqnums.
Definition: units.h:111
bool ext_seqnum_lt(const ext_seqnum_t a, const ext_seqnum_t b)
Check if a is before b, taking possible wrap into account.
Definition: units.h:116
uint32_t ext_seqnum_t
Extended sequence number.
Definition: units.h:103
bool seqnum_le(const seqnum_t a, const seqnum_t b)
Check if a is before or equal to b, taking possible wrap into account.
Definition: units.h:96
int32_t ext_seqnum_diff_t
Extended sequence number delta.
Definition: units.h:108
int16_t seqnum_diff_t
Packet sequence number delta.
Definition: units.h:83
bool ext_seqnum_le(const ext_seqnum_t a, const ext_seqnum_t b)
Check if a is before or equal to b, taking possible wrap into account.
Definition: units.h:121
int16_t blknum_diff_t
FEC packet block number delta.
Definition: units.h:135
uint32_t stream_source_t
Packet stream identifier.
Definition: units.h:27
stream_timestamp_diff_t stream_timestamp_diff(const stream_timestamp_t a, const stream_timestamp_t b)
Compute difference between two timestamps.
Definition: units.h:44
bool stream_timestamp_le(const stream_timestamp_t a, const stream_timestamp_t b)
Check if a is before or equal to b, taking possible wrap into account.
Definition: units.h:55
core::nanoseconds_t stream_timestamp_2_ns(stream_timestamp_t ts, size_t sample_rate)
Convert stream timestamp.to nanoseconds.
bool seqnum_lt(const seqnum_t a, const seqnum_t b)
Check if a is before b, taking possible wrap into account.
Definition: units.h:91
uint16_t seqnum_t
Packet sequence number.
Definition: units.h:78
stream_timestamp_diff_t ns_2_stream_timestamp_delta(core::nanoseconds_t ns, size_t sample_rate)
Convert nanoseconds to stream timestamp delta.
uint16_t blknum_t
FEC packet block number.
Definition: units.h:130
blknum_diff_t blknum_diff(const blknum_t a, const blknum_t b)
Compute difference between two FEC packet block numbers.
Definition: units.h:138
uint32_t stream_timestamp_t
Packet stream timestamp.
Definition: units.h:36
core::nanoseconds_t stream_timestamp_delta_2_ns(stream_timestamp_diff_t ts, size_t sample_rate)
Convert stream timestamp.delta to nanoseconds.
bool blknum_le(const blknum_t a, const blknum_t b)
Check if a is before or equal to b, taking possible wrap into account.
Definition: units.h:148
bool stream_timestamp_lt(const stream_timestamp_t a, const stream_timestamp_t b)
Check if a is before b, taking possible wrap into account.
Definition: units.h:50
bool blknum_lt(const blknum_t a, const blknum_t b)
Check if a is before b, taking possible wrap into account.
Definition: units.h:143
int32_t stream_timestamp_diff_t
Packet stream timestamp delta.
Definition: units.h:41
stream_timestamp_t ns_2_stream_timestamp(core::nanoseconds_t ns, size_t sample_rate)
Convert nanoseconds to stream timestamp.
Root namespace.
Commonly used types and functions.
Time definitions.