stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "modules/rtp_rtcp/include/rtp_header_parser.h" |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 11 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 12 | #include <string.h> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #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 Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 17 | #include "rtc_base/thread_annotations.h" |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | class RtpHeaderParserImpl : public RtpHeaderParser { |
| 22 | public: |
| 23 | RtpHeaderParserImpl(); |
Danil Chapovalov | 2a5ce2b | 2018-02-07 09:38:31 +0100 | [diff] [blame] | 24 | ~RtpHeaderParserImpl() override = default; |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 25 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 26 | bool Parse(const uint8_t* packet, |
| 27 | size_t length, |
| 28 | RTPHeader* header) const override; |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 29 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 30 | bool RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id) override; |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 31 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 32 | bool DeregisterRtpHeaderExtension(RTPExtensionType type) override; |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 33 | |
| 34 | private: |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 35 | rtc::CriticalSection critical_section_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 36 | RtpHeaderExtensionMap rtp_header_extension_map_ |
| 37 | RTC_GUARDED_BY(critical_section_); |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | RtpHeaderParser* RtpHeaderParser::Create() { |
| 41 | return new RtpHeaderParserImpl; |
| 42 | } |
| 43 | |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 44 | RtpHeaderParserImpl::RtpHeaderParserImpl() {} |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 45 | |
pbos@webrtc.org | 62bafae | 2014-07-08 12:10:51 +0000 | [diff] [blame] | 46 | bool RtpHeaderParser::IsRtcp(const uint8_t* packet, size_t length) { |
| 47 | RtpUtility::RtpHeaderParser rtp_parser(packet, length); |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 48 | return rtp_parser.RTCP(); |
| 49 | } |
| 50 | |
pbos@webrtc.org | 62bafae | 2014-07-08 12:10:51 +0000 | [diff] [blame] | 51 | bool RtpHeaderParserImpl::Parse(const uint8_t* packet, |
| 52 | size_t length, |
| 53 | RTPHeader* header) const { |
| 54 | RtpUtility::RtpHeaderParser rtp_parser(packet, length); |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 55 | memset(header, 0, sizeof(*header)); |
| 56 | |
| 57 | RtpHeaderExtensionMap map; |
| 58 | { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 59 | rtc::CritScope cs(&critical_section_); |
danilchap | 1454669 | 2016-12-01 08:39:35 -0800 | [diff] [blame] | 60 | map = rtp_header_extension_map_; |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 61 | } |
| 62 | |
danilchap | f6975f4 | 2015-12-28 10:18:46 -0800 | [diff] [blame] | 63 | const bool valid_rtpheader = rtp_parser.Parse(header, &map); |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 64 | if (!valid_rtpheader) { |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RTPExtensionType type, |
| 71 | uint8_t id) { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 72 | rtc::CritScope cs(&critical_section_); |
danilchap | 1454669 | 2016-12-01 08:39:35 -0800 | [diff] [blame] | 73 | return rtp_header_extension_map_.RegisterByType(id, type); |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RTPExtensionType type) { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 77 | rtc::CritScope cs(&critical_section_); |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 78 | return rtp_header_extension_map_.Deregister(type) == 0; |
| 79 | } |
| 80 | } // namespace webrtc |