rtp_utils.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2011 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 MEDIA_BASE_RTP_UTILS_H_
  11. #define MEDIA_BASE_RTP_UTILS_H_
  12. #include "absl/strings/string_view.h"
  13. #include "api/array_view.h"
  14. #include "rtc_base/byte_order.h"
  15. #include "rtc_base/system/rtc_export.h"
  16. namespace rtc {
  17. struct PacketTimeUpdateParams;
  18. } // namespace rtc
  19. namespace cricket {
  20. const size_t kMinRtpPacketLen = 12;
  21. const size_t kMaxRtpPacketLen = 2048;
  22. const size_t kMinRtcpPacketLen = 4;
  23. struct RtpHeader {
  24. int payload_type;
  25. int seq_num;
  26. uint32_t timestamp;
  27. uint32_t ssrc;
  28. };
  29. enum RtcpTypes {
  30. kRtcpTypeSR = 200, // Sender report payload type.
  31. kRtcpTypeRR = 201, // Receiver report payload type.
  32. kRtcpTypeSDES = 202, // SDES payload type.
  33. kRtcpTypeBye = 203, // BYE payload type.
  34. kRtcpTypeApp = 204, // APP payload type.
  35. kRtcpTypeRTPFB = 205, // Transport layer Feedback message payload type.
  36. kRtcpTypePSFB = 206, // Payload-specific Feedback message payload type.
  37. };
  38. enum class RtpPacketType {
  39. kRtp,
  40. kRtcp,
  41. kUnknown,
  42. };
  43. bool GetRtpPayloadType(const void* data, size_t len, int* value);
  44. bool GetRtpSeqNum(const void* data, size_t len, int* value);
  45. bool GetRtpTimestamp(const void* data, size_t len, uint32_t* value);
  46. bool GetRtpSsrc(const void* data, size_t len, uint32_t* value);
  47. bool GetRtpHeaderLen(const void* data, size_t len, size_t* value);
  48. bool GetRtcpType(const void* data, size_t len, int* value);
  49. bool GetRtcpSsrc(const void* data, size_t len, uint32_t* value);
  50. bool GetRtpHeader(const void* data, size_t len, RtpHeader* header);
  51. bool SetRtpSsrc(void* data, size_t len, uint32_t value);
  52. // Assumes version 2, no padding, no extensions, no csrcs.
  53. bool SetRtpHeader(void* data, size_t len, const RtpHeader& header);
  54. bool IsRtpPacket(rtc::ArrayView<const char> packet);
  55. bool IsRtcpPacket(rtc::ArrayView<const char> packet);
  56. // Checks the packet header to determine if it can be an RTP or RTCP packet.
  57. RtpPacketType InferRtpPacketType(rtc::ArrayView<const char> packet);
  58. // True if |payload type| is 0-127.
  59. bool IsValidRtpPayloadType(int payload_type);
  60. // True if |size| is appropriate for the indicated packet type.
  61. bool IsValidRtpPacketSize(RtpPacketType packet_type, size_t size);
  62. // Returns "RTCP", "RTP" or "Unknown" according to |packet_type|.
  63. absl::string_view RtpPacketTypeToString(RtpPacketType packet_type);
  64. // Verifies that a packet has a valid RTP header.
  65. bool RTC_EXPORT ValidateRtpHeader(const uint8_t* rtp,
  66. size_t length,
  67. size_t* header_length);
  68. // Helper method which updates the absolute send time extension if present.
  69. bool UpdateRtpAbsSendTimeExtension(uint8_t* rtp,
  70. size_t length,
  71. int extension_id,
  72. uint64_t time_us);
  73. // Applies specified |options| to the packet. It updates the absolute send time
  74. // extension header if it is present present then updates HMAC.
  75. bool RTC_EXPORT
  76. ApplyPacketOptions(uint8_t* data,
  77. size_t length,
  78. const rtc::PacketTimeUpdateParams& packet_time_params,
  79. uint64_t time_us);
  80. } // namespace cricket
  81. #endif // MEDIA_BASE_RTP_UTILS_H_