You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2013 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. #include "test/rtp_header_parser.h"
  11. #include <memory>
  12. #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
  13. #include "modules/rtp_rtcp/source/rtp_utility.h"
  14. #include "rtc_base/critical_section.h"
  15. #include "rtc_base/thread_annotations.h"
  16. namespace webrtc {
  17. class RtpHeaderParserImpl : public RtpHeaderParser {
  18. public:
  19. RtpHeaderParserImpl();
  20. ~RtpHeaderParserImpl() override = default;
  21. bool Parse(const uint8_t* packet,
  22. size_t length,
  23. RTPHeader* header) const override;
  24. bool RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id) override;
  25. bool RegisterRtpHeaderExtension(RtpExtension extension) override;
  26. bool DeregisterRtpHeaderExtension(RTPExtensionType type) override;
  27. bool DeregisterRtpHeaderExtension(RtpExtension extension) override;
  28. private:
  29. rtc::CriticalSection critical_section_;
  30. RtpHeaderExtensionMap rtp_header_extension_map_
  31. RTC_GUARDED_BY(critical_section_);
  32. };
  33. std::unique_ptr<RtpHeaderParser> RtpHeaderParser::CreateForTest() {
  34. return std::make_unique<RtpHeaderParserImpl>();
  35. }
  36. RtpHeaderParserImpl::RtpHeaderParserImpl() {}
  37. bool RtpHeaderParser::IsRtcp(const uint8_t* packet, size_t length) {
  38. RtpUtility::RtpHeaderParser rtp_parser(packet, length);
  39. return rtp_parser.RTCP();
  40. }
  41. absl::optional<uint32_t> RtpHeaderParser::GetSsrc(const uint8_t* packet,
  42. size_t length) {
  43. RtpUtility::RtpHeaderParser rtp_parser(packet, length);
  44. RTPHeader header;
  45. if (rtp_parser.Parse(&header, nullptr)) {
  46. return header.ssrc;
  47. }
  48. return absl::nullopt;
  49. }
  50. bool RtpHeaderParserImpl::Parse(const uint8_t* packet,
  51. size_t length,
  52. RTPHeader* header) const {
  53. RtpUtility::RtpHeaderParser rtp_parser(packet, length);
  54. *header = RTPHeader();
  55. RtpHeaderExtensionMap map;
  56. {
  57. rtc::CritScope cs(&critical_section_);
  58. map = rtp_header_extension_map_;
  59. }
  60. const bool valid_rtpheader = rtp_parser.Parse(header, &map);
  61. if (!valid_rtpheader) {
  62. return false;
  63. }
  64. return true;
  65. }
  66. bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RtpExtension extension) {
  67. rtc::CritScope cs(&critical_section_);
  68. return rtp_header_extension_map_.RegisterByUri(extension.id, extension.uri);
  69. }
  70. bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RTPExtensionType type,
  71. uint8_t id) {
  72. rtc::CritScope cs(&critical_section_);
  73. return rtp_header_extension_map_.RegisterByType(id, type);
  74. }
  75. bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RtpExtension extension) {
  76. rtc::CritScope cs(&critical_section_);
  77. return rtp_header_extension_map_.Deregister(
  78. rtp_header_extension_map_.GetType(extension.id));
  79. }
  80. bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RTPExtensionType type) {
  81. rtc::CritScope cs(&critical_section_);
  82. return rtp_header_extension_map_.Deregister(type) == 0;
  83. }
  84. } // namespace webrtc