rtp_demuxer.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef CALL_RTP_DEMUXER_H_
  11. #define CALL_RTP_DEMUXER_H_
  12. #include <map>
  13. #include <set>
  14. #include <string>
  15. #include <utility>
  16. #include <vector>
  17. namespace webrtc {
  18. class RtpPacketReceived;
  19. class RtpPacketSinkInterface;
  20. // This struct describes the criteria that will be used to match packets to a
  21. // specific sink.
  22. struct RtpDemuxerCriteria {
  23. RtpDemuxerCriteria();
  24. ~RtpDemuxerCriteria();
  25. // If not the empty string, will match packets with this MID.
  26. std::string mid;
  27. // If not the empty string, will match packets with this as their RTP stream
  28. // ID or repaired RTP stream ID.
  29. // Note that if both MID and RSID are specified, this will only match packets
  30. // that have both specified (either through RTP header extensions, SSRC
  31. // latching or RTCP).
  32. std::string rsid;
  33. // Will match packets with any of these SSRCs.
  34. std::set<uint32_t> ssrcs;
  35. // Will match packets with any of these payload types.
  36. std::set<uint8_t> payload_types;
  37. // Return string representation of demux criteria to facilitate logging
  38. std::string ToString() const;
  39. };
  40. // This class represents the RTP demuxing, for a single RTP session (i.e., one
  41. // SSRC space, see RFC 7656). It isn't thread aware, leaving responsibility of
  42. // multithreading issues to the user of this class.
  43. // The demuxing algorithm follows the sketch given in the BUNDLE draft:
  44. // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-38#section-10.2
  45. // with modifications to support RTP stream IDs also.
  46. //
  47. // When a packet is received, the RtpDemuxer will route according to the
  48. // following rules:
  49. // 1. If the packet contains the MID header extension, and no sink has been
  50. // added with that MID as a criteria, the packet is not routed.
  51. // 2. If the packet has the MID header extension, but no RSID or RRID extension,
  52. // and the MID is bound to a sink, then bind its SSRC to the same sink and
  53. // forward the packet to that sink. Note that rebinding to the same sink is
  54. // not an error. (Later packets with that SSRC would therefore be forwarded
  55. // to the same sink, whether they have the MID header extension or not.)
  56. // 3. If the packet has the MID header extension and either the RSID or RRID
  57. // extension, and the MID, RSID (or RRID) pair is bound to a sink, then bind
  58. // its SSRC to the same sink and forward the packet to that sink. Later
  59. // packets with that SSRC will be forwarded to the same sink.
  60. // 4. If the packet has the RSID or RRID header extension, but no MID extension,
  61. // and the RSID or RRID is bound to an RSID sink, then bind its SSRC to the
  62. // same sink and forward the packet to that sink. Later packets with that
  63. // SSRC will be forwarded to the same sink.
  64. // 5. If the packet's SSRC is bound to an SSRC through a previous call to
  65. // AddSink, then forward the packet to that sink. Note that the RtpDemuxer
  66. // will not verify the payload type even if included in the sink's criteria.
  67. // The sink is expected to do the check in its handler.
  68. // 6. If the packet's payload type is bound to exactly one payload type sink
  69. // through an earlier call to AddSink, then forward the packet to that sink.
  70. // 7. Otherwise, the packet is not routed.
  71. //
  72. // In summary, the routing algorithm will always try to first match MID and RSID
  73. // (including through SSRC binding), match SSRC directly as needed, and use
  74. // payload types only if all else fails.
  75. class RtpDemuxer {
  76. public:
  77. // Maximum number of unique SSRC bindings allowed. This limit is to prevent
  78. // memory overuse attacks due to a malicious peer sending many packets with
  79. // different SSRCs.
  80. static constexpr int kMaxSsrcBindings = 1000;
  81. // Returns a string that contains all the attributes of the given packet
  82. // relevant for demuxing.
  83. static std::string DescribePacket(const RtpPacketReceived& packet);
  84. RtpDemuxer();
  85. ~RtpDemuxer();
  86. RtpDemuxer(const RtpDemuxer&) = delete;
  87. void operator=(const RtpDemuxer&) = delete;
  88. // Registers a sink that will be notified when RTP packets match its given
  89. // criteria according to the algorithm described in the class description.
  90. // Returns true if the sink was successfully added.
  91. // Returns false in the following situations:
  92. // - Only MID is specified and the MID is already registered.
  93. // - Only RSID is specified and the RSID is already registered.
  94. // - Both MID and RSID is specified and the (MID, RSID) pair is already
  95. // registered.
  96. // - Any of the criteria SSRCs are already registered.
  97. // If false is returned, no changes are made to the demuxer state.
  98. bool AddSink(const RtpDemuxerCriteria& criteria,
  99. RtpPacketSinkInterface* sink);
  100. // Registers a sink. Multiple SSRCs may be mapped to the same sink, but
  101. // each SSRC may only be mapped to one sink. The return value reports
  102. // whether the association has been recorded or rejected. Rejection may occur
  103. // if the SSRC has already been associated with a sink. The previously added
  104. // sink is *not* forgotten.
  105. bool AddSink(uint32_t ssrc, RtpPacketSinkInterface* sink);
  106. // Registers a sink's association to an RSID. Only one sink may be associated
  107. // with a given RSID. Null pointer is not allowed.
  108. void AddSink(const std::string& rsid, RtpPacketSinkInterface* sink);
  109. // Removes a sink. Return value reports if anything was actually removed.
  110. // Null pointer is not allowed.
  111. bool RemoveSink(const RtpPacketSinkInterface* sink);
  112. // Demuxes the given packet and forwards it to the chosen sink. Returns true
  113. // if the packet was forwarded and false if the packet was dropped.
  114. bool OnRtpPacket(const RtpPacketReceived& packet);
  115. // Configure whether to look at the MID header extension when demuxing
  116. // incoming RTP packets. By default this is enabled.
  117. void set_use_mid(bool use_mid) { use_mid_ = use_mid; }
  118. private:
  119. // Returns true if adding a sink with the given criteria would cause conflicts
  120. // with the existing criteria and should be rejected.
  121. bool CriteriaWouldConflict(const RtpDemuxerCriteria& criteria) const;
  122. // Runs the demux algorithm on the given packet and returns the sink that
  123. // should receive the packet.
  124. // Will record any SSRC<->ID associations along the way.
  125. // If the packet should be dropped, this method returns null.
  126. RtpPacketSinkInterface* ResolveSink(const RtpPacketReceived& packet);
  127. // Used by the ResolveSink algorithm.
  128. RtpPacketSinkInterface* ResolveSinkByMid(const std::string& mid,
  129. uint32_t ssrc);
  130. RtpPacketSinkInterface* ResolveSinkByMidRsid(const std::string& mid,
  131. const std::string& rsid,
  132. uint32_t ssrc);
  133. RtpPacketSinkInterface* ResolveSinkByRsid(const std::string& rsid,
  134. uint32_t ssrc);
  135. RtpPacketSinkInterface* ResolveSinkByPayloadType(uint8_t payload_type,
  136. uint32_t ssrc);
  137. // Regenerate the known_mids_ set from information in the sink_by_mid_ and
  138. // sink_by_mid_and_rsid_ maps.
  139. void RefreshKnownMids();
  140. // Map each sink by its component attributes to facilitate quick lookups.
  141. // Payload Type mapping is a multimap because if two sinks register for the
  142. // same payload type, both AddSinks succeed but we must know not to demux on
  143. // that attribute since it is ambiguous.
  144. // Note: Mappings are only modified by AddSink/RemoveSink (except for
  145. // SSRC mapping which receives all MID, payload type, or RSID to SSRC bindings
  146. // discovered when demuxing packets).
  147. std::map<std::string, RtpPacketSinkInterface*> sink_by_mid_;
  148. std::map<uint32_t, RtpPacketSinkInterface*> sink_by_ssrc_;
  149. std::multimap<uint8_t, RtpPacketSinkInterface*> sinks_by_pt_;
  150. std::map<std::pair<std::string, std::string>, RtpPacketSinkInterface*>
  151. sink_by_mid_and_rsid_;
  152. std::map<std::string, RtpPacketSinkInterface*> sink_by_rsid_;
  153. // Tracks all the MIDs that have been identified in added criteria. Used to
  154. // determine if a packet should be dropped right away because the MID is
  155. // unknown.
  156. std::set<std::string> known_mids_;
  157. // Records learned mappings of MID --> SSRC and RSID --> SSRC as packets are
  158. // received.
  159. // This is stored separately from the sink mappings because if a sink is
  160. // removed we want to still remember these associations.
  161. std::map<uint32_t, std::string> mid_by_ssrc_;
  162. std::map<uint32_t, std::string> rsid_by_ssrc_;
  163. // Adds a binding from the SSRC to the given sink.
  164. void AddSsrcSinkBinding(uint32_t ssrc, RtpPacketSinkInterface* sink);
  165. bool use_mid_ = true;
  166. };
  167. } // namespace webrtc
  168. #endif // CALL_RTP_DEMUXER_H_