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 #ifndef MEDIA_CAST_CONGESTION_CONTROL_CONGESTION_CONTROL_H_ 6 #define MEDIA_CAST_CONGESTION_CONTROL_CONGESTION_CONTROL_H_ 7 8 #include <deque> 9 10 #include "base/basictypes.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/time/tick_clock.h" 13 #include "base/time/time.h" 14 15 namespace media { 16 namespace cast { 17 18 class CongestionControl { 19 public: 20 CongestionControl(base::TickClock* clock, 21 uint32 max_bitrate_configured, 22 uint32 min_bitrate_configured, 23 size_t max_unacked_frames); 24 25 virtual ~CongestionControl(); 26 27 void UpdateRtt(base::TimeDelta rtt); 28 29 // Called when an encoded frame is sent to the transport. 30 void SendFrameToTransport(uint32 frame_id, 31 size_t frame_size, 32 base::TimeTicks when); 33 34 // Called when we receive an ACK for a frame. 35 void AckFrame(uint32 frame_id, base::TimeTicks when); 36 37 // Returns the bitrate we should use for the next frame. 38 uint32 GetBitrate(base::TimeTicks playout_time, 39 base::TimeDelta playout_delay); 40 41 private: 42 struct FrameStats { 43 FrameStats(); 44 // Time this frame was sent to the transport. 45 base::TimeTicks sent_time; 46 // Time this frame was acked. 47 base::TimeTicks ack_time; 48 // Size of encoded frame in bits. 49 size_t frame_size; 50 }; 51 52 // Calculate how much "dead air" (idle time) there is between two frames. 53 static base::TimeDelta DeadTime(const FrameStats& a, const FrameStats& b); 54 // Get the FrameStats for a given |frame_id|. 55 // Note: Older FrameStats will be removed automatically. 56 FrameStats* GetFrameStats(uint32 frame_id); 57 // Calculata safe bitrate. This is based on how much we've been 58 // sending in the past. 59 double CalculateSafeBitrate(); 60 61 // For a given frame, calculate when it might be acked. 62 // (Or return the time it was acked, if it was.) 63 base::TimeTicks EstimatedAckTime(uint32 frame_id, double bitrate); 64 // Calculate when we start sending the data for a given frame. 65 // This is done by calculating when we were done sending the previous 66 // frame, but obvoiusly can't be less than |sent_time| (if known). 67 base::TimeTicks EstimatedSendingTime(uint32 frame_id, double bitrate); 68 69 base::TickClock* const clock_; // Not owned by this class. 70 const uint32 max_bitrate_configured_; 71 const uint32 min_bitrate_configured_; 72 std::deque<FrameStats> frame_stats_; 73 uint32 last_frame_stats_; 74 uint32 last_acked_frame_; 75 uint32 last_encoded_frame_; 76 base::TimeDelta rtt_; 77 size_t history_size_; 78 size_t acked_bits_in_history_; 79 base::TimeDelta dead_time_in_history_; 80 81 DISALLOW_COPY_AND_ASSIGN(CongestionControl); 82 }; 83 84 } // namespace cast 85 } // namespace media 86 87 #endif // MEDIA_CAST_CONGESTION_CONTROL_CONGESTION_CONTROL_H_ 88