OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/cast/video_receiver/video_receiver.h" | 5 #include "media/cast/video_receiver/video_receiver.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "media/base/video_frame.h" | 13 #include "media/base/video_frame.h" |
14 #include "media/cast/logging/logging_defines.h" | 14 #include "media/cast/logging/logging_defines.h" |
15 #include "media/cast/transport/cast_transport_defines.h" | 15 #include "media/cast/transport/cast_transport_defines.h" |
16 #include "media/cast/video_receiver/video_decoder.h" | 16 #include "media/cast/video_receiver/video_decoder.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 const int kMinSchedulingDelayMs = 1; | 19 const int kMinSchedulingDelayMs = 1; |
20 const int kMinTimeBetweenOffsetUpdatesMs = 1000; | |
21 const int kTimeOffsetMaxCounter = 10; | |
22 } // namespace | 20 } // namespace |
23 | 21 |
24 namespace media { | 22 namespace media { |
25 namespace cast { | 23 namespace cast { |
26 | 24 |
27 VideoReceiver::VideoReceiver(scoped_refptr<CastEnvironment> cast_environment, | 25 VideoReceiver::VideoReceiver(scoped_refptr<CastEnvironment> cast_environment, |
28 const VideoReceiverConfig& video_config, | 26 const VideoReceiverConfig& video_config, |
29 transport::PacedPacketSender* const packet_sender) | 27 transport::PacedPacketSender* const packet_sender) |
30 : RtpReceiver(cast_environment->Clock(), NULL, &video_config), | 28 : RtpReceiver(cast_environment->Clock(), NULL, &video_config), |
31 cast_environment_(cast_environment), | 29 cast_environment_(cast_environment), |
32 event_subscriber_(kReceiverRtcpEventHistorySize, VIDEO_EVENT), | 30 event_subscriber_(kReceiverRtcpEventHistorySize, VIDEO_EVENT), |
33 codec_(video_config.codec), | 31 codec_(video_config.codec), |
34 target_delay_delta_( | 32 target_playout_delay_( |
35 base::TimeDelta::FromMilliseconds(video_config.rtp_max_delay_ms)), | 33 base::TimeDelta::FromMilliseconds(video_config.rtp_max_delay_ms)), |
36 expected_frame_duration_( | 34 expected_frame_duration_( |
37 base::TimeDelta::FromSeconds(1) / video_config.max_frame_rate), | 35 base::TimeDelta::FromSeconds(1) / video_config.max_frame_rate), |
| 36 reports_are_scheduled_(false), |
38 framer_(cast_environment->Clock(), | 37 framer_(cast_environment->Clock(), |
39 this, | 38 this, |
40 video_config.incoming_ssrc, | 39 video_config.incoming_ssrc, |
41 video_config.decoder_faster_than_max_frame_rate, | 40 video_config.decoder_faster_than_max_frame_rate, |
42 video_config.rtp_max_delay_ms * video_config.max_frame_rate / | 41 video_config.rtp_max_delay_ms * video_config.max_frame_rate / |
43 1000), | 42 1000), |
44 rtcp_(cast_environment_, | 43 rtcp_(cast_environment_, |
45 NULL, | 44 NULL, |
46 NULL, | 45 NULL, |
47 packet_sender, | 46 packet_sender, |
48 GetStatistics(), | 47 GetStatistics(), |
49 video_config.rtcp_mode, | 48 video_config.rtcp_mode, |
50 base::TimeDelta::FromMilliseconds(video_config.rtcp_interval), | 49 base::TimeDelta::FromMilliseconds(video_config.rtcp_interval), |
51 video_config.feedback_ssrc, | 50 video_config.feedback_ssrc, |
52 video_config.incoming_ssrc, | 51 video_config.incoming_ssrc, |
53 video_config.rtcp_c_name, | 52 video_config.rtcp_c_name, |
54 false), | 53 false), |
55 time_offset_counter_(0), | |
56 time_incoming_packet_updated_(false), | |
57 incoming_rtp_timestamp_(0), | |
58 is_waiting_for_consecutive_frame_(false), | 54 is_waiting_for_consecutive_frame_(false), |
| 55 lip_sync_drift_(ClockDriftSmoother::GetDefaultTimeConstant()), |
59 weak_factory_(this) { | 56 weak_factory_(this) { |
60 DCHECK_GT(video_config.rtp_max_delay_ms, 0); | 57 DCHECK_GT(video_config.rtp_max_delay_ms, 0); |
61 DCHECK_GT(video_config.max_frame_rate, 0); | 58 DCHECK_GT(video_config.max_frame_rate, 0); |
62 if (!video_config.use_external_decoder) { | 59 if (!video_config.use_external_decoder) { |
63 video_decoder_.reset(new VideoDecoder(cast_environment, video_config)); | 60 video_decoder_.reset(new VideoDecoder(cast_environment, video_config)); |
64 } | 61 } |
65 decryptor_.Initialize(video_config.aes_key, video_config.aes_iv_mask); | 62 decryptor_.Initialize(video_config.aes_key, video_config.aes_iv_mask); |
66 rtcp_.SetTargetDelay(target_delay_delta_); | 63 rtcp_.SetTargetDelay(target_playout_delay_); |
67 cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber_); | 64 cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber_); |
68 memset(frame_id_to_rtp_timestamp_, 0, sizeof(frame_id_to_rtp_timestamp_)); | 65 memset(frame_id_to_rtp_timestamp_, 0, sizeof(frame_id_to_rtp_timestamp_)); |
69 } | 66 } |
70 | 67 |
71 VideoReceiver::~VideoReceiver() { | 68 VideoReceiver::~VideoReceiver() { |
72 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 69 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
73 cast_environment_->Logging()->RemoveRawEventSubscriber(&event_subscriber_); | 70 cast_environment_->Logging()->RemoveRawEventSubscriber(&event_subscriber_); |
74 } | 71 } |
75 | 72 |
76 void VideoReceiver::InitializeTimers() { | |
77 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
78 ScheduleNextRtcpReport(); | |
79 ScheduleNextCastMessage(); | |
80 } | |
81 | |
82 void VideoReceiver::GetRawVideoFrame( | 73 void VideoReceiver::GetRawVideoFrame( |
83 const VideoFrameDecodedCallback& callback) { | 74 const VideoFrameDecodedCallback& callback) { |
84 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 75 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
85 DCHECK(!callback.is_null()); | 76 DCHECK(!callback.is_null()); |
86 DCHECK(video_decoder_.get()); | 77 DCHECK(video_decoder_.get()); |
87 GetEncodedVideoFrame(base::Bind( | 78 GetEncodedVideoFrame(base::Bind( |
88 &VideoReceiver::DecodeEncodedVideoFrame, | 79 &VideoReceiver::DecodeEncodedVideoFrame, |
89 // Note: Use of Unretained is safe since this Closure is guaranteed to be | 80 // Note: Use of Unretained is safe since this Closure is guaranteed to be |
90 // invoked before destruction of |this|. | 81 // invoked before destruction of |this|. |
91 base::Unretained(this), | 82 base::Unretained(this), |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 // payload yet! Or, at least, peek using a StringPiece instead of a copy. | 145 // payload yet! Or, at least, peek using a StringPiece instead of a copy. |
155 scoped_ptr<transport::EncodedFrame> encoded_frame( | 146 scoped_ptr<transport::EncodedFrame> encoded_frame( |
156 new transport::EncodedFrame()); | 147 new transport::EncodedFrame()); |
157 bool is_consecutively_next_frame = false; | 148 bool is_consecutively_next_frame = false; |
158 if (!framer_.GetEncodedVideoFrame(encoded_frame.get(), | 149 if (!framer_.GetEncodedVideoFrame(encoded_frame.get(), |
159 &is_consecutively_next_frame)) { | 150 &is_consecutively_next_frame)) { |
160 VLOG(1) << "Wait for more video packets to produce a completed frame."; | 151 VLOG(1) << "Wait for more video packets to produce a completed frame."; |
161 return; // OnReceivedPayloadData() will invoke this method in the future. | 152 return; // OnReceivedPayloadData() will invoke this method in the future. |
162 } | 153 } |
163 | 154 |
| 155 const base::TimeTicks playout_time = |
| 156 GetPlayoutTime(encoded_frame->rtp_timestamp); |
| 157 |
164 // If |framer_| has a frame ready that is out of sequence, examine the | 158 // If |framer_| has a frame ready that is out of sequence, examine the |
165 // playout time to determine whether it's acceptable to continue, thereby | 159 // playout time to determine whether it's acceptable to continue, thereby |
166 // skipping one or more frames. Skip if the missing frame wouldn't complete | 160 // skipping one or more frames. Skip if the missing frame wouldn't complete |
167 // playing before the start of playback of the available frame. | 161 // playing before the start of playback of the available frame. |
168 const base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | |
169 const base::TimeTicks playout_time = | |
170 GetPlayoutTime(now, encoded_frame->rtp_timestamp); | |
171 if (!is_consecutively_next_frame) { | 162 if (!is_consecutively_next_frame) { |
| 163 const base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
172 // TODO(miu): Also account for expected decode time here? | 164 // TODO(miu): Also account for expected decode time here? |
173 const base::TimeTicks earliest_possible_end_time_of_missing_frame = | 165 const base::TimeTicks earliest_possible_end_time_of_missing_frame = |
174 now + expected_frame_duration_; | 166 now + expected_frame_duration_; |
175 if (earliest_possible_end_time_of_missing_frame < playout_time) { | 167 if (earliest_possible_end_time_of_missing_frame < playout_time) { |
176 VLOG(1) << "Wait for next consecutive frame instead of skipping."; | 168 VLOG(1) << "Wait for next consecutive frame instead of skipping."; |
177 if (!is_waiting_for_consecutive_frame_) { | 169 if (!is_waiting_for_consecutive_frame_) { |
178 is_waiting_for_consecutive_frame_ = true; | 170 is_waiting_for_consecutive_frame_ = true; |
179 cast_environment_->PostDelayedTask( | 171 cast_environment_->PostDelayedTask( |
180 CastEnvironment::MAIN, | 172 CastEnvironment::MAIN, |
181 FROM_HERE, | 173 FROM_HERE, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 } | 211 } |
220 } | 212 } |
221 | 213 |
222 void VideoReceiver::EmitAvailableEncodedFramesAfterWaiting() { | 214 void VideoReceiver::EmitAvailableEncodedFramesAfterWaiting() { |
223 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 215 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
224 DCHECK(is_waiting_for_consecutive_frame_); | 216 DCHECK(is_waiting_for_consecutive_frame_); |
225 is_waiting_for_consecutive_frame_ = false; | 217 is_waiting_for_consecutive_frame_ = false; |
226 EmitAvailableEncodedFrames(); | 218 EmitAvailableEncodedFrames(); |
227 } | 219 } |
228 | 220 |
229 base::TimeTicks VideoReceiver::GetPlayoutTime(base::TimeTicks now, | 221 base::TimeTicks VideoReceiver::GetPlayoutTime(uint32 rtp_timestamp) const { |
230 uint32 rtp_timestamp) { | 222 return lip_sync_reference_time_ + |
231 // TODO(miu): This and AudioReceiver::GetPlayoutTime() need to be reconciled! | 223 lip_sync_drift_.Current() + |
232 | 224 RtpDeltaToTimeDelta( |
233 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 225 static_cast<int32>(rtp_timestamp - lip_sync_rtp_timestamp_), |
234 // Senders time in ms when this frame was captured. | 226 kVideoFrequency) + |
235 // Note: the senders clock and our local clock might not be synced. | 227 target_playout_delay_; |
236 base::TimeTicks rtp_timestamp_in_ticks; | |
237 | |
238 // Compute the time offset_in_ticks based on the incoming_rtp_timestamp_. | |
239 if (time_offset_counter_ == 0) { | |
240 // Check for received RTCP to sync the stream play it out asap. | |
241 if (rtcp_.RtpTimestampInSenderTime(kVideoFrequency, | |
242 incoming_rtp_timestamp_, | |
243 &rtp_timestamp_in_ticks)) { | |
244 ++time_offset_counter_; | |
245 } | |
246 } else if (time_incoming_packet_updated_) { | |
247 if (rtcp_.RtpTimestampInSenderTime(kVideoFrequency, | |
248 incoming_rtp_timestamp_, | |
249 &rtp_timestamp_in_ticks)) { | |
250 // Time to update the time_offset. | |
251 base::TimeDelta time_offset = | |
252 time_incoming_packet_ - rtp_timestamp_in_ticks; | |
253 // Taking the minimum of the first kTimeOffsetMaxCounter values. We are | |
254 // assuming that we are looking for the minimum offset, which will occur | |
255 // when network conditions are the best. This should occur at least once | |
256 // within the first kTimeOffsetMaxCounter samples. Any drift should be | |
257 // very slow, and negligible for this use case. | |
258 if (time_offset_counter_ == 1) | |
259 time_offset_ = time_offset; | |
260 else if (time_offset_counter_ < kTimeOffsetMaxCounter) { | |
261 time_offset_ = std::min(time_offset_, time_offset); | |
262 } | |
263 if (time_offset_counter_ < kTimeOffsetMaxCounter) | |
264 ++time_offset_counter_; | |
265 } | |
266 } | |
267 // Reset |time_incoming_packet_updated_| to enable a future measurement. | |
268 time_incoming_packet_updated_ = false; | |
269 // Compute the actual rtp_timestamp_in_ticks based on the current timestamp. | |
270 if (!rtcp_.RtpTimestampInSenderTime( | |
271 kVideoFrequency, rtp_timestamp, &rtp_timestamp_in_ticks)) { | |
272 // This can fail if we have not received any RTCP packets in a long time. | |
273 // BUG: These calculations are a placeholder, and to be revisited in a | |
274 // soon-upcoming change. http://crbug.com/356942 | |
275 const int frequency_khz = kVideoFrequency / 1000; | |
276 const base::TimeDelta delta_based_on_rtp_timestamps = | |
277 base::TimeDelta::FromMilliseconds( | |
278 static_cast<int32>(rtp_timestamp - incoming_rtp_timestamp_) / | |
279 frequency_khz); | |
280 return time_incoming_packet_ + delta_based_on_rtp_timestamps; | |
281 } | |
282 | |
283 base::TimeTicks render_time = | |
284 rtp_timestamp_in_ticks + time_offset_ + target_delay_delta_; | |
285 // TODO(miu): This is broken since this "getter" method may be called on | |
286 // frames received out-of-order, which means the playout times for earlier | |
287 // frames will be computed incorrectly. | |
288 #if 0 | |
289 if (last_render_time_ > render_time) | |
290 render_time = last_render_time_; | |
291 last_render_time_ = render_time; | |
292 #endif | |
293 | |
294 return render_time; | |
295 } | 228 } |
296 | 229 |
297 void VideoReceiver::IncomingPacket(scoped_ptr<Packet> packet) { | 230 void VideoReceiver::IncomingPacket(scoped_ptr<Packet> packet) { |
298 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 231 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
299 if (Rtcp::IsRtcpPacket(&packet->front(), packet->size())) { | 232 if (Rtcp::IsRtcpPacket(&packet->front(), packet->size())) { |
300 rtcp_.IncomingRtcpPacket(&packet->front(), packet->size()); | 233 rtcp_.IncomingRtcpPacket(&packet->front(), packet->size()); |
301 } else { | 234 } else { |
302 ReceivedPacket(&packet->front(), packet->size()); | 235 ReceivedPacket(&packet->front(), packet->size()); |
303 } | 236 } |
| 237 if (!reports_are_scheduled_) { |
| 238 ScheduleNextRtcpReport(); |
| 239 ScheduleNextCastMessage(); |
| 240 reports_are_scheduled_ = true; |
| 241 } |
304 } | 242 } |
305 | 243 |
306 void VideoReceiver::OnReceivedPayloadData(const uint8* payload_data, | 244 void VideoReceiver::OnReceivedPayloadData(const uint8* payload_data, |
307 size_t payload_size, | 245 size_t payload_size, |
308 const RtpCastHeader& rtp_header) { | 246 const RtpCastHeader& rtp_header) { |
309 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 247 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
310 | 248 |
311 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | 249 const base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
312 if (time_incoming_packet_.is_null() || | |
313 now - time_incoming_packet_ > | |
314 base::TimeDelta::FromMilliseconds(kMinTimeBetweenOffsetUpdatesMs)) { | |
315 if (time_incoming_packet_.is_null()) | |
316 InitializeTimers(); | |
317 incoming_rtp_timestamp_ = rtp_header.rtp_timestamp; | |
318 // The following incoming packet info is used for syncing sender and | |
319 // receiver clock. Use only the first packet of every frame to obtain a | |
320 // minimal value. | |
321 if (rtp_header.packet_id == 0) { | |
322 time_incoming_packet_ = now; | |
323 time_incoming_packet_updated_ = true; | |
324 } | |
325 } | |
326 | 250 |
327 frame_id_to_rtp_timestamp_[rtp_header.frame_id & 0xff] = | 251 frame_id_to_rtp_timestamp_[rtp_header.frame_id & 0xff] = |
328 rtp_header.rtp_timestamp; | 252 rtp_header.rtp_timestamp; |
329 cast_environment_->Logging()->InsertPacketEvent( | 253 cast_environment_->Logging()->InsertPacketEvent( |
330 now, | 254 now, |
331 PACKET_RECEIVED, | 255 PACKET_RECEIVED, |
332 VIDEO_EVENT, | 256 VIDEO_EVENT, |
333 rtp_header.rtp_timestamp, | 257 rtp_header.rtp_timestamp, |
334 rtp_header.frame_id, | 258 rtp_header.frame_id, |
335 rtp_header.packet_id, | 259 rtp_header.packet_id, |
336 rtp_header.max_packet_id, | 260 rtp_header.max_packet_id, |
337 payload_size); | 261 payload_size); |
338 | 262 |
339 bool duplicate = false; | 263 bool duplicate = false; |
340 const bool complete = | 264 const bool complete = |
341 framer_.InsertPacket(payload_data, payload_size, rtp_header, &duplicate); | 265 framer_.InsertPacket(payload_data, payload_size, rtp_header, &duplicate); |
342 | 266 |
343 // Duplicate packets are ignored. | 267 // Duplicate packets are ignored. |
344 if (duplicate) | 268 if (duplicate) |
345 return; | 269 return; |
346 | 270 |
| 271 // Update lip-sync values upon receiving the first packet of each frame, or if |
| 272 // they have never been set yet. |
| 273 if (rtp_header.packet_id == 0 || lip_sync_reference_time_.is_null()) { |
| 274 RtpTimestamp fresh_sync_rtp; |
| 275 base::TimeTicks fresh_sync_reference; |
| 276 if (!rtcp_.GetLatestLipSyncTimes(&fresh_sync_rtp, &fresh_sync_reference)) { |
| 277 // HACK: The sender should have provided Sender Reports before the first |
| 278 // frame was sent. However, the spec does not currently require this. |
| 279 // Therefore, when the data is missing, the local clock is used to |
| 280 // generate reference timestamps. |
| 281 VLOG(2) << "Lip sync info missing. Falling-back to local clock."; |
| 282 fresh_sync_rtp = rtp_header.rtp_timestamp; |
| 283 fresh_sync_reference = now; |
| 284 } |
| 285 // |lip_sync_reference_time_| is always incremented according to the time |
| 286 // delta computed from the difference in RTP timestamps. Then, |
| 287 // |lip_sync_drift_| accounts for clock drift and also smoothes-out any |
| 288 // sudden/discontinuous shifts in the series of reference time values. |
| 289 if (lip_sync_reference_time_.is_null()) { |
| 290 lip_sync_reference_time_ = fresh_sync_reference; |
| 291 } else { |
| 292 lip_sync_reference_time_ += RtpDeltaToTimeDelta( |
| 293 static_cast<int32>(fresh_sync_rtp - lip_sync_rtp_timestamp_), |
| 294 kVideoFrequency); |
| 295 } |
| 296 lip_sync_rtp_timestamp_ = fresh_sync_rtp; |
| 297 lip_sync_drift_.Update( |
| 298 now, fresh_sync_reference - lip_sync_reference_time_); |
| 299 } |
| 300 |
347 // Video frame not complete; wait for more packets. | 301 // Video frame not complete; wait for more packets. |
348 if (!complete) | 302 if (!complete) |
349 return; | 303 return; |
350 | 304 |
351 EmitAvailableEncodedFrames(); | 305 EmitAvailableEncodedFrames(); |
352 } | 306 } |
353 | 307 |
354 // Send a cast feedback message. Actual message created in the framer (cast | 308 // Send a cast feedback message. Actual message created in the framer (cast |
355 // message builder). | 309 // message builder). |
356 void VideoReceiver::CastFeedback(const RtcpCastMessage& cast_message) { | 310 void VideoReceiver::CastFeedback(const RtcpCastMessage& cast_message) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 } | 363 } |
410 | 364 |
411 void VideoReceiver::SendNextRtcpReport() { | 365 void VideoReceiver::SendNextRtcpReport() { |
412 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 366 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
413 rtcp_.SendRtcpFromRtpReceiver(NULL, NULL); | 367 rtcp_.SendRtcpFromRtpReceiver(NULL, NULL); |
414 ScheduleNextRtcpReport(); | 368 ScheduleNextRtcpReport(); |
415 } | 369 } |
416 | 370 |
417 } // namespace cast | 371 } // namespace cast |
418 } // namespace media | 372 } // namespace media |
OLD | NEW |