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.
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.
9 #ifndef MEDIA_CAST_SENDER_FRAME_SENDER_H_
10 #define MEDIA_CAST_SENDER_FRAME_SENDER_H_
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"
23 struct SenderEncodedFrame
;
27 FrameSender(scoped_refptr
<CastEnvironment
> cast_environment
,
29 CastTransportSender
* const transport_sender
,
32 double max_frame_rate
,
33 base::TimeDelta min_playout_delay
,
34 base::TimeDelta max_playout_delay
,
35 CongestionControl
* congestion_control
);
36 virtual ~FrameSender();
38 int rtp_timebase() const { return rtp_timebase_
; }
40 // Calling this function is only valid if the receiver supports the
41 // "extra_playout_delay", rtp extension.
42 void SetTargetPlayoutDelay(base::TimeDelta new_target_playout_delay
);
44 base::TimeDelta
GetTargetPlayoutDelay() const {
45 return target_playout_delay_
;
48 // Called by the encoder with the next EncodeFrame to send.
49 void SendEncodedFrame(int requested_bitrate_before_encode
,
50 scoped_ptr
<SenderEncodedFrame
> encoded_frame
);
53 // Returns the number of frames in the encoder's backlog.
54 virtual int GetNumberOfFramesInEncoder() const = 0;
56 // Returns the duration of the data in the encoder's backlog plus the duration
57 // of sent, unacknowledged frames.
58 virtual base::TimeDelta
GetInFlightMediaDuration() const = 0;
60 // Called when we get an ACK for a frame.
61 virtual void OnAck(uint32 frame_id
) = 0;
64 // Schedule and execute periodic sending of RTCP report.
65 void ScheduleNextRtcpReport();
66 void SendRtcpReport(bool schedule_future_reports
);
68 void OnMeasuredRoundTripTime(base::TimeDelta rtt
);
70 const scoped_refptr
<CastEnvironment
> cast_environment_
;
72 // Sends encoded frames over the configured transport (e.g., UDP). In
73 // Chromium, this could be a proxy that first sends the frames from a renderer
74 // process to the browser process over IPC, with the browser process being
75 // responsible for "packetizing" the frames and pushing packets into the
77 CastTransportSender
* const transport_sender_
;
82 // Schedule and execute periodic checks for re-sending packets. If no
83 // acknowledgements have been received for "too long," AudioSender will
84 // speculatively re-send certain packets of an unacked frame to kick-start
85 // re-transmission. This is a last resort tactic to prevent the session from
86 // getting stuck after a long outage.
87 void ScheduleNextResendCheck();
89 void ResendForKickstart();
91 // Protected for testability.
92 void OnReceivedCastFeedback(const RtcpCastMessage
& cast_feedback
);
94 // Returns true if too many frames would be in-flight by encoding and sending
95 // the next frame having the given |frame_duration|.
96 bool ShouldDropNextFrame(base::TimeDelta frame_duration
) const;
98 // Record or retrieve a recent history of each frame's timestamps.
99 // Warning: If a frame ID too far in the past is requested, the getters will
100 // silently succeed but return incorrect values. Be sure to respect
101 // media::cast::kMaxUnackedFrames.
102 void RecordLatestFrameTimestamps(uint32 frame_id
,
103 base::TimeTicks reference_time
,
104 RtpTimestamp rtp_timestamp
);
105 base::TimeTicks
GetRecordedReferenceTime(uint32 frame_id
) const;
106 RtpTimestamp
GetRecordedRtpTimestamp(uint32 frame_id
) const;
108 // Returns the number of frames that were sent but not yet acknowledged.
109 int GetUnacknowledgedFrameCount() const;
111 // The total amount of time between a frame's capture/recording on the sender
112 // and its playback on the receiver (i.e., shown to a user). This is fixed as
113 // a value large enough to give the system sufficient time to encode,
114 // transmit/retransmit, receive, decode, and render; given its run-time
115 // environment (sender/receiver hardware performance, network conditions,
117 base::TimeDelta target_playout_delay_
;
118 base::TimeDelta min_playout_delay_
;
119 base::TimeDelta max_playout_delay_
;
121 // If true, we transmit the target playout delay to the receiver.
122 bool send_target_playout_delay_
;
124 // Max encoded frames generated per second.
125 double max_frame_rate_
;
127 // Counts how many RTCP reports are being "aggressively" sent (i.e., one per
128 // frame) at the start of the session. Once a threshold is reached, RTCP
129 // reports are instead sent at the configured interval + random drift.
130 int num_aggressive_rtcp_reports_sent_
;
132 // This is "null" until the first frame is sent. Thereafter, this tracks the
133 // last time any frame was sent or re-sent.
134 base::TimeTicks last_send_time_
;
136 // The ID of the last frame sent. Logic throughout FrameSender assumes this
137 // can safely wrap-around. This member is invalid until
138 // |!last_send_time_.is_null()|.
139 uint32 last_sent_frame_id_
;
141 // The ID of the latest (not necessarily the last) frame that has been
142 // acknowledged. Logic throughout AudioSender assumes this can safely
143 // wrap-around. This member is invalid until |!last_send_time_.is_null()|.
144 uint32 latest_acked_frame_id_
;
146 // Counts the number of duplicate ACK that are being received. When this
147 // number reaches a threshold, the sender will take this as a sign that the
148 // receiver hasn't yet received the first packet of the next frame. In this
149 // case, VideoSender will trigger a re-send of the next frame.
150 int duplicate_ack_counter_
;
152 // This object controls how we change the bitrate to make sure the
153 // buffer doesn't overflow.
154 scoped_ptr
<CongestionControl
> congestion_control_
;
156 // The most recently measured round trip time.
157 base::TimeDelta current_round_trip_time_
;
160 // Returns the maximum media duration currently allowed in-flight. This
161 // fluctuates in response to the currently-measured network latency.
162 base::TimeDelta
GetAllowedInFlightMediaDuration() const;
164 // RTP timestamp increment representing one second.
165 const int rtp_timebase_
;
167 const bool is_audio_
;
169 // Ring buffers to keep track of recent frame timestamps (both in terms of
170 // local reference time and RTP media time). These should only be accessed
171 // through the Record/GetXXX() methods.
172 base::TimeTicks frame_reference_times_
[256];
173 RtpTimestamp frame_rtp_timestamps_
[256];
175 // NOTE: Weak pointers must be invalidated before all other member variables.
176 base::WeakPtrFactory
<FrameSender
> weak_factory_
;
178 DISALLOW_COPY_AND_ASSIGN(FrameSender
);
184 #endif // MEDIA_CAST_SENDER_FRAME_SENDER_H_