Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / cast / rtp_receiver / rtp_receiver.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/cast/rtp_receiver/rtp_receiver.h"
6
7 #include "base/big_endian.h"
8 #include "base/logging.h"
9 #include "media/cast/rtp_receiver/receiver_stats.h"
10 #include "media/cast/rtp_receiver/rtp_parser/rtp_parser.h"
11 #include "media/cast/rtp_receiver/rtp_receiver_defines.h"
12
13 namespace media {
14 namespace cast {
15
16 RtpReceiver::RtpReceiver(base::TickClock* clock,
17                          const AudioReceiverConfig* audio_config,
18                          const VideoReceiverConfig* video_config) :
19     packet_parser_(audio_config ? audio_config->incoming_ssrc :
20                    (video_config ? video_config->incoming_ssrc : 0),
21                    audio_config ? audio_config->rtp_payload_type :
22                    (video_config ? video_config->rtp_payload_type : 0)),
23     stats_(clock) {
24   DCHECK(audio_config || video_config);
25 }
26
27 RtpReceiver::~RtpReceiver() {}
28
29 // static
30 uint32 RtpReceiver::GetSsrcOfSender(const uint8* rtcp_buffer, size_t length) {
31   DCHECK_GE(length, kMinLengthOfRtp) << "Invalid RTP packet";
32   uint32 ssrc_of_sender;
33   base::BigEndianReader big_endian_reader(
34       reinterpret_cast<const char*>(rtcp_buffer), length);
35   big_endian_reader.Skip(8);  // Skip header
36   big_endian_reader.ReadU32(&ssrc_of_sender);
37   return ssrc_of_sender;
38 }
39
40 bool RtpReceiver::ReceivedPacket(const uint8* packet, size_t length) {
41   RtpCastHeader rtp_header;
42   const uint8* payload_data;
43   size_t payload_size;
44   if (!packet_parser_.ParsePacket(
45           packet, length, &rtp_header, &payload_data, &payload_size)) {
46     return false;
47   }
48
49   OnReceivedPayloadData(payload_data, payload_size, rtp_header);
50   stats_.UpdateStatistics(rtp_header);
51   return true;
52 }
53
54 }  // namespace cast
55 }  // namespace media