blob: 5d2971a0c61b337dea56df2a46e9e79318fb08ed [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
13#include "modules/rtp_rtcp/source/rtp_utility.h"
14#include "rtc_base/criticalsection.h"
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000015
16namespace webrtc {
17
18class RtpHeaderParserImpl : public RtpHeaderParser {
19 public:
20 RtpHeaderParserImpl();
21 virtual ~RtpHeaderParserImpl() {}
22
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000023 bool Parse(const uint8_t* packet,
24 size_t length,
25 RTPHeader* header) const override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000026
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000027 bool RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id) override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000028
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000029 bool DeregisterRtpHeaderExtension(RTPExtensionType type) override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000030
31 private:
danilchap7c9426c2016-04-14 03:05:31 -070032 rtc::CriticalSection critical_section_;
danilchap56359be2017-09-07 07:53:45 -070033 RtpHeaderExtensionMap rtp_header_extension_map_
34 RTC_GUARDED_BY(critical_section_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000035};
36
37RtpHeaderParser* RtpHeaderParser::Create() {
38 return new RtpHeaderParserImpl;
39}
40
danilchap7c9426c2016-04-14 03:05:31 -070041RtpHeaderParserImpl::RtpHeaderParserImpl() {}
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000042
pbos@webrtc.org62bafae2014-07-08 12:10:51 +000043bool RtpHeaderParser::IsRtcp(const uint8_t* packet, size_t length) {
44 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000045 return rtp_parser.RTCP();
46}
47
pbos@webrtc.org62bafae2014-07-08 12:10:51 +000048bool RtpHeaderParserImpl::Parse(const uint8_t* packet,
49 size_t length,
50 RTPHeader* header) const {
51 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000052 memset(header, 0, sizeof(*header));
53
54 RtpHeaderExtensionMap map;
55 {
danilchap7c9426c2016-04-14 03:05:31 -070056 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 08:39:35 -080057 map = rtp_header_extension_map_;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000058 }
59
danilchapf6975f42015-12-28 10:18:46 -080060 const bool valid_rtpheader = rtp_parser.Parse(header, &map);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000061 if (!valid_rtpheader) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000062 return false;
63 }
64 return true;
65}
66
67bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RTPExtensionType type,
68 uint8_t id) {
danilchap7c9426c2016-04-14 03:05:31 -070069 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 08:39:35 -080070 return rtp_header_extension_map_.RegisterByType(id, type);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000071}
72
73bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RTPExtensionType type) {
danilchap7c9426c2016-04-14 03:05:31 -070074 rtc::CritScope cs(&critical_section_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000075 return rtp_header_extension_map_.Deregister(type) == 0;
76}
77} // namespace webrtc