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