1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/test/simple_test_tick_clock.h"
6 #include "media/cast/net/cast_transport_defines.h"
7 #include "media/cast/net/rtp/framer.h"
8 #include "media/cast/net/rtp/mock_rtp_payload_feedback.h"
9 #include "testing/gtest/include/gtest/gtest.h"
14 class FramerTest
: public ::testing::Test
{
17 : mock_rtp_payload_feedback_(),
18 framer_(&testing_clock_
, &mock_rtp_payload_feedback_
, 0, true, 0) {
19 payload_
.assign(kMaxIpPacketSize
, 0);
21 EXPECT_CALL(mock_rtp_payload_feedback_
, CastFeedback(testing::_
))
22 .WillRepeatedly(testing::Return());
25 ~FramerTest() override
{}
27 std::vector
<uint8
> payload_
;
28 RtpCastHeader rtp_header_
;
29 MockRtpPayloadFeedback mock_rtp_payload_feedback_
;
31 base::SimpleTestTickClock testing_clock_
;
33 DISALLOW_COPY_AND_ASSIGN(FramerTest
);
36 TEST_F(FramerTest
, EmptyState
) {
38 bool next_frame
= false;
39 bool multiple
= false;
40 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
43 TEST_F(FramerTest
, AlwaysStartWithKey
) {
45 bool next_frame
= false;
46 bool complete
= false;
47 bool multiple
= false;
48 bool duplicate
= false;
50 // Insert non key first frame.
51 complete
= framer_
.InsertPacket(
52 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
53 EXPECT_TRUE(complete
);
54 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
55 rtp_header_
.frame_id
= 1;
56 rtp_header_
.reference_frame_id
= 1;
57 rtp_header_
.is_key_frame
= true;
58 complete
= framer_
.InsertPacket(
59 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
60 EXPECT_TRUE(complete
);
61 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
62 EXPECT_TRUE(next_frame
);
63 EXPECT_TRUE(multiple
);
64 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
65 EXPECT_EQ(1u, frame
.frame_id
);
66 EXPECT_EQ(1u, frame
.referenced_frame_id
);
67 framer_
.ReleaseFrame(frame
.frame_id
);
70 TEST_F(FramerTest
, CompleteFrame
) {
72 bool next_frame
= false;
73 bool complete
= false;
74 bool multiple
= false;
75 bool duplicate
= false;
77 // Start with a complete key frame.
78 rtp_header_
.is_key_frame
= true;
79 complete
= framer_
.InsertPacket(
80 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
81 EXPECT_TRUE(complete
);
82 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
83 EXPECT_TRUE(next_frame
);
84 EXPECT_FALSE(multiple
);
85 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
86 EXPECT_EQ(0u, frame
.frame_id
);
87 EXPECT_EQ(0u, frame
.referenced_frame_id
);
88 framer_
.ReleaseFrame(frame
.frame_id
);
91 ++rtp_header_
.frame_id
;
92 rtp_header_
.reference_frame_id
= rtp_header_
.frame_id
- 1;
93 rtp_header_
.is_key_frame
= false;
94 rtp_header_
.max_packet_id
= 2;
95 complete
= framer_
.InsertPacket(
96 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
97 EXPECT_FALSE(complete
);
98 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
100 // Complete delta - can't skip, as incomplete sequence.
101 ++rtp_header_
.frame_id
;
102 rtp_header_
.reference_frame_id
= rtp_header_
.frame_id
- 1;
103 rtp_header_
.max_packet_id
= 0;
104 complete
= framer_
.InsertPacket(
105 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
106 EXPECT_TRUE(complete
);
107 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
110 TEST_F(FramerTest
, DuplicatePackets
) {
112 bool next_frame
= false;
113 bool complete
= false;
114 bool multiple
= false;
115 bool duplicate
= false;
117 // Start with an incomplete key frame.
118 rtp_header_
.is_key_frame
= true;
119 rtp_header_
.max_packet_id
= 1;
121 complete
= framer_
.InsertPacket(
122 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
123 EXPECT_FALSE(complete
);
124 EXPECT_FALSE(duplicate
);
125 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
127 // Add same packet again in incomplete key frame.
129 complete
= framer_
.InsertPacket(
130 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
131 EXPECT_FALSE(complete
);
132 EXPECT_TRUE(duplicate
);
133 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
135 // Complete key frame.
136 rtp_header_
.packet_id
= 1;
138 complete
= framer_
.InsertPacket(
139 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
140 EXPECT_TRUE(complete
);
141 EXPECT_FALSE(duplicate
);
142 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
143 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
144 EXPECT_FALSE(multiple
);
145 EXPECT_EQ(0u, frame
.referenced_frame_id
);
147 // Add same packet again in complete key frame.
149 complete
= framer_
.InsertPacket(
150 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
151 EXPECT_FALSE(complete
);
152 EXPECT_TRUE(duplicate
);
153 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
154 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
155 EXPECT_EQ(0u, frame
.frame_id
);
156 EXPECT_FALSE(multiple
);
157 EXPECT_EQ(0u, frame
.referenced_frame_id
);
158 framer_
.ReleaseFrame(frame
.frame_id
);
160 // Incomplete delta frame.
161 ++rtp_header_
.frame_id
;
162 rtp_header_
.reference_frame_id
= rtp_header_
.frame_id
- 1;
163 rtp_header_
.packet_id
= 0;
164 rtp_header_
.is_key_frame
= false;
166 complete
= framer_
.InsertPacket(
167 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
168 EXPECT_FALSE(complete
);
169 EXPECT_FALSE(duplicate
);
170 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
172 // Add same packet again in incomplete delta frame.
174 complete
= framer_
.InsertPacket(
175 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
176 EXPECT_FALSE(complete
);
177 EXPECT_TRUE(duplicate
);
178 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
180 // Complete delta frame.
181 rtp_header_
.packet_id
= 1;
183 complete
= framer_
.InsertPacket(
184 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
185 EXPECT_TRUE(complete
);
186 EXPECT_FALSE(duplicate
);
187 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
188 EXPECT_EQ(EncodedFrame::DEPENDENT
, frame
.dependency
);
189 EXPECT_EQ(1u, frame
.frame_id
);
190 EXPECT_EQ(0u, frame
.referenced_frame_id
);
191 EXPECT_FALSE(multiple
);
193 // Add same packet again in complete delta frame.
195 complete
= framer_
.InsertPacket(
196 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
197 EXPECT_FALSE(complete
);
198 EXPECT_TRUE(duplicate
);
199 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
200 EXPECT_EQ(EncodedFrame::DEPENDENT
, frame
.dependency
);
201 EXPECT_EQ(1u, frame
.frame_id
);
202 EXPECT_EQ(0u, frame
.referenced_frame_id
);
203 EXPECT_FALSE(multiple
);
206 TEST_F(FramerTest
, ContinuousSequence
) {
208 bool next_frame
= false;
209 bool complete
= false;
210 bool multiple
= false;
211 bool duplicate
= false;
213 // Start with a complete key frame.
214 rtp_header_
.is_key_frame
= true;
215 complete
= framer_
.InsertPacket(
216 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
217 EXPECT_TRUE(complete
);
218 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
219 EXPECT_TRUE(next_frame
);
220 EXPECT_FALSE(multiple
);
221 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
222 EXPECT_EQ(0u, frame
.frame_id
);
223 EXPECT_EQ(0u, frame
.referenced_frame_id
);
224 framer_
.ReleaseFrame(frame
.frame_id
);
226 // Complete - not continuous.
227 rtp_header_
.frame_id
= 2;
228 rtp_header_
.reference_frame_id
= rtp_header_
.frame_id
- 1;
229 rtp_header_
.is_key_frame
= false;
230 complete
= framer_
.InsertPacket(
231 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
232 EXPECT_TRUE(complete
);
233 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
236 TEST_F(FramerTest
, Wrap
) {
237 // Insert key frame, frame_id = 255 (will jump to that)
239 bool next_frame
= false;
240 bool multiple
= true;
241 bool duplicate
= false;
243 // Start with a complete key frame.
244 rtp_header_
.is_key_frame
= true;
245 rtp_header_
.frame_id
= 255;
246 rtp_header_
.reference_frame_id
= 255;
247 framer_
.InsertPacket(
248 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
249 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
250 EXPECT_TRUE(next_frame
);
251 EXPECT_FALSE(multiple
);
252 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
253 EXPECT_EQ(255u, frame
.frame_id
);
254 EXPECT_EQ(255u, frame
.referenced_frame_id
);
255 framer_
.ReleaseFrame(frame
.frame_id
);
257 // Insert wrapped delta frame - should be continuous.
258 rtp_header_
.is_key_frame
= false;
259 rtp_header_
.frame_id
= 256;
260 framer_
.InsertPacket(
261 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
262 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
263 EXPECT_TRUE(next_frame
);
264 EXPECT_FALSE(multiple
);
265 EXPECT_EQ(EncodedFrame::DEPENDENT
, frame
.dependency
);
266 EXPECT_EQ(256u, frame
.frame_id
);
267 EXPECT_EQ(255u, frame
.referenced_frame_id
);
268 framer_
.ReleaseFrame(frame
.frame_id
);
271 TEST_F(FramerTest
, Reset
) {
273 bool next_frame
= false;
274 bool complete
= false;
275 bool multiple
= true;
276 bool duplicate
= false;
278 // Start with a complete key frame.
279 rtp_header_
.is_key_frame
= true;
280 complete
= framer_
.InsertPacket(
281 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
282 EXPECT_TRUE(complete
);
284 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
287 TEST_F(FramerTest
, RequireKeyAfterReset
) {
289 bool next_frame
= false;
290 bool multiple
= false;
291 bool duplicate
= false;
295 // Start with a complete key frame.
296 rtp_header_
.is_key_frame
= false;
297 rtp_header_
.frame_id
= 0;
298 framer_
.InsertPacket(
299 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
300 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
301 rtp_header_
.frame_id
= 1;
302 rtp_header_
.reference_frame_id
= 1;
303 rtp_header_
.is_key_frame
= true;
304 framer_
.InsertPacket(
305 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
306 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
307 EXPECT_TRUE(next_frame
);
308 EXPECT_TRUE(multiple
);
311 TEST_F(FramerTest
, BasicNonLastReferenceId
) {
313 bool next_frame
= false;
314 bool multiple
= false;
315 bool duplicate
= false;
317 rtp_header_
.is_key_frame
= true;
318 rtp_header_
.frame_id
= 0;
319 framer_
.InsertPacket(
320 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
322 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
323 EXPECT_FALSE(multiple
);
324 framer_
.ReleaseFrame(frame
.frame_id
);
326 rtp_header_
.is_key_frame
= false;
327 rtp_header_
.reference_frame_id
= 0;
328 rtp_header_
.frame_id
= 5;
329 framer_
.InsertPacket(
330 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
332 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
333 EXPECT_FALSE(next_frame
);
334 EXPECT_FALSE(multiple
);
337 TEST_F(FramerTest
, InOrderReferenceFrameSelection
) {
338 // Create pattern: 0, 1, 4, 5.
340 bool next_frame
= false;
341 bool multiple
= false;
342 bool duplicate
= false;
344 rtp_header_
.is_key_frame
= true;
345 rtp_header_
.frame_id
= 0;
346 framer_
.InsertPacket(
347 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
348 rtp_header_
.is_key_frame
= false;
349 rtp_header_
.frame_id
= 1;
350 framer_
.InsertPacket(
351 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
353 // Insert frame #2 partially.
354 rtp_header_
.frame_id
= 2;
355 rtp_header_
.max_packet_id
= 1;
356 framer_
.InsertPacket(
357 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
358 rtp_header_
.frame_id
= 4;
359 rtp_header_
.max_packet_id
= 0;
360 rtp_header_
.reference_frame_id
= 0;
361 framer_
.InsertPacket(
362 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
363 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
364 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
365 EXPECT_EQ(0u, frame
.frame_id
);
366 EXPECT_EQ(0u, frame
.referenced_frame_id
);
367 EXPECT_FALSE(multiple
);
368 framer_
.ReleaseFrame(frame
.frame_id
);
369 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
370 EXPECT_TRUE(next_frame
);
371 EXPECT_TRUE(multiple
);
372 EXPECT_EQ(EncodedFrame::DEPENDENT
, frame
.dependency
);
373 EXPECT_EQ(1u, frame
.frame_id
);
374 EXPECT_EQ(0u, frame
.referenced_frame_id
);
375 framer_
.ReleaseFrame(frame
.frame_id
);
376 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
377 EXPECT_FALSE(next_frame
);
378 EXPECT_FALSE(multiple
);
379 EXPECT_EQ(EncodedFrame::DEPENDENT
, frame
.dependency
);
380 EXPECT_EQ(4u, frame
.frame_id
);
381 EXPECT_EQ(0u, frame
.referenced_frame_id
);
382 framer_
.ReleaseFrame(frame
.frame_id
);
383 // Insert remaining packet of frame #2 - should no be continuous.
384 rtp_header_
.frame_id
= 2;
385 rtp_header_
.packet_id
= 1;
386 framer_
.InsertPacket(
387 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
388 EXPECT_FALSE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
389 rtp_header_
.frame_id
= 5;
390 rtp_header_
.reference_frame_id
= rtp_header_
.frame_id
- 1;
391 rtp_header_
.packet_id
= 0;
392 rtp_header_
.max_packet_id
= 0;
393 framer_
.InsertPacket(
394 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
395 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
396 EXPECT_TRUE(next_frame
);
397 EXPECT_FALSE(multiple
);
398 EXPECT_EQ(EncodedFrame::DEPENDENT
, frame
.dependency
);
399 EXPECT_EQ(5u, frame
.frame_id
);
400 EXPECT_EQ(4u, frame
.referenced_frame_id
);
403 TEST_F(FramerTest
, AudioWrap
) {
404 // All audio frames are marked as key frames.
406 bool next_frame
= false;
407 bool multiple
= false;
408 bool duplicate
= false;
410 rtp_header_
.is_key_frame
= true;
411 rtp_header_
.frame_id
= 254;
412 rtp_header_
.reference_frame_id
= 254;
414 framer_
.InsertPacket(
415 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
416 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
417 EXPECT_TRUE(next_frame
);
418 EXPECT_FALSE(multiple
);
419 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
420 EXPECT_EQ(254u, frame
.frame_id
);
421 EXPECT_EQ(254u, frame
.referenced_frame_id
);
422 framer_
.ReleaseFrame(frame
.frame_id
);
424 rtp_header_
.frame_id
= 255;
425 rtp_header_
.reference_frame_id
= 255;
426 framer_
.InsertPacket(
427 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
429 // Insert wrapped frame - should be continuous.
430 rtp_header_
.frame_id
= 256;
431 rtp_header_
.reference_frame_id
= 256;
432 framer_
.InsertPacket(
433 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
435 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
436 EXPECT_TRUE(next_frame
);
437 EXPECT_TRUE(multiple
);
438 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
439 EXPECT_EQ(255u, frame
.frame_id
);
440 EXPECT_EQ(255u, frame
.referenced_frame_id
);
441 framer_
.ReleaseFrame(frame
.frame_id
);
443 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
444 EXPECT_TRUE(next_frame
);
445 EXPECT_FALSE(multiple
);
446 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
447 EXPECT_EQ(256u, frame
.frame_id
);
448 EXPECT_EQ(256u, frame
.referenced_frame_id
);
449 framer_
.ReleaseFrame(frame
.frame_id
);
452 TEST_F(FramerTest
, AudioWrapWithMissingFrame
) {
453 // All audio frames are marked as key frames.
455 bool next_frame
= false;
456 bool multiple
= true;
457 bool duplicate
= false;
459 // Insert and get first packet.
460 rtp_header_
.is_key_frame
= true;
461 rtp_header_
.frame_id
= 253;
462 rtp_header_
.reference_frame_id
= 253;
463 framer_
.InsertPacket(
464 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
465 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
466 EXPECT_TRUE(next_frame
);
467 EXPECT_FALSE(multiple
);
468 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
469 EXPECT_EQ(253u, frame
.frame_id
);
470 EXPECT_EQ(253u, frame
.referenced_frame_id
);
471 framer_
.ReleaseFrame(frame
.frame_id
);
473 // Insert third and fourth packets.
474 rtp_header_
.frame_id
= 255;
475 rtp_header_
.reference_frame_id
= 255;
476 framer_
.InsertPacket(
477 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
478 rtp_header_
.frame_id
= 256;
479 rtp_header_
.reference_frame_id
= 256;
480 framer_
.InsertPacket(
482 &payload_
[0], payload_
.size(), rtp_header_
, &duplicate
);
484 // Get third and fourth packets.
485 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
486 EXPECT_FALSE(next_frame
);
487 EXPECT_TRUE(multiple
);
488 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
489 EXPECT_EQ(255u, frame
.frame_id
);
490 EXPECT_EQ(255u, frame
.referenced_frame_id
);
491 framer_
.ReleaseFrame(frame
.frame_id
);
492 EXPECT_TRUE(framer_
.GetEncodedFrame(&frame
, &next_frame
, &multiple
));
493 EXPECT_TRUE(next_frame
);
494 EXPECT_FALSE(multiple
);
495 EXPECT_EQ(EncodedFrame::KEY
, frame
.dependency
);
496 EXPECT_EQ(256u, frame
.frame_id
);
497 EXPECT_EQ(256u, frame
.referenced_frame_id
);
498 framer_
.ReleaseFrame(frame
.frame_id
);