| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_CAST_NET_PACING_PACED_SENDER_H_ | 5 #ifndef MEDIA_CAST_NET_PACING_PACED_SENDER_H_ |
| 6 #define MEDIA_CAST_NET_PACING_PACED_SENDER_H_ | 6 #define MEDIA_CAST_NET_PACING_PACED_SENDER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <tuple> | 9 #include <tuple> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 15 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 16 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
| 17 #include "base/time/default_tick_clock.h" | 16 #include "base/time/default_tick_clock.h" |
| 18 #include "base/time/tick_clock.h" | 17 #include "base/time/tick_clock.h" |
| 19 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 20 #include "media/cast/logging/logging_defines.h" | 19 #include "media/cast/logging/logging_defines.h" |
| 21 #include "media/cast/net/cast_transport_config.h" | 20 #include "media/cast/net/cast_transport_config.h" |
| 22 | 21 |
| 23 namespace media { | 22 namespace media { |
| 24 namespace cast { | 23 namespace cast { |
| 25 | 24 |
| 26 // Meant to use as defaults for pacer construction. | 25 // Meant to use as defaults for pacer construction. |
| 27 static const size_t kTargetBurstSize = 10; | 26 static const size_t kTargetBurstSize = 10; |
| 28 static const size_t kMaxBurstSize = 20; | 27 static const size_t kMaxBurstSize = 20; |
| 29 | 28 |
| 30 // The PacketKey is designed to meet two criteria: | 29 // The PacketKey is designed to meet two criteria: |
| 31 // 1. When we re-send the same packet again, we can use the packet key | 30 // 1. When we re-send the same packet again, we can use the packet key |
| 32 // to identify it so that we can de-duplicate packets in the queue. | 31 // to identify it so that we can de-duplicate packets in the queue. |
| 33 // 2. The sort order of the PacketKey determines the order that packets | 32 // 2. The sort order of the PacketKey determines the order that packets |
| 34 // are sent out. | 33 // are sent out. |
| 35 // 3. The PacketKey is unique for each RTP (frame) packet. | 34 // 3. The PacketKey is unique for each RTP (frame) packet. |
| 36 struct PacketKey { | 35 struct PacketKey { |
| 37 enum PacketType { RTCP = 0, RTP = 1 }; | 36 enum PacketType { RTCP = 0, RTP = 1 }; |
| 38 | 37 |
| 39 PacketType packet_type; | 38 PacketType packet_type; |
| 40 uint32 frame_id; | 39 uint32_t frame_id; |
| 41 uint32 ssrc; | 40 uint32_t ssrc; |
| 42 uint16 packet_id; | 41 uint16_t packet_id; |
| 43 | 42 |
| 44 bool operator<(const PacketKey& key) const { | 43 bool operator<(const PacketKey& key) const { |
| 45 return std::tie(packet_type, frame_id, ssrc, packet_id) < | 44 return std::tie(packet_type, frame_id, ssrc, packet_id) < |
| 46 std::tie(key.packet_type, key.frame_id, key.ssrc, key.packet_id); | 45 std::tie(key.packet_type, key.frame_id, key.ssrc, key.packet_id); |
| 47 } | 46 } |
| 48 }; | 47 }; |
| 49 | 48 |
| 50 typedef std::vector<std::pair<PacketKey, PacketRef> > SendPacketVector; | 49 typedef std::vector<std::pair<PacketKey, PacketRef> > SendPacketVector; |
| 51 | 50 |
| 52 // Information used to deduplicate retransmission packets. | 51 // Information used to deduplicate retransmission packets. |
| 53 // There are two criteria for deduplication. | 52 // There are two criteria for deduplication. |
| 54 // | 53 // |
| 55 // 1. Using another muxed stream. | 54 // 1. Using another muxed stream. |
| 56 // Suppose there are multiple streams muxed and sent via the same | 55 // Suppose there are multiple streams muxed and sent via the same |
| 57 // socket. When there is a retransmission request for packet X, we | 56 // socket. When there is a retransmission request for packet X, we |
| 58 // will reject the retransmission if there is a packet sent from | 57 // will reject the retransmission if there is a packet sent from |
| 59 // another stream just before X but not acked. Typically audio stream | 58 // another stream just before X but not acked. Typically audio stream |
| 60 // is used for this purpose. |last_byte_acked_for_audio| provides this | 59 // is used for this purpose. |last_byte_acked_for_audio| provides this |
| 61 // information. | 60 // information. |
| 62 // | 61 // |
| 63 // 2. Using a time interval. | 62 // 2. Using a time interval. |
| 64 // Time between sending the same packet must be greater than | 63 // Time between sending the same packet must be greater than |
| 65 // |resend_interval|. | 64 // |resend_interval|. |
| 66 struct DedupInfo { | 65 struct DedupInfo { |
| 67 DedupInfo(); | 66 DedupInfo(); |
| 68 base::TimeDelta resend_interval; | 67 base::TimeDelta resend_interval; |
| 69 int64 last_byte_acked_for_audio; | 68 int64_t last_byte_acked_for_audio; |
| 70 }; | 69 }; |
| 71 | 70 |
| 72 // We have this pure virtual class to enable mocking. | 71 // We have this pure virtual class to enable mocking. |
| 73 class PacedPacketSender { | 72 class PacedPacketSender { |
| 74 public: | 73 public: |
| 75 virtual bool SendPackets(const SendPacketVector& packets) = 0; | 74 virtual bool SendPackets(const SendPacketVector& packets) = 0; |
| 76 virtual bool ResendPackets(const SendPacketVector& packets, | 75 virtual bool ResendPackets(const SendPacketVector& packets, |
| 77 const DedupInfo& dedup_info) = 0; | 76 const DedupInfo& dedup_info) = 0; |
| 78 virtual bool SendRtcpPacket(uint32 ssrc, PacketRef packet) = 0; | 77 virtual bool SendRtcpPacket(uint32_t ssrc, PacketRef packet) = 0; |
| 79 virtual void CancelSendingPacket(const PacketKey& packet_key) = 0; | 78 virtual void CancelSendingPacket(const PacketKey& packet_key) = 0; |
| 80 | 79 |
| 81 virtual ~PacedPacketSender() {} | 80 virtual ~PacedPacketSender() {} |
| 82 | 81 |
| 83 static PacketKey MakePacketKey(PacketKey::PacketType, | 82 static PacketKey MakePacketKey(PacketKey::PacketType, |
| 84 uint32 frame_id, | 83 uint32_t frame_id, |
| 85 uint32 ssrc, | 84 uint32_t ssrc, |
| 86 uint16 packet_id); | 85 uint16_t packet_id); |
| 87 }; | 86 }; |
| 88 | 87 |
| 89 class PacedSender : public PacedPacketSender, | 88 class PacedSender : public PacedPacketSender, |
| 90 public base::NonThreadSafe, | 89 public base::NonThreadSafe, |
| 91 public base::SupportsWeakPtr<PacedSender> { | 90 public base::SupportsWeakPtr<PacedSender> { |
| 92 public: | 91 public: |
| 93 // |recent_packet_events| is an externally-owned vector where PacedSender will | 92 // |recent_packet_events| is an externally-owned vector where PacedSender will |
| 94 // add PacketEvents related to sending, retransmission, and rejection. The | 93 // add PacketEvents related to sending, retransmission, and rejection. The |
| 95 // |external_transport| should only be used by the Cast receiver and for | 94 // |external_transport| should only be used by the Cast receiver and for |
| 96 // testing. | 95 // testing. |
| 97 PacedSender( | 96 PacedSender( |
| 98 size_t target_burst_size, // Should normally be kTargetBurstSize. | 97 size_t target_burst_size, // Should normally be kTargetBurstSize. |
| 99 size_t max_burst_size, // Should normally be kMaxBurstSize. | 98 size_t max_burst_size, // Should normally be kMaxBurstSize. |
| 100 base::TickClock* clock, | 99 base::TickClock* clock, |
| 101 std::vector<PacketEvent>* recent_packet_events, | 100 std::vector<PacketEvent>* recent_packet_events, |
| 102 PacketSender* external_transport, | 101 PacketSender* external_transport, |
| 103 const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner); | 102 const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner); |
| 104 | 103 |
| 105 ~PacedSender() final; | 104 ~PacedSender() final; |
| 106 | 105 |
| 107 // These must be called before non-RTCP packets are sent. | 106 // These must be called before non-RTCP packets are sent. |
| 108 void RegisterAudioSsrc(uint32 audio_ssrc); | 107 void RegisterAudioSsrc(uint32_t audio_ssrc); |
| 109 void RegisterVideoSsrc(uint32 video_ssrc); | 108 void RegisterVideoSsrc(uint32_t video_ssrc); |
| 110 | 109 |
| 111 // Register SSRC that has a higher priority for sending. Multiple SSRCs can | 110 // Register SSRC that has a higher priority for sending. Multiple SSRCs can |
| 112 // be registered. | 111 // be registered. |
| 113 // Note that it is not expected to register many SSRCs with this method. | 112 // Note that it is not expected to register many SSRCs with this method. |
| 114 // Because IsHigherPriority() is determined in linear time. | 113 // Because IsHigherPriority() is determined in linear time. |
| 115 void RegisterPrioritySsrc(uint32 ssrc); | 114 void RegisterPrioritySsrc(uint32_t ssrc); |
| 116 | 115 |
| 117 // Returns the total number of bytes sent to the socket when the specified | 116 // Returns the total number of bytes sent to the socket when the specified |
| 118 // packet was just sent. | 117 // packet was just sent. |
| 119 // Returns 0 if the packet cannot be found or not yet sent. | 118 // Returns 0 if the packet cannot be found or not yet sent. |
| 120 int64 GetLastByteSentForPacket(const PacketKey& packet_key); | 119 int64_t GetLastByteSentForPacket(const PacketKey& packet_key); |
| 121 | 120 |
| 122 // Returns the total number of bytes sent to the socket when the last payload | 121 // Returns the total number of bytes sent to the socket when the last payload |
| 123 // identified by SSRC is just sent. | 122 // identified by SSRC is just sent. |
| 124 int64 GetLastByteSentForSsrc(uint32 ssrc); | 123 int64_t GetLastByteSentForSsrc(uint32_t ssrc); |
| 125 | 124 |
| 126 // PacedPacketSender implementation. | 125 // PacedPacketSender implementation. |
| 127 bool SendPackets(const SendPacketVector& packets) final; | 126 bool SendPackets(const SendPacketVector& packets) final; |
| 128 bool ResendPackets(const SendPacketVector& packets, | 127 bool ResendPackets(const SendPacketVector& packets, |
| 129 const DedupInfo& dedup_info) final; | 128 const DedupInfo& dedup_info) final; |
| 130 bool SendRtcpPacket(uint32 ssrc, PacketRef packet) final; | 129 bool SendRtcpPacket(uint32_t ssrc, PacketRef packet) final; |
| 131 void CancelSendingPacket(const PacketKey& packet_key) final; | 130 void CancelSendingPacket(const PacketKey& packet_key) final; |
| 132 | 131 |
| 133 private: | 132 private: |
| 134 // Actually sends the packets to the transport. | 133 // Actually sends the packets to the transport. |
| 135 void SendStoredPackets(); | 134 void SendStoredPackets(); |
| 136 | 135 |
| 137 // Convenience method for building a PacketEvent and storing it in the | 136 // Convenience method for building a PacketEvent and storing it in the |
| 138 // externally-owned container of |recent_packet_events_|. | 137 // externally-owned container of |recent_packet_events_|. |
| 139 void LogPacketEvent(const Packet& packet, CastLoggingEvent event); | 138 void LogPacketEvent(const Packet& packet, CastLoggingEvent event); |
| 140 | 139 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 177 |
| 179 // Returns true if the packet should have a higher priority. | 178 // Returns true if the packet should have a higher priority. |
| 180 bool IsHighPriority(const PacketKey& packet_key) const; | 179 bool IsHighPriority(const PacketKey& packet_key) const; |
| 181 | 180 |
| 182 // These are externally-owned objects injected via the constructor. | 181 // These are externally-owned objects injected via the constructor. |
| 183 base::TickClock* const clock_; | 182 base::TickClock* const clock_; |
| 184 std::vector<PacketEvent>* const recent_packet_events_; | 183 std::vector<PacketEvent>* const recent_packet_events_; |
| 185 PacketSender* const transport_; | 184 PacketSender* const transport_; |
| 186 | 185 |
| 187 scoped_refptr<base::SingleThreadTaskRunner> transport_task_runner_; | 186 scoped_refptr<base::SingleThreadTaskRunner> transport_task_runner_; |
| 188 uint32 audio_ssrc_; | 187 uint32_t audio_ssrc_; |
| 189 uint32 video_ssrc_; | 188 uint32_t video_ssrc_; |
| 190 | 189 |
| 191 // Set of SSRCs that have higher priority. This is a vector instead of a | 190 // Set of SSRCs that have higher priority. This is a vector instead of a |
| 192 // set because there's only very few in it (most likely 1). | 191 // set because there's only very few in it (most likely 1). |
| 193 std::vector<uint32> priority_ssrcs_; | 192 std::vector<uint32_t> priority_ssrcs_; |
| 194 typedef std::map<PacketKey, std::pair<PacketType, PacketRef> > PacketList; | 193 typedef std::map<PacketKey, std::pair<PacketType, PacketRef> > PacketList; |
| 195 PacketList packet_list_; | 194 PacketList packet_list_; |
| 196 PacketList priority_packet_list_; | 195 PacketList priority_packet_list_; |
| 197 | 196 |
| 198 struct PacketSendRecord { | 197 struct PacketSendRecord { |
| 199 PacketSendRecord(); | 198 PacketSendRecord(); |
| 200 base::TimeTicks time; // Time when the packet was sent. | 199 base::TimeTicks time; // Time when the packet was sent. |
| 201 int64 last_byte_sent; // Number of bytes sent to network just after this | 200 int64_t last_byte_sent; // Number of bytes sent to network just after this |
| 202 // packet was sent. | 201 // packet was sent. |
| 203 int64 last_byte_sent_for_audio; // Number of bytes sent to network from | 202 int64_t last_byte_sent_for_audio; // Number of bytes sent to network from |
| 204 // audio stream just before this packet. | 203 // audio stream just before this packet. |
| 205 }; | 204 }; |
| 206 typedef std::map<PacketKey, PacketSendRecord> PacketSendHistory; | 205 typedef std::map<PacketKey, PacketSendRecord> PacketSendHistory; |
| 207 PacketSendHistory send_history_; | 206 PacketSendHistory send_history_; |
| 208 PacketSendHistory send_history_buffer_; | 207 PacketSendHistory send_history_buffer_; |
| 209 // Records the last byte sent for payload with a specific SSRC. | 208 // Records the last byte sent for payload with a specific SSRC. |
| 210 std::map<uint32, int64> last_byte_sent_; | 209 std::map<uint32_t, int64_t> last_byte_sent_; |
| 211 | 210 |
| 212 size_t target_burst_size_; | 211 size_t target_burst_size_; |
| 213 size_t max_burst_size_; | 212 size_t max_burst_size_; |
| 214 | 213 |
| 215 // Maximum burst size for the next three bursts. | 214 // Maximum burst size for the next three bursts. |
| 216 size_t current_max_burst_size_; | 215 size_t current_max_burst_size_; |
| 217 size_t next_max_burst_size_; | 216 size_t next_max_burst_size_; |
| 218 size_t next_next_max_burst_size_; | 217 size_t next_next_max_burst_size_; |
| 219 // Number of packets already sent in the current burst. | 218 // Number of packets already sent in the current burst. |
| 220 size_t current_burst_size_; | 219 size_t current_burst_size_; |
| 221 // This is when the current burst ends. | 220 // This is when the current burst ends. |
| 222 base::TimeTicks burst_end_; | 221 base::TimeTicks burst_end_; |
| 223 | 222 |
| 224 State state_; | 223 State state_; |
| 225 | 224 |
| 226 bool has_reached_upper_bound_once_; | 225 bool has_reached_upper_bound_once_; |
| 227 | 226 |
| 228 // NOTE: Weak pointers must be invalidated before all other member variables. | 227 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 229 base::WeakPtrFactory<PacedSender> weak_factory_; | 228 base::WeakPtrFactory<PacedSender> weak_factory_; |
| 230 | 229 |
| 231 DISALLOW_COPY_AND_ASSIGN(PacedSender); | 230 DISALLOW_COPY_AND_ASSIGN(PacedSender); |
| 232 }; | 231 }; |
| 233 | 232 |
| 234 } // namespace cast | 233 } // namespace cast |
| 235 } // namespace media | 234 } // namespace media |
| 236 | 235 |
| 237 #endif // MEDIA_CAST_NET_PACING_PACED_SENDER_H_ | 236 #endif // MEDIA_CAST_NET_PACING_PACED_SENDER_H_ |
| OLD | NEW |