FrameBuffer2 now has Start/Stop methods.
The Stop method is used to signal any thread that is waiting in the NextFrame function and will cause it to return immediately. BUG=webrtc:5514 R=pbos@webrtc.org Review URL: https://codereview.webrtc.org/2105323002 . Cr-Commit-Position: refs/heads/master@{#13349}
This commit is contained in:
@ -47,42 +47,50 @@ FrameBuffer::FrameBuffer(Clock* clock,
|
|||||||
frame_inserted_event_(false, false),
|
frame_inserted_event_(false, false),
|
||||||
jitter_estimator_(jitter_estimator),
|
jitter_estimator_(jitter_estimator),
|
||||||
timing_(timing),
|
timing_(timing),
|
||||||
newest_picture_id_(-1) {}
|
newest_picture_id_(-1),
|
||||||
|
stopped_(false) {}
|
||||||
|
|
||||||
std::unique_ptr<FrameObject> FrameBuffer::NextFrame(int64_t max_wait_time_ms) {
|
std::unique_ptr<FrameObject> FrameBuffer::NextFrame(int64_t max_wait_time_ms) {
|
||||||
int64_t latest_return_time = clock_->TimeInMilliseconds() + max_wait_time_ms;
|
int64_t latest_return_time = clock_->TimeInMilliseconds() + max_wait_time_ms;
|
||||||
|
int64_t now = clock_->TimeInMilliseconds();
|
||||||
|
int64_t wait_ms = max_wait_time_ms;
|
||||||
while (true) {
|
while (true) {
|
||||||
int64_t now = clock_->TimeInMilliseconds();
|
std::map<FrameKey, std::unique_ptr<FrameObject>, FrameComp>::iterator
|
||||||
int64_t wait_ms = max_wait_time_ms;
|
next_frame;
|
||||||
|
{
|
||||||
|
rtc::CritScope lock(&crit_);
|
||||||
|
frame_inserted_event_.Reset();
|
||||||
|
if (stopped_)
|
||||||
|
return std::unique_ptr<FrameObject>();
|
||||||
|
|
||||||
crit_.Enter();
|
now = clock_->TimeInMilliseconds();
|
||||||
frame_inserted_event_.Reset();
|
wait_ms = max_wait_time_ms;
|
||||||
auto next_frame = frames_.end();
|
next_frame = frames_.end();
|
||||||
for (auto frame_it = frames_.begin(); frame_it != frames_.end();
|
for (auto frame_it = frames_.begin(); frame_it != frames_.end();
|
||||||
++frame_it) {
|
++frame_it) {
|
||||||
const FrameObject& frame = *frame_it->second;
|
const FrameObject& frame = *frame_it->second;
|
||||||
if (IsContinuous(frame)) {
|
if (IsContinuous(frame)) {
|
||||||
next_frame = frame_it;
|
next_frame = frame_it;
|
||||||
int64_t render_time = timing_->RenderTimeMs(frame.timestamp, now);
|
int64_t render_time = timing_->RenderTimeMs(frame.timestamp, now);
|
||||||
wait_ms = timing_->MaxWaitingTime(render_time, now);
|
wait_ms = timing_->MaxWaitingTime(render_time, now);
|
||||||
|
|
||||||
// This will cause the frame buffer to prefer high framerate rather
|
// This will cause the frame buffer to prefer high framerate rather
|
||||||
// than high resolution in the case of the decoder not decoding fast
|
// than high resolution in the case of the decoder not decoding fast
|
||||||
// enough and the stream has multiple spatial and temporal layers.
|
// enough and the stream has multiple spatial and temporal layers.
|
||||||
if (wait_ms == 0)
|
if (wait_ms == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
crit_.Leave();
|
|
||||||
|
|
||||||
// If the timout occures, return. Otherwise a new frame has been inserted
|
// If the timout occures, return. Otherwise a new frame has been inserted
|
||||||
// and the best frame to decode next will be selected again.
|
// and the best frame to decode next will be selected again.
|
||||||
wait_ms = std::min<int64_t>(wait_ms, latest_return_time - now);
|
wait_ms = std::min<int64_t>(wait_ms, latest_return_time - now);
|
||||||
wait_ms = std::max<int64_t>(wait_ms, 0);
|
wait_ms = std::max<int64_t>(wait_ms, 0);
|
||||||
if (!frame_inserted_event_.Wait(wait_ms)) {
|
if (!frame_inserted_event_.Wait(wait_ms)) {
|
||||||
crit_.Enter();
|
rtc::CritScope lock(&crit_);
|
||||||
if (next_frame != frames_.end()) {
|
if (next_frame != frames_.end()) {
|
||||||
// TODO(philipel): update jitter estimator with correct values.
|
// TODO(philipel): update jitter estimator with correct values.
|
||||||
jitter_estimator_->UpdateEstimate(100, 100);
|
jitter_estimator_->UpdateEstimate(100, 100);
|
||||||
@ -90,16 +98,25 @@ std::unique_ptr<FrameObject> FrameBuffer::NextFrame(int64_t max_wait_time_ms) {
|
|||||||
decoded_frames_.insert(next_frame->first);
|
decoded_frames_.insert(next_frame->first);
|
||||||
std::unique_ptr<FrameObject> frame = std::move(next_frame->second);
|
std::unique_ptr<FrameObject> frame = std::move(next_frame->second);
|
||||||
frames_.erase(frames_.begin(), ++next_frame);
|
frames_.erase(frames_.begin(), ++next_frame);
|
||||||
crit_.Leave();
|
|
||||||
return frame;
|
return frame;
|
||||||
} else {
|
} else {
|
||||||
crit_.Leave();
|
|
||||||
return std::unique_ptr<FrameObject>();
|
return std::unique_ptr<FrameObject>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FrameBuffer::Start() {
|
||||||
|
rtc::CritScope lock(&crit_);
|
||||||
|
stopped_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FrameBuffer::Stop() {
|
||||||
|
rtc::CritScope lock(&crit_);
|
||||||
|
stopped_ = true;
|
||||||
|
frame_inserted_event_.Set();
|
||||||
|
}
|
||||||
|
|
||||||
void FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
|
void FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
|
||||||
rtc::CritScope lock(&crit_);
|
rtc::CritScope lock(&crit_);
|
||||||
if (newest_picture_id_ == -1)
|
if (newest_picture_id_ == -1)
|
||||||
|
@ -46,6 +46,14 @@ class FrameBuffer {
|
|||||||
// unique ptr if there is no available frame for decoding.
|
// unique ptr if there is no available frame for decoding.
|
||||||
std::unique_ptr<FrameObject> NextFrame(int64_t max_wait_time_ms);
|
std::unique_ptr<FrameObject> NextFrame(int64_t max_wait_time_ms);
|
||||||
|
|
||||||
|
// Start the frame buffer, has no effect if the frame buffer is started.
|
||||||
|
// The frame buffer is started upon construction.
|
||||||
|
void Start();
|
||||||
|
|
||||||
|
// Stop the frame buffer, causing any sleeping thread in NextFrame to
|
||||||
|
// return immediately.
|
||||||
|
void Stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// FrameKey is a pair of (picture id, spatial layer).
|
// FrameKey is a pair of (picture id, spatial layer).
|
||||||
using FrameKey = std::pair<uint16_t, uint8_t>;
|
using FrameKey = std::pair<uint16_t, uint8_t>;
|
||||||
@ -73,6 +81,7 @@ class FrameBuffer {
|
|||||||
VCMJitterEstimator* const jitter_estimator_;
|
VCMJitterEstimator* const jitter_estimator_;
|
||||||
const VCMTiming* const timing_;
|
const VCMTiming* const timing_;
|
||||||
int newest_picture_id_ GUARDED_BY(crit_);
|
int newest_picture_id_ GUARDED_BY(crit_);
|
||||||
|
bool stopped_ GUARDED_BY(crit_);
|
||||||
|
|
||||||
RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
|
RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user