blob: bc05033efafd5673d21cc469d88b443715d75fda [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;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000031
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000032 bool DeregisterRtpHeaderExtension(RTPExtensionType type) override;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000033
34 private:
danilchap7c9426c2016-04-14 03:05:31 -070035 rtc::CriticalSection critical_section_;
danilchap56359be2017-09-07 07:53:45 -070036 RtpHeaderExtensionMap rtp_header_extension_map_
37 RTC_GUARDED_BY(critical_section_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000038};
39
40RtpHeaderParser* RtpHeaderParser::Create() {
41 return new RtpHeaderParserImpl;
42}
43
danilchap7c9426c2016-04-14 03:05:31 -070044RtpHeaderParserImpl::RtpHeaderParserImpl() {}
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000045
pbos@webrtc.org62bafae2014-07-08 12:10:51 +000046bool RtpHeaderParser::IsRtcp(const uint8_t* packet, size_t length) {
47 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000048 return rtp_parser.RTCP();
49}
50
pbos@webrtc.org62bafae2014-07-08 12:10:51 +000051bool RtpHeaderParserImpl::Parse(const uint8_t* packet,
52 size_t length,
53 RTPHeader* header) const {
54 RtpUtility::RtpHeaderParser rtp_parser(packet, length);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000055 memset(header, 0, sizeof(*header));
56
57 RtpHeaderExtensionMap map;
58 {
danilchap7c9426c2016-04-14 03:05:31 -070059 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 08:39:35 -080060 map = rtp_header_extension_map_;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000061 }
62
danilchapf6975f42015-12-28 10:18:46 -080063 const bool valid_rtpheader = rtp_parser.Parse(header, &map);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000064 if (!valid_rtpheader) {
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000065 return false;
66 }
67 return true;
68}
69
70bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RTPExtensionType type,
71 uint8_t id) {
danilchap7c9426c2016-04-14 03:05:31 -070072 rtc::CritScope cs(&critical_section_);
danilchap14546692016-12-01 08:39:35 -080073 return rtp_header_extension_map_.RegisterByType(id, type);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000074}
75
76bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RTPExtensionType type) {
danilchap7c9426c2016-04-14 03:05:31 -070077 rtc::CritScope cs(&critical_section_);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +000078 return rtp_header_extension_map_.Deregister(type) == 0;
79}
80} // namespace webrtc