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