Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / media / cast / net / rtp / rtp_sender.cc
1 // Copyright 2014 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/net/rtp/rtp_sender.h"
6
7 #include "base/big_endian.h"
8 #include "base/logging.h"
9 #include "base/rand_util.h"
10 #include "media/cast/net/cast_transport_defines.h"
11 #include "media/cast/net/pacing/paced_sender.h"
12
13 namespace media {
14 namespace cast {
15
16 namespace {
17
18 // If there is only one referecne to the packet then copy the
19 // reference and return.
20 // Otherwise return a deep copy of the packet.
21 PacketRef FastCopyPacket(const PacketRef& packet) {
22   if (packet->HasOneRef())
23     return packet;
24   return make_scoped_refptr(new base::RefCountedData<Packet>(packet->data));
25 }
26
27 }  // namespace
28
29 RtpSender::RtpSender(
30     base::TickClock* clock,
31     const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner,
32     PacedSender* const transport)
33     : clock_(clock),
34       transport_(transport),
35       transport_task_runner_(transport_task_runner),
36       weak_factory_(this) {
37   // Randomly set sequence number start value.
38   config_.sequence_number = base::RandInt(0, 65535);
39 }
40
41 RtpSender::~RtpSender() {}
42
43 bool RtpSender::Initialize(const CastTransportRtpConfig& config) {
44   storage_.reset(new PacketStorage(config.stored_frames));
45   if (!storage_->IsValid()) {
46     return false;
47   }
48   config_.ssrc = config.ssrc;
49   config_.payload_type = config.rtp_payload_type;
50   packetizer_.reset(new RtpPacketizer(transport_, storage_.get(), config_));
51   return true;
52 }
53
54 void RtpSender::SendFrame(const EncodedFrame& frame) {
55   DCHECK(packetizer_);
56   packetizer_->SendFrameAsPackets(frame);
57 }
58
59 void RtpSender::ResendPackets(
60     const MissingFramesAndPacketsMap& missing_frames_and_packets,
61     bool cancel_rtx_if_not_in_list,
62     base::TimeDelta dedupe_window) {
63   DCHECK(storage_);
64   // Iterate over all frames in the list.
65   for (MissingFramesAndPacketsMap::const_iterator it =
66            missing_frames_and_packets.begin();
67        it != missing_frames_and_packets.end();
68        ++it) {
69     SendPacketVector packets_to_resend;
70     uint8 frame_id = it->first;
71     // Set of packets that the receiver wants us to re-send.
72     // If empty, we need to re-send all packets for this frame.
73     const PacketIdSet& missing_packet_set = it->second;
74
75     bool resend_all = missing_packet_set.find(kRtcpCastAllPacketsLost) !=
76         missing_packet_set.end();
77     bool resend_last = missing_packet_set.find(kRtcpCastLastPacket) !=
78         missing_packet_set.end();
79
80     const SendPacketVector* stored_packets = storage_->GetFrame8(frame_id);
81     if (!stored_packets)
82       continue;
83
84     for (SendPacketVector::const_iterator it = stored_packets->begin();
85          it != stored_packets->end(); ++it) {
86       const PacketKey& packet_key = it->first;
87       const uint16 packet_id = packet_key.second.second;
88
89       // Should we resend the packet?
90       bool resend = resend_all;
91
92       // Should we resend it because it's in the missing_packet_set?
93       if (!resend &&
94           missing_packet_set.find(packet_id) != missing_packet_set.end()) {
95         resend = true;
96       }
97
98       // If we were asked to resend the last packet, check if it's the
99       // last packet.
100       if (!resend && resend_last && (it + 1) == stored_packets->end()) {
101         resend = true;
102       }
103
104       if (resend) {
105         // Resend packet to the network.
106         VLOG(3) << "Resend " << static_cast<int>(frame_id) << ":"
107                 << packet_id;
108         // Set a unique incremental sequence number for every packet.
109         PacketRef packet_copy = FastCopyPacket(it->second);
110         UpdateSequenceNumber(&packet_copy->data);
111         packets_to_resend.push_back(std::make_pair(packet_key, packet_copy));
112       } else if (cancel_rtx_if_not_in_list) {
113         transport_->CancelSendingPacket(it->first);
114       }
115     }
116     transport_->ResendPackets(packets_to_resend, dedupe_window);
117   }
118 }
119
120 void RtpSender::UpdateSequenceNumber(Packet* packet) {
121   // TODO(miu): This is an abstraction violation.  This needs to be a part of
122   // the overall packet (de)serialization consolidation.
123   static const int kByteOffsetToSequenceNumber = 2;
124   base::BigEndianWriter big_endian_writer(
125       reinterpret_cast<char*>((&packet->front()) + kByteOffsetToSequenceNumber),
126       sizeof(uint16));
127   big_endian_writer.WriteU16(packetizer_->NextSequenceNumber());
128 }
129
130 }  //  namespace cast
131 }  // namespace media