- add sources.
[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/logging.h"
8 #include "media/cast/rtp_common/rtp_defines.h"
9 #include "media/cast/rtp_receiver/receiver_stats.h"
10 #include "media/cast/rtp_receiver/rtp_parser/rtp_parser.h"
11 #include "net/base/big_endian.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                          RtpData* incoming_payload_callback) {
20   DCHECK(incoming_payload_callback) << "Invalid argument";
21   DCHECK(audio_config || video_config) << "Invalid argument";
22
23   // Configure parser.
24   RtpParserConfig config;
25   if (audio_config) {
26     config.ssrc = audio_config->incoming_ssrc;
27     config.payload_type = audio_config->rtp_payload_type;
28     config.audio_codec = audio_config->codec;
29     config.audio_channels = audio_config->channels;
30   } else {
31     config.ssrc = video_config->incoming_ssrc;
32     config.payload_type = video_config->rtp_payload_type;
33     config.video_codec = video_config->codec;
34   }
35   stats_.reset(new ReceiverStats(clock));
36   parser_.reset(new RtpParser(incoming_payload_callback, config));
37 }
38
39 RtpReceiver::~RtpReceiver() {}
40
41 // static
42 uint32 RtpReceiver::GetSsrcOfSender(const uint8* rtcp_buffer, size_t length) {
43   DCHECK_GE(length, kMinLengthOfRtp) << "Invalid RTP packet";
44   uint32 ssrc_of_sender;
45   net::BigEndianReader big_endian_reader(rtcp_buffer, length);
46   big_endian_reader.Skip(8);  // Skip header
47   big_endian_reader.ReadU32(&ssrc_of_sender);
48   return ssrc_of_sender;
49 }
50
51 bool RtpReceiver::ReceivedPacket(const uint8* packet, size_t length) {
52   RtpCastHeader rtp_header;
53   if (!parser_->ParsePacket(packet, length, &rtp_header)) return false;
54
55   stats_->UpdateStatistics(rtp_header);
56   return true;
57 }
58
59 void RtpReceiver::GetStatistics(uint8* fraction_lost,
60                                 uint32* cumulative_lost,
61                                 uint32* extended_high_sequence_number,
62                                 uint32* jitter) {
63   stats_->GetStatistics(fraction_lost,
64                         cumulative_lost,
65                         extended_high_sequence_number,
66                         jitter);
67 }
68
69 }  // namespace cast
70 }  // namespace media