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.
5 #include "media/cast/net/rtp/rtp_sender.h"
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"
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())
24 return make_scoped_refptr(new base::RefCountedData
<Packet
>(packet
->data
));
30 base::TickClock
* clock
,
31 const scoped_refptr
<base::SingleThreadTaskRunner
>& transport_task_runner
,
32 PacedSender
* const transport
)
34 transport_(transport
),
35 transport_task_runner_(transport_task_runner
),
37 // Randomly set sequence number start value.
38 config_
.sequence_number
= base::RandInt(0, 65535);
41 RtpSender::~RtpSender() {}
43 bool RtpSender::Initialize(const CastTransportRtpConfig
& config
) {
44 storage_
.reset(new PacketStorage(config
.stored_frames
));
45 if (!storage_
->IsValid()) {
48 config_
.ssrc
= config
.ssrc
;
49 config_
.payload_type
= config
.rtp_payload_type
;
50 packetizer_
.reset(new RtpPacketizer(transport_
, storage_
.get(), config_
));
54 void RtpSender::SendFrame(const EncodedFrame
& frame
) {
56 packetizer_
->SendFrameAsPackets(frame
);
59 void RtpSender::ResendPackets(
60 const MissingFramesAndPacketsMap
& missing_frames_and_packets
,
61 bool cancel_rtx_if_not_in_list
,
62 base::TimeDelta dedupe_window
) {
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();
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
;
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();
80 const SendPacketVector
* stored_packets
= storage_
->GetFrame8(frame_id
);
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
;
89 // Should we resend the packet?
90 bool resend
= resend_all
;
92 // Should we resend it because it's in the missing_packet_set?
94 missing_packet_set
.find(packet_id
) != missing_packet_set
.end()) {
98 // If we were asked to resend the last packet, check if it's the
100 if (!resend
&& resend_last
&& (it
+ 1) == stored_packets
->end()) {
105 // Resend packet to the network.
106 VLOG(3) << "Resend " << static_cast<int>(frame_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
);
116 transport_
->ResendPackets(packets_to_resend
, dedupe_window
);
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
),
127 big_endian_writer
.WriteU16(packetizer_
->NextSequenceNumber());