Index: ppapi/examples/video_encode/video_encode.cc |
diff --git a/ppapi/examples/video_encode/video_encode.cc b/ppapi/examples/video_encode/video_encode.cc |
index 7a55df294cfa2e89ce2ea21f64d974761d554871..229c30ddd42f147ab2b6d450ea392bab9fc4912b 100644 |
--- a/ppapi/examples/video_encode/video_encode.cc |
+++ b/ppapi/examples/video_encode/video_encode.cc |
@@ -44,6 +44,10 @@ |
namespace { |
+double clamp(double min, double max, double value) { |
+ return std::max(std::min(value, max), min); |
+} |
+ |
// IVF container writer. It is possible to parse H264 bitstream using |
// NAL units but for VP8 we need a container to at least find encoded |
// pictures as well as the picture sizes. |
@@ -160,6 +164,7 @@ class VideoEncoderInstance : public pp::Instance { |
VideoProfileToStringMap profile_to_string_; |
bool is_encoding_; |
+ bool is_encode_ticking_; |
bool is_receiving_track_frames_; |
pp::VideoEncoder video_encoder_; |
@@ -185,6 +190,7 @@ VideoEncoderInstance::VideoEncoderInstance(PP_Instance instance, |
pp::Module* module) |
: pp::Instance(instance), |
is_encoding_(false), |
+ is_encode_ticking_(false), |
callback_factory_(this), |
#if defined(USE_VP8_INSTEAD_OF_H264) |
video_profile_(PP_VIDEOPROFILE_VP8_ANY), |
@@ -343,15 +349,28 @@ void VideoEncoderInstance::OnInitializedEncoder(int32_t result) { |
} |
void VideoEncoderInstance::ScheduleNextEncode() { |
+ // Avoid scheduling more than once at a time. |
+ if (is_encode_ticking_) |
+ return; |
+ |
PP_Time now = pp::Module::Get()->core()->GetTime(); |
+ PP_Time tick = 1.0 / 30; |
+ // If the callback was triggered late, we need to account for that |
+ // delay for the next tick. |
+ PP_Time delta = tick - clamp(0, tick, now - last_encode_tick_ - tick); |
+ |
pp::Module::Get()->core()->CallOnMainThread( |
- std::min(std::max(now - last_encode_tick_, 0.0), 1000.0 / 30), |
+ delta * 1000, |
callback_factory_.NewCallback(&VideoEncoderInstance::GetEncoderFrameTick), |
0); |
+ |
last_encode_tick_ = now; |
+ is_encode_ticking_ = true; |
} |
void VideoEncoderInstance::GetEncoderFrameTick(int32_t result) { |
+ is_encode_ticking_ = false; |
+ |
if (is_encoding_) { |
if (!current_track_frame_.is_null()) { |
pp::VideoFrame frame = current_track_frame_; |