niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | 91c6308 | 2012-01-31 10:49:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/video_coding/main/source/receiver.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 15 | #include "webrtc/modules/video_coding/main/interface/video_coding.h" |
| 16 | #include "webrtc/modules/video_coding/main/source/encoded_frame.h" |
| 17 | #include "webrtc/modules/video_coding/main/source/internal_defines.h" |
| 18 | #include "webrtc/modules/video_coding/main/source/media_opt_util.h" |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 19 | #include "webrtc/system_wrappers/interface/clock.h" |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/interface/trace.h" |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/interface/trace_event.h" |
stefan@webrtc.org | 91c6308 | 2012-01-31 10:49:08 +0000 | [diff] [blame] | 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | namespace webrtc { |
| 24 | |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 25 | enum { kMaxReceiverDelayMs = 10000 }; |
| 26 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 27 | VCMReceiver::VCMReceiver(VCMTiming* timing, |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 28 | Clock* clock, |
stefan@webrtc.org | 2baf5f5 | 2013-03-13 08:46:25 +0000 | [diff] [blame] | 29 | EventFactory* event_factory, |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 30 | int32_t vcm_id, |
| 31 | int32_t receiver_id, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | bool master) |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 33 | : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
| 34 | vcm_id_(vcm_id), |
| 35 | clock_(clock), |
| 36 | receiver_id_(receiver_id), |
| 37 | master_(master), |
stefan@webrtc.org | 2baf5f5 | 2013-03-13 08:46:25 +0000 | [diff] [blame] | 38 | jitter_buffer_(clock_, event_factory, vcm_id, receiver_id, master), |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 39 | timing_(timing), |
stefan@webrtc.org | 2baf5f5 | 2013-03-13 08:46:25 +0000 | [diff] [blame] | 40 | render_wait_event_(event_factory->CreateEvent()), |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 41 | state_(kPassive), |
| 42 | max_video_delay_ms_(kMaxVideoDelayMs) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 44 | VCMReceiver::~VCMReceiver() { |
stefan@webrtc.org | 2baf5f5 | 2013-03-13 08:46:25 +0000 | [diff] [blame] | 45 | render_wait_event_->Set(); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 46 | delete crit_sect_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 49 | void VCMReceiver::Reset() { |
| 50 | CriticalSectionScoped cs(crit_sect_); |
| 51 | if (!jitter_buffer_.Running()) { |
| 52 | jitter_buffer_.Start(); |
| 53 | } else { |
| 54 | jitter_buffer_.Flush(); |
| 55 | } |
stefan@webrtc.org | 2baf5f5 | 2013-03-13 08:46:25 +0000 | [diff] [blame] | 56 | render_wait_event_->Reset(); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 57 | if (master_) { |
| 58 | state_ = kReceiving; |
| 59 | } else { |
| 60 | state_ = kPassive; |
| 61 | } |
henrik.lundin@webrtc.org | baf6db5 | 2011-11-02 18:58:39 +0000 | [diff] [blame] | 62 | } |
| 63 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 64 | int32_t VCMReceiver::Initialize() { |
| 65 | CriticalSectionScoped cs(crit_sect_); |
| 66 | Reset(); |
| 67 | if (!master_) { |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 68 | SetNackMode(kNoNack, -1, -1); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 69 | } |
| 70 | return VCM_OK; |
| 71 | } |
| 72 | |
| 73 | void VCMReceiver::UpdateRtt(uint32_t rtt) { |
| 74 | jitter_buffer_.UpdateRtt(rtt); |
| 75 | } |
| 76 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 77 | int32_t VCMReceiver::InsertPacket(const VCMPacket& packet, |
| 78 | uint16_t frame_width, |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 79 | uint16_t frame_height) { |
| 80 | // Find an empty frame. |
| 81 | VCMEncodedFrame* buffer = NULL; |
| 82 | const int32_t error = jitter_buffer_.GetFrame(packet, buffer); |
| 83 | if (error == VCM_OLD_PACKET_ERROR) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | return VCM_OK; |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 85 | } else if (error != VCM_OK) { |
| 86 | return error; |
| 87 | } |
| 88 | assert(buffer); |
| 89 | { |
| 90 | CriticalSectionScoped cs(crit_sect_); |
| 91 | |
| 92 | if (frame_width && frame_height) { |
| 93 | buffer->SetEncodedSize(static_cast<uint32_t>(frame_width), |
| 94 | static_cast<uint32_t>(frame_height)); |
| 95 | } |
| 96 | |
| 97 | if (master_) { |
| 98 | // Only trace the primary receiver to make it possible to parse and plot |
| 99 | // the trace file. |
| 100 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCoding, |
| 101 | VCMId(vcm_id_, receiver_id_), |
| 102 | "Packet seq_no %u of frame %u at %u", |
| 103 | packet.seqNum, packet.timestamp, |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 104 | MaskWord64ToUWord32(clock_->TimeInMilliseconds())); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 105 | } |
| 106 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 107 | const int64_t now_ms = clock_->TimeInMilliseconds(); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 108 | |
| 109 | int64_t render_time_ms = timing_->RenderTimeMs(packet.timestamp, now_ms); |
| 110 | |
| 111 | if (render_time_ms < 0) { |
| 112 | // Render time error. Assume that this is due to some change in the |
| 113 | // incoming video stream and reset the JB and the timing. |
| 114 | jitter_buffer_.Flush(); |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 115 | timing_->Reset(clock_->TimeInMilliseconds()); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 116 | return VCM_FLUSH_INDICATOR; |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 117 | } else if (render_time_ms < now_ms - max_video_delay_ms_) { |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 118 | WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCoding, |
| 119 | VCMId(vcm_id_, receiver_id_), |
| 120 | "This frame should have been rendered more than %u ms ago." |
| 121 | "Flushing jitter buffer and resetting timing.", |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 122 | max_video_delay_ms_); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 123 | jitter_buffer_.Flush(); |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 124 | timing_->Reset(clock_->TimeInMilliseconds()); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 125 | return VCM_FLUSH_INDICATOR; |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 126 | } else if (static_cast<int>(timing_->TargetVideoDelay()) > |
| 127 | max_video_delay_ms_) { |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 128 | WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCoding, |
| 129 | VCMId(vcm_id_, receiver_id_), |
| 130 | "More than %u ms target delay. Flushing jitter buffer and" |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 131 | "resetting timing.", max_video_delay_ms_); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 132 | jitter_buffer_.Flush(); |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 133 | timing_->Reset(clock_->TimeInMilliseconds()); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 134 | return VCM_FLUSH_INDICATOR; |
| 135 | } |
| 136 | |
| 137 | // First packet received belonging to this frame. |
| 138 | if (buffer->Length() == 0) { |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 139 | const int64_t now_ms = clock_->TimeInMilliseconds(); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 140 | if (master_) { |
| 141 | // Only trace the primary receiver to make it possible to parse and plot |
| 142 | // the trace file. |
| 143 | WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCoding, |
| 144 | VCMId(vcm_id_, receiver_id_), |
| 145 | "First packet of frame %u at %u", packet.timestamp, |
| 146 | MaskWord64ToUWord32(now_ms)); |
| 147 | } |
mikhal@webrtc.org | e0e029e | 2013-04-19 21:11:41 +0000 | [diff] [blame] | 148 | render_time_ms = timing_->RenderTimeMs(packet.timestamp, now_ms); |
| 149 | if (render_time_ms >= 0) { |
| 150 | buffer->SetRenderTime(render_time_ms); |
| 151 | } else { |
| 152 | buffer->SetRenderTime(now_ms); |
| 153 | } |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Insert packet into the jitter buffer both media and empty packets. |
| 157 | const VCMFrameBufferEnum |
| 158 | ret = jitter_buffer_.InsertPacket(buffer, packet); |
| 159 | if (ret == kFlushIndicator) { |
| 160 | return VCM_FLUSH_INDICATOR; |
| 161 | } else if (ret < 0) { |
| 162 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCoding, |
| 163 | VCMId(vcm_id_, receiver_id_), |
| 164 | "Error inserting packet seq_no=%u, time_stamp=%u", |
| 165 | packet.seqNum, packet.timestamp); |
| 166 | return VCM_JITTER_BUFFER_ERROR; |
| 167 | } |
| 168 | } |
| 169 | return VCM_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | } |
| 171 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 172 | VCMEncodedFrame* VCMReceiver::FrameForDecoding( |
| 173 | uint16_t max_wait_time_ms, |
| 174 | int64_t& next_render_time_ms, |
| 175 | bool render_timing, |
| 176 | VCMReceiver* dual_receiver) { |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 177 | TRACE_EVENT0("webrtc", "Recv::FrameForDecoding"); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 178 | // No need to enter the critical section here since the jitter buffer |
| 179 | // is thread-safe. |
| 180 | FrameType incoming_frame_type = kVideoFrameDelta; |
| 181 | next_render_time_ms = -1; |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 182 | const int64_t start_time_ms = clock_->TimeInMilliseconds(); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 183 | int64_t ret = jitter_buffer_.NextTimestamp(max_wait_time_ms, |
| 184 | &incoming_frame_type, |
| 185 | &next_render_time_ms); |
| 186 | if (ret < 0) { |
| 187 | // No timestamp in jitter buffer at the moment. |
| 188 | return NULL; |
| 189 | } |
mikhal@webrtc.org | e0e029e | 2013-04-19 21:11:41 +0000 | [diff] [blame] | 190 | const uint32_t time_stamp = static_cast<uint32_t>(ret); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 191 | |
mikhal@webrtc.org | e0e029e | 2013-04-19 21:11:41 +0000 | [diff] [blame] | 192 | // Update the timing. |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 193 | timing_->SetRequiredDelay(jitter_buffer_.EstimatedJitterMs()); |
mikhal@webrtc.org | e0e029e | 2013-04-19 21:11:41 +0000 | [diff] [blame] | 194 | timing_->UpdateCurrentDelay(time_stamp); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 195 | |
| 196 | const int32_t temp_wait_time = max_wait_time_ms - |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 197 | static_cast<int32_t>(clock_->TimeInMilliseconds() - start_time_ms); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 198 | uint16_t new_max_wait_time = static_cast<uint16_t>(VCM_MAX(temp_wait_time, |
| 199 | 0)); |
| 200 | |
| 201 | VCMEncodedFrame* frame = NULL; |
| 202 | |
| 203 | if (render_timing) { |
| 204 | frame = FrameForDecoding(new_max_wait_time, next_render_time_ms, |
| 205 | dual_receiver); |
| 206 | } else { |
| 207 | frame = FrameForRendering(new_max_wait_time, next_render_time_ms, |
| 208 | dual_receiver); |
| 209 | } |
| 210 | |
| 211 | if (frame != NULL) { |
| 212 | bool retransmitted = false; |
| 213 | const int64_t last_packet_time_ms = |
| 214 | jitter_buffer_.LastPacketTime(frame, &retransmitted); |
| 215 | if (last_packet_time_ms >= 0 && !retransmitted) { |
| 216 | // We don't want to include timestamps which have suffered from |
| 217 | // retransmission here, since we compensate with extra retransmission |
| 218 | // delay within the jitter estimate. |
mikhal@webrtc.org | e0e029e | 2013-04-19 21:11:41 +0000 | [diff] [blame] | 219 | timing_->IncomingTimestamp(time_stamp, last_packet_time_ms); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 220 | } |
| 221 | if (dual_receiver != NULL) { |
| 222 | dual_receiver->UpdateState(*frame); |
| 223 | } |
| 224 | } |
| 225 | return frame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 226 | } |
| 227 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 228 | VCMEncodedFrame* VCMReceiver::FrameForDecoding( |
| 229 | uint16_t max_wait_time_ms, |
| 230 | int64_t next_render_time_ms, |
| 231 | VCMReceiver* dual_receiver) { |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 232 | TRACE_EVENT1("webrtc", "FrameForDecoding", |
| 233 | "max_wait", max_wait_time_ms); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 234 | // How long can we wait until we must decode the next frame. |
| 235 | uint32_t wait_time_ms = timing_->MaxWaitingTime( |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 236 | next_render_time_ms, clock_->TimeInMilliseconds()); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 237 | |
| 238 | // Try to get a complete frame from the jitter buffer. |
| 239 | VCMEncodedFrame* frame = jitter_buffer_.GetCompleteFrameForDecoding(0); |
| 240 | |
| 241 | if (frame == NULL && max_wait_time_ms == 0 && wait_time_ms > 0) { |
| 242 | // If we're not allowed to wait for frames to get complete we must |
| 243 | // calculate if it's time to decode, and if it's not we will just return |
| 244 | // for now. |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | if (frame == NULL && VCM_MIN(wait_time_ms, max_wait_time_ms) == 0) { |
| 249 | // No time to wait for a complete frame, check if we have an incomplete. |
| 250 | const bool dual_receiver_enabled_and_passive = (dual_receiver != NULL && |
| 251 | dual_receiver->State() == kPassive && |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 252 | dual_receiver->NackMode() == kNack); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 253 | if (dual_receiver_enabled_and_passive && |
| 254 | !jitter_buffer_.CompleteSequenceWithNextFrame()) { |
| 255 | // Jitter buffer state might get corrupt with this frame. |
| 256 | dual_receiver->CopyJitterBufferStateFromReceiver(*this); |
| 257 | frame = jitter_buffer_.GetFrameForDecoding(); |
| 258 | assert(frame); |
| 259 | } else { |
| 260 | frame = jitter_buffer_.GetFrameForDecoding(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | } |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 262 | } |
| 263 | if (frame == NULL) { |
| 264 | // Wait for a complete frame. |
| 265 | frame = jitter_buffer_.GetCompleteFrameForDecoding(max_wait_time_ms); |
| 266 | } |
| 267 | if (frame == NULL) { |
| 268 | // Get an incomplete frame. |
| 269 | if (timing_->MaxWaitingTime(next_render_time_ms, |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 270 | clock_->TimeInMilliseconds()) > 0) { |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 271 | // Still time to wait for a complete frame. |
| 272 | return NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 273 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 274 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 275 | // No time left to wait, we must decode this frame now. |
| 276 | const bool dual_receiver_enabled_and_passive = (dual_receiver != NULL && |
| 277 | dual_receiver->State() == kPassive && |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 278 | dual_receiver->NackMode() == kNack); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 279 | if (dual_receiver_enabled_and_passive && |
| 280 | !jitter_buffer_.CompleteSequenceWithNextFrame()) { |
| 281 | // Jitter buffer state might get corrupt with this frame. |
| 282 | dual_receiver->CopyJitterBufferStateFromReceiver(*this); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | } |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 284 | |
| 285 | frame = jitter_buffer_.GetFrameForDecoding(); |
| 286 | } |
| 287 | return frame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 288 | } |
| 289 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 290 | VCMEncodedFrame* VCMReceiver::FrameForRendering(uint16_t max_wait_time_ms, |
| 291 | int64_t next_render_time_ms, |
| 292 | VCMReceiver* dual_receiver) { |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 293 | TRACE_EVENT0("webrtc", "FrameForRendering"); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 294 | // How long MUST we wait until we must decode the next frame. This is |
| 295 | // different for the case where we have a renderer which can render at a |
| 296 | // specified time. Here we must wait as long as possible before giving the |
| 297 | // frame to the decoder, which will render the frame as soon as it has been |
| 298 | // decoded. |
| 299 | uint32_t wait_time_ms = timing_->MaxWaitingTime( |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 300 | next_render_time_ms, clock_->TimeInMilliseconds()); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 301 | if (max_wait_time_ms < wait_time_ms) { |
| 302 | // If we're not allowed to wait until the frame is supposed to be rendered |
| 303 | // we will have to return NULL for now. |
| 304 | return NULL; |
| 305 | } |
| 306 | // Wait until it's time to render. |
stefan@webrtc.org | 2baf5f5 | 2013-03-13 08:46:25 +0000 | [diff] [blame] | 307 | render_wait_event_->Wait(wait_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 308 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 309 | // Get a complete frame if possible. |
| 310 | VCMEncodedFrame* frame = jitter_buffer_.GetCompleteFrameForDecoding(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 311 | |
mikhal@webrtc.org | c2a3aa7 | 2013-04-12 19:53:30 +0000 | [diff] [blame] | 312 | if (frame == NULL) { |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 313 | // Get an incomplete frame. |
| 314 | const bool dual_receiver_enabled_and_passive = (dual_receiver != NULL && |
| 315 | dual_receiver->State() == kPassive && |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 316 | dual_receiver->NackMode() == kNack); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 317 | if (dual_receiver_enabled_and_passive && |
| 318 | !jitter_buffer_.CompleteSequenceWithNextFrame()) { |
| 319 | // Jitter buffer state might get corrupt with this frame. |
| 320 | dual_receiver->CopyJitterBufferStateFromReceiver(*this); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 321 | } |
| 322 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 323 | frame = jitter_buffer_.GetFrameForDecoding(); |
| 324 | } |
| 325 | return frame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 326 | } |
| 327 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 328 | void VCMReceiver::ReleaseFrame(VCMEncodedFrame* frame) { |
| 329 | jitter_buffer_.ReleaseFrame(frame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 330 | } |
| 331 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 332 | void VCMReceiver::ReceiveStatistics(uint32_t* bitrate, |
| 333 | uint32_t* framerate) { |
| 334 | assert(bitrate); |
| 335 | assert(framerate); |
| 336 | jitter_buffer_.IncomingRateStatistics(framerate, bitrate); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 337 | } |
| 338 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 339 | void VCMReceiver::ReceivedFrameCount(VCMFrameCount* frame_count) const { |
| 340 | assert(frame_count); |
| 341 | jitter_buffer_.FrameStatistics(&frame_count->numDeltaFrames, |
| 342 | &frame_count->numKeyFrames); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 343 | } |
| 344 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 345 | uint32_t VCMReceiver::DiscardedPackets() const { |
| 346 | return jitter_buffer_.num_discarded_packets(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 347 | } |
| 348 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 349 | void VCMReceiver::SetNackMode(VCMNackMode nackMode, |
| 350 | int low_rtt_nack_threshold_ms, |
| 351 | int high_rtt_nack_threshold_ms) { |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 352 | CriticalSectionScoped cs(crit_sect_); |
| 353 | // Default to always having NACK enabled in hybrid mode. |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 354 | jitter_buffer_.SetNackMode(nackMode, low_rtt_nack_threshold_ms, |
| 355 | high_rtt_nack_threshold_ms); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 356 | if (!master_) { |
| 357 | state_ = kPassive; // The dual decoder defaults to passive. |
| 358 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 359 | } |
| 360 | |
stefan@webrtc.org | becf9c8 | 2013-02-01 15:09:57 +0000 | [diff] [blame] | 361 | void VCMReceiver::SetNackSettings(size_t max_nack_list_size, |
| 362 | int max_packet_age_to_nack) { |
| 363 | jitter_buffer_.SetNackSettings(max_nack_list_size, |
| 364 | max_packet_age_to_nack); |
| 365 | } |
| 366 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 367 | VCMNackMode VCMReceiver::NackMode() const { |
| 368 | CriticalSectionScoped cs(crit_sect_); |
| 369 | return jitter_buffer_.nack_mode(); |
stefan@webrtc.org | 791eec7 | 2011-10-11 07:53:43 +0000 | [diff] [blame] | 370 | } |
| 371 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 372 | VCMNackStatus VCMReceiver::NackList(uint16_t* nack_list, |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 373 | uint16_t size, |
| 374 | uint16_t* nack_list_length) { |
| 375 | bool request_key_frame = false; |
| 376 | uint16_t* internal_nack_list = jitter_buffer_.GetNackList( |
| 377 | nack_list_length, &request_key_frame); |
| 378 | if (request_key_frame) { |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 379 | // This combination is used to trigger key frame requests. |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 380 | return kNackKeyFrameRequest; |
| 381 | } |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 382 | if (*nack_list_length > size) { |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 383 | return kNackNeedMoreMemory; |
| 384 | } |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 385 | if (internal_nack_list != NULL && *nack_list_length > 0) { |
| 386 | memcpy(nack_list, internal_nack_list, *nack_list_length * sizeof(uint16_t)); |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 387 | } |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 388 | return kNackOk; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 389 | } |
| 390 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 391 | // Decide whether we should change decoder state. This should be done if the |
| 392 | // dual decoder has caught up with the decoder decoding with packet losses. |
| 393 | bool VCMReceiver::DualDecoderCaughtUp(VCMEncodedFrame* dual_frame, |
| 394 | VCMReceiver& dual_receiver) const { |
| 395 | if (dual_frame == NULL) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 396 | return false; |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 397 | } |
| 398 | if (jitter_buffer_.LastDecodedTimestamp() == dual_frame->TimeStamp()) { |
| 399 | dual_receiver.UpdateState(kWaitForPrimaryDecode); |
| 400 | return true; |
| 401 | } |
| 402 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 403 | } |
| 404 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 405 | void VCMReceiver::CopyJitterBufferStateFromReceiver( |
| 406 | const VCMReceiver& receiver) { |
| 407 | jitter_buffer_.CopyFrom(receiver.jitter_buffer_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 408 | } |
| 409 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 410 | VCMReceiverState VCMReceiver::State() const { |
| 411 | CriticalSectionScoped cs(crit_sect_); |
| 412 | return state_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 413 | } |
| 414 | |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 415 | int VCMReceiver::SetMinReceiverDelay(int desired_delay_ms) { |
| 416 | CriticalSectionScoped cs(crit_sect_); |
| 417 | if (desired_delay_ms < 0 || desired_delay_ms > kMaxReceiverDelayMs) { |
| 418 | return -1; |
| 419 | } |
| 420 | jitter_buffer_.SetMaxJitterEstimate(desired_delay_ms); |
| 421 | max_video_delay_ms_ = desired_delay_ms + kMaxVideoDelayMs; |
| 422 | timing_->SetMaxVideoDelay(max_video_delay_ms_); |
mikhal@webrtc.org | dbd6a6d | 2013-04-17 16:23:22 +0000 | [diff] [blame] | 423 | // Initializing timing to the desired delay. |
| 424 | timing_->SetRequiredDelay(desired_delay_ms); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 428 | void VCMReceiver::UpdateState(VCMReceiverState new_state) { |
| 429 | CriticalSectionScoped cs(crit_sect_); |
| 430 | assert(!(state_ == kPassive && new_state == kWaitForPrimaryDecode)); |
| 431 | state_ = new_state; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 432 | } |
| 433 | |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 434 | void VCMReceiver::UpdateState(const VCMEncodedFrame& frame) { |
| 435 | if (jitter_buffer_.nack_mode() == kNoNack) { |
| 436 | // Dual decoder mode has not been enabled. |
| 437 | return; |
| 438 | } |
| 439 | // Update the dual receiver state. |
| 440 | if (frame.Complete() && frame.FrameType() == kVideoFrameKey) { |
| 441 | UpdateState(kPassive); |
| 442 | } |
| 443 | if (State() == kWaitForPrimaryDecode && |
| 444 | frame.Complete() && !frame.MissingFrame()) { |
| 445 | UpdateState(kPassive); |
| 446 | } |
| 447 | if (frame.MissingFrame() || !frame.Complete()) { |
| 448 | // State was corrupted, enable dual receiver. |
| 449 | UpdateState(kReceiving); |
| 450 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 451 | } |
stefan@webrtc.org | 1ea4b50 | 2013-01-07 08:49:41 +0000 | [diff] [blame] | 452 | } // namespace webrtc |