Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
encoding_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/encoding_map.h
10 //! @brief RTP encoding map.
11 
12 #ifndef ROC_RTP_ENCODING_MAP_H_
13 #define ROC_RTP_ENCODING_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/encoding.h"
25 
26 namespace roc {
27 namespace rtp {
28 
29 //! RTP encoding map.
30 //! Thread-safe.
31 //! Returned encodings are immutable and can be safely used from
32 //! any thread.
33 class EncodingMap : public core::NonCopyable<> {
34 public:
35  //! Initialize.
37 
38  //! Find encoding by payload type.
39  //! @returns
40  //! pointer to the encoding structure or null if there is no encoding
41  //! registered for this payload type.
42  const Encoding* find_by_pt(unsigned int pt) const;
43 
44  //! Find encoding by sample specification.
45  //! @returns
46  //! pointer to the encoding structure or null if there is no encoding
47  //! with matching specification.
48  const Encoding* find_by_spec(const audio::SampleSpec& spec) const;
49 
50  //! Add encoding to the map.
51  //! @returns
52  //! true if successfully added or false if another encoding 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 Encoding& encoding)
61  : core::RefCounted<Node, core::PoolAllocation>(pool)
62  , encoding(encoding) {
63  }
64 
65  Encoding encoding;
66 
67  unsigned int key() const {
68  return encoding.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 Encoding& enc);
81  void find_codecs_(Encoding& enc);
82 
83  core::Mutex mutex_;
84 
85  core::SlabPool<Node, PreallocatedNodes> node_pool_;
86  core::Hashmap<Node, PreallocatedNodes> node_map_;
87 };
88 
89 } // namespace rtp
90 } // namespace roc
91 
92 #endif // ROC_RTP_ENCODING_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:30
Base class for Hashmap element.
Definition: hashmap_node.h:61
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 encoding map. Thread-safe. Returned encodings are immutable and can be safely used from any threa...
Definition: encoding_map.h:33
const Encoding * find_by_pt(unsigned int pt) const
Find encoding by payload type.
const Encoding * find_by_spec(const audio::SampleSpec &spec) const
Find encoding by sample specification.
EncodingMap(core::IArena &arena)
Initialize.
ROC_ATTR_NODISCARD bool add_encoding(Encoding enc)
Add encoding to the map.
RTP encoding.
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 encoding.
Definition: encoding.h:27
unsigned int payload_type
Payload type.
Definition: encoding.h:29