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" |
11 | 11 |
12 #include <assert.h> | 12 #include <assert.h> |
13 | 13 |
14 #include <algorithm> | 14 #include <algorithm> |
15 #include <utility> | 15 #include <utility> |
16 | 16 |
17 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" | |
17 #include "webrtc/modules/video_coding/main/interface/video_coding.h" | 18 #include "webrtc/modules/video_coding/main/interface/video_coding.h" |
18 #include "webrtc/modules/video_coding/main/source/frame_buffer.h" | 19 #include "webrtc/modules/video_coding/main/source/frame_buffer.h" |
19 #include "webrtc/modules/video_coding/main/source/inter_frame_delay.h" | 20 #include "webrtc/modules/video_coding/main/source/inter_frame_delay.h" |
20 #include "webrtc/modules/video_coding/main/source/internal_defines.h" | 21 #include "webrtc/modules/video_coding/main/source/internal_defines.h" |
21 #include "webrtc/modules/video_coding/main/source/jitter_buffer_common.h" | 22 #include "webrtc/modules/video_coding/main/source/jitter_buffer_common.h" |
22 #include "webrtc/modules/video_coding/main/source/jitter_estimator.h" | 23 #include "webrtc/modules/video_coding/main/source/jitter_estimator.h" |
23 #include "webrtc/modules/video_coding/main/source/packet.h" | 24 #include "webrtc/modules/video_coding/main/source/packet.h" |
24 #include "webrtc/system_wrappers/interface/clock.h" | 25 #include "webrtc/system_wrappers/interface/clock.h" |
25 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" | 26 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
26 #include "webrtc/system_wrappers/interface/event_wrapper.h" | 27 #include "webrtc/system_wrappers/interface/event_wrapper.h" |
27 #include "webrtc/system_wrappers/interface/logging.h" | 28 #include "webrtc/system_wrappers/interface/logging.h" |
28 #include "webrtc/system_wrappers/interface/metrics.h" | 29 #include "webrtc/system_wrappers/interface/metrics.h" |
29 #include "webrtc/system_wrappers/interface/trace_event.h" | 30 #include "webrtc/system_wrappers/interface/trace_event.h" |
30 | 31 |
31 namespace webrtc { | 32 namespace webrtc { |
32 | 33 |
34 // Interval for updating SS data. | |
35 static const uint32_t kSsCleanupIntervalSec = 60; | |
36 | |
33 // Use this rtt if no value has been reported. | 37 // Use this rtt if no value has been reported. |
34 static const int64_t kDefaultRtt = 200; | 38 static const int64_t kDefaultRtt = 200; |
35 | 39 |
36 typedef std::pair<uint32_t, VCMFrameBuffer*> FrameListPair; | 40 typedef std::pair<uint32_t, VCMFrameBuffer*> FrameListPair; |
37 | 41 |
38 bool IsKeyFrame(FrameListPair pair) { | 42 bool IsKeyFrame(FrameListPair pair) { |
39 return pair.second->FrameType() == kVideoFrameKey; | 43 return pair.second->FrameType() == kVideoFrameKey; |
40 } | 44 } |
41 | 45 |
42 bool HasNonEmptyState(FrameListPair pair) { | 46 bool HasNonEmptyState(FrameListPair pair) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 } | 110 } |
107 | 111 |
108 void FrameList::Reset(UnorderedFrameList* free_frames) { | 112 void FrameList::Reset(UnorderedFrameList* free_frames) { |
109 while (!empty()) { | 113 while (!empty()) { |
110 begin()->second->Reset(); | 114 begin()->second->Reset(); |
111 free_frames->push_back(begin()->second); | 115 free_frames->push_back(begin()->second); |
112 erase(begin()); | 116 erase(begin()); |
113 } | 117 } |
114 } | 118 } |
115 | 119 |
120 bool Vp9SsMap::Insert(const VCMPacket& packet) { | |
121 if (!packet.codecSpecificHeader.codecHeader.VP9.ss_data_available) | |
122 return false; | |
123 | |
124 ss_map_[packet.timestamp] = packet.codecSpecificHeader.codecHeader.VP9.gof; | |
125 return true; | |
126 } | |
127 | |
128 void Vp9SsMap::Reset() { | |
129 ss_map_.clear(); | |
130 } | |
131 | |
132 bool Vp9SsMap::Find(uint32_t timestamp, SsMap::const_iterator* ss_it) const { | |
133 bool found = false; | |
134 for (SsMap::const_iterator it = ss_map_.begin(); it != ss_map_.end(); ++it) { | |
135 if (it->first == timestamp || IsNewerTimestamp(timestamp, it->first)) { | |
136 *ss_it = it; | |
137 found = true; | |
138 } | |
139 } | |
140 return found; | |
141 } | |
142 | |
143 void Vp9SsMap::RemoveOld(uint32_t timestamp) { | |
144 if (!TimeForCleanup(timestamp)) | |
145 return; | |
146 | |
147 SsMap::const_iterator it; | |
148 if (!Find(timestamp, &it)) | |
149 return; | |
150 | |
151 ss_map_.erase(ss_map_.begin(), it); | |
152 // To handle timestamp wrap in case SS data is sent very seldom. | |
153 AdvanceFront(timestamp); | |
stefan-webrtc
2015/10/16 06:45:06
Is there any risk that we may be changing the orde
åsapersson
2015/10/16 10:52:26
Don't think so. Only one SS that is older than tim
stefan-webrtc
2015/10/16 12:26:11
You are right. Maybe it's still worth adding an DC
åsapersson
2015/10/20 13:32:20
I think the decoding state can be reset (when gett
åsapersson
2015/10/21 07:11:40
Removed the check. An older SS could possibly be i
| |
154 } | |
155 | |
156 bool Vp9SsMap::TimeForCleanup(uint32_t timestamp) const { | |
157 if (ss_map_.empty() || !IsNewerTimestamp(timestamp, ss_map_.begin()->first)) | |
158 return false; | |
159 | |
160 uint32_t diff = timestamp - ss_map_.begin()->first; | |
161 return diff / kVideoPayloadTypeFrequency >= kSsCleanupIntervalSec; | |
162 } | |
163 | |
164 void Vp9SsMap::AdvanceFront(uint32_t timestamp) { | |
165 assert(!ss_map_.empty()); | |
166 GofInfoVP9 gof = ss_map_.begin()->second; | |
167 ss_map_.erase(ss_map_.begin()); | |
168 ss_map_[timestamp] = gof; | |
169 } | |
170 | |
171 bool Vp9SsMap::UpdatePacket(VCMPacket* packet) const { | |
172 uint8_t gof_idx = packet->codecSpecificHeader.codecHeader.VP9.gof_idx; | |
173 if (gof_idx == kNoGofIdx) | |
174 return false; // No update needed. | |
175 | |
176 SsMap::const_iterator it; | |
177 if (!Find(packet->timestamp, &it)) | |
178 return false; // Corresponding SS not yet received. | |
179 | |
180 if (gof_idx >= it->second.num_frames_in_gof) | |
181 return false; // Corresponding SS not yet received. | |
182 | |
183 RTPVideoHeaderVP9& vp9 = packet->codecSpecificHeader.codecHeader.VP9; | |
stefan-webrtc
2015/10/16 06:45:06
Make this a pointer instead.
åsapersson
2015/10/16 10:52:26
Done.
| |
184 vp9.temporal_idx = it->second.temporal_idx[gof_idx]; | |
185 vp9.temporal_up_switch = it->second.temporal_up_switch[gof_idx]; | |
186 | |
187 // TODO(asapersson): Set vp9.ref_picture_id[i] and add usage. | |
188 vp9.num_ref_pics = it->second.num_ref_pics[gof_idx]; | |
189 for (size_t i = 0; i < it->second.num_ref_pics[gof_idx]; ++i) { | |
190 vp9.pid_diff[i] = it->second.pid_diff[gof_idx][i]; | |
191 } | |
192 return true; | |
193 } | |
194 | |
195 void Vp9SsMap::UpdateFrames(FrameList* frames) const { | |
196 for (const auto& it : *frames) { | |
197 uint8_t gof_idx = it.second->CodecSpecific()->codecSpecific.VP9.gof_idx; | |
198 if (gof_idx == kNoGofIdx) { | |
199 continue; | |
200 } | |
201 SsMap::const_iterator ss_iter; | |
202 if (Find(it.second->TimeStamp(), &ss_iter)) { | |
203 if (gof_idx >= ss_iter->second.num_frames_in_gof) { | |
204 continue; // Corresponding SS not yet received. | |
205 } | |
206 it.second->SetGofInfo(ss_iter->second, gof_idx); | |
207 } | |
208 } | |
209 } | |
210 | |
116 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, | 211 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, |
117 rtc::scoped_ptr<EventWrapper> event) | 212 rtc::scoped_ptr<EventWrapper> event) |
118 : clock_(clock), | 213 : clock_(clock), |
119 running_(false), | 214 running_(false), |
120 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | 215 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
121 frame_event_(event.Pass()), | 216 frame_event_(event.Pass()), |
122 max_number_of_frames_(kStartNumberOfFrames), | 217 max_number_of_frames_(kStartNumberOfFrames), |
123 free_frames_(), | 218 free_frames_(), |
124 decodable_frames_(), | 219 decodable_frames_(), |
125 incomplete_frames_(), | 220 incomplete_frames_(), |
126 last_decoded_state_(), | 221 last_decoded_state_(), |
127 first_packet_since_reset_(true), | 222 first_packet_since_reset_(true), |
128 last_gof_timestamp_(0), | |
129 last_gof_valid_(false), | |
130 stats_callback_(NULL), | 223 stats_callback_(NULL), |
131 incoming_frame_rate_(0), | 224 incoming_frame_rate_(0), |
132 incoming_frame_count_(0), | 225 incoming_frame_count_(0), |
133 time_last_incoming_frame_count_(0), | 226 time_last_incoming_frame_count_(0), |
134 incoming_bit_count_(0), | 227 incoming_bit_count_(0), |
135 incoming_bit_rate_(0), | 228 incoming_bit_rate_(0), |
136 num_consecutive_old_packets_(0), | 229 num_consecutive_old_packets_(0), |
137 num_packets_(0), | 230 num_packets_(0), |
138 num_duplicated_packets_(0), | 231 num_duplicated_packets_(0), |
139 num_discarded_packets_(0), | 232 num_discarded_packets_(0), |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 num_discarded_packets_ = 0; | 308 num_discarded_packets_ = 0; |
216 time_first_packet_ms_ = 0; | 309 time_first_packet_ms_ = 0; |
217 | 310 |
218 // Start in a non-signaled state. | 311 // Start in a non-signaled state. |
219 waiting_for_completion_.frame_size = 0; | 312 waiting_for_completion_.frame_size = 0; |
220 waiting_for_completion_.timestamp = 0; | 313 waiting_for_completion_.timestamp = 0; |
221 waiting_for_completion_.latest_packet_time = -1; | 314 waiting_for_completion_.latest_packet_time = -1; |
222 first_packet_since_reset_ = true; | 315 first_packet_since_reset_ = true; |
223 rtt_ms_ = kDefaultRtt; | 316 rtt_ms_ = kDefaultRtt; |
224 last_decoded_state_.Reset(); | 317 last_decoded_state_.Reset(); |
225 last_gof_valid_ = false; | 318 vp9_ss_map_.Reset(); |
226 } | 319 } |
227 | 320 |
228 void VCMJitterBuffer::Stop() { | 321 void VCMJitterBuffer::Stop() { |
229 crit_sect_->Enter(); | 322 crit_sect_->Enter(); |
230 UpdateHistograms(); | 323 UpdateHistograms(); |
231 running_ = false; | 324 running_ = false; |
232 last_decoded_state_.Reset(); | 325 last_decoded_state_.Reset(); |
233 last_gof_valid_ = false; | 326 vp9_ss_map_.Reset(); |
234 | 327 |
235 // Make sure all frames are free and reset. | 328 // Make sure all frames are free and reset. |
236 for (FrameList::iterator it = decodable_frames_.begin(); | 329 for (FrameList::iterator it = decodable_frames_.begin(); |
237 it != decodable_frames_.end(); ++it) { | 330 it != decodable_frames_.end(); ++it) { |
238 free_frames_.push_back(it->second); | 331 free_frames_.push_back(it->second); |
239 } | 332 } |
240 for (FrameList::iterator it = incomplete_frames_.begin(); | 333 for (FrameList::iterator it = incomplete_frames_.begin(); |
241 it != incomplete_frames_.end(); ++it) { | 334 it != incomplete_frames_.end(); ++it) { |
242 free_frames_.push_back(it->second); | 335 free_frames_.push_back(it->second); |
243 } | 336 } |
(...skipping 11 matching lines...) Expand all Loading... | |
255 bool VCMJitterBuffer::Running() const { | 348 bool VCMJitterBuffer::Running() const { |
256 CriticalSectionScoped cs(crit_sect_); | 349 CriticalSectionScoped cs(crit_sect_); |
257 return running_; | 350 return running_; |
258 } | 351 } |
259 | 352 |
260 void VCMJitterBuffer::Flush() { | 353 void VCMJitterBuffer::Flush() { |
261 CriticalSectionScoped cs(crit_sect_); | 354 CriticalSectionScoped cs(crit_sect_); |
262 decodable_frames_.Reset(&free_frames_); | 355 decodable_frames_.Reset(&free_frames_); |
263 incomplete_frames_.Reset(&free_frames_); | 356 incomplete_frames_.Reset(&free_frames_); |
264 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. | 357 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. |
265 last_gof_valid_ = false; | 358 vp9_ss_map_.Reset(); |
266 num_consecutive_old_packets_ = 0; | 359 num_consecutive_old_packets_ = 0; |
267 // Also reset the jitter and delay estimates | 360 // Also reset the jitter and delay estimates |
268 jitter_estimate_.Reset(); | 361 jitter_estimate_.Reset(); |
269 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); | 362 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); |
270 waiting_for_completion_.frame_size = 0; | 363 waiting_for_completion_.frame_size = 0; |
271 waiting_for_completion_.timestamp = 0; | 364 waiting_for_completion_.timestamp = 0; |
272 waiting_for_completion_.latest_packet_time = -1; | 365 waiting_for_completion_.latest_packet_time = -1; |
273 first_packet_since_reset_ = true; | 366 first_packet_since_reset_ = true; |
274 missing_sequence_numbers_.clear(); | 367 missing_sequence_numbers_.clear(); |
275 } | 368 } |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
586 LOG(LS_WARNING) | 679 LOG(LS_WARNING) |
587 << num_consecutive_old_packets_ | 680 << num_consecutive_old_packets_ |
588 << " consecutive old packets received. Flushing the jitter buffer."; | 681 << " consecutive old packets received. Flushing the jitter buffer."; |
589 Flush(); | 682 Flush(); |
590 return kFlushIndicator; | 683 return kFlushIndicator; |
591 } | 684 } |
592 return kOldPacket; | 685 return kOldPacket; |
593 } | 686 } |
594 | 687 |
595 if (packet.codec == kVideoCodecVP9) { | 688 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) { | 689 if (packet.codecSpecificHeader.codecHeader.VP9.flexible_mode) { |
599 // TODO(asapersson): Add support for flexible mode. | 690 // TODO(asapersson): Add support for flexible mode. |
600 return kGeneralError; | 691 return kGeneralError; |
601 } | 692 } |
602 if (packet.codecSpecificHeader.codecHeader.VP9.ss_data_available) { | 693 if (vp9_ss_map_.Insert(packet)) { |
603 if (!last_gof_valid_ || | 694 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 } | 695 } |
611 if (last_gof_valid_ && | 696 vp9_ss_map_.UpdatePacket(const_cast<VCMPacket*>(&packet)); |
612 !packet.codecSpecificHeader.codecHeader.VP9.flexible_mode) { | 697 |
613 uint8_t gof_idx = packet.codecSpecificHeader.codecHeader.VP9.gof_idx; | 698 if (!last_decoded_state_.in_initial_state()) |
614 if (gof_idx != kNoGofIdx) { | 699 vp9_ss_map_.RemoveOld(last_decoded_state_.time_stamp()); |
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 } | 700 } |
626 | 701 |
627 num_consecutive_old_packets_ = 0; | 702 num_consecutive_old_packets_ = 0; |
628 | 703 |
629 VCMFrameBuffer* frame; | 704 VCMFrameBuffer* frame; |
630 FrameList* frame_list; | 705 FrameList* frame_list; |
631 const VCMFrameBufferEnum error = GetFrame(packet, &frame, &frame_list); | 706 const VCMFrameBufferEnum error = GetFrame(packet, &frame, &frame_list); |
632 if (error != kNoError) | 707 if (error != kNoError) |
633 return error; | 708 return error; |
634 | 709 |
(...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1253 } | 1328 } |
1254 // Evaluate if the RTT is higher than |high_rtt_nack_threshold_ms_|, and in | 1329 // Evaluate if the RTT is higher than |high_rtt_nack_threshold_ms_|, and in |
1255 // that case we don't wait for retransmissions. | 1330 // that case we don't wait for retransmissions. |
1256 if (high_rtt_nack_threshold_ms_ >= 0 && | 1331 if (high_rtt_nack_threshold_ms_ >= 0 && |
1257 rtt_ms_ >= high_rtt_nack_threshold_ms_) { | 1332 rtt_ms_ >= high_rtt_nack_threshold_ms_) { |
1258 return false; | 1333 return false; |
1259 } | 1334 } |
1260 return true; | 1335 return true; |
1261 } | 1336 } |
1262 } // namespace webrtc | 1337 } // namespace webrtc |
OLD | NEW |