blob: 713e64d83c31ae02205c0837a3da4cd9c496d23c [file] [log] [blame]
stefan@webrtc.orga5cb98c2013-05-29 12:12:511/*
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 */
Tommi25eb47c2019-08-29 14:39:0510#include "test/rtp_header_parser.h"
stefan@webrtc.orga5cb98c2013-05-29 12:12:5111
Tommi25eb47c2019-08-29 14:39:0512#include <memory>
Yves Gerey988cc082018-10-23 10:03:0113
Mirko Bonadei92ea95e2017-09-15 04:47:3114#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
15#include "modules/rtp_rtcp/source/rtp_utility.h"
Steve Anton10542f22019-01-11 17:11:0016#include "rtc_base/critical_section.h"
Yves Gerey988cc082018-10-23 10:03:0117#include "rtc_base/thread_annotations.h"
stefan@webrtc.orga5cb98c2013-05-29 12:12:5118
19namespace webrtc {
20
21class RtpHeaderParserImpl : public RtpHeaderParser {
22 public:
23 RtpHeaderParserImpl();
Danil Chapovalov2a5ce2b2018-02-07 08:38:3124 ~RtpHeaderParserImpl() override = default;
stefan@webrtc.orga5cb98c2013-05-29 12:12:5125
kjellander@webrtc.org14665ff2015-03-04 12:58:3526 bool Parse(const uint8_t* packet,
27 size_t length,
28 RTPHeader* header) const override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:5129
kjellander@webrtc.org14665ff2015-03-04 12:58:3530 bool RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id) override;
Sebastian Janssonfd201712018-11-12 15:44:1631 bool RegisterRtpHeaderExtension(RtpExtension extension) override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:5132
kjellander@webrtc.org14665ff2015-03-04 12:58:3533 bool DeregisterRtpHeaderExtension(RTPExtensionType type) override;
Sebastian Janssonfd201712018-11-12 15:44:1634 bool DeregisterRtpHeaderExtension(RtpExtension extension) override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:5135
36 private:
danilchap7c9426c2016-04-14 10:05:3137 rtc::CriticalSection critical_section_;
danilchap56359be2017-09-07 14:53:4538 RtpHeaderExtensionMap rtp_header_extension_map_
39 RTC_GUARDED_BY(critical_section_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:5140};
41
Tommi25eb47c2019-08-29 14:39:0542std::unique_ptr<RtpHeaderParser> RtpHeaderParser::CreateForTest() {
Mirko Bonadei317a1f02019-09-17 15:06:1843 return std::make_unique<RtpHeaderParserImpl>();
stefan@webrtc.orga5cb98c2013-05-29 12:12:5144}
45
danilchap7c9426c2016-04-14 10:05:3146RtpHeaderParserImpl::RtpHeaderParserImpl() {}
stefan@webrtc.orga5cb98c2013-05-29 12:12:5147
pbos@webrtc.org62bafae2014-07-08 12:10:5148bool RtpHeaderParser::IsRtcp(const uint8_t* packet, size_t length) {
49 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:5150 return rtp_parser.RTCP();
51}
52
Sebastian Jansson1e427612019-03-05 13:25:0353absl::optional<uint32_t> RtpHeaderParser::GetSsrc(const uint8_t* packet,
54 size_t length) {
55 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
56 RTPHeader header;
57 if (rtp_parser.Parse(&header, nullptr)) {
58 return header.ssrc;
59 }
60 return absl::nullopt;
61}
62
pbos@webrtc.org62bafae2014-07-08 12:10:5163bool RtpHeaderParserImpl::Parse(const uint8_t* packet,
64 size_t length,
65 RTPHeader* header) const {
66 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
Niels Möllera533e002019-03-26 12:14:3067 *header = RTPHeader();
stefan@webrtc.orga5cb98c2013-05-29 12:12:5168
69 RtpHeaderExtensionMap map;
70 {
danilchap7c9426c2016-04-14 10:05:3171 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 16:39:3572 map = rtp_header_extension_map_;
stefan@webrtc.orga5cb98c2013-05-29 12:12:5173 }
74
danilchapf6975f42015-12-28 18:18:4675 const bool valid_rtpheader = rtp_parser.Parse(header, &map);
stefan@webrtc.orga5cb98c2013-05-29 12:12:5176 if (!valid_rtpheader) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:5177 return false;
78 }
79 return true;
80}
Sebastian Janssonfd201712018-11-12 15:44:1681bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RtpExtension extension) {
82 rtc::CritScope cs(&critical_section_);
83 return rtp_header_extension_map_.RegisterByUri(extension.id, extension.uri);
84}
stefan@webrtc.orga5cb98c2013-05-29 12:12:5185
86bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RTPExtensionType type,
87 uint8_t id) {
danilchap7c9426c2016-04-14 10:05:3188 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 16:39:3589 return rtp_header_extension_map_.RegisterByType(id, type);
stefan@webrtc.orga5cb98c2013-05-29 12:12:5190}
91
Sebastian Janssonfd201712018-11-12 15:44:1692bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RtpExtension extension) {
93 rtc::CritScope cs(&critical_section_);
94 return rtp_header_extension_map_.Deregister(
95 rtp_header_extension_map_.GetType(extension.id));
96}
97
stefan@webrtc.orga5cb98c2013-05-29 12:12:5198bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RTPExtensionType type) {
danilchap7c9426c2016-04-14 10:05:3199 rtc::CritScope cs(&critical_section_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51100 return rtp_header_extension_map_.Deregister(type) == 0;
101}
102} // namespace webrtc