blob: 1a4ba42ff77caf938556979b3b6b6b52e9fc6588 [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
Tommi25eb47c2019-08-29 16:39:05 +020014#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
16#include "modules/rtp_rtcp/source/rtp_utility.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/critical_section.h"
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "rtc_base/thread_annotations.h"
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000019
20namespace webrtc {
21
22class RtpHeaderParserImpl : public RtpHeaderParser {
23 public:
24 RtpHeaderParserImpl();
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010025 ~RtpHeaderParserImpl() override = default;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000026
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000027 bool Parse(const uint8_t* packet,
28 size_t length,
29 RTPHeader* header) const override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000030
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000031 bool RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id) override;
Sebastian Janssonfd201712018-11-12 16:44:16 +010032 bool RegisterRtpHeaderExtension(RtpExtension extension) override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000033
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000034 bool DeregisterRtpHeaderExtension(RTPExtensionType type) override;
Sebastian Janssonfd201712018-11-12 16:44:16 +010035 bool DeregisterRtpHeaderExtension(RtpExtension extension) override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000036
37 private:
danilchap7c9426c2016-04-14 03:05:31 -070038 rtc::CriticalSection critical_section_;
danilchap56359be2017-09-07 07:53:45 -070039 RtpHeaderExtensionMap rtp_header_extension_map_
40 RTC_GUARDED_BY(critical_section_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000041};
42
Tommi25eb47c2019-08-29 16:39:05 +020043std::unique_ptr<RtpHeaderParser> RtpHeaderParser::CreateForTest() {
44 return absl::make_unique<RtpHeaderParserImpl>();
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000045}
46
danilchap7c9426c2016-04-14 03:05:31 -070047RtpHeaderParserImpl::RtpHeaderParserImpl() {}
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000048
pbos@webrtc.org62bafae2014-07-08 12:10:51 +000049bool RtpHeaderParser::IsRtcp(const uint8_t* packet, size_t length) {
50 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000051 return rtp_parser.RTCP();
52}
53
Sebastian Jansson1e427612019-03-05 14:25:03 +010054absl::optional<uint32_t> RtpHeaderParser::GetSsrc(const uint8_t* packet,
55 size_t length) {
56 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
57 RTPHeader header;
58 if (rtp_parser.Parse(&header, nullptr)) {
59 return header.ssrc;
60 }
61 return absl::nullopt;
62}
63
pbos@webrtc.org62bafae2014-07-08 12:10:51 +000064bool RtpHeaderParserImpl::Parse(const uint8_t* packet,
65 size_t length,
66 RTPHeader* header) const {
67 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
Niels Möllera533e002019-03-26 13:14:30 +010068 *header = RTPHeader();
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000069
70 RtpHeaderExtensionMap map;
71 {
danilchap7c9426c2016-04-14 03:05:31 -070072 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 08:39:35 -080073 map = rtp_header_extension_map_;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000074 }
75
danilchapf6975f42015-12-28 10:18:46 -080076 const bool valid_rtpheader = rtp_parser.Parse(header, &map);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000077 if (!valid_rtpheader) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000078 return false;
79 }
80 return true;
81}
Sebastian Janssonfd201712018-11-12 16:44:16 +010082bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RtpExtension extension) {
83 rtc::CritScope cs(&critical_section_);
84 return rtp_header_extension_map_.RegisterByUri(extension.id, extension.uri);
85}
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000086
87bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RTPExtensionType type,
88 uint8_t id) {
danilchap7c9426c2016-04-14 03:05:31 -070089 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 08:39:35 -080090 return rtp_header_extension_map_.RegisterByType(id, type);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000091}
92
Sebastian Janssonfd201712018-11-12 16:44:16 +010093bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RtpExtension extension) {
94 rtc::CritScope cs(&critical_section_);
95 return rtp_header_extension_map_.Deregister(
96 rtp_header_extension_map_.GetType(extension.id));
97}
98
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000099bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RTPExtensionType type) {
danilchap7c9426c2016-04-14 03:05:31 -0700100 rtc::CritScope cs(&critical_section_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000101 return rtp_header_extension_map_.Deregister(type) == 0;
102}
103} // namespace webrtc