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