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/base/checks.h" |
17 #include "webrtc/base/trace_event.h" | 18 #include "webrtc/base/trace_event.h" |
| 19 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" |
18 #include "webrtc/modules/video_coding/main/interface/video_coding.h" | 20 #include "webrtc/modules/video_coding/main/interface/video_coding.h" |
19 #include "webrtc/modules/video_coding/main/source/frame_buffer.h" | 21 #include "webrtc/modules/video_coding/main/source/frame_buffer.h" |
20 #include "webrtc/modules/video_coding/main/source/inter_frame_delay.h" | 22 #include "webrtc/modules/video_coding/main/source/inter_frame_delay.h" |
21 #include "webrtc/modules/video_coding/main/source/internal_defines.h" | 23 #include "webrtc/modules/video_coding/main/source/internal_defines.h" |
22 #include "webrtc/modules/video_coding/main/source/jitter_buffer_common.h" | 24 #include "webrtc/modules/video_coding/main/source/jitter_buffer_common.h" |
23 #include "webrtc/modules/video_coding/main/source/jitter_estimator.h" | 25 #include "webrtc/modules/video_coding/main/source/jitter_estimator.h" |
24 #include "webrtc/modules/video_coding/main/source/packet.h" | 26 #include "webrtc/modules/video_coding/main/source/packet.h" |
25 #include "webrtc/system_wrappers/interface/clock.h" | 27 #include "webrtc/system_wrappers/interface/clock.h" |
26 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" | 28 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
27 #include "webrtc/system_wrappers/interface/event_wrapper.h" | 29 #include "webrtc/system_wrappers/interface/event_wrapper.h" |
28 #include "webrtc/system_wrappers/interface/logging.h" | 30 #include "webrtc/system_wrappers/interface/logging.h" |
29 #include "webrtc/system_wrappers/interface/metrics.h" | 31 #include "webrtc/system_wrappers/interface/metrics.h" |
30 | 32 |
31 namespace webrtc { | 33 namespace webrtc { |
32 | 34 |
| 35 // Interval for updating SS data. |
| 36 static const uint32_t kSsCleanupIntervalSec = 60; |
| 37 |
33 // Use this rtt if no value has been reported. | 38 // Use this rtt if no value has been reported. |
34 static const int64_t kDefaultRtt = 200; | 39 static const int64_t kDefaultRtt = 200; |
35 | 40 |
36 typedef std::pair<uint32_t, VCMFrameBuffer*> FrameListPair; | 41 typedef std::pair<uint32_t, VCMFrameBuffer*> FrameListPair; |
37 | 42 |
38 bool IsKeyFrame(FrameListPair pair) { | 43 bool IsKeyFrame(FrameListPair pair) { |
39 return pair.second->FrameType() == kVideoFrameKey; | 44 return pair.second->FrameType() == kVideoFrameKey; |
40 } | 45 } |
41 | 46 |
42 bool HasNonEmptyState(FrameListPair pair) { | 47 bool HasNonEmptyState(FrameListPair pair) { |
(...skipping 63 matching lines...) Loading... |
106 } | 111 } |
107 | 112 |
108 void FrameList::Reset(UnorderedFrameList* free_frames) { | 113 void FrameList::Reset(UnorderedFrameList* free_frames) { |
109 while (!empty()) { | 114 while (!empty()) { |
110 begin()->second->Reset(); | 115 begin()->second->Reset(); |
111 free_frames->push_back(begin()->second); | 116 free_frames->push_back(begin()->second); |
112 erase(begin()); | 117 erase(begin()); |
113 } | 118 } |
114 } | 119 } |
115 | 120 |
| 121 bool Vp9SsMap::Insert(const VCMPacket& packet) { |
| 122 if (!packet.codecSpecificHeader.codecHeader.VP9.ss_data_available) |
| 123 return false; |
| 124 |
| 125 ss_map_[packet.timestamp] = packet.codecSpecificHeader.codecHeader.VP9.gof; |
| 126 return true; |
| 127 } |
| 128 |
| 129 void Vp9SsMap::Reset() { |
| 130 ss_map_.clear(); |
| 131 } |
| 132 |
| 133 bool Vp9SsMap::Find(uint32_t timestamp, SsMap::iterator* it_out) { |
| 134 bool found = false; |
| 135 for (SsMap::iterator it = ss_map_.begin(); it != ss_map_.end(); ++it) { |
| 136 if (it->first == timestamp || IsNewerTimestamp(timestamp, it->first)) { |
| 137 *it_out = it; |
| 138 found = true; |
| 139 } |
| 140 } |
| 141 return found; |
| 142 } |
| 143 |
| 144 void Vp9SsMap::RemoveOld(uint32_t timestamp) { |
| 145 if (!TimeForCleanup(timestamp)) |
| 146 return; |
| 147 |
| 148 SsMap::iterator it; |
| 149 if (!Find(timestamp, &it)) |
| 150 return; |
| 151 |
| 152 ss_map_.erase(ss_map_.begin(), it); |
| 153 AdvanceFront(timestamp); |
| 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 RTC_DCHECK(!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) { |
| 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::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; // Assume corresponding SS not yet received. |
| 182 |
| 183 RTPVideoHeaderVP9* vp9 = &packet->codecSpecificHeader.codecHeader.VP9; |
| 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) { |
| 196 for (const auto& frame_it : *frames) { |
| 197 uint8_t gof_idx = |
| 198 frame_it.second->CodecSpecific()->codecSpecific.VP9.gof_idx; |
| 199 if (gof_idx == kNoGofIdx) { |
| 200 continue; |
| 201 } |
| 202 SsMap::iterator ss_it; |
| 203 if (Find(frame_it.second->TimeStamp(), &ss_it)) { |
| 204 if (gof_idx >= ss_it->second.num_frames_in_gof) { |
| 205 continue; // Assume corresponding SS not yet received. |
| 206 } |
| 207 frame_it.second->SetGofInfo(ss_it->second, gof_idx); |
| 208 } |
| 209 } |
| 210 } |
| 211 |
116 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, | 212 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, |
117 rtc::scoped_ptr<EventWrapper> event) | 213 rtc::scoped_ptr<EventWrapper> event) |
118 : clock_(clock), | 214 : clock_(clock), |
119 running_(false), | 215 running_(false), |
120 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | 216 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
121 frame_event_(event.Pass()), | 217 frame_event_(event.Pass()), |
122 max_number_of_frames_(kStartNumberOfFrames), | 218 max_number_of_frames_(kStartNumberOfFrames), |
123 free_frames_(), | 219 free_frames_(), |
124 decodable_frames_(), | 220 decodable_frames_(), |
125 incomplete_frames_(), | 221 incomplete_frames_(), |
126 last_decoded_state_(), | 222 last_decoded_state_(), |
127 first_packet_since_reset_(true), | 223 first_packet_since_reset_(true), |
128 last_gof_timestamp_(0), | |
129 last_gof_valid_(false), | |
130 stats_callback_(NULL), | 224 stats_callback_(NULL), |
131 incoming_frame_rate_(0), | 225 incoming_frame_rate_(0), |
132 incoming_frame_count_(0), | 226 incoming_frame_count_(0), |
133 time_last_incoming_frame_count_(0), | 227 time_last_incoming_frame_count_(0), |
134 incoming_bit_count_(0), | 228 incoming_bit_count_(0), |
135 incoming_bit_rate_(0), | 229 incoming_bit_rate_(0), |
136 num_consecutive_old_packets_(0), | 230 num_consecutive_old_packets_(0), |
137 num_packets_(0), | 231 num_packets_(0), |
138 num_duplicated_packets_(0), | 232 num_duplicated_packets_(0), |
139 num_discarded_packets_(0), | 233 num_discarded_packets_(0), |
(...skipping 75 matching lines...) Loading... |
215 num_discarded_packets_ = 0; | 309 num_discarded_packets_ = 0; |
216 time_first_packet_ms_ = 0; | 310 time_first_packet_ms_ = 0; |
217 | 311 |
218 // Start in a non-signaled state. | 312 // Start in a non-signaled state. |
219 waiting_for_completion_.frame_size = 0; | 313 waiting_for_completion_.frame_size = 0; |
220 waiting_for_completion_.timestamp = 0; | 314 waiting_for_completion_.timestamp = 0; |
221 waiting_for_completion_.latest_packet_time = -1; | 315 waiting_for_completion_.latest_packet_time = -1; |
222 first_packet_since_reset_ = true; | 316 first_packet_since_reset_ = true; |
223 rtt_ms_ = kDefaultRtt; | 317 rtt_ms_ = kDefaultRtt; |
224 last_decoded_state_.Reset(); | 318 last_decoded_state_.Reset(); |
225 last_gof_valid_ = false; | 319 vp9_ss_map_.Reset(); |
226 } | 320 } |
227 | 321 |
228 void VCMJitterBuffer::Stop() { | 322 void VCMJitterBuffer::Stop() { |
229 crit_sect_->Enter(); | 323 crit_sect_->Enter(); |
230 UpdateHistograms(); | 324 UpdateHistograms(); |
231 running_ = false; | 325 running_ = false; |
232 last_decoded_state_.Reset(); | 326 last_decoded_state_.Reset(); |
233 last_gof_valid_ = false; | 327 vp9_ss_map_.Reset(); |
234 | 328 |
235 // Make sure all frames are free and reset. | 329 // Make sure all frames are free and reset. |
236 for (FrameList::iterator it = decodable_frames_.begin(); | 330 for (FrameList::iterator it = decodable_frames_.begin(); |
237 it != decodable_frames_.end(); ++it) { | 331 it != decodable_frames_.end(); ++it) { |
238 free_frames_.push_back(it->second); | 332 free_frames_.push_back(it->second); |
239 } | 333 } |
240 for (FrameList::iterator it = incomplete_frames_.begin(); | 334 for (FrameList::iterator it = incomplete_frames_.begin(); |
241 it != incomplete_frames_.end(); ++it) { | 335 it != incomplete_frames_.end(); ++it) { |
242 free_frames_.push_back(it->second); | 336 free_frames_.push_back(it->second); |
243 } | 337 } |
(...skipping 11 matching lines...) Loading... |
255 bool VCMJitterBuffer::Running() const { | 349 bool VCMJitterBuffer::Running() const { |
256 CriticalSectionScoped cs(crit_sect_); | 350 CriticalSectionScoped cs(crit_sect_); |
257 return running_; | 351 return running_; |
258 } | 352 } |
259 | 353 |
260 void VCMJitterBuffer::Flush() { | 354 void VCMJitterBuffer::Flush() { |
261 CriticalSectionScoped cs(crit_sect_); | 355 CriticalSectionScoped cs(crit_sect_); |
262 decodable_frames_.Reset(&free_frames_); | 356 decodable_frames_.Reset(&free_frames_); |
263 incomplete_frames_.Reset(&free_frames_); | 357 incomplete_frames_.Reset(&free_frames_); |
264 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. | 358 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. |
265 last_gof_valid_ = false; | 359 vp9_ss_map_.Reset(); |
266 num_consecutive_old_packets_ = 0; | 360 num_consecutive_old_packets_ = 0; |
267 // Also reset the jitter and delay estimates | 361 // Also reset the jitter and delay estimates |
268 jitter_estimate_.Reset(); | 362 jitter_estimate_.Reset(); |
269 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); | 363 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); |
270 waiting_for_completion_.frame_size = 0; | 364 waiting_for_completion_.frame_size = 0; |
271 waiting_for_completion_.timestamp = 0; | 365 waiting_for_completion_.timestamp = 0; |
272 waiting_for_completion_.latest_packet_time = -1; | 366 waiting_for_completion_.latest_packet_time = -1; |
273 first_packet_since_reset_ = true; | 367 first_packet_since_reset_ = true; |
274 missing_sequence_numbers_.clear(); | 368 missing_sequence_numbers_.clear(); |
275 } | 369 } |
(...skipping 309 matching lines...) Loading... |
585 if (num_consecutive_old_packets_ > kMaxConsecutiveOldPackets) { | 679 if (num_consecutive_old_packets_ > kMaxConsecutiveOldPackets) { |
586 LOG(LS_WARNING) | 680 LOG(LS_WARNING) |
587 << num_consecutive_old_packets_ | 681 << num_consecutive_old_packets_ |
588 << " consecutive old packets received. Flushing the jitter buffer."; | 682 << " consecutive old packets received. Flushing the jitter buffer."; |
589 Flush(); | 683 Flush(); |
590 return kFlushIndicator; | 684 return kFlushIndicator; |
591 } | 685 } |
592 return kOldPacket; | 686 return kOldPacket; |
593 } | 687 } |
594 | 688 |
| 689 num_consecutive_old_packets_ = 0; |
| 690 |
595 if (packet.codec == kVideoCodecVP9) { | 691 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) { | 692 if (packet.codecSpecificHeader.codecHeader.VP9.flexible_mode) { |
599 // TODO(asapersson): Add support for flexible mode. | 693 // TODO(asapersson): Add support for flexible mode. |
600 return kGeneralError; | 694 return kGeneralError; |
601 } | 695 } |
602 if (packet.codecSpecificHeader.codecHeader.VP9.ss_data_available) { | 696 if (!packet.codecSpecificHeader.codecHeader.VP9.flexible_mode) { |
603 if (!last_gof_valid_ || | 697 if (vp9_ss_map_.Insert(packet)) |
604 IsNewerTimestamp(packet.timestamp, last_gof_timestamp_)) { | 698 vp9_ss_map_.UpdateFrames(&incomplete_frames_); |
605 last_gof_.CopyGofInfoVP9( | 699 |
606 packet.codecSpecificHeader.codecHeader.VP9.gof); | 700 vp9_ss_map_.UpdatePacket(const_cast<VCMPacket*>(&packet)); |
607 last_gof_timestamp_ = packet.timestamp; | |
608 last_gof_valid_ = true; | |
609 } | |
610 } | 701 } |
611 if (last_gof_valid_ && | 702 if (!last_decoded_state_.in_initial_state()) |
612 !packet.codecSpecificHeader.codecHeader.VP9.flexible_mode) { | 703 vp9_ss_map_.RemoveOld(last_decoded_state_.time_stamp()); |
613 uint8_t gof_idx = packet.codecSpecificHeader.codecHeader.VP9.gof_idx; | |
614 if (gof_idx != kNoGofIdx) { | |
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 } | 704 } |
626 | 705 |
627 num_consecutive_old_packets_ = 0; | |
628 | |
629 VCMFrameBuffer* frame; | 706 VCMFrameBuffer* frame; |
630 FrameList* frame_list; | 707 FrameList* frame_list; |
631 const VCMFrameBufferEnum error = GetFrame(packet, &frame, &frame_list); | 708 const VCMFrameBufferEnum error = GetFrame(packet, &frame, &frame_list); |
632 if (error != kNoError) | 709 if (error != kNoError) |
633 return error; | 710 return error; |
634 | 711 |
635 int64_t now_ms = clock_->TimeInMilliseconds(); | 712 int64_t now_ms = clock_->TimeInMilliseconds(); |
636 // We are keeping track of the first and latest seq numbers, and | 713 // We are keeping track of the first and latest seq numbers, and |
637 // the number of wraps to be able to calculate how many packets we expect. | 714 // the number of wraps to be able to calculate how many packets we expect. |
638 if (first_packet_since_reset_) { | 715 if (first_packet_since_reset_) { |
(...skipping 614 matching lines...) Loading... |
1253 } | 1330 } |
1254 // Evaluate if the RTT is higher than |high_rtt_nack_threshold_ms_|, and in | 1331 // Evaluate if the RTT is higher than |high_rtt_nack_threshold_ms_|, and in |
1255 // that case we don't wait for retransmissions. | 1332 // that case we don't wait for retransmissions. |
1256 if (high_rtt_nack_threshold_ms_ >= 0 && | 1333 if (high_rtt_nack_threshold_ms_ >= 0 && |
1257 rtt_ms_ >= high_rtt_nack_threshold_ms_) { | 1334 rtt_ms_ >= high_rtt_nack_threshold_ms_) { |
1258 return false; | 1335 return false; |
1259 } | 1336 } |
1260 return true; | 1337 return true; |
1261 } | 1338 } |
1262 } // namespace webrtc | 1339 } // namespace webrtc |
OLD | NEW |