OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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 |
(...skipping 21 matching lines...) Expand all Loading... | |
32 void RtpFrameReferenceFinder::ManageFrame( | 32 void RtpFrameReferenceFinder::ManageFrame( |
33 std::unique_ptr<RtpFrameObject> frame) { | 33 std::unique_ptr<RtpFrameObject> frame) { |
34 rtc::CritScope lock(&crit_); | 34 rtc::CritScope lock(&crit_); |
35 | 35 |
36 // If we have cleared past this frame, drop it. | 36 // If we have cleared past this frame, drop it. |
37 if (cleared_to_seq_num_ != -1 && | 37 if (cleared_to_seq_num_ != -1 && |
38 AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) { | 38 AheadOf<uint16_t>(cleared_to_seq_num_, frame->first_seq_num())) { |
39 return; | 39 return; |
40 } | 40 } |
41 | 41 |
42 if (stashed_frames_.size() > kMaxStashedFrames) | |
43 stashed_frames_.pop_back(); | |
stefan-webrtc
2017/04/25 15:27:48
Here we pop_back(), before we did pop_front()?
philipel
2017/04/26 12:22:27
The order has changed so new frames are at the fro
| |
44 | |
45 stashed_frames_.push_front(std::move(frame)); | |
46 RetryStashedFrames(); | |
47 } | |
48 | |
49 void RtpFrameReferenceFinder::RetryStashedFrames() { | |
50 bool complete_frame = false; | |
51 do { | |
52 complete_frame = false; | |
53 for (auto frame_it = stashed_frames_.begin(); | |
54 frame_it != stashed_frames_.end();) { | |
55 FrameDecision decision = ManageFrameInternal(frame_it->get()); | |
56 | |
57 switch (decision) { | |
58 case kStash: | |
59 ++frame_it; | |
60 break; | |
61 case kSend: | |
62 complete_frame = true; | |
63 frame_callback_->OnCompleteFrame(std::move(*frame_it)); | |
64 FALLTHROUGH(); | |
65 case kDrop: | |
66 frame_it = stashed_frames_.erase(frame_it); | |
67 } | |
68 } | |
69 } while (complete_frame); | |
70 } | |
71 | |
72 RtpFrameReferenceFinder::FrameDecision | |
73 RtpFrameReferenceFinder::ManageFrameInternal(RtpFrameObject* frame) { | |
42 switch (frame->codec_type()) { | 74 switch (frame->codec_type()) { |
43 case kVideoCodecFlexfec: | 75 case kVideoCodecFlexfec: |
44 case kVideoCodecULPFEC: | 76 case kVideoCodecULPFEC: |
45 case kVideoCodecRED: | 77 case kVideoCodecRED: |
46 RTC_NOTREACHED(); | 78 RTC_NOTREACHED(); |
47 break; | 79 break; |
48 case kVideoCodecVP8: | 80 case kVideoCodecVP8: |
49 ManageFrameVp8(std::move(frame)); | 81 return ManageFrameVp8(frame); |
50 break; | |
51 case kVideoCodecVP9: | 82 case kVideoCodecVP9: |
52 ManageFrameVp9(std::move(frame)); | 83 return ManageFrameVp9(frame); |
53 break; | |
54 // Since the EndToEndTests use kVicdeoCodecUnknow we treat it the same as | 84 // Since the EndToEndTests use kVicdeoCodecUnknow we treat it the same as |
55 // kVideoCodecGeneric. | 85 // kVideoCodecGeneric. |
56 // TODO(philipel): Take a look at the EndToEndTests and see if maybe they | 86 // TODO(philipel): Take a look at the EndToEndTests and see if maybe they |
57 // should be changed to use kVideoCodecGeneric instead. | 87 // should be changed to use kVideoCodecGeneric instead. |
58 case kVideoCodecUnknown: | 88 case kVideoCodecUnknown: |
59 case kVideoCodecH264: | 89 case kVideoCodecH264: |
60 case kVideoCodecI420: | 90 case kVideoCodecI420: |
61 case kVideoCodecGeneric: | 91 case kVideoCodecGeneric: |
62 ManageFrameGeneric(std::move(frame), kNoPictureId); | 92 return ManageFrameGeneric(frame, kNoPictureId); |
63 break; | |
64 } | 93 } |
65 } | 94 } |
66 | 95 |
67 void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) { | 96 void RtpFrameReferenceFinder::PaddingReceived(uint16_t seq_num) { |
68 rtc::CritScope lock(&crit_); | 97 rtc::CritScope lock(&crit_); |
69 auto clean_padding_to = | 98 auto clean_padding_to = |
70 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge); | 99 stashed_padding_.lower_bound(seq_num - kMaxPaddingAge); |
71 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to); | 100 stashed_padding_.erase(stashed_padding_.begin(), clean_padding_to); |
72 stashed_padding_.insert(seq_num); | 101 stashed_padding_.insert(seq_num); |
73 UpdateLastPictureIdWithPadding(seq_num); | 102 UpdateLastPictureIdWithPadding(seq_num); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 // for a while there is a risk that new frames will appear to be older than | 146 // for a while there is a risk that new frames will appear to be older than |
118 // the keyframe they belong to due to wrapping sequence number. In order | 147 // the keyframe they belong to due to wrapping sequence number. In order |
119 // to prevent this we advance the picture id of the keyframe every so often. | 148 // to prevent this we advance the picture id of the keyframe every so often. |
120 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) { | 149 if (ForwardDiff(gop_seq_num_it->first, seq_num) > 10000) { |
121 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size()); | 150 RTC_DCHECK_EQ(1ul, last_seq_num_gop_.size()); |
122 last_seq_num_gop_[seq_num] = gop_seq_num_it->second; | 151 last_seq_num_gop_[seq_num] = gop_seq_num_it->second; |
123 last_seq_num_gop_.erase(gop_seq_num_it); | 152 last_seq_num_gop_.erase(gop_seq_num_it); |
124 } | 153 } |
125 } | 154 } |
126 | 155 |
127 void RtpFrameReferenceFinder::RetryStashedFrames() { | 156 RtpFrameReferenceFinder::FrameDecision |
128 size_t num_stashed_frames = stashed_frames_.size(); | 157 RtpFrameReferenceFinder::ManageFrameGeneric(RtpFrameObject* frame, |
129 | 158 int picture_id) { |
130 // Clean up stashed frames if there are too many. | |
131 while (stashed_frames_.size() > kMaxStashedFrames) | |
132 stashed_frames_.pop_front(); | |
133 | |
134 // Since frames are stashed if there is not enough data to determine their | |
135 // frame references we should at most check |stashed_frames_.size()| in | |
136 // order to not pop and push frames in and endless loop. | |
137 // NOTE! This function may be called recursively, hence the | |
138 // "!stashed_frames_.empty()" condition. | |
139 for (size_t i = 0; i < num_stashed_frames && !stashed_frames_.empty(); ++i) { | |
140 std::unique_ptr<RtpFrameObject> frame = std::move(stashed_frames_.front()); | |
141 stashed_frames_.pop_front(); | |
142 ManageFrame(std::move(frame)); | |
143 } | |
144 } | |
145 | |
146 void RtpFrameReferenceFinder::ManageFrameGeneric( | |
147 std::unique_ptr<RtpFrameObject> frame, | |
148 int picture_id) { | |
149 // If |picture_id| is specified then we use that to set the frame references, | 159 // If |picture_id| is specified then we use that to set the frame references, |
150 // otherwise we use sequence number. | 160 // otherwise we use sequence number. |
151 if (picture_id != kNoPictureId) { | 161 if (picture_id != kNoPictureId) { |
152 if (last_unwrap_ == -1) | 162 if (last_unwrap_ == -1) |
153 last_unwrap_ = picture_id; | 163 last_unwrap_ = picture_id; |
154 | 164 |
155 frame->picture_id = UnwrapPictureId(picture_id % kPicIdLength); | 165 frame->picture_id = UnwrapPictureId(picture_id % kPicIdLength); |
156 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1; | 166 frame->num_references = frame->frame_type() == kVideoFrameKey ? 0 : 1; |
157 frame->references[0] = frame->picture_id - 1; | 167 frame->references[0] = frame->picture_id - 1; |
158 frame_callback_->OnCompleteFrame(std::move(frame)); | 168 return kSend; |
159 return; | |
160 } | 169 } |
161 | 170 |
162 if (frame->frame_type() == kVideoFrameKey) { | 171 if (frame->frame_type() == kVideoFrameKey) { |
163 last_seq_num_gop_.insert(std::make_pair( | 172 last_seq_num_gop_.insert(std::make_pair( |
164 frame->last_seq_num(), | 173 frame->last_seq_num(), |
165 std::make_pair(frame->last_seq_num(), frame->last_seq_num()))); | 174 std::make_pair(frame->last_seq_num(), frame->last_seq_num()))); |
166 } | 175 } |
167 | 176 |
168 // We have received a frame but not yet a keyframe, stash this frame. | 177 // We have received a frame but not yet a keyframe, stash this frame. |
169 if (last_seq_num_gop_.empty()) { | 178 if (last_seq_num_gop_.empty()) |
170 stashed_frames_.push_back(std::move(frame)); | 179 return kStash; |
171 return; | |
172 } | |
173 | 180 |
174 // Clean up info for old keyframes but make sure to keep info | 181 // Clean up info for old keyframes but make sure to keep info |
175 // for the last keyframe. | 182 // for the last keyframe. |
176 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100); | 183 auto clean_to = last_seq_num_gop_.lower_bound(frame->last_seq_num() - 100); |
177 for (auto it = last_seq_num_gop_.begin(); | 184 for (auto it = last_seq_num_gop_.begin(); |
178 it != clean_to && last_seq_num_gop_.size() > 1;) { | 185 it != clean_to && last_seq_num_gop_.size() > 1;) { |
179 it = last_seq_num_gop_.erase(it); | 186 it = last_seq_num_gop_.erase(it); |
180 } | 187 } |
181 | 188 |
182 // Find the last sequence number of the last frame for the keyframe | 189 // Find the last sequence number of the last frame for the keyframe |
183 // that this frame indirectly references. | 190 // that this frame indirectly references. |
184 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num()); | 191 auto seq_num_it = last_seq_num_gop_.upper_bound(frame->last_seq_num()); |
185 if (seq_num_it == last_seq_num_gop_.begin()) { | 192 if (seq_num_it == last_seq_num_gop_.begin()) { |
186 LOG(LS_WARNING) << "Generic frame with packet range [" | 193 LOG(LS_WARNING) << "Generic frame with packet range [" |
187 << frame->first_seq_num() << ", " << frame->last_seq_num() | 194 << frame->first_seq_num() << ", " << frame->last_seq_num() |
188 << "] has no GoP, dropping frame."; | 195 << "] has no GoP, dropping frame."; |
189 return; | 196 return kDrop; |
190 } | 197 } |
191 seq_num_it--; | 198 seq_num_it--; |
192 | 199 |
193 // Make sure the packet sequence numbers are continuous, otherwise stash | 200 // Make sure the packet sequence numbers are continuous, otherwise stash |
194 // this frame. | 201 // this frame. |
195 uint16_t last_picture_id_gop = seq_num_it->second.first; | 202 uint16_t last_picture_id_gop = seq_num_it->second.first; |
196 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second; | 203 uint16_t last_picture_id_with_padding_gop = seq_num_it->second.second; |
197 if (frame->frame_type() == kVideoFrameDelta) { | 204 if (frame->frame_type() == kVideoFrameDelta) { |
198 uint16_t prev_seq_num = frame->first_seq_num() - 1; | 205 uint16_t prev_seq_num = frame->first_seq_num() - 1; |
199 if (prev_seq_num != last_picture_id_with_padding_gop) { | 206 |
200 stashed_frames_.push_back(std::move(frame)); | 207 if (prev_seq_num != last_picture_id_with_padding_gop) |
201 return; | 208 return kStash; |
202 } | |
203 } | 209 } |
204 | 210 |
205 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first)); | 211 RTC_DCHECK(AheadOrAt(frame->last_seq_num(), seq_num_it->first)); |
206 | 212 |
207 // Since keyframes can cause reordering we can't simply assign the | 213 // Since keyframes can cause reordering we can't simply assign the |
208 // picture id according to some incrementing counter. | 214 // picture id according to some incrementing counter. |
209 frame->picture_id = frame->last_seq_num(); | 215 frame->picture_id = frame->last_seq_num(); |
210 frame->num_references = frame->frame_type() == kVideoFrameDelta; | 216 frame->num_references = frame->frame_type() == kVideoFrameDelta; |
211 frame->references[0] = last_picture_id_gop; | 217 frame->references[0] = last_picture_id_gop; |
212 if (AheadOf(frame->picture_id, last_picture_id_gop)) { | 218 if (AheadOf(frame->picture_id, last_picture_id_gop)) { |
213 seq_num_it->second.first = frame->picture_id; | 219 seq_num_it->second.first = frame->picture_id; |
214 seq_num_it->second.second = frame->picture_id; | 220 seq_num_it->second.second = frame->picture_id; |
215 } | 221 } |
216 | 222 |
217 last_picture_id_ = frame->picture_id; | 223 last_picture_id_ = frame->picture_id; |
218 UpdateLastPictureIdWithPadding(frame->picture_id); | 224 UpdateLastPictureIdWithPadding(frame->picture_id); |
219 frame_callback_->OnCompleteFrame(std::move(frame)); | 225 return kSend; |
220 RetryStashedFrames(); | |
221 } | 226 } |
222 | 227 |
223 void RtpFrameReferenceFinder::ManageFrameVp8( | 228 RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp8( |
224 std::unique_ptr<RtpFrameObject> frame) { | 229 RtpFrameObject* frame) { |
225 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); | 230 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); |
226 if (!rtp_codec_header) | 231 if (!rtp_codec_header) |
227 return; | 232 return kDrop; |
228 | 233 |
229 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8; | 234 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8; |
230 | 235 |
231 if (codec_header.pictureId == kNoPictureId || | 236 if (codec_header.pictureId == kNoPictureId || |
232 codec_header.temporalIdx == kNoTemporalIdx || | 237 codec_header.temporalIdx == kNoTemporalIdx || |
233 codec_header.tl0PicIdx == kNoTl0PicIdx) { | 238 codec_header.tl0PicIdx == kNoTl0PicIdx) { |
234 ManageFrameGeneric(std::move(frame), codec_header.pictureId); | 239 return ManageFrameGeneric(std::move(frame), codec_header.pictureId); |
235 return; | |
236 } | 240 } |
237 | 241 |
238 frame->picture_id = codec_header.pictureId % kPicIdLength; | 242 frame->picture_id = codec_header.pictureId % kPicIdLength; |
239 | 243 |
240 if (last_unwrap_ == -1) | 244 if (last_unwrap_ == -1) |
241 last_unwrap_ = codec_header.pictureId; | 245 last_unwrap_ = codec_header.pictureId; |
242 | 246 |
243 if (last_picture_id_ == -1) | 247 if (last_picture_id_ == -1) |
244 last_picture_id_ = frame->picture_id; | 248 last_picture_id_ = frame->picture_id; |
245 | 249 |
(...skipping 15 matching lines...) Expand all Loading... | |
261 // Clean up info about not yet received frames that are too old. | 265 // Clean up info about not yet received frames that are too old. |
262 uint16_t old_picture_id = | 266 uint16_t old_picture_id = |
263 Subtract<kPicIdLength>(frame->picture_id, kMaxNotYetReceivedFrames); | 267 Subtract<kPicIdLength>(frame->picture_id, kMaxNotYetReceivedFrames); |
264 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id); | 268 auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id); |
265 not_yet_received_frames_.erase(not_yet_received_frames_.begin(), | 269 not_yet_received_frames_.erase(not_yet_received_frames_.begin(), |
266 clean_frames_to); | 270 clean_frames_to); |
267 | 271 |
268 if (frame->frame_type() == kVideoFrameKey) { | 272 if (frame->frame_type() == kVideoFrameKey) { |
269 frame->num_references = 0; | 273 frame->num_references = 0; |
270 layer_info_[codec_header.tl0PicIdx].fill(-1); | 274 layer_info_[codec_header.tl0PicIdx].fill(-1); |
271 CompletedFrameVp8(std::move(frame)); | 275 CompletedFrameVp8(frame); |
272 return; | 276 return kSend; |
273 } | 277 } |
274 | 278 |
275 auto layer_info_it = layer_info_.find(codec_header.temporalIdx == 0 | 279 auto layer_info_it = layer_info_.find(codec_header.temporalIdx == 0 |
276 ? codec_header.tl0PicIdx - 1 | 280 ? codec_header.tl0PicIdx - 1 |
277 : codec_header.tl0PicIdx); | 281 : codec_header.tl0PicIdx); |
278 | 282 |
279 // If we don't have the base layer frame yet, stash this frame. | 283 // If we don't have the base layer frame yet, stash this frame. |
280 if (layer_info_it == layer_info_.end()) { | 284 if (layer_info_it == layer_info_.end()) |
281 stashed_frames_.push_back(std::move(frame)); | 285 return kStash; |
282 return; | |
283 } | |
284 | 286 |
285 // A non keyframe base layer frame has been received, copy the layer info | 287 // A non keyframe base layer frame has been received, copy the layer info |
286 // from the previous base layer frame and set a reference to the previous | 288 // from the previous base layer frame and set a reference to the previous |
287 // base layer frame. | 289 // base layer frame. |
288 if (codec_header.temporalIdx == 0) { | 290 if (codec_header.temporalIdx == 0) { |
289 layer_info_it = | 291 layer_info_it = |
290 layer_info_ | 292 layer_info_ |
291 .insert(make_pair(codec_header.tl0PicIdx, layer_info_it->second)) | 293 .insert(make_pair(codec_header.tl0PicIdx, layer_info_it->second)) |
292 .first; | 294 .first; |
293 frame->num_references = 1; | 295 frame->num_references = 1; |
294 frame->references[0] = layer_info_it->second[0]; | 296 frame->references[0] = layer_info_it->second[0]; |
295 CompletedFrameVp8(std::move(frame)); | 297 CompletedFrameVp8(frame); |
296 return; | 298 return kSend; |
297 } | 299 } |
298 | 300 |
299 // Layer sync frame, this frame only references its base layer frame. | 301 // Layer sync frame, this frame only references its base layer frame. |
300 if (codec_header.layerSync) { | 302 if (codec_header.layerSync) { |
301 frame->num_references = 1; | 303 frame->num_references = 1; |
302 frame->references[0] = layer_info_it->second[0]; | 304 frame->references[0] = layer_info_it->second[0]; |
303 | 305 |
304 CompletedFrameVp8(std::move(frame)); | 306 CompletedFrameVp8(frame); |
305 return; | 307 return kSend; |
306 } | 308 } |
307 | 309 |
308 // Find all references for this frame. | 310 // Find all references for this frame. |
309 frame->num_references = 0; | 311 frame->num_references = 0; |
310 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) { | 312 for (uint8_t layer = 0; layer <= codec_header.temporalIdx; ++layer) { |
311 // If we have not yet received a previous frame on this temporal layer, | 313 // If we have not yet received a previous frame on this temporal layer, |
312 // stash this frame. | 314 // stash this frame. |
313 if (layer_info_it->second[layer] == -1) { | 315 if (layer_info_it->second[layer] == -1) |
314 stashed_frames_.push_back(std::move(frame)); | 316 return kStash; |
315 return; | |
316 } | |
317 | 317 |
318 // If the last frame on this layer is ahead of this frame it means that | 318 // If the last frame on this layer is ahead of this frame it means that |
319 // a layer sync frame has been received after this frame for the same | 319 // a layer sync frame has been received after this frame for the same |
320 // base layer frame, drop this frame. | 320 // base layer frame, drop this frame. |
321 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer], | 321 if (AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[layer], |
322 frame->picture_id)) { | 322 frame->picture_id)) { |
323 return; | 323 return kDrop; |
324 } | 324 } |
325 | 325 |
326 // If we have not yet received a frame between this frame and the referenced | 326 // If we have not yet received a frame between this frame and the referenced |
327 // frame then we have to wait for that frame to be completed first. | 327 // frame then we have to wait for that frame to be completed first. |
328 auto not_received_frame_it = | 328 auto not_received_frame_it = |
329 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]); | 329 not_yet_received_frames_.upper_bound(layer_info_it->second[layer]); |
330 if (not_received_frame_it != not_yet_received_frames_.end() && | 330 if (not_received_frame_it != not_yet_received_frames_.end() && |
331 AheadOf<uint16_t, kPicIdLength>(frame->picture_id, | 331 AheadOf<uint16_t, kPicIdLength>(frame->picture_id, |
332 *not_received_frame_it)) { | 332 *not_received_frame_it)) { |
333 stashed_frames_.push_back(std::move(frame)); | 333 return kStash; |
334 return; | |
335 } | 334 } |
336 | 335 |
337 if (!(AheadOf<uint16_t, kPicIdLength>(frame->picture_id, | 336 if (!(AheadOf<uint16_t, kPicIdLength>(frame->picture_id, |
338 layer_info_it->second[layer]))) { | 337 layer_info_it->second[layer]))) { |
339 LOG(LS_WARNING) << "Frame with picture id " << frame->picture_id | 338 LOG(LS_WARNING) << "Frame with picture id " << frame->picture_id |
340 << " and packet range [" << frame->first_seq_num() << ", " | 339 << " and packet range [" << frame->first_seq_num() << ", " |
341 << frame->last_seq_num() << "] already received, " | 340 << frame->last_seq_num() << "] already received, " |
342 << " dropping frame."; | 341 << " dropping frame."; |
343 return; | 342 return kDrop; |
344 } | 343 } |
345 | 344 |
346 ++frame->num_references; | 345 ++frame->num_references; |
347 frame->references[layer] = layer_info_it->second[layer]; | 346 frame->references[layer] = layer_info_it->second[layer]; |
348 } | 347 } |
349 | 348 |
350 CompletedFrameVp8(std::move(frame)); | 349 CompletedFrameVp8(frame); |
350 return kSend; | |
351 } | 351 } |
352 | 352 |
353 void RtpFrameReferenceFinder::CompletedFrameVp8( | 353 void RtpFrameReferenceFinder::CompletedFrameVp8(RtpFrameObject* frame) { |
354 std::unique_ptr<RtpFrameObject> frame) { | |
355 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); | 354 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); |
356 if (!rtp_codec_header) | 355 RTC_DCHECK(rtp_codec_header); |
357 return; | |
358 | |
359 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8; | 356 const RTPVideoHeaderVP8& codec_header = rtp_codec_header->VP8; |
360 | 357 |
361 uint8_t tl0_pic_idx = codec_header.tl0PicIdx; | 358 uint8_t tl0_pic_idx = codec_header.tl0PicIdx; |
362 uint8_t temporal_index = codec_header.temporalIdx; | 359 uint8_t temporal_index = codec_header.temporalIdx; |
363 auto layer_info_it = layer_info_.find(tl0_pic_idx); | 360 auto layer_info_it = layer_info_.find(tl0_pic_idx); |
364 | 361 |
365 // Update this layer info and newer. | 362 // Update this layer info and newer. |
366 while (layer_info_it != layer_info_.end()) { | 363 while (layer_info_it != layer_info_.end()) { |
367 if (layer_info_it->second[temporal_index] != -1 && | 364 if (layer_info_it->second[temporal_index] != -1 && |
368 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_index], | 365 AheadOf<uint16_t, kPicIdLength>(layer_info_it->second[temporal_index], |
369 frame->picture_id)) { | 366 frame->picture_id)) { |
370 // The frame was not newer, then no subsequent layer info have to be | 367 // The frame was not newer, then no subsequent layer info have to be |
371 // update. | 368 // update. |
372 break; | 369 break; |
373 } | 370 } |
374 | 371 |
375 layer_info_it->second[codec_header.temporalIdx] = frame->picture_id; | 372 layer_info_it->second[codec_header.temporalIdx] = frame->picture_id; |
376 ++tl0_pic_idx; | 373 ++tl0_pic_idx; |
377 layer_info_it = layer_info_.find(tl0_pic_idx); | 374 layer_info_it = layer_info_.find(tl0_pic_idx); |
378 } | 375 } |
379 not_yet_received_frames_.erase(frame->picture_id); | 376 not_yet_received_frames_.erase(frame->picture_id); |
380 | 377 |
381 for (size_t i = 0; i < frame->num_references; ++i) | 378 for (size_t i = 0; i < frame->num_references; ++i) |
382 frame->references[i] = UnwrapPictureId(frame->references[i]); | 379 frame->references[i] = UnwrapPictureId(frame->references[i]); |
383 frame->picture_id = UnwrapPictureId(frame->picture_id); | 380 frame->picture_id = UnwrapPictureId(frame->picture_id); |
384 | |
385 frame_callback_->OnCompleteFrame(std::move(frame)); | |
386 RetryStashedFrames(); | |
387 } | 381 } |
388 | 382 |
389 void RtpFrameReferenceFinder::ManageFrameVp9( | 383 RtpFrameReferenceFinder::FrameDecision RtpFrameReferenceFinder::ManageFrameVp9( |
390 std::unique_ptr<RtpFrameObject> frame) { | 384 RtpFrameObject* frame) { |
391 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); | 385 rtc::Optional<RTPVideoTypeHeader> rtp_codec_header = frame->GetCodecHeader(); |
392 if (!rtp_codec_header) | 386 RTC_DCHECK(rtp_codec_header); |
393 return; | |
394 | |
395 const RTPVideoHeaderVP9& codec_header = rtp_codec_header->VP9; | 387 const RTPVideoHeaderVP9& codec_header = rtp_codec_header->VP9; |
396 | 388 |
397 bool old_frame = Vp9PidTl0Fix(*frame, &rtp_codec_header->VP9.picture_id, | 389 bool old_frame = Vp9PidTl0Fix(*frame, &rtp_codec_header->VP9.picture_id, |
398 &rtp_codec_header->VP9.tl0_pic_idx); | 390 &rtp_codec_header->VP9.tl0_pic_idx); |
399 if (old_frame) | 391 if (old_frame) |
400 return; | 392 return kDrop; |
401 | 393 |
402 if (codec_header.picture_id == kNoPictureId || | 394 if (codec_header.picture_id == kNoPictureId || |
403 codec_header.temporal_idx == kNoTemporalIdx) { | 395 codec_header.temporal_idx == kNoTemporalIdx) { |
404 ManageFrameGeneric(std::move(frame), codec_header.picture_id); | 396 return ManageFrameGeneric(std::move(frame), codec_header.picture_id); |
405 return; | |
406 } | 397 } |
407 | 398 |
408 frame->spatial_layer = codec_header.spatial_idx; | 399 frame->spatial_layer = codec_header.spatial_idx; |
409 frame->inter_layer_predicted = codec_header.inter_layer_predicted; | 400 frame->inter_layer_predicted = codec_header.inter_layer_predicted; |
410 frame->picture_id = codec_header.picture_id % kPicIdLength; | 401 frame->picture_id = codec_header.picture_id % kPicIdLength; |
411 | 402 |
412 if (last_unwrap_ == -1) | 403 if (last_unwrap_ == -1) |
413 last_unwrap_ = codec_header.picture_id; | 404 last_unwrap_ = codec_header.picture_id; |
414 | 405 |
415 if (last_picture_id_ == -1) | 406 if (last_picture_id_ == -1) |
416 last_picture_id_ = frame->picture_id; | 407 last_picture_id_ = frame->picture_id; |
417 | 408 |
418 if (codec_header.flexible_mode) { | 409 if (codec_header.flexible_mode) { |
419 frame->num_references = codec_header.num_ref_pics; | 410 frame->num_references = codec_header.num_ref_pics; |
420 for (size_t i = 0; i < frame->num_references; ++i) { | 411 for (size_t i = 0; i < frame->num_references; ++i) { |
421 frame->references[i] = | 412 frame->references[i] = |
422 Subtract<1 << 16>(frame->picture_id, codec_header.pid_diff[i]); | 413 Subtract<1 << 16>(frame->picture_id, codec_header.pid_diff[i]); |
423 } | 414 } |
424 | 415 |
425 CompletedFrameVp9(std::move(frame)); | 416 CompletedFrameVp9(frame); |
426 return; | 417 return kSend; |
427 } | 418 } |
428 | 419 |
429 if (codec_header.ss_data_available) { | 420 if (codec_header.ss_data_available) { |
430 // Scalability structures can only be sent with tl0 frames. | 421 // Scalability structures can only be sent with tl0 frames. |
431 if (codec_header.temporal_idx != 0) { | 422 if (codec_header.temporal_idx != 0) { |
432 LOG(LS_WARNING) << "Received scalability structure on a non base layer" | 423 LOG(LS_WARNING) << "Received scalability structure on a non base layer" |
433 " frame. Scalability structure ignored."; | 424 " frame. Scalability structure ignored."; |
434 } else { | 425 } else { |
435 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1); | 426 current_ss_idx_ = Add<kMaxGofSaved>(current_ss_idx_, 1); |
436 scalability_structures_[current_ss_idx_] = codec_header.gof; | 427 scalability_structures_[current_ss_idx_] = codec_header.gof; |
(...skipping 11 matching lines...) Expand all Loading... | |
448 gof_info_.erase(gof_info_.begin(), clean_gof_info_to); | 439 gof_info_.erase(gof_info_.begin(), clean_gof_info_to); |
449 | 440 |
450 if (frame->frame_type() == kVideoFrameKey) { | 441 if (frame->frame_type() == kVideoFrameKey) { |
451 // When using GOF all keyframes must include the scalability structure. | 442 // When using GOF all keyframes must include the scalability structure. |
452 if (!codec_header.ss_data_available) | 443 if (!codec_header.ss_data_available) |
453 LOG(LS_WARNING) << "Received keyframe without scalability structure"; | 444 LOG(LS_WARNING) << "Received keyframe without scalability structure"; |
454 | 445 |
455 frame->num_references = 0; | 446 frame->num_references = 0; |
456 GofInfo info = gof_info_.find(codec_header.tl0_pic_idx)->second; | 447 GofInfo info = gof_info_.find(codec_header.tl0_pic_idx)->second; |
457 FrameReceivedVp9(frame->picture_id, &info); | 448 FrameReceivedVp9(frame->picture_id, &info); |
458 CompletedFrameVp9(std::move(frame)); | 449 CompletedFrameVp9(frame); |
459 return; | 450 return kSend; |
460 } | 451 } |
461 | 452 |
462 auto gof_info_it = gof_info_.find( | 453 auto gof_info_it = gof_info_.find( |
463 (codec_header.temporal_idx == 0 && !codec_header.ss_data_available) | 454 (codec_header.temporal_idx == 0 && !codec_header.ss_data_available) |
464 ? codec_header.tl0_pic_idx - 1 | 455 ? codec_header.tl0_pic_idx - 1 |
465 : codec_header.tl0_pic_idx); | 456 : codec_header.tl0_pic_idx); |
466 | 457 |
467 // Gof info for this frame is not available yet, stash this frame. | 458 // Gof info for this frame is not available yet, stash this frame. |
468 if (gof_info_it == gof_info_.end()) { | 459 if (gof_info_it == gof_info_.end()) |
469 stashed_frames_.push_back(std::move(frame)); | 460 return kStash; |
470 return; | |
471 } | |
472 | 461 |
473 GofInfo* info = &gof_info_it->second; | 462 GofInfo* info = &gof_info_it->second; |
474 FrameReceivedVp9(frame->picture_id, info); | 463 FrameReceivedVp9(frame->picture_id, info); |
475 | 464 |
476 // Make sure we don't miss any frame that could potentially have the | 465 // Make sure we don't miss any frame that could potentially have the |
477 // up switch flag set. | 466 // up switch flag set. |
478 if (MissingRequiredFrameVp9(frame->picture_id, *info)) { | 467 if (MissingRequiredFrameVp9(frame->picture_id, *info)) |
479 stashed_frames_.push_back(std::move(frame)); | 468 return kStash; |
480 return; | |
481 } | |
482 | 469 |
483 if (codec_header.temporal_up_switch) { | 470 if (codec_header.temporal_up_switch) { |
484 auto pid_tidx = | 471 auto pid_tidx = |
485 std::make_pair(frame->picture_id, codec_header.temporal_idx); | 472 std::make_pair(frame->picture_id, codec_header.temporal_idx); |
486 up_switch_.insert(pid_tidx); | 473 up_switch_.insert(pid_tidx); |
487 } | 474 } |
488 | 475 |
489 // If this is a base layer frame that contains a scalability structure | 476 // If this is a base layer frame that contains a scalability structure |
490 // then gof info has already been inserted earlier, so we only want to | 477 // then gof info has already been inserted earlier, so we only want to |
491 // insert if we haven't done so already. | 478 // insert if we haven't done so already. |
(...skipping 18 matching lines...) Expand all Loading... | |
510 frame->picture_id, info->gof->pid_diff[gof_idx][i]); | 497 frame->picture_id, info->gof->pid_diff[gof_idx][i]); |
511 | 498 |
512 // If this is a reference to a frame earlier than the last up switch point, | 499 // If this is a reference to a frame earlier than the last up switch point, |
513 // then ignore this reference. | 500 // then ignore this reference. |
514 if (UpSwitchInIntervalVp9(frame->picture_id, codec_header.temporal_idx, | 501 if (UpSwitchInIntervalVp9(frame->picture_id, codec_header.temporal_idx, |
515 frame->references[i])) { | 502 frame->references[i])) { |
516 --frame->num_references; | 503 --frame->num_references; |
517 } | 504 } |
518 } | 505 } |
519 | 506 |
520 CompletedFrameVp9(std::move(frame)); | 507 CompletedFrameVp9(frame); |
508 return kSend; | |
521 } | 509 } |
522 | 510 |
523 bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id, | 511 bool RtpFrameReferenceFinder::MissingRequiredFrameVp9(uint16_t picture_id, |
524 const GofInfo& info) { | 512 const GofInfo& info) { |
525 size_t diff = | 513 size_t diff = |
526 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id); | 514 ForwardDiff<uint16_t, kPicIdLength>(info.gof->pid_start, picture_id); |
527 size_t gof_idx = diff % info.gof->num_frames_in_gof; | 515 size_t gof_idx = diff % info.gof->num_frames_in_gof; |
528 size_t temporal_idx = info.gof->temporal_idx[gof_idx]; | 516 size_t temporal_idx = info.gof->temporal_idx[gof_idx]; |
529 | 517 |
530 // For every reference this frame has, check if there is a frame missing in | 518 // For every reference this frame has, check if there is a frame missing in |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
582 up_switch_it != up_switch_.end() && | 570 up_switch_it != up_switch_.end() && |
583 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first); | 571 AheadOf<uint16_t, kPicIdLength>(picture_id, up_switch_it->first); |
584 ++up_switch_it) { | 572 ++up_switch_it) { |
585 if (up_switch_it->second < temporal_idx) | 573 if (up_switch_it->second < temporal_idx) |
586 return true; | 574 return true; |
587 } | 575 } |
588 | 576 |
589 return false; | 577 return false; |
590 } | 578 } |
591 | 579 |
592 void RtpFrameReferenceFinder::CompletedFrameVp9( | 580 void RtpFrameReferenceFinder::CompletedFrameVp9(RtpFrameObject* frame) { |
stefan-webrtc
2017/04/25 15:27:48
CompletedFrameVp9 and CompletedFrameVp8 don't real
philipel
2017/04/26 12:22:27
Renamed CompletedFrameVp9 to UnwrapPictureIds sinc
| |
593 std::unique_ptr<RtpFrameObject> frame) { | |
594 for (size_t i = 0; i < frame->num_references; ++i) | 581 for (size_t i = 0; i < frame->num_references; ++i) |
595 frame->references[i] = UnwrapPictureId(frame->references[i]); | 582 frame->references[i] = UnwrapPictureId(frame->references[i]); |
596 frame->picture_id = UnwrapPictureId(frame->picture_id); | 583 frame->picture_id = UnwrapPictureId(frame->picture_id); |
597 | |
598 frame_callback_->OnCompleteFrame(std::move(frame)); | |
599 RetryStashedFrames(); | |
600 } | 584 } |
601 | 585 |
602 uint16_t RtpFrameReferenceFinder::UnwrapPictureId(uint16_t picture_id) { | 586 uint16_t RtpFrameReferenceFinder::UnwrapPictureId(uint16_t picture_id) { |
603 RTC_DCHECK_NE(-1, last_unwrap_); | 587 RTC_DCHECK_NE(-1, last_unwrap_); |
604 | 588 |
605 uint16_t unwrap_truncated = last_unwrap_ % kPicIdLength; | 589 uint16_t unwrap_truncated = last_unwrap_ % kPicIdLength; |
606 uint16_t diff = MinDiff<uint16_t, kPicIdLength>(unwrap_truncated, picture_id); | 590 uint16_t diff = MinDiff<uint16_t, kPicIdLength>(unwrap_truncated, picture_id); |
607 | 591 |
608 if (AheadOf<uint16_t, kPicIdLength>(picture_id, unwrap_truncated)) | 592 if (AheadOf<uint16_t, kPicIdLength>(picture_id, unwrap_truncated)) |
609 last_unwrap_ = Add<1 << 16>(last_unwrap_, diff); | 593 last_unwrap_ = Add<1 << 16>(last_unwrap_, diff); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
751 if (!gof_info_.empty() && | 735 if (!gof_info_.empty() && |
752 AheadOf<uint8_t>(gof_info_.begin()->first, fixed_tl0)) { | 736 AheadOf<uint8_t>(gof_info_.begin()->first, fixed_tl0)) { |
753 return true; | 737 return true; |
754 } | 738 } |
755 } | 739 } |
756 return false; | 740 return false; |
757 } | 741 } |
758 | 742 |
759 } // namespace video_coding | 743 } // namespace video_coding |
760 } // namespace webrtc | 744 } // namespace webrtc |
OLD | NEW |