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 "content/renderer/media/video_track_adapter.h" | 5 #include "content/renderer/media/video_track_adapter.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 double min_aspect_ratio, | 84 double min_aspect_ratio, |
85 double max_aspect_ratio, | 85 double max_aspect_ratio, |
86 double max_frame_rate) const; | 86 double max_frame_rate) const; |
87 | 87 |
88 bool IsEmpty() const; | 88 bool IsEmpty() const; |
89 | 89 |
90 private: | 90 private: |
91 virtual ~VideoFrameResolutionAdapter(); | 91 virtual ~VideoFrameResolutionAdapter(); |
92 friend class base::RefCountedThreadSafe<VideoFrameResolutionAdapter>; | 92 friend class base::RefCountedThreadSafe<VideoFrameResolutionAdapter>; |
93 | 93 |
94 virtual void DoDeliverFrame( | 94 void DoDeliverFrame(const scoped_refptr<media::VideoFrame>& frame, |
wolenetz
2015/03/02 23:51:09
nit: ditto update CL description to include this,
mcasas
2015/03/03 15:40:52
Acknowledged.
| |
95 const scoped_refptr<media::VideoFrame>& frame, | 95 const media::VideoCaptureFormat& format, |
96 const media::VideoCaptureFormat& format, | 96 const base::TimeTicks& estimated_capture_time); |
97 const base::TimeTicks& estimated_capture_time); | |
98 | 97 |
99 // Returns |true| if the input frame rate is higher that the requested max | 98 // Returns |true| if the input frame rate is higher that the requested max |
100 // frame rate and |frame| should be dropped. | 99 // frame rate and |frame| should be dropped. |
101 bool MaybeDropFrame(const scoped_refptr<media::VideoFrame>& frame, | 100 bool MaybeDropFrame(const scoped_refptr<media::VideoFrame>& frame, |
102 float source_frame_rate); | 101 float source_frame_rate); |
103 | 102 |
104 // Bound to the IO-thread. | 103 // Bound to the IO-thread. |
105 base::ThreadChecker io_thread_checker_; | 104 base::ThreadChecker io_thread_checker_; |
106 | 105 |
107 // The task runner where we will release VideoCaptureDeliverFrameCB | 106 // The task runner where we will release VideoCaptureDeliverFrameCB |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 } | 249 } |
251 | 250 |
252 bool VideoTrackAdapter::VideoFrameResolutionAdapter::MaybeDropFrame( | 251 bool VideoTrackAdapter::VideoFrameResolutionAdapter::MaybeDropFrame( |
253 const scoped_refptr<media::VideoFrame>& frame, | 252 const scoped_refptr<media::VideoFrame>& frame, |
254 float source_frame_rate) { | 253 float source_frame_rate) { |
255 DCHECK(io_thread_checker_.CalledOnValidThread()); | 254 DCHECK(io_thread_checker_.CalledOnValidThread()); |
256 | 255 |
257 // Do not drop frames if max frame rate hasn't been specified or the source | 256 // Do not drop frames if max frame rate hasn't been specified or the source |
258 // frame rate is known and is lower than max. | 257 // frame rate is known and is lower than max. |
259 if (max_frame_rate_ == 0.0f || | 258 if (max_frame_rate_ == 0.0f || |
260 (source_frame_rate > 0 && | 259 (source_frame_rate > 0 && source_frame_rate <= max_frame_rate_)) { |
261 source_frame_rate <= max_frame_rate_)) { | |
262 return false; | 260 return false; |
263 } | 261 } |
264 | 262 |
265 const double delta_ms = | 263 const double delta_ms = |
266 (frame->timestamp() - last_time_stamp_).InMillisecondsF(); | 264 (frame->timestamp() - last_time_stamp_).InMillisecondsF(); |
267 | 265 |
268 // Check if the time since the last frame is completely off. | 266 // Check if the time since the last frame is completely off. |
269 if (delta_ms < 0 || delta_ms > kMaxTimeInMsBetweenFrames) { | 267 if (delta_ms < 0 || delta_ms > kMaxTimeInMsBetweenFrames) { |
270 // Reset |last_time_stamp_| and fps calculation. | 268 // Reset |last_time_stamp_| and fps calculation. |
271 last_time_stamp_ = frame->timestamp(); | 269 last_time_stamp_ = frame->timestamp(); |
(...skipping 29 matching lines...) Expand all Loading... | |
301 keep_frame_counter_ += max_frame_rate_ / frame_rate_; | 299 keep_frame_counter_ += max_frame_rate_ / frame_rate_; |
302 if (keep_frame_counter_ >= 1) { | 300 if (keep_frame_counter_ >= 1) { |
303 keep_frame_counter_ -= 1; | 301 keep_frame_counter_ -= 1; |
304 // Keep the frame. | 302 // Keep the frame. |
305 return false; | 303 return false; |
306 } | 304 } |
307 DVLOG(3) << "Drop frame. Input frame_rate_ " << frame_rate_ << "."; | 305 DVLOG(3) << "Drop frame. Input frame_rate_ " << frame_rate_ << "."; |
308 return true; | 306 return true; |
309 } | 307 } |
310 | 308 |
311 void VideoTrackAdapter:: | 309 void VideoTrackAdapter::VideoFrameResolutionAdapter::DoDeliverFrame( |
312 VideoFrameResolutionAdapter::DoDeliverFrame( | |
313 const scoped_refptr<media::VideoFrame>& frame, | 310 const scoped_refptr<media::VideoFrame>& frame, |
314 const media::VideoCaptureFormat& format, | 311 const media::VideoCaptureFormat& format, |
315 const base::TimeTicks& estimated_capture_time) { | 312 const base::TimeTicks& estimated_capture_time) { |
316 DCHECK(io_thread_checker_.CalledOnValidThread()); | 313 DCHECK(io_thread_checker_.CalledOnValidThread()); |
317 for (std::vector<VideoIdCallbackPair>::const_iterator it = callbacks_.begin(); | 314 for (const auto& it : callbacks_) |
318 it != callbacks_.end(); ++it) { | 315 it.second.Run(frame, format, estimated_capture_time); |
319 it->second.Run(frame, format, estimated_capture_time); | |
320 } | |
321 } | 316 } |
322 | 317 |
323 void VideoTrackAdapter::VideoFrameResolutionAdapter::AddCallback( | 318 void VideoTrackAdapter::VideoFrameResolutionAdapter::AddCallback( |
324 const MediaStreamVideoTrack* track, | 319 const MediaStreamVideoTrack* track, |
325 const VideoCaptureDeliverFrameCB& callback) { | 320 const VideoCaptureDeliverFrameCB& callback) { |
326 DCHECK(io_thread_checker_.CalledOnValidThread()); | 321 DCHECK(io_thread_checker_.CalledOnValidThread()); |
327 callbacks_.push_back(std::make_pair(track, callback)); | 322 callbacks_.push_back(std::make_pair(track, callback)); |
328 } | 323 } |
329 | 324 |
330 void VideoTrackAdapter::VideoFrameResolutionAdapter::RemoveCallback( | 325 void VideoTrackAdapter::VideoFrameResolutionAdapter::RemoveCallback( |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 muted_state_(false), | 368 muted_state_(false), |
374 frame_counter_(0), | 369 frame_counter_(0), |
375 source_frame_rate_(0.0f) { | 370 source_frame_rate_(0.0f) { |
376 DCHECK(io_message_loop_.get()); | 371 DCHECK(io_message_loop_.get()); |
377 } | 372 } |
378 | 373 |
379 VideoTrackAdapter::~VideoTrackAdapter() { | 374 VideoTrackAdapter::~VideoTrackAdapter() { |
380 DCHECK(adapters_.empty()); | 375 DCHECK(adapters_.empty()); |
381 } | 376 } |
382 | 377 |
383 void VideoTrackAdapter::AddTrack( | 378 void VideoTrackAdapter::AddTrack(const MediaStreamVideoTrack* track, |
384 const MediaStreamVideoTrack* track, | 379 VideoCaptureDeliverFrameCB frame_callback, |
385 VideoCaptureDeliverFrameCB frame_callback, | 380 int max_width, |
386 int max_width, | 381 int max_height, |
387 int max_height, | 382 double min_aspect_ratio, |
388 double min_aspect_ratio, | 383 double max_aspect_ratio, |
389 double max_aspect_ratio, | 384 double max_frame_rate) { |
390 double max_frame_rate) { | |
391 DCHECK(thread_checker_.CalledOnValidThread()); | 385 DCHECK(thread_checker_.CalledOnValidThread()); |
392 | 386 |
393 io_message_loop_->PostTask( | 387 io_message_loop_->PostTask( |
394 FROM_HERE, | 388 FROM_HERE, |
395 base::Bind(&VideoTrackAdapter::AddTrackOnIO, | 389 base::Bind(&VideoTrackAdapter::AddTrackOnIO, |
396 this, track, frame_callback, gfx::Size(max_width, max_height), | 390 this, track, frame_callback, gfx::Size(max_width, max_height), |
397 min_aspect_ratio, max_aspect_ratio, max_frame_rate)); | 391 min_aspect_ratio, max_aspect_ratio, max_frame_rate)); |
398 } | 392 } |
399 | 393 |
400 void VideoTrackAdapter::AddTrackOnIO( | 394 void VideoTrackAdapter::AddTrackOnIO(const MediaStreamVideoTrack* track, |
401 const MediaStreamVideoTrack* track, | 395 VideoCaptureDeliverFrameCB frame_callback, |
402 VideoCaptureDeliverFrameCB frame_callback, | 396 const gfx::Size& max_frame_size, |
403 const gfx::Size& max_frame_size, | 397 double min_aspect_ratio, |
404 double min_aspect_ratio, | 398 double max_aspect_ratio, |
405 double max_aspect_ratio, | 399 double max_frame_rate) { |
406 double max_frame_rate) { | |
407 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 400 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
408 scoped_refptr<VideoFrameResolutionAdapter> adapter; | 401 scoped_refptr<VideoFrameResolutionAdapter> adapter; |
409 for (FrameAdapters::const_iterator it = adapters_.begin(); | 402 for (const auto& it : adapters_) { |
410 it != adapters_.end(); ++it) { | 403 if (it->ConstraintsMatch(max_frame_size, min_aspect_ratio, |
411 if ((*it)->ConstraintsMatch(max_frame_size, min_aspect_ratio, | 404 max_aspect_ratio, max_frame_rate)) { |
412 max_aspect_ratio, max_frame_rate)) { | 405 adapter = it.get(); |
413 adapter = it->get(); | |
414 break; | 406 break; |
415 } | 407 } |
416 } | 408 } |
417 if (!adapter.get()) { | 409 if (!adapter.get()) { |
418 adapter = new VideoFrameResolutionAdapter(renderer_task_runner_, | 410 adapter = new VideoFrameResolutionAdapter(renderer_task_runner_, |
419 max_frame_size, | 411 max_frame_size, |
420 min_aspect_ratio, | 412 min_aspect_ratio, |
421 max_aspect_ratio, | 413 max_aspect_ratio, |
422 max_frame_rate); | 414 max_frame_rate); |
423 adapters_.push_back(adapter); | 415 adapters_.push_back(adapter); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 } | 484 } |
493 } | 485 } |
494 | 486 |
495 void VideoTrackAdapter::DeliverFrameOnIO( | 487 void VideoTrackAdapter::DeliverFrameOnIO( |
496 const scoped_refptr<media::VideoFrame>& frame, | 488 const scoped_refptr<media::VideoFrame>& frame, |
497 const media::VideoCaptureFormat& format, | 489 const media::VideoCaptureFormat& format, |
498 const base::TimeTicks& estimated_capture_time) { | 490 const base::TimeTicks& estimated_capture_time) { |
499 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 491 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
500 TRACE_EVENT0("video", "VideoTrackAdapter::DeliverFrameOnIO"); | 492 TRACE_EVENT0("video", "VideoTrackAdapter::DeliverFrameOnIO"); |
501 ++frame_counter_; | 493 ++frame_counter_; |
502 for (FrameAdapters::iterator it = adapters_.begin(); | 494 for (const auto& it : adapters_) |
503 it != adapters_.end(); ++it) { | 495 it->DeliverFrame(frame, format, estimated_capture_time); |
504 (*it)->DeliverFrame(frame, format, estimated_capture_time); | |
505 } | |
506 } | 496 } |
507 | 497 |
508 void VideoTrackAdapter::CheckFramesReceivedOnIO( | 498 void VideoTrackAdapter::CheckFramesReceivedOnIO( |
509 const OnMutedCallback& set_muted_state_callback, | 499 const OnMutedCallback& set_muted_state_callback, |
510 uint64 old_frame_counter_snapshot) { | 500 uint64 old_frame_counter_snapshot) { |
511 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 501 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
512 | 502 |
513 if (!monitoring_frame_rate_) | 503 if (!monitoring_frame_rate_) |
514 return; | 504 return; |
515 | 505 |
516 DVLOG_IF(1, old_frame_counter_snapshot == frame_counter_) | 506 DVLOG_IF(1, old_frame_counter_snapshot == frame_counter_) |
517 << "No frames have passed, setting source as Muted."; | 507 << "No frames have passed, setting source as Muted."; |
518 | 508 |
519 bool muted_state = old_frame_counter_snapshot == frame_counter_; | 509 bool muted_state = old_frame_counter_snapshot == frame_counter_; |
520 if (muted_state_ != muted_state) { | 510 if (muted_state_ != muted_state) { |
521 set_muted_state_callback.Run(muted_state); | 511 set_muted_state_callback.Run(muted_state); |
522 muted_state_ = muted_state; | 512 muted_state_ = muted_state; |
523 } | 513 } |
524 | 514 |
525 io_message_loop_->PostDelayedTask(FROM_HERE, | 515 io_message_loop_->PostDelayedTask(FROM_HERE, |
526 base::Bind(&VideoTrackAdapter::CheckFramesReceivedOnIO, this, | 516 base::Bind(&VideoTrackAdapter::CheckFramesReceivedOnIO, this, |
527 set_muted_state_callback, frame_counter_), | 517 set_muted_state_callback, frame_counter_), |
528 base::TimeDelta::FromSecondsD(kNormalFrameTimeoutInFrameIntervals / | 518 base::TimeDelta::FromSecondsD(kNormalFrameTimeoutInFrameIntervals / |
529 source_frame_rate_)); | 519 source_frame_rate_)); |
530 } | 520 } |
531 | 521 |
532 } // namespace content | 522 } // namespace content |
OLD | NEW |