2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-05-30 10:45:18 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-01-13 15:21:30 +00:00
|
|
|
#include "webrtc/modules/video_render/video_render_frames.h"
|
2012-05-30 10:45:18 +00:00
|
|
|
|
2013-08-05 16:22:53 +00:00
|
|
|
#include <assert.h>
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-07-12 08:12:08 +00:00
|
|
|
#include "webrtc/modules/interface/module_common_types.h"
|
|
|
|
|
#include "webrtc/system_wrappers/interface/tick_util.h"
|
|
|
|
|
#include "webrtc/system_wrappers/interface/trace.h"
|
2012-05-30 10:45:18 +00:00
|
|
|
|
2011-07-07 08:21:25 +00:00
|
|
|
namespace webrtc {
|
|
|
|
|
|
2014-01-13 15:21:30 +00:00
|
|
|
const uint32_t KEventMaxWaitTimeMs = 200;
|
2013-04-10 08:09:04 +00:00
|
|
|
const uint32_t kMinRenderDelayMs = 10;
|
|
|
|
|
const uint32_t kMaxRenderDelayMs= 500;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-05-30 10:45:18 +00:00
|
|
|
VideoRenderFrames::VideoRenderFrames()
|
2014-01-13 15:21:30 +00:00
|
|
|
: render_delay_ms_(10) {
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-12 12:25:16 +00:00
|
|
|
int32_t VideoRenderFrames::AddFrame(const I420VideoFrame& new_frame) {
|
2013-04-10 08:09:04 +00:00
|
|
|
const int64_t time_now = TickTime::MillisecondTimestamp();
|
2012-05-30 10:45:18 +00:00
|
|
|
|
2013-12-16 18:24:37 +00:00
|
|
|
// Drop old frames only when there are other frames in the queue, otherwise, a
|
|
|
|
|
// really slow system never renders any frames.
|
2014-01-13 15:21:30 +00:00
|
|
|
if (!incoming_frames_.empty() &&
|
2015-03-12 12:25:16 +00:00
|
|
|
new_frame.render_time_ms() + KOldRenderTimestampMS < time_now) {
|
2013-12-16 18:24:37 +00:00
|
|
|
WEBRTC_TRACE(kTraceWarning,
|
|
|
|
|
kTraceVideoRenderer,
|
|
|
|
|
-1,
|
2013-05-21 00:16:01 +00:00
|
|
|
"%s: too old frame, timestamp=%u.",
|
2013-12-16 18:24:37 +00:00
|
|
|
__FUNCTION__,
|
2015-03-12 12:25:16 +00:00
|
|
|
new_frame.timestamp());
|
2012-05-30 10:45:18 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2013-12-16 18:24:37 +00:00
|
|
|
|
2015-03-12 12:25:16 +00:00
|
|
|
if (new_frame.render_time_ms() > time_now + KFutureRenderTimestampMS) {
|
2012-05-30 10:45:18 +00:00
|
|
|
WEBRTC_TRACE(kTraceWarning, kTraceVideoRenderer, -1,
|
2013-05-21 00:16:01 +00:00
|
|
|
"%s: frame too long into the future, timestamp=%u.",
|
2015-03-12 12:25:16 +00:00
|
|
|
__FUNCTION__, new_frame.timestamp());
|
2012-05-30 10:45:18 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-12 12:25:16 +00:00
|
|
|
incoming_frames_.push_back(new_frame);
|
2014-01-13 15:21:30 +00:00
|
|
|
return static_cast<int32_t>(incoming_frames_.size());
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-12 12:25:16 +00:00
|
|
|
I420VideoFrame VideoRenderFrames::FrameToRender() {
|
|
|
|
|
I420VideoFrame render_frame;
|
|
|
|
|
// Get the newest frame that can be released for rendering.
|
|
|
|
|
while (!incoming_frames_.empty() && TimeToNextFrameRelease() <= 0) {
|
|
|
|
|
render_frame = incoming_frames_.front();
|
|
|
|
|
incoming_frames_.pop_front();
|
2012-05-30 10:45:18 +00:00
|
|
|
}
|
|
|
|
|
return render_frame;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-10 08:09:04 +00:00
|
|
|
int32_t VideoRenderFrames::ReleaseAllFrames() {
|
2014-01-13 15:21:30 +00:00
|
|
|
incoming_frames_.clear();
|
2012-05-30 10:45:18 +00:00
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-10 08:09:04 +00:00
|
|
|
uint32_t VideoRenderFrames::TimeToNextFrameRelease() {
|
2014-01-13 15:21:30 +00:00
|
|
|
if (incoming_frames_.empty()) {
|
|
|
|
|
return KEventMaxWaitTimeMs;
|
|
|
|
|
}
|
2015-03-12 12:25:16 +00:00
|
|
|
const int64_t time_to_release = incoming_frames_.front().render_time_ms() -
|
|
|
|
|
render_delay_ms_ -
|
|
|
|
|
TickTime::MillisecondTimestamp();
|
|
|
|
|
return time_to_release < 0 ? 0u : static_cast<uint32_t>(time_to_release);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-10 08:09:04 +00:00
|
|
|
int32_t VideoRenderFrames::SetRenderDelay(
|
|
|
|
|
const uint32_t render_delay) {
|
2012-09-28 11:27:35 +00:00
|
|
|
if (render_delay < kMinRenderDelayMs ||
|
|
|
|
|
render_delay > kMaxRenderDelayMs) {
|
|
|
|
|
WEBRTC_TRACE(kTraceWarning, kTraceVideoRenderer,
|
|
|
|
|
-1, "%s(%d): Invalid argument.", __FUNCTION__,
|
|
|
|
|
render_delay);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-30 10:45:18 +00:00
|
|
|
render_delay_ms_ = render_delay;
|
|
|
|
|
return 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-30 10:45:18 +00:00
|
|
|
} // namespace webrtc
|