| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "media/base/video_util.h" | 15 #include "media/base/video_util.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // Amount of frame intervals to wait before considering the source as muted, for | 21 // Amount of frame intervals to wait before considering the source as muted, for |
| 22 // the first frame and under normal conditions, respectively. First frame might | 22 // the first frame and under normal conditions, respectively. First frame might |
| 23 // take longer to arrive due to source startup. | 23 // take longer to arrive due to source startup. |
| 24 const float kFirstFrameTimeoutInFrameIntervals = 100.0f; | 24 const float kFirstFrameTimeoutInFrameIntervals = 100.0f; |
| 25 const float kNormalFrameTimeoutInFrameIntervals = 25.0f; | 25 const float kNormalFrameTimeoutInFrameIntervals = 25.0f; |
| 26 | 26 |
| 27 // Min delta time between two frames allowed without being dropped if a max |
| 28 // frame rate is specified. |
| 29 const int kMinTimeInMsBetweenFrames = 5; |
| 30 |
| 27 // Empty method used for keeping a reference to the original media::VideoFrame | 31 // Empty method used for keeping a reference to the original media::VideoFrame |
| 28 // in VideoFrameResolutionAdapter::DeliverFrame if cropping is needed. | 32 // in VideoFrameResolutionAdapter::DeliverFrame if cropping is needed. |
| 29 // The reference to |frame| is kept in the closure that calls this method. | 33 // The reference to |frame| is kept in the closure that calls this method. |
| 30 void ReleaseOriginalFrame( | 34 void ReleaseOriginalFrame( |
| 31 const scoped_refptr<media::VideoFrame>& frame) { | 35 const scoped_refptr<media::VideoFrame>& frame) { |
| 32 } | 36 } |
| 33 | 37 |
| 34 void ResetCallbackOnMainRenderThread( | 38 void ResetCallbackOnMainRenderThread( |
| 35 scoped_ptr<VideoCaptureDeliverFrameCB> callback) { | 39 scoped_ptr<VideoCaptureDeliverFrameCB> callback) { |
| 36 // |callback| will be deleted when this exits. | 40 // |callback| will be deleted when this exits. |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 } | 222 } |
| 219 DoDeliverFrame(video_frame, format, estimated_capture_time); | 223 DoDeliverFrame(video_frame, format, estimated_capture_time); |
| 220 } | 224 } |
| 221 | 225 |
| 222 bool VideoTrackAdapter::VideoFrameResolutionAdapter::MaybeDropFrame( | 226 bool VideoTrackAdapter::VideoFrameResolutionAdapter::MaybeDropFrame( |
| 223 const scoped_refptr<media::VideoFrame>& frame) { | 227 const scoped_refptr<media::VideoFrame>& frame) { |
| 224 if (max_frame_rate_ == 0.0f) | 228 if (max_frame_rate_ == 0.0f) |
| 225 return false; | 229 return false; |
| 226 | 230 |
| 227 base::TimeDelta delta = frame->timestamp() - last_time_stamp_; | 231 base::TimeDelta delta = frame->timestamp() - last_time_stamp_; |
| 232 if (delta.InMilliseconds() < kMinTimeInMsBetweenFrames) { |
| 233 // We have seen video frames being delivered from camera devices back to |
| 234 // back. The simple AR filter for frame rate calculation is too short to |
| 235 // handle that. http://crbug/394315 |
| 236 // TODO(perkj): Can we come up with a way to fix the times stamps and the |
| 237 // timing when frames are delivered so all frames can be used? |
| 238 // The time stamps are generated by Chrome and not the actual device. |
| 239 // Most likely the back to back problem is caused by software and not the |
| 240 // actual camera. |
| 241 DVLOG(3) << "Drop frame since delta time since previous frame is " |
| 242 << delta.InMilliseconds() << "ms."; |
| 243 return true; |
| 244 } |
| 228 last_time_stamp_ = frame->timestamp(); | 245 last_time_stamp_ = frame->timestamp(); |
| 229 if (delta.ToInternalValue() == 0 || delta == last_time_stamp_) | 246 if (delta == last_time_stamp_) // First received frame. |
| 230 return false; | 247 return false; |
| 231 // Calculate the moving average frame rate. Use a simple filter with 0.1 | 248 // Calculate the frame rate using a simple AR filter. |
| 232 // weight of the current sample. | 249 // Use a simple filter with 0.1 weight of the current sample. |
| 233 frame_rate_ = 100 / delta.InMillisecondsF() + 0.9 * frame_rate_; | 250 frame_rate_ = 100 / delta.InMillisecondsF() + 0.9 * frame_rate_; |
| 234 | 251 |
| 235 // Prefer to not drop frames. | 252 // Prefer to not drop frames. |
| 236 if (max_frame_rate_ + 0.5f > frame_rate_) | 253 if (max_frame_rate_ + 0.5f > frame_rate_) |
| 237 return false; // Keep this frame. | 254 return false; // Keep this frame. |
| 238 | 255 |
| 239 // The input frame rate is higher than requested. | 256 // The input frame rate is higher than requested. |
| 240 // Decide if we should keep this frame or drop it. | 257 // Decide if we should keep this frame or drop it. |
| 241 keep_frame_counter_ += max_frame_rate_ / frame_rate_; | 258 keep_frame_counter_ += max_frame_rate_ / frame_rate_; |
| 242 if (keep_frame_counter_ >= 1) { | 259 if (keep_frame_counter_ >= 1) { |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 // Rearm the monitoring while there are active Tracks, i.e. as long as the | 453 // Rearm the monitoring while there are active Tracks, i.e. as long as the |
| 437 // owner MediaStreamSource is active. | 454 // owner MediaStreamSource is active. |
| 438 io_message_loop_->PostDelayedTask(FROM_HERE, | 455 io_message_loop_->PostDelayedTask(FROM_HERE, |
| 439 base::Bind(&VideoTrackAdapter::CheckFramesReceivedOnIO, this, | 456 base::Bind(&VideoTrackAdapter::CheckFramesReceivedOnIO, this, |
| 440 set_muted_state_callback, frame_counter_), | 457 set_muted_state_callback, frame_counter_), |
| 441 base::TimeDelta::FromSecondsD(kNormalFrameTimeoutInFrameIntervals / | 458 base::TimeDelta::FromSecondsD(kNormalFrameTimeoutInFrameIntervals / |
| 442 source_frame_rate_)); | 459 source_frame_rate_)); |
| 443 } | 460 } |
| 444 | 461 |
| 445 } // namespace content | 462 } // namespace content |
| OLD | NEW |