blob: 6481a403a0436a3d3e46f45cc77b2e2cb14a12b1 [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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "modules/rtp_rtcp/include/rtp_header_parser.h"
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000011
Yves Gerey988cc082018-10-23 12:03:01 +020012#include <string.h>
13
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"
16#include "rtc_base/criticalsection.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
42RtpHeaderParser* RtpHeaderParser::Create() {
43 return new RtpHeaderParserImpl;
44}
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
pbos@webrtc.org62bafae2014-07-08 12:10:51 +000053bool RtpHeaderParserImpl::Parse(const uint8_t* packet,
54 size_t length,
55 RTPHeader* header) const {
56 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000057 memset(header, 0, sizeof(*header));
58
59 RtpHeaderExtensionMap map;
60 {
danilchap7c9426c2016-04-14 03:05:31 -070061 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 08:39:35 -080062 map = rtp_header_extension_map_;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000063 }
64
danilchapf6975f42015-12-28 10:18:46 -080065 const bool valid_rtpheader = rtp_parser.Parse(header, &map);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000066 if (!valid_rtpheader) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000067 return false;
68 }
69 return true;
70}
Sebastian Janssonfd201712018-11-12 16:44:16 +010071bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RtpExtension extension) {
72 rtc::CritScope cs(&critical_section_);
73 return rtp_header_extension_map_.RegisterByUri(extension.id, extension.uri);
74}
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000075
76bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RTPExtensionType type,
77 uint8_t id) {
danilchap7c9426c2016-04-14 03:05:31 -070078 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 08:39:35 -080079 return rtp_header_extension_map_.RegisterByType(id, type);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000080}
81
Sebastian Janssonfd201712018-11-12 16:44:16 +010082bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RtpExtension extension) {
83 rtc::CritScope cs(&critical_section_);
84 return rtp_header_extension_map_.Deregister(
85 rtp_header_extension_map_.GetType(extension.id));
86}
87
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000088bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RTPExtensionType type) {
danilchap7c9426c2016-04-14 03:05:31 -070089 rtc::CritScope cs(&critical_section_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000090 return rtp_header_extension_map_.Deregister(type) == 0;
91}
92} // namespace webrtc