OLD | NEW |
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" | |
18 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
19 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
20 #include "webrtc/base/trace_event.h" | 19 #include "webrtc/base/trace_event.h" |
21 #include "webrtc/modules/video_coding/include/video_coding_defines.h" | 20 #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
22 #include "webrtc/modules/video_coding/jitter_estimator.h" | 21 #include "webrtc/modules/video_coding/jitter_estimator.h" |
23 #include "webrtc/modules/video_coding/timing.h" | 22 #include "webrtc/modules/video_coding/timing.h" |
24 #include "webrtc/system_wrappers/include/clock.h" | 23 #include "webrtc/system_wrappers/include/clock.h" |
25 #include "webrtc/system_wrappers/include/metrics.h" | 24 #include "webrtc/system_wrappers/include/metrics.h" |
26 | 25 |
27 namespace webrtc { | 26 namespace webrtc { |
(...skipping 13 matching lines...) Expand all Loading... |
41 VCMReceiveStatisticsCallback* stats_callback) | 40 VCMReceiveStatisticsCallback* stats_callback) |
42 : clock_(clock), | 41 : clock_(clock), |
43 new_continuous_frame_event_(false, false), | 42 new_continuous_frame_event_(false, false), |
44 jitter_estimator_(jitter_estimator), | 43 jitter_estimator_(jitter_estimator), |
45 timing_(timing), | 44 timing_(timing), |
46 inter_frame_delay_(clock_->TimeInMilliseconds()), | 45 inter_frame_delay_(clock_->TimeInMilliseconds()), |
47 last_decoded_frame_it_(frames_.end()), | 46 last_decoded_frame_it_(frames_.end()), |
48 last_continuous_frame_it_(frames_.end()), | 47 last_continuous_frame_it_(frames_.end()), |
49 num_frames_history_(0), | 48 num_frames_history_(0), |
50 num_frames_buffered_(0), | 49 num_frames_buffered_(0), |
51 stopped_(0), | 50 stopped_(false), |
52 protection_mode_(kProtectionNack), | 51 protection_mode_(kProtectionNack), |
53 stats_callback_(stats_callback) {} | 52 stats_callback_(stats_callback) {} |
54 | 53 |
55 FrameBuffer::~FrameBuffer() {} | 54 FrameBuffer::~FrameBuffer() {} |
56 | 55 |
57 FrameBuffer::ReturnReason FrameBuffer::NextFrame( | 56 FrameBuffer::ReturnReason FrameBuffer::NextFrame( |
58 int64_t max_wait_time_ms, | 57 int64_t max_wait_time_ms, |
59 std::unique_ptr<FrameObject>* frame_out) { | 58 std::unique_ptr<FrameObject>* frame_out) { |
60 TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame"); | 59 TRACE_EVENT0("webrtc", "FrameBuffer::NextFrame"); |
61 int64_t latest_return_time_ms = | 60 int64_t latest_return_time_ms = |
62 clock_->TimeInMilliseconds() + max_wait_time_ms; | 61 clock_->TimeInMilliseconds() + max_wait_time_ms; |
63 int64_t wait_ms = max_wait_time_ms; | 62 int64_t wait_ms = max_wait_time_ms; |
| 63 int64_t now_ms = 0; |
64 | 64 |
65 do { | 65 do { |
66 if (rtc::AtomicOps::AcquireLoad(&stopped_)) | 66 now_ms = clock_->TimeInMilliseconds(); |
67 return kStopped; | |
68 | |
69 int64_t now_ms = clock_->TimeInMilliseconds(); | |
70 wait_ms = max_wait_time_ms; | |
71 { | 67 { |
72 rtc::CritScope lock(&crit_); | 68 rtc::CritScope lock(&crit_); |
73 new_continuous_frame_event_.Reset(); | 69 new_continuous_frame_event_.Reset(); |
| 70 if (stopped_) |
| 71 return kStopped; |
| 72 |
| 73 wait_ms = max_wait_time_ms; |
| 74 |
74 // Need to hold |crit_| in order to use |frames_|, therefore we | 75 // 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 | 76 // set it here in the loop instead of outside the loop in order to not |
76 // acquire the lock unnecesserily. | 77 // acquire the lock unnecesserily. |
77 next_frame_it_ = frames_.end(); | 78 next_frame_it_ = frames_.end(); |
78 | 79 |
79 // |frame_it| points to the first frame after the | 80 // |frame_it| points to the first frame after the |
80 // |last_decoded_frame_it_|. | 81 // |last_decoded_frame_it_|. |
81 auto frame_it = frames_.end(); | 82 auto frame_it = frames_.end(); |
82 if (last_decoded_frame_it_ == frames_.end()) { | 83 if (last_decoded_frame_it_ == frames_.end()) { |
83 frame_it = frames_.begin(); | 84 frame_it = frames_.begin(); |
(...skipping 27 matching lines...) Expand all Loading... |
111 continue; | 112 continue; |
112 | 113 |
113 break; | 114 break; |
114 } | 115 } |
115 } // rtc::Critscope lock(&crit_); | 116 } // rtc::Critscope lock(&crit_); |
116 | 117 |
117 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms); | 118 wait_ms = std::min<int64_t>(wait_ms, latest_return_time_ms - now_ms); |
118 wait_ms = std::max<int64_t>(wait_ms, 0); | 119 wait_ms = std::max<int64_t>(wait_ms, 0); |
119 } while (new_continuous_frame_event_.Wait(wait_ms)); | 120 } while (new_continuous_frame_event_.Wait(wait_ms)); |
120 | 121 |
121 rtc::CritScope lock(&crit_); | 122 { |
122 int64_t now_ms = clock_->TimeInMilliseconds(); | 123 rtc::CritScope lock(&crit_); |
123 if (next_frame_it_ != frames_.end()) { | 124 now_ms = clock_->TimeInMilliseconds(); |
124 std::unique_ptr<FrameObject> frame = | 125 if (next_frame_it_ != frames_.end()) { |
125 std::move(next_frame_it_->second.frame); | 126 std::unique_ptr<FrameObject> 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()); |
| 135 } |
| 136 |
| 137 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0; |
| 138 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult)); |
| 139 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms); |
133 } | 140 } |
134 | 141 |
135 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0; | 142 UpdateJitterDelay(); |
136 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult)); | 143 |
137 timing_->UpdateCurrentDelay(frame->RenderTime(), now_ms); | 144 PropagateDecodability(next_frame_it_->second); |
| 145 AdvanceLastDecodedFrame(next_frame_it_); |
| 146 last_decoded_frame_timestamp_ = frame->timestamp; |
| 147 *frame_out = std::move(frame); |
| 148 return kFrameFound; |
138 } | 149 } |
139 | |
140 UpdateJitterDelay(); | |
141 | |
142 PropagateDecodability(next_frame_it_->second); | |
143 AdvanceLastDecodedFrame(next_frame_it_); | |
144 last_decoded_frame_timestamp_ = frame->timestamp; | |
145 *frame_out = std::move(frame); | |
146 return kFrameFound; | |
147 } | 150 } |
148 | 151 |
149 if (latest_return_time_ms - now_ms > 0) { | 152 if (latest_return_time_ms - now_ms > 0) { |
150 // If |next_frame_it_ == frames_.end()| and there is still time left, it | 153 // If |next_frame_it_ == frames_.end()| and there is still time left, it |
151 // means that the frame buffer was cleared as the thread in this function | 154 // means that the frame buffer was cleared as the thread in this function |
152 // was waiting to acquire |crit_| in order to return. Wait for the | 155 // was waiting to acquire |crit_| in order to return. Wait for the |
153 // remaining time and then return. | 156 // remaining time and then return. |
154 return NextFrame(latest_return_time_ms - now_ms, frame_out); | 157 return NextFrame(latest_return_time_ms - now_ms, frame_out); |
155 } | 158 } |
156 | 159 |
157 return kTimeout; | 160 return kTimeout; |
158 } | 161 } |
159 | 162 |
160 void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) { | 163 void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) { |
161 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode"); | 164 TRACE_EVENT0("webrtc", "FrameBuffer::SetProtectionMode"); |
162 rtc::CritScope lock(&crit_); | 165 rtc::CritScope lock(&crit_); |
163 protection_mode_ = mode; | 166 protection_mode_ = mode; |
164 } | 167 } |
165 | 168 |
166 // Start() and Stop() must be called on the same thread. | |
167 // The value of stopped_ can only be changed on this thread. | |
168 void FrameBuffer::Start() { | 169 void FrameBuffer::Start() { |
169 TRACE_EVENT0("webrtc", "FrameBuffer::Start"); | 170 TRACE_EVENT0("webrtc", "FrameBuffer::Start"); |
170 rtc::AtomicOps::ReleaseStore(&stopped_, 0); | 171 rtc::CritScope lock(&crit_); |
| 172 stopped_ = false; |
171 } | 173 } |
172 | 174 |
173 void FrameBuffer::Stop() { | 175 void FrameBuffer::Stop() { |
174 TRACE_EVENT0("webrtc", "FrameBuffer::Stop"); | 176 TRACE_EVENT0("webrtc", "FrameBuffer::Stop"); |
175 // TODO(tommi,philipel): Previously, we grabbed the |crit_| lock when decoding | 177 rtc::CritScope lock(&crit_); |
176 // was being stopped. On Android, with captured frames being delivered on the | 178 stopped_ = true; |
177 // decoder thread, this consistently caused the 'busy loop' checks in the | |
178 // PlatformThread to trigger on "VoiceProcessThread", "ModuleProcessThread" | |
179 // and occasionally on "PacerThread". | |
180 // Look into what was going on and why |Stop()| wasn't able to grab the lock | |
181 // and stop the FrameBuffer while that happened. | |
182 // See bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=7331 | |
183 rtc::AtomicOps::Increment(&stopped_); | |
184 new_continuous_frame_event_.Set(); | 179 new_continuous_frame_event_.Set(); |
185 } | 180 } |
186 | 181 |
187 int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) { | 182 int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) { |
188 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame"); | 183 TRACE_EVENT0("webrtc", "FrameBuffer::InsertFrame"); |
189 RTC_DCHECK(frame); | 184 RTC_DCHECK(frame); |
190 | |
191 if (rtc::AtomicOps::AcquireLoad(&stopped_)) | |
192 return -1; | |
193 | |
194 if (stats_callback_) | 185 if (stats_callback_) |
195 stats_callback_->OnCompleteFrame(frame->num_references == 0, frame->size()); | 186 stats_callback_->OnCompleteFrame(frame->num_references == 0, frame->size()); |
196 | |
197 FrameKey key(frame->picture_id, frame->spatial_layer); | 187 FrameKey key(frame->picture_id, frame->spatial_layer); |
198 | 188 |
199 rtc::CritScope lock(&crit_); | 189 rtc::CritScope lock(&crit_); |
| 190 |
200 int last_continuous_picture_id = | 191 int last_continuous_picture_id = |
201 last_continuous_frame_it_ == frames_.end() | 192 last_continuous_frame_it_ == frames_.end() |
202 ? -1 | 193 ? -1 |
203 : last_continuous_frame_it_->first.picture_id; | 194 : last_continuous_frame_it_->first.picture_id; |
204 | 195 |
205 if (num_frames_buffered_ >= kMaxFramesBuffered) { | 196 if (num_frames_buffered_ >= kMaxFramesBuffered) { |
206 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id | 197 LOG(LS_WARNING) << "Frame with (picture_id:spatial_id) (" << key.picture_id |
207 << ":" << static_cast<int>(key.spatial_layer) | 198 << ":" << static_cast<int>(key.spatial_layer) |
208 << ") could not be inserted due to the frame " | 199 << ") could not be inserted due to the frame " |
209 << "buffer being full, dropping frame."; | 200 << "buffer being full, dropping frame."; |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 frames_.clear(); | 428 frames_.clear(); |
438 last_decoded_frame_it_ = frames_.end(); | 429 last_decoded_frame_it_ = frames_.end(); |
439 last_continuous_frame_it_ = frames_.end(); | 430 last_continuous_frame_it_ = frames_.end(); |
440 next_frame_it_ = frames_.end(); | 431 next_frame_it_ = frames_.end(); |
441 num_frames_history_ = 0; | 432 num_frames_history_ = 0; |
442 num_frames_buffered_ = 0; | 433 num_frames_buffered_ = 0; |
443 } | 434 } |
444 | 435 |
445 } // namespace video_coding | 436 } // namespace video_coding |
446 } // namespace webrtc | 437 } // namespace webrtc |
OLD | NEW |