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"
24 FrameSender(scoped_refptr
<CastEnvironment
> cast_environment
,
25 CastTransportSender
* const transport_sender
,
26 base::TimeDelta rtcp_interval
,
29 double max_frame_rate
,
30 base::TimeDelta playout_delay
);
31 virtual ~FrameSender();
33 // Calling this function is only valid if the receiver supports the
34 // "extra_playout_delay", rtp extension.
35 void SetTargetPlayoutDelay(base::TimeDelta new_target_playout_delay
);
37 base::TimeDelta
GetTargetPlayoutDelay() const {
38 return target_playout_delay_
;
42 // Schedule and execute periodic sending of RTCP report.
43 void ScheduleNextRtcpReport();
44 void SendRtcpReport(bool schedule_future_reports
);
46 void OnReceivedRtt(base::TimeDelta rtt
,
47 base::TimeDelta avg_rtt
,
48 base::TimeDelta min_rtt
,
49 base::TimeDelta max_rtt
);
51 bool is_rtt_available() const { return rtt_available_
; }
53 const scoped_refptr
<CastEnvironment
> cast_environment_
;
55 // Sends encoded frames over the configured transport (e.g., UDP). In
56 // Chromium, this could be a proxy that first sends the frames from a renderer
57 // process to the browser process over IPC, with the browser process being
58 // responsible for "packetizing" the frames and pushing packets into the
60 CastTransportSender
* const transport_sender_
;
64 // RTT information from RTCP.
67 base::TimeDelta avg_rtt_
;
68 base::TimeDelta min_rtt_
;
69 base::TimeDelta max_rtt_
;
72 // Schedule and execute periodic checks for re-sending packets. If no
73 // acknowledgements have been received for "too long," AudioSender will
74 // 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
76 // getting stuck after a long outage.
77 void ScheduleNextResendCheck();
79 void ResendForKickstart();
81 // 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
83 // silently succeed but return incorrect values. Be sure to respect
84 // media::cast::kMaxUnackedFrames.
85 void RecordLatestFrameTimestamps(uint32 frame_id
,
86 base::TimeTicks reference_time
,
87 RtpTimestamp rtp_timestamp
);
88 base::TimeTicks
GetRecordedReferenceTime(uint32 frame_id
) const;
89 RtpTimestamp
GetRecordedRtpTimestamp(uint32 frame_id
) const;
91 const base::TimeDelta rtcp_interval_
;
93 // 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
95 // a value large enough to give the system sufficient time to encode,
96 // transmit/retransmit, receive, decode, and render; given its run-time
97 // environment (sender/receiver hardware performance, network conditions,
99 base::TimeDelta target_playout_delay_
;
101 // If true, we transmit the target playout delay to the receiver.
102 bool send_target_playout_delay_
;
104 // Max encoded frames generated per second.
105 double max_frame_rate_
;
107 // Maximum number of outstanding frames before the encoding and sending of
108 // new frames shall halt.
109 int max_unacked_frames_
;
111 // 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
113 // reports are instead sent at the configured interval + random drift.
114 int num_aggressive_rtcp_reports_sent_
;
116 // This is "null" until the first frame is sent. Thereafter, this tracks the
117 // last time any frame was sent or re-sent.
118 base::TimeTicks last_send_time_
;
120 // The ID of the last frame sent. Logic throughout FrameSender assumes this
121 // can safely wrap-around. This member is invalid until
122 // |!last_send_time_.is_null()|.
123 uint32 last_sent_frame_id_
;
125 // The ID of the latest (not necessarily the last) frame that has been
126 // acknowledged. Logic throughout AudioSender assumes this can safely
127 // wrap-around. This member is invalid until |!last_send_time_.is_null()|.
128 uint32 latest_acked_frame_id_
;
130 // 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
132 // 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.
134 int duplicate_ack_counter_
;
136 // If this sender is ready for use, this is STATUS_AUDIO_INITIALIZED or
137 // STATUS_VIDEO_INITIALIZED.
138 CastInitializationStatus cast_initialization_status_
;
141 // RTP timestamp increment representing one second.
142 const int rtp_timebase_
;
144 // 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
146 // through the Record/GetXXX() methods.
147 base::TimeTicks frame_reference_times_
[256];
148 RtpTimestamp frame_rtp_timestamps_
[256];
150 // NOTE: Weak pointers must be invalidated before all other member variables.
151 base::WeakPtrFactory
<FrameSender
> weak_factory_
;
153 DISALLOW_COPY_AND_ASSIGN(FrameSender
);
159 #endif // MEDIA_CAST_SENDER_FRAME_SENDER_H_