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 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 source ID identifying packet stream.
22 typedef uint32_t source_t;
23 
24 //! Packet sequence number in packet stream.
25 typedef uint16_t seqnum_t;
26 
27 //! Packet sequence numbers difference.
28 typedef int16_t seqnum_diff_t;
29 
30 //! Compute difference between two seqnums.
31 inline seqnum_diff_t seqnum_diff(seqnum_t a, seqnum_t b) {
32  return seqnum_diff_t(a - b);
33 }
34 
35 //! Check if a is before b taking possible wrap into account.
36 inline bool seqnum_lt(seqnum_t a, seqnum_t b) {
37  return seqnum_diff(a, b) < 0;
38 }
39 
40 //! Check if a is before or equal to b taking possible wrap into account.
41 inline bool seqnum_le(seqnum_t a, seqnum_t b) {
42  return seqnum_diff(a, b) <= 0;
43 }
44 
45 //! Audio packet timestamp.
46 typedef uint32_t timestamp_t;
47 
48 //! Audio packet timestamps difference.
49 typedef int32_t timestamp_diff_t;
50 
51 //! Compute difference between two timestamps.
52 inline timestamp_diff_t timestamp_diff(timestamp_t a, timestamp_t b) {
53  return timestamp_diff_t(a - b);
54 }
55 
56 //! Check if a is before b taking possible wrap into account.
57 inline bool timestamp_lt(timestamp_t a, timestamp_t b) {
58  return timestamp_diff(a, b) < 0;
59 }
60 
61 //! Check if a is before or equal to b taking possible wrap into account.
62 inline bool timestamp_le(timestamp_t a, timestamp_t b) {
63  return timestamp_diff(a, b) <= 0;
64 }
65 
66 //! Convert nanoseconds to number of samples.
67 inline timestamp_diff_t timestamp_from_ns(core::nanoseconds_t ns, size_t sample_rate) {
68  return timestamp_diff_t(roundf(float(ns) / core::Second * sample_rate));
69 }
70 
71 //! Convert number of samples to nanoseconds.
72 inline core::nanoseconds_t timestamp_to_ns(timestamp_diff_t ts, size_t sample_rate) {
73  return core::nanoseconds_t(roundf(float(ts) / sample_rate * core::Second));
74 }
75 
76 //! Bitmask of channels present in audio packet.
77 typedef uint32_t channel_mask_t;
78 
79 //! Get number of channels in mask.
80 static inline size_t num_channels(channel_mask_t ch_mask) {
81  size_t n_ch = 0;
82  for (; ch_mask != 0; ch_mask >>= 1) {
83  if (ch_mask & 1) {
84  n_ch++;
85  }
86  }
87  return n_ch;
88 }
89 
90 //! FEC block number in a packet stream.
91 typedef uint16_t blknum_t;
92 
93 //! FEC block numbers difference.
94 typedef int16_t blknum_diff_t;
95 
96 //! Compute difference between two FEC block numbers.
97 inline blknum_diff_t blknum_diff(blknum_t a, blknum_t b) {
98  return blknum_diff_t(a - b);
99 }
100 
101 //! Check if a is before b taking possible wrap into account.
102 inline bool blknum_lt(blknum_t a, blknum_t b) {
103  return blknum_diff(a, b) < 0;
104 }
105 
106 //! Check if a is before or equal to b taking possible wrap into account.
107 inline bool blknum_le(blknum_t a, blknum_t b) {
108  return blknum_diff(a, b) <= 0;
109 }
110 
111 } // namespace packet
112 } // namespace roc
113 
114 #endif // ROC_PACKET_UNITS_H_
seqnum_diff_t seqnum_diff(seqnum_t a, seqnum_t b)
Compute difference between two seqnums.
Definition: units.h:31
bool blknum_le(blknum_t a, blknum_t b)
Check if a is before or equal to b taking possible wrap into account.
Definition: units.h:107
Time definitions.
blknum_diff_t blknum_diff(blknum_t a, blknum_t b)
Compute difference between two FEC block numbers.
Definition: units.h:97
bool timestamp_le(timestamp_t a, timestamp_t b)
Check if a is before or equal to b taking possible wrap into account.
Definition: units.h:62
Root namespace.
timestamp_diff_t timestamp_diff(timestamp_t a, timestamp_t b)
Compute difference between two timestamps.
Definition: units.h:52
uint16_t blknum_t
FEC block number in a packet stream.
Definition: units.h:91
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:21
uint32_t source_t
Packet source ID identifying packet stream.
Definition: units.h:22
uint32_t channel_mask_t
Bitmask of channels present in audio packet.
Definition: units.h:77
uint32_t timestamp_t
Audio packet timestamp.
Definition: units.h:46
Commonly used types and functions.
bool blknum_lt(blknum_t a, blknum_t b)
Check if a is before b taking possible wrap into account.
Definition: units.h:102
bool seqnum_lt(seqnum_t a, seqnum_t b)
Check if a is before b taking possible wrap into account.
Definition: units.h:36
timestamp_diff_t timestamp_from_ns(core::nanoseconds_t ns, size_t sample_rate)
Convert nanoseconds to number of samples.
Definition: units.h:67
int16_t blknum_diff_t
FEC block numbers difference.
Definition: units.h:94
core::nanoseconds_t timestamp_to_ns(timestamp_diff_t ts, size_t sample_rate)
Convert number of samples to nanoseconds.
Definition: units.h:72
int16_t seqnum_diff_t
Packet sequence numbers difference.
Definition: units.h:28
bool seqnum_le(seqnum_t a, seqnum_t b)
Check if a is before or equal to b taking possible wrap into account.
Definition: units.h:41
const nanoseconds_t Second
One second represented in nanoseconds.
Definition: time.h:33
bool timestamp_lt(timestamp_t a, timestamp_t b)
Check if a is before b taking possible wrap into account.
Definition: units.h:57
int32_t timestamp_diff_t
Audio packet timestamps difference.
Definition: units.h:49
uint16_t seqnum_t
Packet sequence number in packet stream.
Definition: units.h:25