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 */
10#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
11
12#include "webrtc/base/scoped_ptr.h"
13#include "webrtc/modules/rtp_rtcp/source/rtp_header_extension.h"
14#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
15#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
16
17namespace webrtc {
18
19class RtpHeaderParserImpl : public RtpHeaderParser {
20 public:
21  RtpHeaderParserImpl();
22  virtual ~RtpHeaderParserImpl() {}
23
24  bool Parse(const uint8_t* packet,
25             size_t length,
26             RTPHeader* header) const override;
27
28  bool RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id) override;
29
30  bool DeregisterRtpHeaderExtension(RTPExtensionType type) override;
31
32 private:
33  rtc::scoped_ptr<CriticalSectionWrapper> critical_section_;
34  RtpHeaderExtensionMap rtp_header_extension_map_ GUARDED_BY(critical_section_);
35};
36
37RtpHeaderParser* RtpHeaderParser::Create() {
38  return new RtpHeaderParserImpl;
39}
40
41RtpHeaderParserImpl::RtpHeaderParserImpl()
42    : critical_section_(CriticalSectionWrapper::CreateCriticalSection()) {}
43
44bool RtpHeaderParser::IsRtcp(const uint8_t* packet, size_t length) {
45  RtpUtility::RtpHeaderParser rtp_parser(packet, length);
46  return rtp_parser.RTCP();
47}
48
49bool RtpHeaderParserImpl::Parse(const uint8_t* packet,
50                                size_t length,
51                                RTPHeader* header) const {
52  RtpUtility::RtpHeaderParser rtp_parser(packet, length);
53  memset(header, 0, sizeof(*header));
54
55  RtpHeaderExtensionMap map;
56  {
57    CriticalSectionScoped cs(critical_section_.get());
58    rtp_header_extension_map_.GetCopy(&map);
59  }
60
61  const bool valid_rtpheader = rtp_parser.Parse(header, &map);
62  if (!valid_rtpheader) {
63    return false;
64  }
65  return true;
66}
67
68bool RtpHeaderParserImpl::RegisterRtpHeaderExtension(RTPExtensionType type,
69                                                     uint8_t id) {
70  CriticalSectionScoped cs(critical_section_.get());
71  return rtp_header_extension_map_.Register(type, id) == 0;
72}
73
74bool RtpHeaderParserImpl::DeregisterRtpHeaderExtension(RTPExtensionType type) {
75  CriticalSectionScoped cs(critical_section_.get());
76  return rtp_header_extension_map_.Deregister(type) == 0;
77}
78}  // namespace webrtc
79