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.
5 #ifndef MEDIA_CAST_TRANSPORT_PACING_PACED_SENDER_H_
6 #define MEDIA_CAST_TRANSPORT_PACING_PACED_SENDER_H_
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "base/time/default_tick_clock.h"
17 #include "base/time/tick_clock.h"
18 #include "base/time/time.h"
19 #include "media/cast/transport/cast_transport_config.h"
20 #include "media/cast/transport/transport/udp_transport.h"
29 // Use std::pair for free comparison operators.
30 // { capture_time, ssrc, packet_id }
31 // The PacketKey is designed to meet two criteria:
32 // 1. When we re-send the same packet again, we can use the packet key
33 // to identify it so that we can de-duplicate packets in the queue.
34 // 2. The sort order of the PacketKey determines the order that packets
35 // are sent out. Using the capture_time as the first member basically
36 // means that older packets are sent first.
37 typedef std::pair<base::TimeTicks, std::pair<uint32, uint16> > PacketKey;
38 typedef std::vector<std::pair<PacketKey, PacketRef> > SendPacketVector;
40 // We have this pure virtual class to enable mocking.
41 class PacedPacketSender {
43 virtual bool SendPackets(const SendPacketVector& packets) = 0;
44 virtual bool ResendPackets(const SendPacketVector& packets,
45 base::TimeDelta dedupe_window) = 0;
46 virtual bool SendRtcpPacket(uint32 ssrc, PacketRef packet) = 0;
47 virtual void CancelSendingPacket(const PacketKey& packet_key) = 0;
49 virtual ~PacedPacketSender() {}
51 static PacketKey MakePacketKey(const base::TimeTicks& ticks,
56 class PacedSender : public PacedPacketSender,
57 public base::NonThreadSafe,
58 public base::SupportsWeakPtr<PacedSender> {
60 // The |external_transport| should only be used by the Cast receiver and for
63 base::TickClock* clock,
65 PacketSender* external_transport,
66 const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner);
68 virtual ~PacedSender();
70 // These must be called before non-RTCP packets are sent.
71 void RegisterAudioSsrc(uint32 audio_ssrc);
72 void RegisterVideoSsrc(uint32 video_ssrc);
74 // PacedPacketSender implementation.
75 virtual bool SendPackets(const SendPacketVector& packets) OVERRIDE;
76 virtual bool ResendPackets(const SendPacketVector& packets,
77 base::TimeDelta dedupe_window) OVERRIDE;
78 virtual bool SendRtcpPacket(uint32 ssrc, PacketRef packet) OVERRIDE;
79 virtual void CancelSendingPacket(const PacketKey& packet_key) OVERRIDE;
82 // Actually sends the packets to the transport.
83 void SendStoredPackets();
84 void LogPacketEvent(const Packet& packet, CastLoggingEvent event);
92 // In an unblocked state, we can send more packets.
93 // We have to check the current time against |burst_end_| to see if we are
94 // appending to the current burst or if we can start a new one.
96 // In this state, we are waiting for a callback from the udp transport.
97 // This happens when the OS-level buffer is full. Once we receive the
98 // callback, we go to State_Unblocked and see if we can write more packets
99 // to the current burst. (Or the next burst if enough time has passed.)
100 State_TransportBlocked,
101 // Once we've written enough packets for a time slice, we go into this
102 // state and PostDelayTask a call to ourselves to wake up when we can
110 // Returns the next packet to send. RTCP packets have highest priority,
111 // resend packets have second highest priority and then comes everything
113 PacketRef GetNextPacket(PacketType* packet_type,
114 PacketKey* packet_key);
116 base::TickClock* const clock_; // Not owned by this class.
117 LoggingImpl* const logging_; // Not owned by this class.
118 PacketSender* transport_; // Not owned by this class.
119 scoped_refptr<base::SingleThreadTaskRunner> transport_task_runner_;
122 std::map<PacketKey, std::pair<PacketType, PacketRef> > packet_list_;
123 std::map<PacketKey, base::TimeTicks> sent_time_;
124 std::map<PacketKey, base::TimeTicks> sent_time_buffer_;
126 // Maximum burst size for the next three bursts.
127 size_t max_burst_size_;
128 size_t next_max_burst_size_;
129 size_t next_next_max_burst_size_;
130 // Number of packets already sent in the current burst.
131 size_t current_burst_size_;
132 // This is when the current burst ends.
133 base::TimeTicks burst_end_;
137 // NOTE: Weak pointers must be invalidated before all other member variables.
138 base::WeakPtrFactory<PacedSender> weak_factory_;
140 DISALLOW_COPY_AND_ASSIGN(PacedSender);
143 } // namespace transport
147 #endif // MEDIA_CAST_TRANSPORT_PACING_PACED_SENDER_H_