Chromium Code Reviews| 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) { | |
|
stefan-webrtc
2015/10/16 12:26:11
I think we should break this class out to its own
åsapersson
2015/10/20 13:32:20
Ok. Will do in a follow up CL.
| |
| 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; | |
|
philipel1
2015/10/16 13:37:08
A bit more descriptive variable names would be nic
åsapersson
2015/10/20 13:32:20
Renamed to it_out.
| |
| 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; | |
|
philipel1
2015/10/16 13:37:08
Rename 'it' to 'ss_it'.
| |
| 148 if (!Find(timestamp, &it)) | |
| 149 return; | |
| 150 | |
| 151 ss_map_.erase(ss_map_.begin(), it); | |
| 152 AdvanceFront(timestamp); | |
| 153 } | |
| 154 | |
| 155 bool Vp9SsMap::TimeForCleanup(uint32_t timestamp) const { | |
| 156 if (ss_map_.empty() || !IsNewerTimestamp(timestamp, ss_map_.begin()->first)) | |
| 157 return false; | |
| 158 | |
| 159 uint32_t diff = timestamp - ss_map_.begin()->first; | |
| 160 return diff / kVideoPayloadTypeFrequency >= kSsCleanupIntervalSec; | |
| 161 } | |
| 162 | |
| 163 void Vp9SsMap::AdvanceFront(uint32_t timestamp) { | |
| 164 assert(!ss_map_.empty()); | |
| 165 GofInfoVP9 gof = ss_map_.begin()->second; | |
| 166 ss_map_.erase(ss_map_.begin()); | |
| 167 ss_map_[timestamp] = gof; | |
| 168 } | |
| 169 | |
| 170 bool Vp9SsMap::UpdatePacket(VCMPacket* packet) const { | |
| 171 uint8_t gof_idx = packet->codecSpecificHeader.codecHeader.VP9.gof_idx; | |
| 172 if (gof_idx == kNoGofIdx) | |
| 173 return false; // No update needed. | |
| 174 | |
| 175 SsMap::const_iterator it; | |
| 176 if (!Find(packet->timestamp, &it)) | |
| 177 return false; // Corresponding SS not yet received. | |
| 178 | |
| 179 if (gof_idx >= it->second.num_frames_in_gof) | |
| 180 return false; // Corresponding SS not yet received. | |
|
philipel1
2015/10/16 13:37:08
Can't differentiate between invalid input (incorre
åsapersson
2015/10/20 13:32:20
Done.
| |
| 181 | |
| 182 RTPVideoHeaderVP9* vp9 = &packet->codecSpecificHeader.codecHeader.VP9; | |
| 183 vp9->temporal_idx = it->second.temporal_idx[gof_idx]; | |
| 184 vp9->temporal_up_switch = it->second.temporal_up_switch[gof_idx]; | |
| 185 | |
| 186 // TODO(asapersson): Set vp9.ref_picture_id[i] and add usage. | |
| 187 vp9->num_ref_pics = it->second.num_ref_pics[gof_idx]; | |
| 188 for (size_t i = 0; i < it->second.num_ref_pics[gof_idx]; ++i) { | |
| 189 vp9->pid_diff[i] = it->second.pid_diff[gof_idx][i]; | |
| 190 } | |
| 191 return true; | |
| 192 } | |
| 193 | |
| 194 void Vp9SsMap::UpdateFrames(FrameList* frames) const { | |
| 195 for (const auto& it : *frames) { | |
|
philipel1
2015/10/16 13:37:08
Rename 'it' to 'frame_it'.
åsapersson
2015/10/20 13:32:20
Done.
| |
| 196 uint8_t gof_idx = it.second->CodecSpecific()->codecSpecific.VP9.gof_idx; | |
| 197 if (gof_idx == kNoGofIdx) { | |
| 198 continue; | |
| 199 } | |
| 200 SsMap::const_iterator ss_iter; | |
| 201 if (Find(it.second->TimeStamp(), &ss_iter)) { | |
| 202 if (gof_idx >= ss_iter->second.num_frames_in_gof) { | |
| 203 continue; // Corresponding SS not yet received. | |
|
philipel1
2015/10/16 13:37:08
Again, assuming SS has not been received yet.
åsapersson
2015/10/20 13:32:20
Done.
| |
| 204 } | |
| 205 it.second->SetGofInfo(ss_iter->second, gof_idx); | |
| 206 } | |
| 207 } | |
| 208 } | |
| 209 | |
| 116 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, | 210 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, |
| 117 rtc::scoped_ptr<EventWrapper> event) | 211 rtc::scoped_ptr<EventWrapper> event) |
| 118 : clock_(clock), | 212 : clock_(clock), |
| 119 running_(false), | 213 running_(false), |
| 120 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | 214 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
| 121 frame_event_(event.Pass()), | 215 frame_event_(event.Pass()), |
| 122 max_number_of_frames_(kStartNumberOfFrames), | 216 max_number_of_frames_(kStartNumberOfFrames), |
| 123 free_frames_(), | 217 free_frames_(), |
| 124 decodable_frames_(), | 218 decodable_frames_(), |
| 125 incomplete_frames_(), | 219 incomplete_frames_(), |
| 126 last_decoded_state_(), | 220 last_decoded_state_(), |
| 127 first_packet_since_reset_(true), | 221 first_packet_since_reset_(true), |
| 128 last_gof_timestamp_(0), | |
| 129 last_gof_valid_(false), | |
| 130 stats_callback_(NULL), | 222 stats_callback_(NULL), |
| 131 incoming_frame_rate_(0), | 223 incoming_frame_rate_(0), |
| 132 incoming_frame_count_(0), | 224 incoming_frame_count_(0), |
| 133 time_last_incoming_frame_count_(0), | 225 time_last_incoming_frame_count_(0), |
| 134 incoming_bit_count_(0), | 226 incoming_bit_count_(0), |
| 135 incoming_bit_rate_(0), | 227 incoming_bit_rate_(0), |
| 136 num_consecutive_old_packets_(0), | 228 num_consecutive_old_packets_(0), |
| 137 num_packets_(0), | 229 num_packets_(0), |
| 138 num_duplicated_packets_(0), | 230 num_duplicated_packets_(0), |
| 139 num_discarded_packets_(0), | 231 num_discarded_packets_(0), |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 num_discarded_packets_ = 0; | 307 num_discarded_packets_ = 0; |
| 216 time_first_packet_ms_ = 0; | 308 time_first_packet_ms_ = 0; |
| 217 | 309 |
| 218 // Start in a non-signaled state. | 310 // Start in a non-signaled state. |
| 219 waiting_for_completion_.frame_size = 0; | 311 waiting_for_completion_.frame_size = 0; |
| 220 waiting_for_completion_.timestamp = 0; | 312 waiting_for_completion_.timestamp = 0; |
| 221 waiting_for_completion_.latest_packet_time = -1; | 313 waiting_for_completion_.latest_packet_time = -1; |
| 222 first_packet_since_reset_ = true; | 314 first_packet_since_reset_ = true; |
| 223 rtt_ms_ = kDefaultRtt; | 315 rtt_ms_ = kDefaultRtt; |
| 224 last_decoded_state_.Reset(); | 316 last_decoded_state_.Reset(); |
| 225 last_gof_valid_ = false; | 317 vp9_ss_map_.Reset(); |
| 226 } | 318 } |
| 227 | 319 |
| 228 void VCMJitterBuffer::Stop() { | 320 void VCMJitterBuffer::Stop() { |
| 229 crit_sect_->Enter(); | 321 crit_sect_->Enter(); |
| 230 UpdateHistograms(); | 322 UpdateHistograms(); |
| 231 running_ = false; | 323 running_ = false; |
| 232 last_decoded_state_.Reset(); | 324 last_decoded_state_.Reset(); |
| 233 last_gof_valid_ = false; | 325 vp9_ss_map_.Reset(); |
| 234 | 326 |
| 235 // Make sure all frames are free and reset. | 327 // Make sure all frames are free and reset. |
| 236 for (FrameList::iterator it = decodable_frames_.begin(); | 328 for (FrameList::iterator it = decodable_frames_.begin(); |
| 237 it != decodable_frames_.end(); ++it) { | 329 it != decodable_frames_.end(); ++it) { |
| 238 free_frames_.push_back(it->second); | 330 free_frames_.push_back(it->second); |
| 239 } | 331 } |
| 240 for (FrameList::iterator it = incomplete_frames_.begin(); | 332 for (FrameList::iterator it = incomplete_frames_.begin(); |
| 241 it != incomplete_frames_.end(); ++it) { | 333 it != incomplete_frames_.end(); ++it) { |
| 242 free_frames_.push_back(it->second); | 334 free_frames_.push_back(it->second); |
| 243 } | 335 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 255 bool VCMJitterBuffer::Running() const { | 347 bool VCMJitterBuffer::Running() const { |
| 256 CriticalSectionScoped cs(crit_sect_); | 348 CriticalSectionScoped cs(crit_sect_); |
| 257 return running_; | 349 return running_; |
| 258 } | 350 } |
| 259 | 351 |
| 260 void VCMJitterBuffer::Flush() { | 352 void VCMJitterBuffer::Flush() { |
| 261 CriticalSectionScoped cs(crit_sect_); | 353 CriticalSectionScoped cs(crit_sect_); |
| 262 decodable_frames_.Reset(&free_frames_); | 354 decodable_frames_.Reset(&free_frames_); |
| 263 incomplete_frames_.Reset(&free_frames_); | 355 incomplete_frames_.Reset(&free_frames_); |
| 264 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. | 356 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. |
| 265 last_gof_valid_ = false; | 357 vp9_ss_map_.Reset(); |
| 266 num_consecutive_old_packets_ = 0; | 358 num_consecutive_old_packets_ = 0; |
| 267 // Also reset the jitter and delay estimates | 359 // Also reset the jitter and delay estimates |
| 268 jitter_estimate_.Reset(); | 360 jitter_estimate_.Reset(); |
| 269 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); | 361 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); |
| 270 waiting_for_completion_.frame_size = 0; | 362 waiting_for_completion_.frame_size = 0; |
| 271 waiting_for_completion_.timestamp = 0; | 363 waiting_for_completion_.timestamp = 0; |
| 272 waiting_for_completion_.latest_packet_time = -1; | 364 waiting_for_completion_.latest_packet_time = -1; |
| 273 first_packet_since_reset_ = true; | 365 first_packet_since_reset_ = true; |
| 274 missing_sequence_numbers_.clear(); | 366 missing_sequence_numbers_.clear(); |
| 275 } | 367 } |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 585 if (num_consecutive_old_packets_ > kMaxConsecutiveOldPackets) { | 677 if (num_consecutive_old_packets_ > kMaxConsecutiveOldPackets) { |
| 586 LOG(LS_WARNING) | 678 LOG(LS_WARNING) |
| 587 << num_consecutive_old_packets_ | 679 << num_consecutive_old_packets_ |
| 588 << " consecutive old packets received. Flushing the jitter buffer."; | 680 << " consecutive old packets received. Flushing the jitter buffer."; |
| 589 Flush(); | 681 Flush(); |
| 590 return kFlushIndicator; | 682 return kFlushIndicator; |
| 591 } | 683 } |
| 592 return kOldPacket; | 684 return kOldPacket; |
| 593 } | 685 } |
| 594 | 686 |
| 687 num_consecutive_old_packets_ = 0; | |
| 688 | |
| 595 if (packet.codec == kVideoCodecVP9) { | 689 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) { | 690 if (packet.codecSpecificHeader.codecHeader.VP9.flexible_mode) { |
| 599 // TODO(asapersson): Add support for flexible mode. | 691 // TODO(asapersson): Add support for flexible mode. |
| 600 return kGeneralError; | 692 return kGeneralError; |
| 601 } | 693 } |
| 602 if (packet.codecSpecificHeader.codecHeader.VP9.ss_data_available) { | 694 if (!packet.codecSpecificHeader.codecHeader.VP9.flexible_mode) { |
| 603 if (!last_gof_valid_ || | 695 if (vp9_ss_map_.Insert(packet)) { |
| 604 IsNewerTimestamp(packet.timestamp, last_gof_timestamp_)) { | 696 vp9_ss_map_.UpdateFrames(&incomplete_frames_); |
| 605 last_gof_.CopyGofInfoVP9( | |
| 606 packet.codecSpecificHeader.codecHeader.VP9.gof); | |
| 607 last_gof_timestamp_ = packet.timestamp; | |
| 608 last_gof_valid_ = true; | |
| 609 } | 697 } |
| 610 } | 698 vp9_ss_map_.UpdatePacket(const_cast<VCMPacket*>(&packet)); |
| 611 if (last_gof_valid_ && | 699 |
| 612 !packet.codecSpecificHeader.codecHeader.VP9.flexible_mode) { | 700 if (!last_decoded_state_.in_initial_state()) |
| 613 uint8_t gof_idx = packet.codecSpecificHeader.codecHeader.VP9.gof_idx; | 701 vp9_ss_map_.RemoveOld(last_decoded_state_.time_stamp()); |
| 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 } | 702 } |
| 625 } | 703 } |
| 626 | 704 |
| 627 num_consecutive_old_packets_ = 0; | |
| 628 | |
| 629 VCMFrameBuffer* frame; | 705 VCMFrameBuffer* frame; |
| 630 FrameList* frame_list; | 706 FrameList* frame_list; |
| 631 const VCMFrameBufferEnum error = GetFrame(packet, &frame, &frame_list); | 707 const VCMFrameBufferEnum error = GetFrame(packet, &frame, &frame_list); |
| 632 if (error != kNoError) | 708 if (error != kNoError) |
| 633 return error; | 709 return error; |
| 634 | 710 |
| 635 int64_t now_ms = clock_->TimeInMilliseconds(); | 711 int64_t now_ms = clock_->TimeInMilliseconds(); |
| 636 // We are keeping track of the first and latest seq numbers, and | 712 // 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. | 713 // the number of wraps to be able to calculate how many packets we expect. |
| 638 if (first_packet_since_reset_) { | 714 if (first_packet_since_reset_) { |
| (...skipping 614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1253 } | 1329 } |
| 1254 // Evaluate if the RTT is higher than |high_rtt_nack_threshold_ms_|, and in | 1330 // Evaluate if the RTT is higher than |high_rtt_nack_threshold_ms_|, and in |
| 1255 // that case we don't wait for retransmissions. | 1331 // that case we don't wait for retransmissions. |
| 1256 if (high_rtt_nack_threshold_ms_ >= 0 && | 1332 if (high_rtt_nack_threshold_ms_ >= 0 && |
| 1257 rtt_ms_ >= high_rtt_nack_threshold_ms_) { | 1333 rtt_ms_ >= high_rtt_nack_threshold_ms_) { |
| 1258 return false; | 1334 return false; |
| 1259 } | 1335 } |
| 1260 return true; | 1336 return true; |
| 1261 } | 1337 } |
| 1262 } // namespace webrtc | 1338 } // namespace webrtc |
| OLD | NEW |