OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 #include "webrtc/modules/video_coding/main/source/jitter_buffer.h" | 10 #include "webrtc/modules/video_coding/main/source/jitter_buffer.h" |
(...skipping 12 matching lines...) Expand all Loading... | |
23 #include "webrtc/modules/video_coding/main/source/packet.h" | 23 #include "webrtc/modules/video_coding/main/source/packet.h" |
24 #include "webrtc/system_wrappers/interface/clock.h" | 24 #include "webrtc/system_wrappers/interface/clock.h" |
25 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" | 25 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
26 #include "webrtc/system_wrappers/interface/event_wrapper.h" | 26 #include "webrtc/system_wrappers/interface/event_wrapper.h" |
27 #include "webrtc/system_wrappers/interface/logging.h" | 27 #include "webrtc/system_wrappers/interface/logging.h" |
28 #include "webrtc/system_wrappers/interface/metrics.h" | 28 #include "webrtc/system_wrappers/interface/metrics.h" |
29 #include "webrtc/system_wrappers/interface/trace_event.h" | 29 #include "webrtc/system_wrappers/interface/trace_event.h" |
30 | 30 |
31 namespace webrtc { | 31 namespace webrtc { |
32 | 32 |
33 // Interval for updating SS data. | |
34 static const uint32_t kPeriodicUpdateIntervalSec = 60; | |
35 | |
33 // Use this rtt if no value has been reported. | 36 // Use this rtt if no value has been reported. |
34 static const int64_t kDefaultRtt = 200; | 37 static const int64_t kDefaultRtt = 200; |
35 | 38 |
36 typedef std::pair<uint32_t, VCMFrameBuffer*> FrameListPair; | 39 typedef std::pair<uint32_t, VCMFrameBuffer*> FrameListPair; |
37 | 40 |
38 bool IsKeyFrame(FrameListPair pair) { | 41 bool IsKeyFrame(FrameListPair pair) { |
39 return pair.second->FrameType() == kVideoFrameKey; | 42 return pair.second->FrameType() == kVideoFrameKey; |
40 } | 43 } |
41 | 44 |
42 bool HasNonEmptyState(FrameListPair pair) { | 45 bool HasNonEmptyState(FrameListPair pair) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 } | 109 } |
107 | 110 |
108 void FrameList::Reset(UnorderedFrameList* free_frames) { | 111 void FrameList::Reset(UnorderedFrameList* free_frames) { |
109 while (!empty()) { | 112 while (!empty()) { |
110 begin()->second->Reset(); | 113 begin()->second->Reset(); |
111 free_frames->push_back(begin()->second); | 114 free_frames->push_back(begin()->second); |
112 erase(begin()); | 115 erase(begin()); |
113 } | 116 } |
114 } | 117 } |
115 | 118 |
119 bool Vp9SsMap::Insert(const VCMPacket& packet) { | |
120 if (!packet.codecSpecificHeader.codecHeader.VP9.ss_data_available) | |
121 return false; | |
122 | |
123 ss_map_[packet.timestamp] = packet.codecSpecificHeader.codecHeader.VP9.gof; | |
124 return true; | |
125 } | |
126 | |
127 void Vp9SsMap::Reset() { | |
128 ss_map_.clear(); | |
129 } | |
130 | |
131 bool Vp9SsMap::Find(uint32_t timestamp, uint32_t* ss_timestamp) const { | |
132 bool found = false; | |
133 for (const auto& it : ss_map_) { | |
134 if (it.first == timestamp || IsNewerTimestamp(timestamp, it.first)) { | |
135 *ss_timestamp = it.first; | |
136 found = true; | |
137 } | |
138 } | |
139 return found; | |
140 } | |
stefan-webrtc
2015/10/13 13:49:43
You may be able to implement this with ss_map_.low
åsapersson
2015/10/14 15:13:51
Should be around one or two (or as many that are r
stefan-webrtc
2015/10/16 06:45:05
Acknowledged.
| |
141 | |
142 void Vp9SsMap::RemoveOld(uint32_t timestamp) { | |
143 if (!TimeForPeriodicUpdate(timestamp)) | |
144 return; | |
145 | |
146 uint32_t ss_timestamp; | |
147 if (!Find(timestamp, &ss_timestamp)) | |
stefan-webrtc
2015/10/13 13:49:43
It would be nice if Find() instead could return an
åsapersson
2015/10/14 15:13:50
Done.
| |
148 return; | |
149 | |
150 RemoveOlderThan(ss_timestamp); | |
151 AdvanceFront(timestamp); | |
152 } | |
153 | |
154 bool Vp9SsMap::TimeForPeriodicUpdate(uint32_t timestamp) const { | |
stefan-webrtc
2015/10/13 13:49:43
What exactly is a periodic update?
åsapersson
2015/10/14 15:13:51
Renamed this function. Looks for if it is time to
stefan-webrtc
2015/10/16 06:45:05
Please add this as a comment to the method in the
åsapersson
2015/10/16 10:52:26
Done.
| |
155 if (ss_map_.empty() || !IsNewerTimestamp(timestamp, ss_map_.begin()->first)) | |
156 return false; | |
157 | |
158 uint32_t diff_sec = (timestamp - ss_map_.begin()->first) / 90000U; | |
stefan-webrtc
2015/10/13 13:49:43
Name the constant 90000. We should already have th
åsapersson
2015/10/14 15:13:50
Done.
| |
159 return diff_sec >= kPeriodicUpdateIntervalSec; | |
160 } | |
161 | |
162 void Vp9SsMap::RemoveOlderThan(uint32_t timestamp) { | |
163 while (!ss_map_.empty() && | |
164 IsNewerTimestamp(timestamp, ss_map_.begin()->first)) { | |
165 ss_map_.erase(ss_map_.begin()); | |
166 } | |
167 } | |
168 | |
169 void Vp9SsMap::AdvanceFront(uint32_t timestamp) { | |
170 assert(!ss_map_.empty()); | |
171 GofInfoVP9 gof = ss_map_.begin()->second; | |
172 ss_map_.erase(ss_map_.begin()); | |
173 ss_map_[timestamp] = gof; | |
stefan-webrtc
2015/10/13 13:49:43
How is this supposed to work? Looks like we are ch
åsapersson
2015/10/14 15:13:51
See comment above.
| |
174 } | |
175 | |
176 bool Vp9SsMap::UpdatePacket(const VCMPacket* packet) { | |
stefan-webrtc
2015/10/13 13:49:43
Can this method be const?
åsapersson
2015/10/14 15:13:50
Done.
| |
177 uint8_t gof_idx = packet->codecSpecificHeader.codecHeader.VP9.gof_idx; | |
178 if (gof_idx == kNoGofIdx) | |
179 return false; // No update needed. | |
180 | |
181 uint32_t ss_timestamp; | |
182 if (!Find(packet->timestamp, &ss_timestamp)) | |
183 return false; // Corresponding SS not yet received. | |
184 | |
185 auto it = ss_map_.find(ss_timestamp); | |
186 if (gof_idx >= it->second.num_frames_in_gof) | |
187 return false; // Corresponding SS not yet received. | |
188 | |
189 RTPVideoTypeHeader* hdr = const_cast<RTPVideoTypeHeader*>( | |
stefan-webrtc
2015/10/13 13:49:43
Preferably no const_casts... Can we do it in some
åsapersson
2015/10/14 15:13:50
Moved const_cast to when calling function.
| |
190 &packet->codecSpecificHeader.codecHeader); | |
191 hdr->VP9.temporal_idx = it->second.temporal_idx[gof_idx]; | |
192 hdr->VP9.temporal_up_switch = it->second.temporal_up_switch[gof_idx]; | |
193 | |
194 // TODO(asapersson): Set hdr->VP9.ref_picture_id[i] and add usage. | |
195 hdr->VP9.num_ref_pics = it->second.num_ref_pics[gof_idx]; | |
196 for (size_t i = 0; i < it->second.num_ref_pics[gof_idx]; ++i) { | |
197 hdr->VP9.pid_diff[i] = it->second.pid_diff[gof_idx][i]; | |
198 } | |
199 return true; | |
200 } | |
201 | |
202 void Vp9SsMap::UpdateFrames(FrameList* frames) { | |
203 for (FrameList::iterator it = frames->begin(); it != frames->end(); ++it) { | |
stefan-webrtc
2015/10/13 13:49:43
for (auto& it : *frames)
åsapersson
2015/10/14 15:13:51
Done.
| |
204 uint8_t gof_idx = it->second->CodecSpecific()->codecSpecific.VP9.gof_idx; | |
205 if (gof_idx == kNoGofIdx) { | |
206 continue; | |
207 } | |
208 uint32_t ss_timestamp; | |
209 if (Find(it->second->TimeStamp(), &ss_timestamp)) { | |
210 auto ss_iter = ss_map_.find(ss_timestamp); | |
stefan-webrtc
2015/10/13 13:49:42
Wouldn't it be better to have Find() return this i
åsapersson
2015/10/14 15:13:51
Done.
| |
211 if (gof_idx >= ss_iter->second.num_frames_in_gof) { | |
212 continue; | |
stefan-webrtc
2015/10/13 13:49:43
Does this mean we haven't received the right SS ye
åsapersson
2015/10/14 15:13:51
The right not yet received, added a comment.
| |
213 } | |
214 it->second->SetGofInfo(ss_iter->second, gof_idx); | |
215 } | |
216 } | |
217 } | |
218 | |
116 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, | 219 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, |
117 rtc::scoped_ptr<EventWrapper> event) | 220 rtc::scoped_ptr<EventWrapper> event) |
118 : clock_(clock), | 221 : clock_(clock), |
119 running_(false), | 222 running_(false), |
120 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | 223 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
121 frame_event_(event.Pass()), | 224 frame_event_(event.Pass()), |
122 max_number_of_frames_(kStartNumberOfFrames), | 225 max_number_of_frames_(kStartNumberOfFrames), |
123 free_frames_(), | 226 free_frames_(), |
124 decodable_frames_(), | 227 decodable_frames_(), |
125 incomplete_frames_(), | 228 incomplete_frames_(), |
126 last_decoded_state_(), | 229 last_decoded_state_(), |
127 first_packet_since_reset_(true), | 230 first_packet_since_reset_(true), |
128 last_gof_timestamp_(0), | |
129 last_gof_valid_(false), | |
130 stats_callback_(NULL), | 231 stats_callback_(NULL), |
131 incoming_frame_rate_(0), | 232 incoming_frame_rate_(0), |
132 incoming_frame_count_(0), | 233 incoming_frame_count_(0), |
133 time_last_incoming_frame_count_(0), | 234 time_last_incoming_frame_count_(0), |
134 incoming_bit_count_(0), | 235 incoming_bit_count_(0), |
135 incoming_bit_rate_(0), | 236 incoming_bit_rate_(0), |
136 num_consecutive_old_packets_(0), | 237 num_consecutive_old_packets_(0), |
137 num_packets_(0), | 238 num_packets_(0), |
138 num_duplicated_packets_(0), | 239 num_duplicated_packets_(0), |
139 num_discarded_packets_(0), | 240 num_discarded_packets_(0), |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 num_discarded_packets_ = 0; | 316 num_discarded_packets_ = 0; |
216 time_first_packet_ms_ = 0; | 317 time_first_packet_ms_ = 0; |
217 | 318 |
218 // Start in a non-signaled state. | 319 // Start in a non-signaled state. |
219 waiting_for_completion_.frame_size = 0; | 320 waiting_for_completion_.frame_size = 0; |
220 waiting_for_completion_.timestamp = 0; | 321 waiting_for_completion_.timestamp = 0; |
221 waiting_for_completion_.latest_packet_time = -1; | 322 waiting_for_completion_.latest_packet_time = -1; |
222 first_packet_since_reset_ = true; | 323 first_packet_since_reset_ = true; |
223 rtt_ms_ = kDefaultRtt; | 324 rtt_ms_ = kDefaultRtt; |
224 last_decoded_state_.Reset(); | 325 last_decoded_state_.Reset(); |
225 last_gof_valid_ = false; | 326 vp9_ss_map_.Reset(); |
226 } | 327 } |
227 | 328 |
228 void VCMJitterBuffer::Stop() { | 329 void VCMJitterBuffer::Stop() { |
229 crit_sect_->Enter(); | 330 crit_sect_->Enter(); |
230 UpdateHistograms(); | 331 UpdateHistograms(); |
231 running_ = false; | 332 running_ = false; |
232 last_decoded_state_.Reset(); | 333 last_decoded_state_.Reset(); |
233 last_gof_valid_ = false; | 334 vp9_ss_map_.Reset(); |
234 | 335 |
235 // Make sure all frames are free and reset. | 336 // Make sure all frames are free and reset. |
236 for (FrameList::iterator it = decodable_frames_.begin(); | 337 for (FrameList::iterator it = decodable_frames_.begin(); |
237 it != decodable_frames_.end(); ++it) { | 338 it != decodable_frames_.end(); ++it) { |
238 free_frames_.push_back(it->second); | 339 free_frames_.push_back(it->second); |
239 } | 340 } |
240 for (FrameList::iterator it = incomplete_frames_.begin(); | 341 for (FrameList::iterator it = incomplete_frames_.begin(); |
241 it != incomplete_frames_.end(); ++it) { | 342 it != incomplete_frames_.end(); ++it) { |
242 free_frames_.push_back(it->second); | 343 free_frames_.push_back(it->second); |
243 } | 344 } |
(...skipping 11 matching lines...) Expand all Loading... | |
255 bool VCMJitterBuffer::Running() const { | 356 bool VCMJitterBuffer::Running() const { |
256 CriticalSectionScoped cs(crit_sect_); | 357 CriticalSectionScoped cs(crit_sect_); |
257 return running_; | 358 return running_; |
258 } | 359 } |
259 | 360 |
260 void VCMJitterBuffer::Flush() { | 361 void VCMJitterBuffer::Flush() { |
261 CriticalSectionScoped cs(crit_sect_); | 362 CriticalSectionScoped cs(crit_sect_); |
262 decodable_frames_.Reset(&free_frames_); | 363 decodable_frames_.Reset(&free_frames_); |
263 incomplete_frames_.Reset(&free_frames_); | 364 incomplete_frames_.Reset(&free_frames_); |
264 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. | 365 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. |
265 last_gof_valid_ = false; | 366 vp9_ss_map_.Reset(); |
266 num_consecutive_old_packets_ = 0; | 367 num_consecutive_old_packets_ = 0; |
267 // Also reset the jitter and delay estimates | 368 // Also reset the jitter and delay estimates |
268 jitter_estimate_.Reset(); | 369 jitter_estimate_.Reset(); |
269 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); | 370 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); |
270 waiting_for_completion_.frame_size = 0; | 371 waiting_for_completion_.frame_size = 0; |
271 waiting_for_completion_.timestamp = 0; | 372 waiting_for_completion_.timestamp = 0; |
272 waiting_for_completion_.latest_packet_time = -1; | 373 waiting_for_completion_.latest_packet_time = -1; |
273 first_packet_since_reset_ = true; | 374 first_packet_since_reset_ = true; |
274 missing_sequence_numbers_.clear(); | 375 missing_sequence_numbers_.clear(); |
275 } | 376 } |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
586 LOG(LS_WARNING) | 687 LOG(LS_WARNING) |
587 << num_consecutive_old_packets_ | 688 << num_consecutive_old_packets_ |
588 << " consecutive old packets received. Flushing the jitter buffer."; | 689 << " consecutive old packets received. Flushing the jitter buffer."; |
589 Flush(); | 690 Flush(); |
590 return kFlushIndicator; | 691 return kFlushIndicator; |
591 } | 692 } |
592 return kOldPacket; | 693 return kOldPacket; |
593 } | 694 } |
594 | 695 |
595 if (packet.codec == kVideoCodecVP9) { | 696 if (packet.codec == kVideoCodecVP9) { |
596 // TODO(asapersson): Move this code to appropriate place. | |
597 // TODO(asapersson): Handle out of order GOF. | |
598 if (packet.codecSpecificHeader.codecHeader.VP9.flexible_mode) { | 697 if (packet.codecSpecificHeader.codecHeader.VP9.flexible_mode) { |
599 // TODO(asapersson): Add support for flexible mode. | 698 // TODO(asapersson): Add support for flexible mode. |
600 return kGeneralError; | 699 return kGeneralError; |
601 } | 700 } |
602 if (packet.codecSpecificHeader.codecHeader.VP9.ss_data_available) { | 701 if (vp9_ss_map_.Insert(packet)) { |
603 if (!last_gof_valid_ || | 702 vp9_ss_map_.UpdateFrames(&incomplete_frames_); |
604 IsNewerTimestamp(packet.timestamp, last_gof_timestamp_)) { | |
605 last_gof_.CopyGofInfoVP9( | |
606 packet.codecSpecificHeader.codecHeader.VP9.gof); | |
607 last_gof_timestamp_ = packet.timestamp; | |
608 last_gof_valid_ = true; | |
609 } | |
610 } | 703 } |
611 if (last_gof_valid_ && | 704 vp9_ss_map_.UpdatePacket(&packet); |
612 !packet.codecSpecificHeader.codecHeader.VP9.flexible_mode) { | 705 |
613 uint8_t gof_idx = packet.codecSpecificHeader.codecHeader.VP9.gof_idx; | 706 if (!last_decoded_state_.in_initial_state()) |
614 if (gof_idx != kNoGofIdx) { | 707 vp9_ss_map_.RemoveOld(last_decoded_state_.time_stamp()); |
stefan-webrtc
2015/10/13 13:49:43
Can you explain why this is needed now?
åsapersson
2015/10/14 15:13:50
To not make the map possibly grow too large.
| |
615 if (gof_idx >= last_gof_.num_frames_in_gof) { | |
616 LOG(LS_WARNING) << "Incorrect gof_idx: " << gof_idx; | |
617 return kGeneralError; | |
618 } | |
619 RTPVideoTypeHeader* hdr = const_cast<RTPVideoTypeHeader*>( | |
620 &packet.codecSpecificHeader.codecHeader); | |
621 hdr->VP9.temporal_idx = last_gof_.temporal_idx[gof_idx]; | |
622 hdr->VP9.temporal_up_switch = last_gof_.temporal_up_switch[gof_idx]; | |
623 } | |
624 } | |
625 } | 708 } |
626 | 709 |
627 num_consecutive_old_packets_ = 0; | 710 num_consecutive_old_packets_ = 0; |
628 | 711 |
629 VCMFrameBuffer* frame; | 712 VCMFrameBuffer* frame; |
630 FrameList* frame_list; | 713 FrameList* frame_list; |
631 const VCMFrameBufferEnum error = GetFrame(packet, &frame, &frame_list); | 714 const VCMFrameBufferEnum error = GetFrame(packet, &frame, &frame_list); |
632 if (error != kNoError) | 715 if (error != kNoError) |
633 return error; | 716 return error; |
634 | 717 |
(...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1253 } | 1336 } |
1254 // Evaluate if the RTT is higher than |high_rtt_nack_threshold_ms_|, and in | 1337 // Evaluate if the RTT is higher than |high_rtt_nack_threshold_ms_|, and in |
1255 // that case we don't wait for retransmissions. | 1338 // that case we don't wait for retransmissions. |
1256 if (high_rtt_nack_threshold_ms_ >= 0 && | 1339 if (high_rtt_nack_threshold_ms_ >= 0 && |
1257 rtt_ms_ >= high_rtt_nack_threshold_ms_) { | 1340 rtt_ms_ >= high_rtt_nack_threshold_ms_) { |
1258 return false; | 1341 return false; |
1259 } | 1342 } |
1260 return true; | 1343 return true; |
1261 } | 1344 } |
1262 } // namespace webrtc | 1345 } // namespace webrtc |
OLD | NEW |