Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
format_map.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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_rtp/format_map.h
10 //! @brief RTP payload format map.
11 
12 #ifndef ROC_RTP_FORMAT_MAP_H_
13 #define ROC_RTP_FORMAT_MAP_H_
14 
15 #include "roc_audio/sample_spec.h"
17 #include "roc_core/attributes.h"
18 #include "roc_core/hashmap.h"
19 #include "roc_core/iarena.h"
20 #include "roc_core/mutex.h"
21 #include "roc_core/noncopyable.h"
22 #include "roc_core/ref_counted.h"
23 #include "roc_core/slab_pool.h"
24 #include "roc_rtp/format.h"
25 
26 namespace roc {
27 namespace rtp {
28 
29 //! RTP payload format map.
30 //! Thread-safe.
31 //! Returned formats are immutable and can be safely used from
32 //! any thread.
33 class FormatMap : public core::NonCopyable<> {
34 public:
35  //! Initialize.
37 
38  //! Find format by payload type.
39  //! @returns
40  //! pointer to the format structure or null if there is no format
41  //! registered for this payload type.
42  const Format* find_by_pt(unsigned int pt) const;
43 
44  //! Find format by sample specification.
45  //! @returns
46  //! pointer to the format structure or null if there is no format
47  //! with matching specification.
48  const Format* find_by_spec(const audio::SampleSpec& spec) const;
49 
50  //! Add format to the map.
51  //! @returns
52  //! true if successfully added or false if another format with the same
53  //! payload type already exists.
55 
56 private:
57  enum { PreallocatedNodes = 16 };
58 
59  struct Node : core::RefCounted<Node, core::PoolAllocation>, core::HashmapNode {
60  Node(core::IPool& pool, const Format& format)
61  : core::RefCounted<Node, core::PoolAllocation>(pool)
62  , format(format) {
63  }
64 
65  Format format;
66 
67  unsigned int key() const {
68  return format.payload_type;
69  }
70 
71  static core::hashsum_t key_hash(unsigned int pt) {
72  return core::hashsum_int(pt);
73  }
74 
75  static bool key_equal(unsigned int pt1, unsigned int pt2) {
76  return pt1 == pt2;
77  }
78  };
79 
80  void add_builtin_(const Format& fmt);
81 
82  core::Mutex mutex_;
83 
84  core::SlabPool<Node, PreallocatedNodes> node_pool_;
85  core::Hashmap<Node, PreallocatedNodes> node_map_;
86 };
87 
88 } // namespace rtp
89 } // namespace roc
90 
91 #endif // ROC_RTP_FORMAT_MAP_H_
Allocation policies.
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:26
Base class for hashmap element.
Definition: hashmap_node.h:27
Memory arena interface.
Definition: iarena.h:23
Memory pool interface.
Definition: ipool.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Base class for object with reference counter.
Definition: ref_counted.h:40
RTP payload format map. Thread-safe. Returned formats are immutable and can be safely used from any t...
Definition: format_map.h:33
const Format * find_by_spec(const audio::SampleSpec &spec) const
Find format by sample specification.
FormatMap(core::IArena &arena)
Initialize.
const Format * find_by_pt(unsigned int pt) const
Find format by payload type.
ROC_ATTR_NODISCARD bool add_format(const Format &fmt)
Add format to the map.
RTP payload format.
Intrusive hash table.
Memory arena interface.
Mutex.
hashsum_t hashsum_int(int16_t)
Compute hash of 16-bit integer.
size_t hashsum_t
Hash type.
Definition: hashsum.h:21
Root namespace.
Non-copyable object.
Base class for object with reference counter.
Sample specifications.
Memory pool.
RTP payload format.
Definition: format.h:26
unsigned int payload_type
Payload type.
Definition: format.h:28