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. 4 // 5 // This is the base class for an object that send frames to a receiver. 6 // TODO(hclam): Refactor such that there is no separate AudioSender vs. 7 // VideoSender, and the functionality of both is rolled into this class. 8 9 #ifndef MEDIA_CAST_SENDER_FRAME_SENDER_H_ 10 #define MEDIA_CAST_SENDER_FRAME_SENDER_H_ 11 12 #include "base/basictypes.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/time/time.h" 16 #include "media/cast/cast_environment.h" 17 #include "media/cast/net/rtcp/rtcp.h" 18 #include "media/cast/sender/congestion_control.h" 19 20 namespace media { 21 namespace cast { 22 23 class FrameSender { 24 public: 25 FrameSender(scoped_refptr<CastEnvironment> cast_environment, 26 bool is_audio, 27 CastTransportSender* const transport_sender, 28 base::TimeDelta rtcp_interval, 29 int rtp_timebase, 30 uint32 ssrc, 31 double max_frame_rate, 32 base::TimeDelta min_playout_delay, 33 base::TimeDelta max_playout_delay, 34 CongestionControl* congestion_control); 35 virtual ~FrameSender(); 36 37 int rtp_timebase() const { return rtp_timebase_; } 38 39 // Calling this function is only valid if the receiver supports the 40 // "extra_playout_delay", rtp extension. 41 void SetTargetPlayoutDelay(base::TimeDelta new_target_playout_delay); 42 43 base::TimeDelta GetTargetPlayoutDelay() const { 44 return target_playout_delay_; 45 } 46 47 // Called by the encoder with the next EncodeFrame to send. 48 void SendEncodedFrame(int requested_bitrate_before_encode, 49 scoped_ptr<EncodedFrame> encoded_frame); 50 51 protected: 52 // Returns the number of frames in the encoder's backlog. 53 virtual int GetNumberOfFramesInEncoder() const = 0; 54 55 // Returns the duration of the data in the encoder's backlog plus the duration 56 // of sent, unacknowledged frames. 57 virtual base::TimeDelta GetInFlightMediaDuration() const = 0; 58 59 // Called when we get an ACK for a frame. 60 virtual void OnAck(uint32 frame_id) = 0; 61 62 protected: 63 // Schedule and execute periodic sending of RTCP report. 64 void ScheduleNextRtcpReport(); 65 void SendRtcpReport(bool schedule_future_reports); 66 67 void OnMeasuredRoundTripTime(base::TimeDelta rtt); 68 69 const scoped_refptr<CastEnvironment> cast_environment_; 70 71 // Sends encoded frames over the configured transport (e.g., UDP). In 72 // Chromium, this could be a proxy that first sends the frames from a renderer 73 // process to the browser process over IPC, with the browser process being 74 // responsible for "packetizing" the frames and pushing packets into the 75 // network layer. 76 CastTransportSender* const transport_sender_; 77 78 const uint32 ssrc_; 79 80 protected: 81 // Schedule and execute periodic checks for re-sending packets. If no 82 // acknowledgements have been received for "too long," AudioSender will 83 // speculatively re-send certain packets of an unacked frame to kick-start 84 // re-transmission. This is a last resort tactic to prevent the session from 85 // getting stuck after a long outage. 86 void ScheduleNextResendCheck(); 87 void ResendCheck(); 88 void ResendForKickstart(); 89 90 // Protected for testability. 91 void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback); 92 93 // Returns true if too many frames would be in-flight by encoding and sending 94 // the next frame having the given |frame_duration|. 95 bool ShouldDropNextFrame(base::TimeDelta frame_duration) const; 96 97 // Record or retrieve a recent history of each frame's timestamps. 98 // Warning: If a frame ID too far in the past is requested, the getters will 99 // silently succeed but return incorrect values. Be sure to respect 100 // media::cast::kMaxUnackedFrames. 101 void RecordLatestFrameTimestamps(uint32 frame_id, 102 base::TimeTicks reference_time, 103 RtpTimestamp rtp_timestamp); 104 base::TimeTicks GetRecordedReferenceTime(uint32 frame_id) const; 105 RtpTimestamp GetRecordedRtpTimestamp(uint32 frame_id) const; 106 107 // Returns the number of frames that were sent but not yet acknowledged. 108 int GetUnacknowledgedFrameCount() const; 109 110 const base::TimeDelta rtcp_interval_; 111 112 // The total amount of time between a frame's capture/recording on the sender 113 // and its playback on the receiver (i.e., shown to a user). This is fixed as 114 // a value large enough to give the system sufficient time to encode, 115 // transmit/retransmit, receive, decode, and render; given its run-time 116 // environment (sender/receiver hardware performance, network conditions, 117 // etc.). 118 base::TimeDelta target_playout_delay_; 119 base::TimeDelta min_playout_delay_; 120 base::TimeDelta max_playout_delay_; 121 122 // If true, we transmit the target playout delay to the receiver. 123 bool send_target_playout_delay_; 124 125 // Max encoded frames generated per second. 126 double max_frame_rate_; 127 128 // Maximum number of outstanding frames before the encoding and sending of 129 // new frames shall halt. 130 int max_unacked_frames_; 131 132 // Counts how many RTCP reports are being "aggressively" sent (i.e., one per 133 // frame) at the start of the session. Once a threshold is reached, RTCP 134 // reports are instead sent at the configured interval + random drift. 135 int num_aggressive_rtcp_reports_sent_; 136 137 // This is "null" until the first frame is sent. Thereafter, this tracks the 138 // last time any frame was sent or re-sent. 139 base::TimeTicks last_send_time_; 140 141 // The ID of the last frame sent. Logic throughout FrameSender assumes this 142 // can safely wrap-around. This member is invalid until 143 // |!last_send_time_.is_null()|. 144 uint32 last_sent_frame_id_; 145 146 // The ID of the latest (not necessarily the last) frame that has been 147 // acknowledged. Logic throughout AudioSender assumes this can safely 148 // wrap-around. This member is invalid until |!last_send_time_.is_null()|. 149 uint32 latest_acked_frame_id_; 150 151 // Counts the number of duplicate ACK that are being received. When this 152 // number reaches a threshold, the sender will take this as a sign that the 153 // receiver hasn't yet received the first packet of the next frame. In this 154 // case, VideoSender will trigger a re-send of the next frame. 155 int duplicate_ack_counter_; 156 157 // If this sender is ready for use, this is STATUS_AUDIO_INITIALIZED or 158 // STATUS_VIDEO_INITIALIZED. 159 CastInitializationStatus cast_initialization_status_; 160 161 // This object controls how we change the bitrate to make sure the 162 // buffer doesn't overflow. 163 scoped_ptr<CongestionControl> congestion_control_; 164 165 // The most recently measured round trip time. 166 base::TimeDelta current_round_trip_time_; 167 168 private: 169 // Returns the maximum media duration currently allowed in-flight. This 170 // fluctuates in response to the currently-measured network latency. 171 base::TimeDelta GetAllowedInFlightMediaDuration() const; 172 173 // RTP timestamp increment representing one second. 174 const int rtp_timebase_; 175 176 const bool is_audio_; 177 178 // Ring buffers to keep track of recent frame timestamps (both in terms of 179 // local reference time and RTP media time). These should only be accessed 180 // through the Record/GetXXX() methods. 181 base::TimeTicks frame_reference_times_[256]; 182 RtpTimestamp frame_rtp_timestamps_[256]; 183 184 // NOTE: Weak pointers must be invalidated before all other member variables. 185 base::WeakPtrFactory<FrameSender> weak_factory_; 186 187 DISALLOW_COPY_AND_ASSIGN(FrameSender); 188 }; 189 190 } // namespace cast 191 } // namespace media 192 193 #endif // MEDIA_CAST_SENDER_FRAME_SENDER_H_ 194