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.
FastCopyPacket(const PacketRef & 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
RtpSender(base::TickClock * clock,const scoped_refptr<base::SingleThreadTaskRunner> & transport_task_runner,PacedSender * const transport)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
~RtpSender()41 RtpSender::~RtpSender() {}
42
Initialize(const CastTransportRtpConfig & config)43 bool RtpSender::Initialize(const CastTransportRtpConfig& config) {
44 config_.ssrc = config.ssrc;
45 config_.payload_type = config.rtp_payload_type;
46 packetizer_.reset(new RtpPacketizer(transport_, &storage_, config_));
47 return true;
48 }
49
SendFrame(const EncodedFrame & frame)50 void RtpSender::SendFrame(const EncodedFrame& frame) {
51 DCHECK(packetizer_);
52 packetizer_->SendFrameAsPackets(frame);
53 LOG_IF(DFATAL, storage_.GetNumberOfStoredFrames() > kMaxUnackedFrames)
54 << "Possible bug: Frames are not being actively released from storage.";
55 }
56
ResendPackets(const MissingFramesAndPacketsMap & missing_frames_and_packets,bool cancel_rtx_if_not_in_list,const DedupInfo & dedup_info)57 void RtpSender::ResendPackets(
58 const MissingFramesAndPacketsMap& missing_frames_and_packets,
59 bool cancel_rtx_if_not_in_list, const DedupInfo& dedup_info) {
60 // Iterate over all frames in the list.
61 for (MissingFramesAndPacketsMap::const_iterator it =
62 missing_frames_and_packets.begin();
63 it != missing_frames_and_packets.end();
64 ++it) {
65 SendPacketVector packets_to_resend;
66 uint8 frame_id = it->first;
67 // Set of packets that the receiver wants us to re-send.
68 // If empty, we need to re-send all packets for this frame.
69 const PacketIdSet& missing_packet_set = it->second;
70
71 bool resend_all = missing_packet_set.find(kRtcpCastAllPacketsLost) !=
72 missing_packet_set.end();
73 bool resend_last = missing_packet_set.find(kRtcpCastLastPacket) !=
74 missing_packet_set.end();
75
76 const SendPacketVector* stored_packets = storage_.GetFrame8(frame_id);
77 if (!stored_packets)
78 continue;
79
80 for (SendPacketVector::const_iterator it = stored_packets->begin();
81 it != stored_packets->end(); ++it) {
82 const PacketKey& packet_key = it->first;
83 const uint16 packet_id = packet_key.second.second;
84
85 // Should we resend the packet?
86 bool resend = resend_all;
87
88 // Should we resend it because it's in the missing_packet_set?
89 if (!resend &&
90 missing_packet_set.find(packet_id) != missing_packet_set.end()) {
91 resend = true;
92 }
93
94 // If we were asked to resend the last packet, check if it's the
95 // last packet.
96 if (!resend && resend_last && (it + 1) == stored_packets->end()) {
97 resend = true;
98 }
99
100 if (resend) {
101 // Resend packet to the network.
102 VLOG(3) << "Resend " << static_cast<int>(frame_id) << ":"
103 << packet_id;
104 // Set a unique incremental sequence number for every packet.
105 PacketRef packet_copy = FastCopyPacket(it->second);
106 UpdateSequenceNumber(&packet_copy->data);
107 packets_to_resend.push_back(std::make_pair(packet_key, packet_copy));
108 } else if (cancel_rtx_if_not_in_list) {
109 transport_->CancelSendingPacket(it->first);
110 }
111 }
112 transport_->ResendPackets(packets_to_resend, dedup_info);
113 }
114 }
115
CancelSendingFrames(const std::vector<uint32> & frame_ids)116 void RtpSender::CancelSendingFrames(const std::vector<uint32>& frame_ids) {
117 for (std::vector<uint32>::const_iterator i = frame_ids.begin();
118 i != frame_ids.end(); ++i) {
119 const SendPacketVector* stored_packets = storage_.GetFrame8(*i & 0xFF);
120 if (!stored_packets)
121 continue;
122 for (SendPacketVector::const_iterator j = stored_packets->begin();
123 j != stored_packets->end(); ++j) {
124 transport_->CancelSendingPacket(j->first);
125 }
126 storage_.ReleaseFrame(*i);
127 }
128 }
129
ResendFrameForKickstart(uint32 frame_id,base::TimeDelta dedupe_window)130 void RtpSender::ResendFrameForKickstart(uint32 frame_id,
131 base::TimeDelta dedupe_window) {
132 // Send the last packet of the encoded frame to kick start
133 // retransmission. This gives enough information to the receiver what
134 // packets and frames are missing.
135 MissingFramesAndPacketsMap missing_frames_and_packets;
136 PacketIdSet missing;
137 missing.insert(kRtcpCastLastPacket);
138 missing_frames_and_packets.insert(std::make_pair(frame_id, missing));
139
140 // Sending this extra packet is to kick-start the session. There is
141 // no need to optimize re-transmission for this case.
142 DedupInfo dedup_info;
143 dedup_info.resend_interval = dedupe_window;
144 ResendPackets(missing_frames_and_packets, false, dedup_info);
145 }
146
UpdateSequenceNumber(Packet * packet)147 void RtpSender::UpdateSequenceNumber(Packet* packet) {
148 // TODO(miu): This is an abstraction violation. This needs to be a part of
149 // the overall packet (de)serialization consolidation.
150 static const int kByteOffsetToSequenceNumber = 2;
151 base::BigEndianWriter big_endian_writer(
152 reinterpret_cast<char*>((&packet->front()) + kByteOffsetToSequenceNumber),
153 sizeof(uint16));
154 big_endian_writer.WriteU16(packetizer_->NextSequenceNumber());
155 }
156
GetLastByteSentForFrame(uint32 frame_id)157 int64 RtpSender::GetLastByteSentForFrame(uint32 frame_id) {
158 const SendPacketVector* stored_packets = storage_.GetFrame8(frame_id & 0xFF);
159 if (!stored_packets)
160 return 0;
161 PacketKey last_packet_key = stored_packets->rbegin()->first;
162 return transport_->GetLastByteSentForPacket(last_packet_key);
163 }
164
165 } // namespace cast
166 } // namespace media
167