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