Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(460)

Side by Side Diff: webrtc/modules/video_coding/frame_buffer2.cc

Issue 2744813002: Start documenting the threading model (Closed)
Patch Set: Convert thread checks in Android code to DCHECKs Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/video_coding/frame_buffer2.h" 11 #include "webrtc/modules/video_coding/frame_buffer2.h"
12 12
13 #include <algorithm> 13 #include <algorithm>
14 #include <cstring> 14 #include <cstring>
15 #include <queue> 15 #include <queue>
16 16
17 #include "webrtc/base/atomicops.h"
17 #include "webrtc/base/checks.h" 18 #include "webrtc/base/checks.h"
18 #include "webrtc/base/logging.h" 19 #include "webrtc/base/logging.h"
19 #include "webrtc/base/trace_event.h" 20 #include "webrtc/base/trace_event.h"
20 #include "webrtc/modules/video_coding/include/video_coding_defines.h" 21 #include "webrtc/modules/video_coding/include/video_coding_defines.h"
21 #include "webrtc/modules/video_coding/jitter_estimator.h" 22 #include "webrtc/modules/video_coding/jitter_estimator.h"
22 #include "webrtc/modules/video_coding/timing.h" 23 #include "webrtc/modules/video_coding/timing.h"
23 #include "webrtc/system_wrappers/include/clock.h" 24 #include "webrtc/system_wrappers/include/clock.h"
24 #include "webrtc/system_wrappers/include/metrics.h" 25 #include "webrtc/system_wrappers/include/metrics.h"
25 26
26 namespace webrtc { 27 namespace webrtc {
27 namespace video_coding { 28 namespace video_coding {
28 29
29 namespace { 30 namespace {
30 // Max number of frames the buffer will hold. 31 // Max number of frames the buffer will hold.
31 constexpr int kMaxFramesBuffered = 600; 32 constexpr int kMaxFramesBuffered = 600;
32 33
33 // Max number of decoded frame info that will be saved. 34 // Max number of decoded frame info that will be saved.
34 constexpr int kMaxFramesHistory = 50; 35 constexpr int kMaxFramesHistory = 50;
35 } // namespace 36 } // namespace
36 37
37 FrameBuffer::FrameBuffer(Clock* clock, 38 FrameBuffer::FrameBuffer(Clock* clock,
38 VCMJitterEstimator* jitter_estimator, 39 VCMJitterEstimator* jitter_estimator,
39 VCMTiming* timing, 40 VCMTiming* timing,
40 VCMReceiveStatisticsCallback* stats_callback) 41 VCMReceiveStatisticsCallback* stats_callback)
41 : clock_(clock), 42 : clock_(clock),
42 new_countinuous_frame_event_(false, false), 43 new_continuous_frame_event_(false, false),
43 jitter_estimator_(jitter_estimator), 44 jitter_estimator_(jitter_estimator),
44 timing_(timing), 45 timing_(timing),
45 inter_frame_delay_(clock_->TimeInMilliseconds()), 46 inter_frame_delay_(clock_->TimeInMilliseconds()),
46 last_decoded_frame_it_(frames_.end()), 47 last_decoded_frame_it_(frames_.end()),
47 last_continuous_frame_it_(frames_.end()), 48 last_continuous_frame_it_(frames_.end()),
48 num_frames_history_(0), 49 num_frames_history_(0),
49 num_frames_buffered_(0), 50 num_frames_buffered_(0),
50 stopped_(false), 51 stopped_(0),
51 protection_mode_(kProtectionNack), 52 protection_mode_(kProtectionNack),
52 stats_callback_(stats_callback) {} 53 stats_callback_(stats_callback) {}
53 54
54 FrameBuffer::~FrameBuffer() {} 55 FrameBuffer::~FrameBuffer() {}
55 56
56 FrameBuffer::ReturnReason FrameBuffer::NextFrame( 57 FrameBuffer::ReturnReason FrameBuffer::NextFrame(
57 int64_t max_wait_time_ms, 58 int64_t max_wait_time_ms,
58 std::unique_ptr<FrameObject>* frame_out) { 59 std::unique_ptr<FrameObject>* frame_out) {
59 TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame"); 60 TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame");
60 int64_t latest_return_time_ms = 61 int64_t latest_return_time_ms =
61 clock_->TimeInMilliseconds() + max_wait_time_ms; 62 clock_->TimeInMilliseconds() + max_wait_time_ms;
62 int64_t wait_ms = max_wait_time_ms; 63 int64_t wait_ms = max_wait_time_ms;
63 64
64 do { 65 do {
66 if (rtc::AtomicOps::AcquireLoad(&stopped_))
67 return kStopped;
68
65 int64_t now_ms = clock_->TimeInMilliseconds(); 69 int64_t now_ms = clock_->TimeInMilliseconds();
70 wait_ms = max_wait_time_ms;
66 { 71 {
67 rtc::CritScope lock(&crit_); 72 rtc::CritScope lock(&crit_);
68 new_countinuous_frame_event_.Reset();
69 if (stopped_)
70 return kStopped;
71
72 wait_ms = max_wait_time_ms;
73
74 // Need to hold |crit_| in order to use |frames_|, therefore we 73 // Need to hold |crit_| in order to use |frames_|, therefore we
75 // set it here in the loop instead of outside the loop in order to not 74 // set it here in the loop instead of outside the loop in order to not
76 // acquire the lock unnecesserily. 75 // acquire the lock unnecesserily.
77 next_frame_it_ = frames_.end(); 76 next_frame_it_ = frames_.end();
78 77
79 // |frame_it| points to the first frame after the 78 // |frame_it| points to the first frame after the
80 // |last_decoded_frame_it_|. 79 // |last_decoded_frame_it_|.
81 auto frame_it = frames_.end(); 80 auto frame_it = frames_.end();
82 if (last_decoded_frame_it_ == frames_.end()) { 81 if (last_decoded_frame_it_ == frames_.end()) {
83 frame_it = frames_.begin(); 82 frame_it = frames_.begin();
(...skipping 25 matching lines...) Expand all
109 // enough and the stream has multiple spatial and temporal layers. 108 // enough and the stream has multiple spatial and temporal layers.
110 if (wait_ms == 0) 109 if (wait_ms == 0)
111 continue; 110 continue;
112 111
113 break; 112 break;
114 } 113 }
115 } // rtc::Critscope lock(&crit_); 114 } // rtc::Critscope lock(&crit_);
116 115
117 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms); 116 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms);
118 wait_ms = std::max<int64_t>(wait_ms, 0); 117 wait_ms = std::max<int64_t>(wait_ms, 0);
119 } while (new_countinuous_frame_event_.Wait(wait_ms)); 118 } while (new_continuous_frame_event_.Wait(wait_ms));
119
120 if (rtc::AtomicOps::AcquireLoad(&stopped_))
121 return kStopped;
120 122
121 rtc::CritScope lock(&crit_); 123 rtc::CritScope lock(&crit_);
122 int64_t now_ms = clock_->TimeInMilliseconds(); 124 int64_t now_ms = clock_->TimeInMilliseconds();
123 if (next_frame_it_ != frames_.end()) { 125 if (next_frame_it_ != frames_.end()) {
124 std::unique_ptr<FrameObject> frame = 126 std::unique_ptr<FrameObject> frame =
125 std::move(next_frame_it_->second.frame); 127 std::move(next_frame_it_->second.frame);
126 128
127 if (!frame->delayed_by_retransmission()) { 129 if (!frame->delayed_by_retransmission()) {
128 int64_t frame_delay; 130 int64_t frame_delay;
129 131
130 if (inter_frame_delay_.CalculateDelay(frame->timestamp, &frame_delay, 132 if (inter_frame_delay_.CalculateDelay(frame->timestamp, &frame_delay,
131 frame->ReceivedTime())) { 133 frame->ReceivedTime())) {
132 jitter_estimator_->UpdateEstimate(frame_delay, frame->size()); 134 jitter_estimator_->UpdateEstimate(frame_delay, frame->size());
133 } 135 }
134 136
135 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0; 137 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
136 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult)); 138 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
137 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms); 139 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms);
138 } 140 }
139 141
140 UpdateJitterDelay(); 142 UpdateJitterDelay();
141 143
142 PropagateDecodability(next_frame_it_->second); 144 PropagateDecodability(next_frame_it_->second);
143 AdvanceLastDecodedFrame(next_frame_it_); 145 AdvanceLastDecodedFrame(next_frame_it_);
144 last_decoded_frame_timestamp_ = frame->timestamp; 146 last_decoded_frame_timestamp_ = frame->timestamp;
145 *frame_out = std::move(frame); 147 *frame_out = std::move(frame);
146 return kFrameFound; 148 return kFrameFound;
147 } else if (latest_return_time_ms - now_ms > 0) { 149 }
150
151 if (latest_return_time_ms - now_ms > 0) {
148 // If |next_frame_it_ == frames_.end()| and there is still time left, it 152 // If |next_frame_it_ == frames_.end()| and there is still time left, it
149 // means that the frame buffer was cleared as the thread in this function 153 // means that the frame buffer was cleared as the thread in this function
150 // was waiting to acquire |crit_| in order to return. Wait for the 154 // was waiting to acquire |crit_| in order to return. Wait for the
151 // remaining time and then return. 155 // remaining time and then return.
152 return NextFrame(latest_return_time_ms - now_ms, frame_out); 156 return NextFrame(latest_return_time_ms - now_ms, frame_out);
153 } else {
154 return kTimeout;
155 } 157 }
158
159 return kTimeout;
156 } 160 }
157 161
158 void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) { 162 void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
159 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode"); 163 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode");
160 rtc::CritScope lock(&crit_); 164 rtc::CritScope lock(&crit_);
161 protection_mode_ = mode; 165 protection_mode_ = mode;
162 } 166 }
163 167
168 // Start() and Stop() must be called on the same thread.
169 // The value of stopped_ can only be changed on this thread.
164 void FrameBuffer::Start() { 170 void FrameBuffer::Start() {
165 TRACE_EVENT0("webrtc", "FrameBuffer::Start"); 171 TRACE_EVENT0("webrtc", "FrameBuffer::Start");
166 rtc::CritScope lock(&crit_); 172 rtc::AtomicOps::ReleaseStore(&stopped_, 0);
167 stopped_ = false;
168 } 173 }
169 174
170 void FrameBuffer::Stop() { 175 void FrameBuffer::Stop() {
171 TRACE_EVENT0("webrtc", "FrameBuffer::Stop"); 176 TRACE_EVENT0("webrtc", "FrameBuffer::Stop");
172 rtc::CritScope lock(&crit_); 177 rtc::AtomicOps::Increment(&stopped_);
173 stopped_ = true; 178 new_continuous_frame_event_.Set();
174 new_countinuous_frame_event_.Set();
175 } 179 }
176 180
177 int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) { 181 int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
178 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame"); 182 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame");
179 rtc::CritScope lock(&crit_);
180 RTC_DCHECK(frame); 183 RTC_DCHECK(frame);
181 184
185 if (rtc::AtomicOps::AcquireLoad(&stopped_))
186 return -1;
187
182 if (stats_callback_) 188 if (stats_callback_)
183 stats_callback_->OnCompleteFrame(frame->num_references == 0, frame->size()); 189 stats_callback_->OnCompleteFrame(frame->num_references == 0, frame->size());
184 190
185 FrameKey key(frame->picture_id, frame->spatial_layer); 191 FrameKey key(frame->picture_id, frame->spatial_layer);
192
193 rtc::CritScope lock(&crit_);
186 int last_continuous_picture_id = 194 int last_continuous_picture_id =
187 last_continuous_frame_it_ == frames_.end() 195 last_continuous_frame_it_ == frames_.end()
188 ? -1 196 ? -1
189 : last_continuous_frame_it_->first.picture_id; 197 : last_continuous_frame_it_->first.picture_id;
190 198
191 if (num_frames_buffered_ >= kMaxFramesBuffered) { 199 if (num_frames_buffered_ >= kMaxFramesBuffered) {
192 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id 200 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id
193 << ":" << static_cast<int>(key.spatial_layer) 201 << ":" << static_cast<int>(key.spatial_layer)
194 << ") could not be inserted due to the frame " 202 << ") could not be inserted due to the frame "
195 << "buffer being full, dropping frame."; 203 << "buffer being full, dropping frame.";
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 ++num_frames_buffered_; 252 ++num_frames_buffered_;
245 253
246 if (info->second.num_missing_continuous == 0) { 254 if (info->second.num_missing_continuous == 0) {
247 info->second.continuous = true; 255 info->second.continuous = true;
248 PropagateContinuity(info); 256 PropagateContinuity(info);
249 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id; 257 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
250 258
251 // Since we now have new continuous frames there might be a better frame 259 // Since we now have new continuous frames there might be a better frame
252 // to return from NextFrame. Signal that thread so that it again can choose 260 // to return from NextFrame. Signal that thread so that it again can choose
253 // which frame to return. 261 // which frame to return.
254 new_countinuous_frame_event_.Set(); 262 new_continuous_frame_event_.Set();
255 } 263 }
256 264
257 return last_continuous_picture_id; 265 return last_continuous_picture_id;
258 } 266 }
259 267
260 void FrameBuffer::PropagateContinuity(FrameMap::iterator start) { 268 void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
261 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity"); 269 TRACE_EVENT0("webrtc", "FrameBuffer::PropagateContinuity");
262 RTC_DCHECK(start->second.continuous); 270 RTC_DCHECK(start->second.continuous);
263 if (last_continuous_frame_it_ == frames_.end()) 271 if (last_continuous_frame_it_ == frames_.end())
264 last_continuous_frame_it_ = start; 272 last_continuous_frame_it_ = start;
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 frames_.clear(); 431 frames_.clear();
424 last_decoded_frame_it_ = frames_.end(); 432 last_decoded_frame_it_ = frames_.end();
425 last_continuous_frame_it_ = frames_.end(); 433 last_continuous_frame_it_ = frames_.end();
426 next_frame_it_ = frames_.end(); 434 next_frame_it_ = frames_.end();
427 num_frames_history_ = 0; 435 num_frames_history_ = 0;
428 num_frames_buffered_ = 0; 436 num_frames_buffered_ = 0;
429 } 437 }
430 438
431 } // namespace video_coding 439 } // namespace video_coding
432 } // namespace webrtc 440 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/video_coding/frame_buffer2.h ('k') | webrtc/modules/video_coding/generic_decoder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698