| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/receiver/frame_receiver.h" | 5 #include "media/cast/receiver/frame_receiver.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/big_endian.h" | 10 #include "base/big_endian.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 | 106 |
| 107 if (!reports_are_scheduled_) { | 107 if (!reports_are_scheduled_) { |
| 108 ScheduleNextRtcpReport(); | 108 ScheduleNextRtcpReport(); |
| 109 ScheduleNextCastMessage(); | 109 ScheduleNextCastMessage(); |
| 110 reports_are_scheduled_ = true; | 110 reports_are_scheduled_ = true; |
| 111 } | 111 } |
| 112 | 112 |
| 113 return true; | 113 return true; |
| 114 } | 114 } |
| 115 | 115 |
| 116 base::WeakPtr<FrameReceiver> FrameReceiver::AsWeakPtr() { |
| 117 return weak_factory_.GetWeakPtr(); |
| 118 } |
| 119 |
| 116 void FrameReceiver::ProcessParsedPacket(const RtpCastHeader& rtp_header, | 120 void FrameReceiver::ProcessParsedPacket(const RtpCastHeader& rtp_header, |
| 117 const uint8_t* payload_data, | 121 const uint8_t* payload_data, |
| 118 size_t payload_size) { | 122 size_t payload_size) { |
| 119 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 123 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 120 | 124 |
| 121 const base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | 125 const base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
| 122 | 126 |
| 123 frame_id_to_rtp_timestamp_[rtp_header.frame_id.lower_8_bits()] = | 127 frame_id_to_rtp_timestamp_[rtp_header.frame_id.lower_8_bits()] = |
| 124 rtp_header.rtp_timestamp; | 128 rtp_header.rtp_timestamp; |
| 125 | 129 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 if (!is_consecutively_next_frame) { | 239 if (!is_consecutively_next_frame) { |
| 236 // This assumes that decoding takes as long as playing, which might | 240 // This assumes that decoding takes as long as playing, which might |
| 237 // not be true. | 241 // not be true. |
| 238 const base::TimeTicks earliest_possible_end_time_of_missing_frame = | 242 const base::TimeTicks earliest_possible_end_time_of_missing_frame = |
| 239 now + expected_frame_duration_ * 2; | 243 now + expected_frame_duration_ * 2; |
| 240 if (earliest_possible_end_time_of_missing_frame < playout_time) { | 244 if (earliest_possible_end_time_of_missing_frame < playout_time) { |
| 241 VLOG(1) << "Wait for next consecutive frame instead of skipping."; | 245 VLOG(1) << "Wait for next consecutive frame instead of skipping."; |
| 242 if (!is_waiting_for_consecutive_frame_) { | 246 if (!is_waiting_for_consecutive_frame_) { |
| 243 is_waiting_for_consecutive_frame_ = true; | 247 is_waiting_for_consecutive_frame_ = true; |
| 244 cast_environment_->PostDelayedTask( | 248 cast_environment_->PostDelayedTask( |
| 245 CastEnvironment::MAIN, | 249 CastEnvironment::MAIN, FROM_HERE, |
| 246 FROM_HERE, | |
| 247 base::Bind(&FrameReceiver::EmitAvailableEncodedFramesAfterWaiting, | 250 base::Bind(&FrameReceiver::EmitAvailableEncodedFramesAfterWaiting, |
| 248 weak_factory_.GetWeakPtr()), | 251 AsWeakPtr()), |
| 249 playout_time - now); | 252 playout_time - now); |
| 250 } | 253 } |
| 251 return; | 254 return; |
| 252 } | 255 } |
| 253 } | 256 } |
| 254 | 257 |
| 255 // At this point, we have the complete next frame, or a decodable | 258 // At this point, we have the complete next frame, or a decodable |
| 256 // frame from somewhere later in the stream, AND we have given up | 259 // frame from somewhere later in the stream, AND we have given up |
| 257 // on waiting for any frames in between, so now we can ACK the frame. | 260 // on waiting for any frames in between, so now we can ACK the frame. |
| 258 framer_.AckFrame(encoded_frame->frame_id); | 261 framer_.AckFrame(encoded_frame->frame_id); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 270 encoded_frame->data.swap(decrypted_data); | 273 encoded_frame->data.swap(decrypted_data); |
| 271 } | 274 } |
| 272 | 275 |
| 273 // At this point, we have a decrypted EncodedFrame ready to be emitted. | 276 // At this point, we have a decrypted EncodedFrame ready to be emitted. |
| 274 encoded_frame->reference_time = playout_time; | 277 encoded_frame->reference_time = playout_time; |
| 275 framer_.ReleaseFrame(encoded_frame->frame_id); | 278 framer_.ReleaseFrame(encoded_frame->frame_id); |
| 276 if (encoded_frame->new_playout_delay_ms) { | 279 if (encoded_frame->new_playout_delay_ms) { |
| 277 target_playout_delay_ = base::TimeDelta::FromMilliseconds( | 280 target_playout_delay_ = base::TimeDelta::FromMilliseconds( |
| 278 encoded_frame->new_playout_delay_ms); | 281 encoded_frame->new_playout_delay_ms); |
| 279 } | 282 } |
| 280 cast_environment_->PostTask(CastEnvironment::MAIN, | 283 cast_environment_->PostTask( |
| 281 FROM_HERE, | 284 CastEnvironment::MAIN, FROM_HERE, |
| 282 base::Bind(&FrameReceiver::EmitOneFrame, | 285 base::Bind(&FrameReceiver::EmitOneFrame, AsWeakPtr(), |
| 283 weak_factory_.GetWeakPtr(), | 286 frame_request_queue_.front(), base::Passed(&encoded_frame))); |
| 284 frame_request_queue_.front(), | |
| 285 base::Passed(&encoded_frame))); | |
| 286 frame_request_queue_.pop_front(); | 287 frame_request_queue_.pop_front(); |
| 287 } | 288 } |
| 288 } | 289 } |
| 289 | 290 |
| 290 void FrameReceiver::EmitAvailableEncodedFramesAfterWaiting() { | 291 void FrameReceiver::EmitAvailableEncodedFramesAfterWaiting() { |
| 291 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 292 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 292 DCHECK(is_waiting_for_consecutive_frame_); | 293 DCHECK(is_waiting_for_consecutive_frame_); |
| 293 is_waiting_for_consecutive_frame_ = false; | 294 is_waiting_for_consecutive_frame_ = false; |
| 294 EmitAvailableEncodedFrames(); | 295 EmitAvailableEncodedFrames(); |
| 295 } | 296 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 316 | 317 |
| 317 void FrameReceiver::ScheduleNextCastMessage() { | 318 void FrameReceiver::ScheduleNextCastMessage() { |
| 318 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 319 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 319 base::TimeTicks send_time; | 320 base::TimeTicks send_time; |
| 320 framer_.TimeToSendNextCastMessage(&send_time); | 321 framer_.TimeToSendNextCastMessage(&send_time); |
| 321 base::TimeDelta time_to_send = | 322 base::TimeDelta time_to_send = |
| 322 send_time - cast_environment_->Clock()->NowTicks(); | 323 send_time - cast_environment_->Clock()->NowTicks(); |
| 323 time_to_send = std::max( | 324 time_to_send = std::max( |
| 324 time_to_send, base::TimeDelta::FromMilliseconds(kMinSchedulingDelayMs)); | 325 time_to_send, base::TimeDelta::FromMilliseconds(kMinSchedulingDelayMs)); |
| 325 cast_environment_->PostDelayedTask( | 326 cast_environment_->PostDelayedTask( |
| 326 CastEnvironment::MAIN, | 327 CastEnvironment::MAIN, FROM_HERE, |
| 327 FROM_HERE, | 328 base::Bind(&FrameReceiver::SendNextCastMessage, AsWeakPtr()), |
| 328 base::Bind(&FrameReceiver::SendNextCastMessage, | |
| 329 weak_factory_.GetWeakPtr()), | |
| 330 time_to_send); | 329 time_to_send); |
| 331 } | 330 } |
| 332 | 331 |
| 333 void FrameReceiver::SendNextCastMessage() { | 332 void FrameReceiver::SendNextCastMessage() { |
| 334 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 333 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 335 framer_.SendCastMessage(); // Will only send a message if it is time. | 334 framer_.SendCastMessage(); // Will only send a message if it is time. |
| 336 ScheduleNextCastMessage(); | 335 ScheduleNextCastMessage(); |
| 337 } | 336 } |
| 338 | 337 |
| 339 void FrameReceiver::ScheduleNextRtcpReport() { | 338 void FrameReceiver::ScheduleNextRtcpReport() { |
| 340 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 339 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 341 | 340 |
| 342 cast_environment_->PostDelayedTask( | 341 cast_environment_->PostDelayedTask( |
| 343 CastEnvironment::MAIN, FROM_HERE, | 342 CastEnvironment::MAIN, FROM_HERE, |
| 344 base::Bind(&FrameReceiver::SendNextRtcpReport, | 343 base::Bind(&FrameReceiver::SendNextRtcpReport, AsWeakPtr()), |
| 345 weak_factory_.GetWeakPtr()), | |
| 346 base::TimeDelta::FromMilliseconds(kRtcpReportIntervalMs)); | 344 base::TimeDelta::FromMilliseconds(kRtcpReportIntervalMs)); |
| 347 } | 345 } |
| 348 | 346 |
| 349 void FrameReceiver::SendNextRtcpReport() { | 347 void FrameReceiver::SendNextRtcpReport() { |
| 350 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 348 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 351 const base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | 349 const base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
| 352 RtpReceiverStatistics stats = stats_.GetStatistics(); | 350 RtpReceiverStatistics stats = stats_.GetStatistics(); |
| 353 SendRtcpReport(rtcp_.local_ssrc(), rtcp_.remote_ssrc(), | 351 SendRtcpReport(rtcp_.local_ssrc(), rtcp_.remote_ssrc(), |
| 354 CreateRtcpTimeData(now), nullptr, nullptr, base::TimeDelta(), | 352 CreateRtcpTimeData(now), nullptr, nullptr, base::TimeDelta(), |
| 355 nullptr, &stats); | 353 nullptr, &stats); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 transport_->AddCastFeedback(*cast_message, target_delay); | 394 transport_->AddCastFeedback(*cast_message, target_delay); |
| 397 if (pli_message) | 395 if (pli_message) |
| 398 transport_->AddPli(*pli_message); | 396 transport_->AddPli(*pli_message); |
| 399 if (rtcp_events) | 397 if (rtcp_events) |
| 400 transport_->AddRtcpEvents(*rtcp_events); | 398 transport_->AddRtcpEvents(*rtcp_events); |
| 401 transport_->SendRtcpFromRtpReceiver(); | 399 transport_->SendRtcpFromRtpReceiver(); |
| 402 } | 400 } |
| 403 | 401 |
| 404 } // namespace cast | 402 } // namespace cast |
| 405 } // namespace media | 403 } // namespace media |
| OLD | NEW |