blob: 713e64d83c31ae02205c0837a3da4cd9c496d23c [file] [log] [blame]
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001/*
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 16:39:05 +020010#include "test/rtp_header_parser.h"
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000011
Tommi25eb47c2019-08-29 16:39:05 +020012#include <memory>
Yves Gerey988cc082018-10-23 12:03:01 +020013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
15#include "modules/rtp_rtcp/source/rtp_utility.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/critical_section.h"
Yves Gerey988cc082018-10-23 12:03:01 +020017#include "rtc_base/thread_annotations.h"
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000018
19namespace webrtc {
20
21class RtpHeaderParserImpl : public RtpHeaderParser {
22 public:
23 RtpHeaderParserImpl();
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010024 ~RtpHeaderParserImpl() override = default;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000025
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000026 bool Parse(const uint8_t* packet,
27 size_t length,
28 RTPHeader* header) const override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000029
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000030 bool RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id) override;
Sebastian Janssonfd201712018-11-12 16:44:16 +010031 bool RegisterRtpHeaderExtension(RtpExtension extension) override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000032
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000033 bool DeregisterRtpHeaderExtension(RTPExtensionType type) override;
Sebastian Janssonfd201712018-11-12 16:44:16 +010034 bool DeregisterRtpHeaderExtension(RtpExtension extension) override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000035
36 private:
danilchap7c9426c2016-04-14 03:05:31 -070037 rtc::CriticalSection critical_section_;
danilchap56359be2017-09-07 07:53:45 -070038 RtpHeaderExtensionMap rtp_header_extension_map_
39 RTC_GUARDED_BY(critical_section_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000040};
41
Tommi25eb47c2019-08-29 16:39:05 +020042std::unique_ptr<RtpHeaderParser> RtpHeaderParser::CreateForTest() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020043 return std::make_unique<RtpHeaderParserImpl>();
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000044}
45
danilchap7c9426c2016-04-14 03:05:31 -070046RtpHeaderParserImpl::RtpHeaderParserImpl() {}
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000047
pbos@webrtc.org62bafae2014-07-08 12:10:51 +000048bool RtpHeaderParser::IsRtcp(const uint8_t* packet, size_t length) {
49 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000050 return rtp_parser.RTCP();
51}
52
Sebastian Jansson1e427612019-03-05 14:25:03 +010053absl::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:51 +000063bool 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 13:14:30 +010067 *header = RTPHeader();
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000068
69 RtpHeaderExtensionMap map;
70 {
danilchap7c9426c2016-04-14 03:05:31 -070071 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 08:39:35 -080072 map = rtp_header_extension_map_;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000073 }
74
danilchapf6975f42015-12-28 10:18:46 -080075 const bool valid_rtpheader = rtp_parser.Parse(header, &map);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000076 if (!valid_rtpheader) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000077 return false;
78 }
79 return true;
80}
Sebastian Janssonfd201712018-11-12 16:44:16 +010081bool 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:51 +000085
86bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RTPExtensionType type,
87 uint8_t id) {
danilchap7c9426c2016-04-14 03:05:31 -070088 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 08:39:35 -080089 return rtp_header_extension_map_.RegisterByType(id, type);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000090}
91
Sebastian Janssonfd201712018-11-12 16:44:16 +010092bool 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:51 +000098bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RTPExtensionType type) {
danilchap7c9426c2016-04-14 03:05:31 -070099 rtc::CritScope cs(&critical_section_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000100 return rtp_header_extension_map_.Deregister(type) == 0;
101}
102} // namespace webrtc