blob: 45686acb4c6f552b3c45176c37f1774ca244d96f [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"
Markus Handell60ed4592020-07-09 13:18:0616#include "rtc_base/synchronization/mutex.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:
Markus Handell60ed4592020-07-09 13:18:0637 mutable Mutex mutex_;
38 RtpHeaderExtensionMap rtp_header_extension_map_ RTC_GUARDED_BY(mutex_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:5139};
40
Tommi25eb47c2019-08-29 14:39:0541std::unique_ptr<RtpHeaderParser> RtpHeaderParser::CreateForTest() {
Mirko Bonadei317a1f02019-09-17 15:06:1842 return std::make_unique<RtpHeaderParserImpl>();
stefan@webrtc.orga5cb98c2013-05-29 12:12:5143}
44
danilchap7c9426c2016-04-14 10:05:3145RtpHeaderParserImpl::RtpHeaderParserImpl() {}
stefan@webrtc.orga5cb98c2013-05-29 12:12:5146
pbos@webrtc.org62bafae2014-07-08 12:10:5147bool RtpHeaderParser::IsRtcp(const uint8_t* packet, size_t length) {
48 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:5149 return rtp_parser.RTCP();
50}
51
Sebastian Jansson1e427612019-03-05 13:25:0352absl::optional<uint32_t> RtpHeaderParser::GetSsrc(const uint8_t* packet,
53 size_t length) {
54 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
55 RTPHeader header;
56 if (rtp_parser.Parse(&header, nullptr)) {
57 return header.ssrc;
58 }
59 return absl::nullopt;
60}
61
pbos@webrtc.org62bafae2014-07-08 12:10:5162bool RtpHeaderParserImpl::Parse(const uint8_t* packet,
63 size_t length,
64 RTPHeader* header) const {
65 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
Niels Möllera533e002019-03-26 12:14:3066 *header = RTPHeader();
stefan@webrtc.orga5cb98c2013-05-29 12:12:5167
68 RtpHeaderExtensionMap map;
69 {
Markus Handell60ed4592020-07-09 13:18:0670 MutexLock lock(&mutex_);
danilchap14546692016-12-01 16:39:3571 map = rtp_header_extension_map_;
stefan@webrtc.orga5cb98c2013-05-29 12:12:5172 }
73
danilchapf6975f42015-12-28 18:18:4674 const bool valid_rtpheader = rtp_parser.Parse(header, &map);
stefan@webrtc.orga5cb98c2013-05-29 12:12:5175 if (!valid_rtpheader) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:5176 return false;
77 }
78 return true;
79}
Sebastian Janssonfd201712018-11-12 15:44:1680bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RtpExtension extension) {
Markus Handell60ed4592020-07-09 13:18:0681 MutexLock lock(&mutex_);
Sebastian Janssonfd201712018-11-12 15:44:1682 return rtp_header_extension_map_.RegisterByUri(extension.id, extension.uri);
83}
stefan@webrtc.orga5cb98c2013-05-29 12:12:5184
85bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RTPExtensionType type,
86 uint8_t id) {
Markus Handell60ed4592020-07-09 13:18:0687 MutexLock lock(&mutex_);
danilchap14546692016-12-01 16:39:3588 return rtp_header_extension_map_.RegisterByType(id, type);
stefan@webrtc.orga5cb98c2013-05-29 12:12:5189}
90
Sebastian Janssonfd201712018-11-12 15:44:1691bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RtpExtension extension) {
Markus Handell60ed4592020-07-09 13:18:0692 MutexLock lock(&mutex_);
Sebastian Janssonfd201712018-11-12 15:44:1693 return rtp_header_extension_map_.Deregister(
94 rtp_header_extension_map_.GetType(extension.id));
95}
96
stefan@webrtc.orga5cb98c2013-05-29 12:12:5197bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RTPExtensionType type) {
Markus Handell60ed4592020-07-09 13:18:0698 MutexLock lock(&mutex_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:5199 return rtp_header_extension_map_.Deregister(type) == 0;
100}
101} // namespace webrtc