source_tracker.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2019 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 MODULES_RTP_RTCP_SOURCE_SOURCE_TRACKER_H_
  11. #define MODULES_RTP_RTCP_SOURCE_SOURCE_TRACKER_H_
  12. #include <cstdint>
  13. #include <list>
  14. #include <unordered_map>
  15. #include <utility>
  16. #include <vector>
  17. #include "absl/types/optional.h"
  18. #include "api/rtp_packet_infos.h"
  19. #include "api/transport/rtp/rtp_source.h"
  20. #include "rtc_base/synchronization/mutex.h"
  21. #include "rtc_base/time_utils.h"
  22. #include "system_wrappers/include/clock.h"
  23. namespace webrtc {
  24. //
  25. // Tracker for `RTCRtpContributingSource` and `RTCRtpSynchronizationSource`:
  26. // - https://w3c.github.io/webrtc-pc/#dom-rtcrtpcontributingsource
  27. // - https://w3c.github.io/webrtc-pc/#dom-rtcrtpsynchronizationsource
  28. //
  29. class SourceTracker {
  30. public:
  31. // Amount of time before the entry associated with an update is removed. See:
  32. // https://w3c.github.io/webrtc-pc/#dom-rtcrtpreceiver-getcontributingsources
  33. static constexpr int64_t kTimeoutMs = 10000; // 10 seconds
  34. explicit SourceTracker(Clock* clock);
  35. SourceTracker(const SourceTracker& other) = delete;
  36. SourceTracker(SourceTracker&& other) = delete;
  37. SourceTracker& operator=(const SourceTracker& other) = delete;
  38. SourceTracker& operator=(SourceTracker&& other) = delete;
  39. // Updates the source entries when a frame is delivered to the
  40. // RTCRtpReceiver's MediaStreamTrack.
  41. void OnFrameDelivered(const RtpPacketInfos& packet_infos);
  42. // Returns an |RtpSource| for each unique SSRC and CSRC identifier updated in
  43. // the last |kTimeoutMs| milliseconds. Entries appear in reverse chronological
  44. // order (i.e. with the most recently updated entries appearing first).
  45. std::vector<RtpSource> GetSources() const;
  46. private:
  47. struct SourceKey {
  48. SourceKey(RtpSourceType source_type, uint32_t source)
  49. : source_type(source_type), source(source) {}
  50. // Type of |source|.
  51. RtpSourceType source_type;
  52. // CSRC or SSRC identifier of the contributing or synchronization source.
  53. uint32_t source;
  54. };
  55. struct SourceKeyComparator {
  56. bool operator()(const SourceKey& lhs, const SourceKey& rhs) const {
  57. return (lhs.source_type == rhs.source_type) && (lhs.source == rhs.source);
  58. }
  59. };
  60. struct SourceKeyHasher {
  61. size_t operator()(const SourceKey& value) const {
  62. return static_cast<size_t>(value.source_type) +
  63. static_cast<size_t>(value.source) * 11076425802534262905ULL;
  64. }
  65. };
  66. struct SourceEntry {
  67. // Timestamp indicating the most recent time a frame from an RTP packet,
  68. // originating from this source, was delivered to the RTCRtpReceiver's
  69. // MediaStreamTrack. Its reference clock is the outer class's |clock_|.
  70. int64_t timestamp_ms;
  71. // Audio level from an RFC 6464 or RFC 6465 header extension received with
  72. // the most recent packet used to assemble the frame associated with
  73. // |timestamp_ms|. May be absent. Only relevant for audio receivers. See the
  74. // specs for `RTCRtpContributingSource` for more info.
  75. absl::optional<uint8_t> audio_level;
  76. // Absolute capture time header extension received or interpolated from the
  77. // most recent packet used to assemble the frame. For more info see
  78. // https://webrtc.org/experiments/rtp-hdrext/abs-capture-time/
  79. absl::optional<AbsoluteCaptureTime> absolute_capture_time;
  80. // RTP timestamp of the most recent packet used to assemble the frame
  81. // associated with |timestamp_ms|.
  82. uint32_t rtp_timestamp;
  83. };
  84. using SourceList = std::list<std::pair<const SourceKey, SourceEntry>>;
  85. using SourceMap = std::unordered_map<SourceKey,
  86. SourceList::iterator,
  87. SourceKeyHasher,
  88. SourceKeyComparator>;
  89. // Updates an entry by creating it (if it didn't previously exist) and moving
  90. // it to the front of the list. Returns a reference to the entry.
  91. SourceEntry& UpdateEntry(const SourceKey& key)
  92. RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
  93. // Removes entries that have timed out. Marked as "const" so that we can do
  94. // pruning in getters.
  95. void PruneEntries(int64_t now_ms) const RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
  96. Clock* const clock_;
  97. mutable Mutex lock_;
  98. // Entries are stored in reverse chronological order (i.e. with the most
  99. // recently updated entries appearing first). Mutability is needed for timeout
  100. // pruning in const functions.
  101. mutable SourceList list_ RTC_GUARDED_BY(lock_);
  102. mutable SourceMap map_ RTC_GUARDED_BY(lock_);
  103. };
  104. } // namespace webrtc
  105. #endif // MODULES_RTP_RTCP_SOURCE_SOURCE_TRACKER_H_