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 | 10 |
11 #include "webrtc/modules/video_coding/receiver.h" | 11 #include "webrtc/modules/video_coding/receiver.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 | 14 |
15 #include <cstdlib> | 15 #include <cstdlib> |
| 16 #include <vector> |
16 | 17 |
17 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
18 #include "webrtc/base/trace_event.h" | 19 #include "webrtc/base/trace_event.h" |
19 #include "webrtc/modules/video_coding/encoded_frame.h" | 20 #include "webrtc/modules/video_coding/encoded_frame.h" |
20 #include "webrtc/modules/video_coding/internal_defines.h" | 21 #include "webrtc/modules/video_coding/internal_defines.h" |
21 #include "webrtc/modules/video_coding/media_opt_util.h" | 22 #include "webrtc/modules/video_coding/media_opt_util.h" |
22 #include "webrtc/system_wrappers/include/clock.h" | 23 #include "webrtc/system_wrappers/include/clock.h" |
23 | 24 |
24 namespace webrtc { | 25 namespace webrtc { |
25 | 26 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 void VCMReceiver::UpdateRtt(int64_t rtt) { | 65 void VCMReceiver::UpdateRtt(int64_t rtt) { |
65 jitter_buffer_.UpdateRtt(rtt); | 66 jitter_buffer_.UpdateRtt(rtt); |
66 } | 67 } |
67 | 68 |
68 int32_t VCMReceiver::InsertPacket(const VCMPacket& packet, | 69 int32_t VCMReceiver::InsertPacket(const VCMPacket& packet, |
69 uint16_t frame_width, | 70 uint16_t frame_width, |
70 uint16_t frame_height) { | 71 uint16_t frame_height) { |
71 // Insert the packet into the jitter buffer. The packet can either be empty or | 72 // Insert the packet into the jitter buffer. The packet can either be empty or |
72 // contain media at this point. | 73 // contain media at this point. |
73 bool retransmitted = false; | 74 bool retransmitted = false; |
74 const VCMFrameBufferEnum ret = jitter_buffer_.InsertPacket(packet, | 75 const VCMFrameBufferEnum ret = |
75 &retransmitted); | 76 jitter_buffer_.InsertPacket(packet, &retransmitted); |
76 if (ret == kOldPacket) { | 77 if (ret == kOldPacket) { |
77 return VCM_OK; | 78 return VCM_OK; |
78 } else if (ret == kFlushIndicator) { | 79 } else if (ret == kFlushIndicator) { |
79 return VCM_FLUSH_INDICATOR; | 80 return VCM_FLUSH_INDICATOR; |
80 } else if (ret < 0) { | 81 } else if (ret < 0) { |
81 return VCM_JITTER_BUFFER_ERROR; | 82 return VCM_JITTER_BUFFER_ERROR; |
82 } | 83 } |
83 if (ret == kCompleteSession && !retransmitted) { | 84 if (ret == kCompleteSession && !retransmitted) { |
84 // We don't want to include timestamps which have suffered from | 85 // We don't want to include timestamps which have suffered from |
85 // retransmission here, since we compensate with extra retransmission | 86 // retransmission here, since we compensate with extra retransmission |
86 // delay within the jitter estimate. | 87 // delay within the jitter estimate. |
87 timing_->IncomingTimestamp(packet.timestamp, clock_->TimeInMilliseconds()); | 88 timing_->IncomingTimestamp(packet.timestamp, clock_->TimeInMilliseconds()); |
88 } | 89 } |
89 return VCM_OK; | 90 return VCM_OK; |
90 } | 91 } |
91 | 92 |
92 void VCMReceiver::TriggerDecoderShutdown() { | 93 void VCMReceiver::TriggerDecoderShutdown() { |
93 jitter_buffer_.Stop(); | 94 jitter_buffer_.Stop(); |
94 render_wait_event_->Set(); | 95 render_wait_event_->Set(); |
95 } | 96 } |
96 | 97 |
97 VCMEncodedFrame* VCMReceiver::FrameForDecoding(uint16_t max_wait_time_ms, | 98 VCMEncodedFrame* VCMReceiver::FrameForDecoding(uint16_t max_wait_time_ms, |
98 int64_t& next_render_time_ms, | 99 int64_t* next_render_time_ms, |
99 bool prefer_late_decoding) { | 100 bool prefer_late_decoding) { |
100 const int64_t start_time_ms = clock_->TimeInMilliseconds(); | 101 const int64_t start_time_ms = clock_->TimeInMilliseconds(); |
101 uint32_t frame_timestamp = 0; | 102 uint32_t frame_timestamp = 0; |
102 // Exhaust wait time to get a complete frame for decoding. | 103 // Exhaust wait time to get a complete frame for decoding. |
103 bool found_frame = jitter_buffer_.NextCompleteTimestamp( | 104 bool found_frame = |
104 max_wait_time_ms, &frame_timestamp); | 105 jitter_buffer_.NextCompleteTimestamp(max_wait_time_ms, &frame_timestamp); |
105 | 106 |
106 if (!found_frame) | 107 if (!found_frame) |
107 found_frame = jitter_buffer_.NextMaybeIncompleteTimestamp(&frame_timestamp); | 108 found_frame = jitter_buffer_.NextMaybeIncompleteTimestamp(&frame_timestamp); |
108 | 109 |
109 if (!found_frame) | 110 if (!found_frame) |
110 return NULL; | 111 return NULL; |
111 | 112 |
112 // We have a frame - Set timing and render timestamp. | 113 // We have a frame - Set timing and render timestamp. |
113 timing_->SetJitterDelay(jitter_buffer_.EstimatedJitterMs()); | 114 timing_->SetJitterDelay(jitter_buffer_.EstimatedJitterMs()); |
114 const int64_t now_ms = clock_->TimeInMilliseconds(); | 115 const int64_t now_ms = clock_->TimeInMilliseconds(); |
115 timing_->UpdateCurrentDelay(frame_timestamp); | 116 timing_->UpdateCurrentDelay(frame_timestamp); |
116 next_render_time_ms = timing_->RenderTimeMs(frame_timestamp, now_ms); | 117 *next_render_time_ms = timing_->RenderTimeMs(frame_timestamp, now_ms); |
117 // Check render timing. | 118 // Check render timing. |
118 bool timing_error = false; | 119 bool timing_error = false; |
119 // Assume that render timing errors are due to changes in the video stream. | 120 // Assume that render timing errors are due to changes in the video stream. |
120 if (next_render_time_ms < 0) { | 121 if (*next_render_time_ms < 0) { |
121 timing_error = true; | 122 timing_error = true; |
122 } else if (std::abs(next_render_time_ms - now_ms) > max_video_delay_ms_) { | 123 } else if (std::abs(*next_render_time_ms - now_ms) > max_video_delay_ms_) { |
123 int frame_delay = static_cast<int>(std::abs(next_render_time_ms - now_ms)); | 124 int frame_delay = static_cast<int>(std::abs(*next_render_time_ms - now_ms)); |
124 LOG(LS_WARNING) << "A frame about to be decoded is out of the configured " | 125 LOG(LS_WARNING) << "A frame about to be decoded is out of the configured " |
125 << "delay bounds (" << frame_delay << " > " | 126 << "delay bounds (" << frame_delay << " > " |
126 << max_video_delay_ms_ | 127 << max_video_delay_ms_ |
127 << "). Resetting the video jitter buffer."; | 128 << "). Resetting the video jitter buffer."; |
128 timing_error = true; | 129 timing_error = true; |
129 } else if (static_cast<int>(timing_->TargetVideoDelay()) > | 130 } else if (static_cast<int>(timing_->TargetVideoDelay()) > |
130 max_video_delay_ms_) { | 131 max_video_delay_ms_) { |
131 LOG(LS_WARNING) << "The video target delay has grown larger than " | 132 LOG(LS_WARNING) << "The video target delay has grown larger than " |
132 << max_video_delay_ms_ << " ms. Resetting jitter buffer."; | 133 << max_video_delay_ms_ << " ms. Resetting jitter buffer."; |
133 timing_error = true; | 134 timing_error = true; |
134 } | 135 } |
135 | 136 |
136 if (timing_error) { | 137 if (timing_error) { |
137 // Timing error => reset timing and flush the jitter buffer. | 138 // Timing error => reset timing and flush the jitter buffer. |
138 jitter_buffer_.Flush(); | 139 jitter_buffer_.Flush(); |
139 timing_->Reset(); | 140 timing_->Reset(); |
140 return NULL; | 141 return NULL; |
141 } | 142 } |
142 | 143 |
143 if (prefer_late_decoding) { | 144 if (prefer_late_decoding) { |
144 // Decode frame as close as possible to the render timestamp. | 145 // Decode frame as close as possible to the render timestamp. |
145 const int32_t available_wait_time = max_wait_time_ms - | 146 const int32_t available_wait_time = |
| 147 max_wait_time_ms - |
146 static_cast<int32_t>(clock_->TimeInMilliseconds() - start_time_ms); | 148 static_cast<int32_t>(clock_->TimeInMilliseconds() - start_time_ms); |
147 uint16_t new_max_wait_time = static_cast<uint16_t>( | 149 uint16_t new_max_wait_time = |
148 VCM_MAX(available_wait_time, 0)); | 150 static_cast<uint16_t>(VCM_MAX(available_wait_time, 0)); |
149 uint32_t wait_time_ms = timing_->MaxWaitingTime( | 151 uint32_t wait_time_ms = timing_->MaxWaitingTime( |
150 next_render_time_ms, clock_->TimeInMilliseconds()); | 152 *next_render_time_ms, clock_->TimeInMilliseconds()); |
151 if (new_max_wait_time < wait_time_ms) { | 153 if (new_max_wait_time < wait_time_ms) { |
152 // We're not allowed to wait until the frame is supposed to be rendered, | 154 // We're not allowed to wait until the frame is supposed to be rendered, |
153 // waiting as long as we're allowed to avoid busy looping, and then return | 155 // waiting as long as we're allowed to avoid busy looping, and then return |
154 // NULL. Next call to this function might return the frame. | 156 // NULL. Next call to this function might return the frame. |
155 render_wait_event_->Wait(new_max_wait_time); | 157 render_wait_event_->Wait(new_max_wait_time); |
156 return NULL; | 158 return NULL; |
157 } | 159 } |
158 // Wait until it's time to render. | 160 // Wait until it's time to render. |
159 render_wait_event_->Wait(wait_time_ms); | 161 render_wait_event_->Wait(wait_time_ms); |
160 } | 162 } |
161 | 163 |
162 // Extract the frame from the jitter buffer and set the render time. | 164 // Extract the frame from the jitter buffer and set the render time. |
163 VCMEncodedFrame* frame = jitter_buffer_.ExtractAndSetDecode(frame_timestamp); | 165 VCMEncodedFrame* frame = jitter_buffer_.ExtractAndSetDecode(frame_timestamp); |
164 if (frame == NULL) { | 166 if (frame == NULL) { |
165 return NULL; | 167 return NULL; |
166 } | 168 } |
167 frame->SetRenderTime(next_render_time_ms); | 169 frame->SetRenderTime(*next_render_time_ms); |
168 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", frame->TimeStamp(), | 170 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", frame->TimeStamp(), "SetRenderTS", |
169 "SetRenderTS", "render_time", next_render_time_ms); | 171 "render_time", *next_render_time_ms); |
170 if (!frame->Complete()) { | 172 if (!frame->Complete()) { |
171 // Update stats for incomplete frames. | 173 // Update stats for incomplete frames. |
172 bool retransmitted = false; | 174 bool retransmitted = false; |
173 const int64_t last_packet_time_ms = | 175 const int64_t last_packet_time_ms = |
174 jitter_buffer_.LastPacketTime(frame, &retransmitted); | 176 jitter_buffer_.LastPacketTime(frame, &retransmitted); |
175 if (last_packet_time_ms >= 0 && !retransmitted) { | 177 if (last_packet_time_ms >= 0 && !retransmitted) { |
176 // We don't want to include timestamps which have suffered from | 178 // We don't want to include timestamps which have suffered from |
177 // retransmission here, since we compensate with extra retransmission | 179 // retransmission here, since we compensate with extra retransmission |
178 // delay within the jitter estimate. | 180 // delay within the jitter estimate. |
179 timing_->IncomingTimestamp(frame_timestamp, last_packet_time_ms); | 181 timing_->IncomingTimestamp(frame_timestamp, last_packet_time_ms); |
180 } | 182 } |
181 } | 183 } |
182 return frame; | 184 return frame; |
183 } | 185 } |
184 | 186 |
185 void VCMReceiver::ReleaseFrame(VCMEncodedFrame* frame) { | 187 void VCMReceiver::ReleaseFrame(VCMEncodedFrame* frame) { |
186 jitter_buffer_.ReleaseFrame(frame); | 188 jitter_buffer_.ReleaseFrame(frame); |
187 } | 189 } |
188 | 190 |
189 void VCMReceiver::ReceiveStatistics(uint32_t* bitrate, | 191 void VCMReceiver::ReceiveStatistics(uint32_t* bitrate, uint32_t* framerate) { |
190 uint32_t* framerate) { | |
191 assert(bitrate); | 192 assert(bitrate); |
192 assert(framerate); | 193 assert(framerate); |
193 jitter_buffer_.IncomingRateStatistics(framerate, bitrate); | 194 jitter_buffer_.IncomingRateStatistics(framerate, bitrate); |
194 } | 195 } |
195 | 196 |
196 uint32_t VCMReceiver::DiscardedPackets() const { | 197 uint32_t VCMReceiver::DiscardedPackets() const { |
197 return jitter_buffer_.num_discarded_packets(); | 198 return jitter_buffer_.num_discarded_packets(); |
198 } | 199 } |
199 | 200 |
200 void VCMReceiver::SetNackMode(VCMNackMode nackMode, | 201 void VCMReceiver::SetNackMode(VCMNackMode nackMode, |
201 int64_t low_rtt_nack_threshold_ms, | 202 int64_t low_rtt_nack_threshold_ms, |
202 int64_t high_rtt_nack_threshold_ms) { | 203 int64_t high_rtt_nack_threshold_ms) { |
203 CriticalSectionScoped cs(crit_sect_); | 204 CriticalSectionScoped cs(crit_sect_); |
204 // Default to always having NACK enabled in hybrid mode. | 205 // Default to always having NACK enabled in hybrid mode. |
205 jitter_buffer_.SetNackMode(nackMode, low_rtt_nack_threshold_ms, | 206 jitter_buffer_.SetNackMode(nackMode, low_rtt_nack_threshold_ms, |
206 high_rtt_nack_threshold_ms); | 207 high_rtt_nack_threshold_ms); |
207 } | 208 } |
208 | 209 |
209 void VCMReceiver::SetNackSettings(size_t max_nack_list_size, | 210 void VCMReceiver::SetNackSettings(size_t max_nack_list_size, |
210 int max_packet_age_to_nack, | 211 int max_packet_age_to_nack, |
211 int max_incomplete_time_ms) { | 212 int max_incomplete_time_ms) { |
212 jitter_buffer_.SetNackSettings(max_nack_list_size, | 213 jitter_buffer_.SetNackSettings(max_nack_list_size, max_packet_age_to_nack, |
213 max_packet_age_to_nack, | |
214 max_incomplete_time_ms); | 214 max_incomplete_time_ms); |
215 } | 215 } |
216 | 216 |
217 VCMNackMode VCMReceiver::NackMode() const { | 217 VCMNackMode VCMReceiver::NackMode() const { |
218 CriticalSectionScoped cs(crit_sect_); | 218 CriticalSectionScoped cs(crit_sect_); |
219 return jitter_buffer_.nack_mode(); | 219 return jitter_buffer_.nack_mode(); |
220 } | 220 } |
221 | 221 |
222 std::vector<uint16_t> VCMReceiver::NackList(bool* request_key_frame) { | 222 std::vector<uint16_t> VCMReceiver::NackList(bool* request_key_frame) { |
223 return jitter_buffer_.GetNackList(request_key_frame); | 223 return jitter_buffer_.GetNackList(request_key_frame); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 uint32_t render_end = timing_->RenderTimeMs(timestamp_end, now_ms); | 259 uint32_t render_end = timing_->RenderTimeMs(timestamp_end, now_ms); |
260 return render_end - render_start; | 260 return render_end - render_start; |
261 } | 261 } |
262 | 262 |
263 void VCMReceiver::RegisterStatsCallback( | 263 void VCMReceiver::RegisterStatsCallback( |
264 VCMReceiveStatisticsCallback* callback) { | 264 VCMReceiveStatisticsCallback* callback) { |
265 jitter_buffer_.RegisterStatsCallback(callback); | 265 jitter_buffer_.RegisterStatsCallback(callback); |
266 } | 266 } |
267 | 267 |
268 } // namespace webrtc | 268 } // namespace webrtc |
OLD | NEW |