OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 #ifndef MEDIA_CAST_VIDEO_SENDER_VIDEO_SENDER_H_ | |
6 #define MEDIA_CAST_VIDEO_SENDER_VIDEO_SENDER_H_ | |
7 | |
8 #include "base/callback.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/threading/non_thread_safe.h" | |
13 #include "base/time/tick_clock.h" | |
14 #include "base/time/time.h" | |
15 #include "media/cast/cast_config.h" | |
16 #include "media/cast/cast_environment.h" | |
17 #include "media/cast/congestion_control/congestion_control.h" | |
18 #include "media/cast/logging/logging_defines.h" | |
19 #include "media/cast/rtcp/rtcp.h" | |
20 #include "media/cast/rtp_timestamp_helper.h" | |
21 | |
22 namespace media { | |
23 | |
24 class VideoFrame; | |
25 | |
26 namespace cast { | |
27 | |
28 class LocalVideoEncoderCallback; | |
29 class VideoEncoder; | |
30 | |
31 namespace transport { | |
32 class CastTransportSender; | |
33 } | |
34 | |
35 // Not thread safe. Only called from the main cast thread. | |
36 // This class owns all objects related to sending video, objects that create RTP | |
37 // packets, congestion control, video encoder, parsing and sending of | |
38 // RTCP packets. | |
39 // Additionally it posts a bunch of delayed tasks to the main thread for various | |
40 // timeouts. | |
41 class VideoSender : public RtcpSenderFeedback, | |
42 public base::NonThreadSafe, | |
43 public base::SupportsWeakPtr<VideoSender> { | |
44 public: | |
45 VideoSender(scoped_refptr<CastEnvironment> cast_environment, | |
46 const VideoSenderConfig& video_config, | |
47 const CreateVideoEncodeAcceleratorCallback& create_vea_cb, | |
48 const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb, | |
49 transport::CastTransportSender* const transport_sender); | |
50 | |
51 virtual ~VideoSender(); | |
52 | |
53 CastInitializationStatus InitializationResult() const { | |
54 return cast_initialization_status_; | |
55 } | |
56 | |
57 // Note: It is not guaranteed that |video_frame| will actually be encoded and | |
58 // sent, if VideoSender detects too many frames in flight. Therefore, clients | |
59 // should be careful about the rate at which this method is called. | |
60 // | |
61 // Note: It is invalid to call this method if InitializationResult() returns | |
62 // anything but STATUS_VIDEO_INITIALIZED. | |
63 void InsertRawVideoFrame(const scoped_refptr<media::VideoFrame>& video_frame, | |
64 const base::TimeTicks& capture_time); | |
65 | |
66 // Only called from the main cast thread. | |
67 void IncomingRtcpPacket(scoped_ptr<Packet> packet); | |
68 | |
69 protected: | |
70 // Protected for testability. | |
71 virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) | |
72 OVERRIDE; | |
73 | |
74 private: | |
75 // Schedule and execute periodic sending of RTCP report. | |
76 void ScheduleNextRtcpReport(); | |
77 void SendRtcpReport(bool schedule_future_reports); | |
78 | |
79 // Schedule and execute periodic checks for re-sending packets. If no | |
80 // acknowledgements have been received for "too long," VideoSender will | |
81 // speculatively re-send certain packets of an unacked frame to kick-start | |
82 // re-transmission. This is a last resort tactic to prevent the session from | |
83 // getting stuck after a long outage. | |
84 void ScheduleNextResendCheck(); | |
85 void ResendCheck(); | |
86 void ResendForKickstart(); | |
87 | |
88 // Returns true if there are too many frames in flight, as defined by the | |
89 // configured target playout delay plus simple logic. When this is true, | |
90 // InsertRawVideoFrame() will silenty drop frames instead of sending them to | |
91 // the video encoder. | |
92 bool AreTooManyFramesInFlight() const; | |
93 | |
94 // Called by the |video_encoder_| with the next EncodeFrame to send. | |
95 void SendEncodedVideoFrame(int requested_bitrate_before_encode, | |
96 scoped_ptr<transport::EncodedFrame> encoded_frame); | |
97 | |
98 const scoped_refptr<CastEnvironment> cast_environment_; | |
99 | |
100 // The total amount of time between a frame's capture/recording on the sender | |
101 // and its playback on the receiver (i.e., shown to a user). This is fixed as | |
102 // a value large enough to give the system sufficient time to encode, | |
103 // transmit/retransmit, receive, decode, and render; given its run-time | |
104 // environment (sender/receiver hardware performance, network conditions, | |
105 // etc.). | |
106 const base::TimeDelta target_playout_delay_; | |
107 | |
108 // Sends encoded frames over the configured transport (e.g., UDP). In | |
109 // Chromium, this could be a proxy that first sends the frames from a renderer | |
110 // process to the browser process over IPC, with the browser process being | |
111 // responsible for "packetizing" the frames and pushing packets into the | |
112 // network layer. | |
113 transport::CastTransportSender* const transport_sender_; | |
114 | |
115 // Maximum number of outstanding frames before the encoding and sending of | |
116 // new frames shall halt. | |
117 const int max_unacked_frames_; | |
118 | |
119 // Encodes media::VideoFrame images into EncodedFrames. Per configuration, | |
120 // this will point to either the internal software-based encoder or a proxy to | |
121 // a hardware-based encoder. | |
122 scoped_ptr<VideoEncoder> video_encoder_; | |
123 | |
124 // Manages sending/receiving of RTCP packets, including sender/receiver | |
125 // reports. | |
126 Rtcp rtcp_; | |
127 | |
128 // Records lip-sync (i.e., mapping of RTP <--> NTP timestamps), and | |
129 // extrapolates this mapping to any other point in time. | |
130 RtpTimestampHelper rtp_timestamp_helper_; | |
131 | |
132 // Counts how many RTCP reports are being "aggressively" sent (i.e., one per | |
133 // frame) at the start of the session. Once a threshold is reached, RTCP | |
134 // reports are instead sent at the configured interval + random drift. | |
135 int num_aggressive_rtcp_reports_sent_; | |
136 | |
137 // The number of frames currently being processed in |video_encoder_|. | |
138 int frames_in_encoder_; | |
139 | |
140 // This is "null" until the first frame is sent. Thereafter, this tracks the | |
141 // last time any frame was sent or re-sent. | |
142 base::TimeTicks last_send_time_; | |
143 | |
144 // The ID of the last frame sent. Logic throughout VideoSender assumes this | |
145 // can safely wrap-around. This member is invalid until | |
146 // |!last_send_time_.is_null()|. | |
147 uint32 last_sent_frame_id_; | |
148 | |
149 // The ID of the latest (not necessarily the last) frame that has been | |
150 // acknowledged. Logic throughout VideoSender assumes this can safely | |
151 // wrap-around. This member is invalid until |!last_send_time_.is_null()|. | |
152 uint32 latest_acked_frame_id_; | |
153 | |
154 // Counts the number of duplicate ACK that are being received. When this | |
155 // number reaches a threshold, the sender will take this as a sign that the | |
156 // receiver hasn't yet received the first packet of the next frame. In this | |
157 // case, VideoSender will trigger a re-send of the next frame. | |
158 int duplicate_ack_counter_; | |
159 | |
160 // When we get close to the max number of un-acked frames, we set lower | |
161 // the bitrate drastically to ensure that we catch up. Without this we | |
162 // risk getting stuck in a catch-up state forever. | |
163 CongestionControl congestion_control_; | |
164 | |
165 // If this sender is ready for use, this is STATUS_VIDEO_INITIALIZED. | |
166 CastInitializationStatus cast_initialization_status_; | |
167 | |
168 // This is a "good enough" mapping for finding the RTP timestamp associated | |
169 // with a video frame. The key is the lowest 8 bits of frame id (which is | |
170 // what is sent via RTCP). This map is used for logging purposes. | |
171 RtpTimestamp frame_id_to_rtp_timestamp_[256]; | |
172 | |
173 // NOTE: Weak pointers must be invalidated before all other member variables. | |
174 base::WeakPtrFactory<VideoSender> weak_factory_; | |
175 | |
176 DISALLOW_COPY_AND_ASSIGN(VideoSender); | |
177 }; | |
178 | |
179 } // namespace cast | |
180 } // namespace media | |
181 | |
182 #endif // MEDIA_CAST_VIDEO_SENDER_VIDEO_SENDER_H_ | |
OLD | NEW |