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"
25 FrameSender(scoped_refptr
<CastEnvironment
> cast_environment
,
27 CastTransportSender
* const transport_sender
,
28 base::TimeDelta rtcp_interval
,
31 double max_frame_rate
,
32 base::TimeDelta min_playout_delay
,
33 base::TimeDelta max_playout_delay
,
34 CongestionControl
* congestion_control
);
35 virtual ~FrameSender();
37 int rtp_timebase() const { return rtp_timebase_
; }
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
);
43 base::TimeDelta
GetTargetPlayoutDelay() const {
44 return target_playout_delay_
;
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
);
52 // Returns the number of frames in the encoder's backlog.
53 virtual int GetNumberOfFramesInEncoder() const = 0;
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;
59 // Called when we get an ACK for a frame.
60 virtual void OnAck(uint32 frame_id
) = 0;
63 // Schedule and execute periodic sending of RTCP report.
64 void ScheduleNextRtcpReport();
65 void SendRtcpReport(bool schedule_future_reports
);
67 void OnMeasuredRoundTripTime(base::TimeDelta rtt
);
69 const scoped_refptr
<CastEnvironment
> cast_environment_
;
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
76 CastTransportSender
* const transport_sender_
;
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();
88 void ResendForKickstart();
90 // Protected for testability.
91 void OnReceivedCastFeedback(const RtcpCastMessage
& cast_feedback
);
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;
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;
107 // Returns the number of frames that were sent but not yet acknowledged.
108 int GetUnacknowledgedFrameCount() const;
110 const base::TimeDelta rtcp_interval_
;
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,
118 base::TimeDelta target_playout_delay_
;
119 base::TimeDelta min_playout_delay_
;
120 base::TimeDelta max_playout_delay_
;
122 // If true, we transmit the target playout delay to the receiver.
123 bool send_target_playout_delay_
;
125 // Max encoded frames generated per second.
126 double max_frame_rate_
;
128 // Counts how many RTCP reports are being "aggressively" sent (i.e., one per
129 // frame) at the start of the session. Once a threshold is reached, RTCP
130 // reports are instead sent at the configured interval + random drift.
131 int num_aggressive_rtcp_reports_sent_
;
133 // This is "null" until the first frame is sent. Thereafter, this tracks the
134 // last time any frame was sent or re-sent.
135 base::TimeTicks last_send_time_
;
137 // The ID of the last frame sent. Logic throughout FrameSender assumes this
138 // can safely wrap-around. This member is invalid until
139 // |!last_send_time_.is_null()|.
140 uint32 last_sent_frame_id_
;
142 // The ID of the latest (not necessarily the last) frame that has been
143 // acknowledged. Logic throughout AudioSender assumes this can safely
144 // wrap-around. This member is invalid until |!last_send_time_.is_null()|.
145 uint32 latest_acked_frame_id_
;
147 // Counts the number of duplicate ACK that are being received. When this
148 // number reaches a threshold, the sender will take this as a sign that the
149 // receiver hasn't yet received the first packet of the next frame. In this
150 // case, VideoSender will trigger a re-send of the next frame.
151 int duplicate_ack_counter_
;
153 // If this sender is ready for use, this is STATUS_AUDIO_INITIALIZED or
154 // STATUS_VIDEO_INITIALIZED.
155 CastInitializationStatus cast_initialization_status_
;
157 // This object controls how we change the bitrate to make sure the
158 // buffer doesn't overflow.
159 scoped_ptr
<CongestionControl
> congestion_control_
;
161 // The most recently measured round trip time.
162 base::TimeDelta current_round_trip_time_
;
165 // Returns the maximum media duration currently allowed in-flight. This
166 // fluctuates in response to the currently-measured network latency.
167 base::TimeDelta
GetAllowedInFlightMediaDuration() const;
169 // RTP timestamp increment representing one second.
170 const int rtp_timebase_
;
172 const bool is_audio_
;
174 // Ring buffers to keep track of recent frame timestamps (both in terms of
175 // local reference time and RTP media time). These should only be accessed
176 // through the Record/GetXXX() methods.
177 base::TimeTicks frame_reference_times_
[256];
178 RtpTimestamp frame_rtp_timestamps_
[256];
180 // NOTE: Weak pointers must be invalidated before all other member variables.
181 base::WeakPtrFactory
<FrameSender
> weak_factory_
;
183 DISALLOW_COPY_AND_ASSIGN(FrameSender
);
189 #endif // MEDIA_CAST_SENDER_FRAME_SENDER_H_