| 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 // This is the base class for an object that send frames to a receiver. | 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. | 6 // TODO(hclam): Refactor such that there is no separate AudioSender vs. |
| 7 // VideoSender, and the functionality of both is rolled into this class. | 7 // VideoSender, and the functionality of both is rolled into this class. |
| 8 | 8 |
| 9 #ifndef MEDIA_CAST_SENDER_FRAME_SENDER_H_ | 9 #ifndef MEDIA_CAST_SENDER_FRAME_SENDER_H_ |
| 10 #define MEDIA_CAST_SENDER_FRAME_SENDER_H_ | 10 #define MEDIA_CAST_SENDER_FRAME_SENDER_H_ |
| 11 | 11 |
| 12 #include <deque> |
| 13 |
| 12 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 13 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 15 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 16 #include "media/cast/cast_environment.h" | 18 #include "media/cast/cast_environment.h" |
| 17 #include "media/cast/net/rtcp/rtcp.h" | 19 #include "media/cast/net/rtcp/rtcp.h" |
| 18 #include "media/cast/sender/congestion_control.h" | 20 #include "media/cast/sender/congestion_control.h" |
| 19 | 21 |
| 20 namespace media { | 22 namespace media { |
| 21 namespace cast { | 23 namespace cast { |
| 22 | 24 |
| 23 class FrameSender { | 25 class FrameSender { |
| 24 public: | 26 public: |
| 25 FrameSender(scoped_refptr<CastEnvironment> cast_environment, | 27 FrameSender(scoped_refptr<CastEnvironment> cast_environment, |
| 26 bool is_audio, | 28 bool is_audio, |
| 27 CastTransportSender* const transport_sender, | 29 CastTransportSender* const transport_sender, |
| 28 base::TimeDelta rtcp_interval, | 30 base::TimeDelta rtcp_interval, |
| 29 int rtp_timebase, | 31 int rtp_timebase, |
| 30 uint32 ssrc, | 32 uint32 ssrc, |
| 31 double max_frame_rate, | 33 double max_frame_rate, |
| 32 base::TimeDelta playout_delay, | 34 base::TimeDelta playout_delay, |
| 33 CongestionControl* congestion_control); | 35 CongestionControl* congestion_control); |
| 34 virtual ~FrameSender(); | 36 virtual ~FrameSender(); |
| 35 | 37 |
| 38 int rtp_timebase() const { return rtp_timebase_; } |
| 39 |
| 36 // Calling this function is only valid if the receiver supports the | 40 // Calling this function is only valid if the receiver supports the |
| 37 // "extra_playout_delay", rtp extension. | 41 // "extra_playout_delay", rtp extension. |
| 38 void SetTargetPlayoutDelay(base::TimeDelta new_target_playout_delay); | 42 void SetTargetPlayoutDelay(base::TimeDelta new_target_playout_delay); |
| 39 | 43 |
| 40 base::TimeDelta GetTargetPlayoutDelay() const { | 44 base::TimeDelta GetTargetPlayoutDelay() const { |
| 41 return target_playout_delay_; | 45 return target_playout_delay_; |
| 42 } | 46 } |
| 43 | 47 |
| 44 // Called by the encoder with the next EncodeFrame to send. | 48 // Called by the encoder with the next EncodeFrame to send. |
| 45 void SendEncodedFrame(int requested_bitrate_before_encode, | 49 void SendEncodedFrame(int requested_bitrate_before_encode, |
| 46 scoped_ptr<EncodedFrame> encoded_frame); | 50 scoped_ptr<EncodedFrame> encoded_frame); |
| 47 | 51 |
| 48 protected: | 52 protected: |
| 49 // Returns the number of frames in the encoder's backlog. | |
| 50 virtual int GetNumberOfFramesInEncoder() const = 0; | |
| 51 | |
| 52 // Called when we get an ACK for a frame. | 53 // Called when we get an ACK for a frame. |
| 53 virtual void OnAck(uint32 frame_id) = 0; | 54 virtual void OnAck(uint32 frame_id) = 0; |
| 54 | 55 |
| 55 protected: | 56 protected: |
| 56 // Schedule and execute periodic sending of RTCP report. | 57 // Schedule and execute periodic sending of RTCP report. |
| 57 void ScheduleNextRtcpReport(); | 58 void ScheduleNextRtcpReport(); |
| 58 void SendRtcpReport(bool schedule_future_reports); | 59 void SendRtcpReport(bool schedule_future_reports); |
| 59 | 60 |
| 60 void OnMeasuredRoundTripTime(base::TimeDelta rtt); | 61 void OnMeasuredRoundTripTime(base::TimeDelta rtt); |
| 61 | 62 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 76 // speculatively re-send certain packets of an unacked frame to kick-start | 77 // speculatively re-send certain packets of an unacked frame to kick-start |
| 77 // re-transmission. This is a last resort tactic to prevent the session from | 78 // re-transmission. This is a last resort tactic to prevent the session from |
| 78 // getting stuck after a long outage. | 79 // getting stuck after a long outage. |
| 79 void ScheduleNextResendCheck(); | 80 void ScheduleNextResendCheck(); |
| 80 void ResendCheck(); | 81 void ResendCheck(); |
| 81 void ResendForKickstart(); | 82 void ResendForKickstart(); |
| 82 | 83 |
| 83 // Protected for testability. | 84 // Protected for testability. |
| 84 void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback); | 85 void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback); |
| 85 | 86 |
| 86 // Returns true if there are too many frames in flight, or if the media | |
| 87 // duration of the frames in flight would be too high by sending the next | |
| 88 // frame. The latter metric is determined from the given |capture_time| | |
| 89 // for the next frame to be encoded and sent. | |
| 90 bool ShouldDropNextFrame(base::TimeTicks capture_time) const; | |
| 91 | |
| 92 // Record or retrieve a recent history of each frame's timestamps. | 87 // Record or retrieve a recent history of each frame's timestamps. |
| 93 // Warning: If a frame ID too far in the past is requested, the getters will | 88 // Warning: If a frame ID too far in the past is requested, the getters will |
| 94 // silently succeed but return incorrect values. Be sure to respect | 89 // silently succeed but return incorrect values. Be sure to respect |
| 95 // media::cast::kMaxUnackedFrames. | 90 // media::cast::kMaxUnackedFrames. |
| 96 void RecordLatestFrameTimestamps(uint32 frame_id, | 91 void RecordLatestFrameTimestamps(uint32 frame_id, |
| 97 base::TimeTicks reference_time, | 92 base::TimeTicks reference_time, |
| 98 RtpTimestamp rtp_timestamp); | 93 RtpTimestamp rtp_timestamp); |
| 99 base::TimeTicks GetRecordedReferenceTime(uint32 frame_id) const; | 94 base::TimeTicks GetRecordedReferenceTime(uint32 frame_id) const; |
| 100 RtpTimestamp GetRecordedRtpTimestamp(uint32 frame_id) const; | 95 RtpTimestamp GetRecordedRtpTimestamp(uint32 frame_id) const; |
| 101 | 96 |
| 97 // Returns the number of frames that were sent but not yet acknowledged. |
| 98 int GetUnackedFrameCount() const; |
| 99 |
| 100 // Returns the maximum media duration currently allowed in-flight. This |
| 101 // fluctuates in response to the currently-measured network latency. |
| 102 base::TimeDelta GetAllowedInFlightMediaDuration() const; |
| 103 |
| 102 const base::TimeDelta rtcp_interval_; | 104 const base::TimeDelta rtcp_interval_; |
| 103 | 105 |
| 104 // The total amount of time between a frame's capture/recording on the sender | 106 // The total amount of time between a frame's capture/recording on the sender |
| 105 // and its playback on the receiver (i.e., shown to a user). This is fixed as | 107 // and its playback on the receiver (i.e., shown to a user). This is fixed as |
| 106 // a value large enough to give the system sufficient time to encode, | 108 // a value large enough to give the system sufficient time to encode, |
| 107 // transmit/retransmit, receive, decode, and render; given its run-time | 109 // transmit/retransmit, receive, decode, and render; given its run-time |
| 108 // environment (sender/receiver hardware performance, network conditions, | 110 // environment (sender/receiver hardware performance, network conditions, |
| 109 // etc.). | 111 // etc.). |
| 110 base::TimeDelta target_playout_delay_; | 112 base::TimeDelta target_playout_delay_; |
| 111 | 113 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 141 // Counts the number of duplicate ACK that are being received. When this | 143 // Counts the number of duplicate ACK that are being received. When this |
| 142 // number reaches a threshold, the sender will take this as a sign that the | 144 // number reaches a threshold, the sender will take this as a sign that the |
| 143 // receiver hasn't yet received the first packet of the next frame. In this | 145 // receiver hasn't yet received the first packet of the next frame. In this |
| 144 // case, VideoSender will trigger a re-send of the next frame. | 146 // case, VideoSender will trigger a re-send of the next frame. |
| 145 int duplicate_ack_counter_; | 147 int duplicate_ack_counter_; |
| 146 | 148 |
| 147 // If this sender is ready for use, this is STATUS_AUDIO_INITIALIZED or | 149 // If this sender is ready for use, this is STATUS_AUDIO_INITIALIZED or |
| 148 // STATUS_VIDEO_INITIALIZED. | 150 // STATUS_VIDEO_INITIALIZED. |
| 149 CastInitializationStatus cast_initialization_status_; | 151 CastInitializationStatus cast_initialization_status_; |
| 150 | 152 |
| 151 // RTP timestamp increment representing one second. | |
| 152 const int rtp_timebase_; | |
| 153 | |
| 154 // This object controls how we change the bitrate to make sure the | 153 // This object controls how we change the bitrate to make sure the |
| 155 // buffer doesn't overflow. | 154 // buffer doesn't overflow. |
| 156 scoped_ptr<CongestionControl> congestion_control_; | 155 scoped_ptr<CongestionControl> congestion_control_; |
| 157 | 156 |
| 158 private: | 157 private: |
| 158 // RTP timestamp increment representing one second. |
| 159 const int rtp_timebase_; |
| 160 |
| 159 const bool is_audio_; | 161 const bool is_audio_; |
| 160 | 162 |
| 161 // Ring buffers to keep track of recent frame timestamps (both in terms of | 163 // Ring buffers to keep track of recent frame timestamps (both in terms of |
| 162 // local reference time and RTP media time). These should only be accessed | 164 // local reference time and RTP media time). These should only be accessed |
| 163 // through the Record/GetXXX() methods. | 165 // through the Record/GetXXX() methods. |
| 164 base::TimeTicks frame_reference_times_[256]; | 166 base::TimeTicks frame_reference_times_[256]; |
| 165 RtpTimestamp frame_rtp_timestamps_[256]; | 167 RtpTimestamp frame_rtp_timestamps_[256]; |
| 166 | 168 |
| 167 // The most recently measured round trip time. | 169 // The most recently measured round trip time, and a recent history of |
| 170 // maximums. |
| 168 base::TimeDelta current_round_trip_time_; | 171 base::TimeDelta current_round_trip_time_; |
| 172 std::deque<base::TimeDelta> max_rtt_buckets_; |
| 173 base::TimeTicks last_max_rtt_bucket_rotation_; |
| 174 |
| 175 // The current maximum expected one-way trip time on the network. This is |
| 176 // re-computed as each RTT measurement is received, and affects the media |
| 177 // duration allowed to be in-flight. |
| 178 base::TimeDelta expected_max_one_way_trip_time_; |
| 169 | 179 |
| 170 // NOTE: Weak pointers must be invalidated before all other member variables. | 180 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 171 base::WeakPtrFactory<FrameSender> weak_factory_; | 181 base::WeakPtrFactory<FrameSender> weak_factory_; |
| 172 | 182 |
| 173 DISALLOW_COPY_AND_ASSIGN(FrameSender); | 183 DISALLOW_COPY_AND_ASSIGN(FrameSender); |
| 174 }; | 184 }; |
| 175 | 185 |
| 176 } // namespace cast | 186 } // namespace cast |
| 177 } // namespace media | 187 } // namespace media |
| 178 | 188 |
| 179 #endif // MEDIA_CAST_SENDER_FRAME_SENDER_H_ | 189 #endif // MEDIA_CAST_SENDER_FRAME_SENDER_H_ |
| OLD | NEW |