1 // Copyright 2013 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/framer/framer.h"
7 #include "media/cast/rtp_receiver/mock_rtp_payload_feedback.h"
8 #include "testing/gtest/include/gtest/gtest.h"
13 class FramerTest
: public ::testing::Test
{
16 : mock_rtp_payload_feedback_(),
17 framer_(&testing_clock_
, &mock_rtp_payload_feedback_
, 0, true, 0) {
18 payload_
.assign(kMaxIpPacketSize
, 0);
20 EXPECT_CALL(mock_rtp_payload_feedback_
, CastFeedback(testing::_
))
21 .WillRepeatedly(testing::Return());
24 virtual ~FramerTest() {}
26 std::vector
<uint8
> payload_
;
27 RtpCastHeader rtp_header_
;
28 MockRtpPayloadFeedback mock_rtp_payload_feedback_
;
30 base::SimpleTestTickClock testing_clock_
;
32 DISALLOW_COPY_AND_ASSIGN(FramerTest
);
35 TEST_F(FramerTest
, EmptyState
) {
36 transport::EncodedVideoFrame frame
;
37 bool next_frame
= false;
38 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
41 TEST_F(FramerTest
, AlwaysStartWithKey
) {
42 transport::EncodedVideoFrame frame
;
43 bool next_frame
= false;
44 bool complete
= false;
45 bool duplicate
= false;
47 // Insert non key first frame.
48 complete
= framer_
.InsertPacket(
49 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
50 EXPECT_TRUE(complete
);
51 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
52 rtp_header_
.frame_id
= 1;
53 rtp_header_
.reference_frame_id
= 1;
54 rtp_header_
.is_key_frame
= true;
55 complete
= framer_
.InsertPacket(
56 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
57 EXPECT_TRUE(complete
);
58 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
59 EXPECT_TRUE(next_frame
);
60 EXPECT_EQ(1u, frame
.frame_id
);
61 EXPECT_TRUE(frame
.key_frame
);
62 framer_
.ReleaseFrame(frame
.frame_id
);
65 TEST_F(FramerTest
, CompleteFrame
) {
66 transport::EncodedVideoFrame frame
;
67 bool next_frame
= false;
68 bool complete
= false;
69 bool duplicate
= false;
71 // Start with a complete key frame.
72 rtp_header_
.is_key_frame
= true;
73 complete
= framer_
.InsertPacket(
74 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
75 EXPECT_TRUE(complete
);
76 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
77 EXPECT_TRUE(next_frame
);
78 EXPECT_EQ(0u, frame
.frame_id
);
79 EXPECT_TRUE(frame
.key_frame
);
80 framer_
.ReleaseFrame(frame
.frame_id
);
83 ++rtp_header_
.frame_id
;
84 rtp_header_
.reference_frame_id
= rtp_header_
.frame_id
- 1;
85 rtp_header_
.is_key_frame
= false;
86 rtp_header_
.max_packet_id
= 2;
87 complete
= framer_
.InsertPacket(
88 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
89 EXPECT_FALSE(complete
);
90 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
92 // Complete delta - can't skip, as incomplete sequence.
93 ++rtp_header_
.frame_id
;
94 rtp_header_
.reference_frame_id
= rtp_header_
.frame_id
- 1;
95 rtp_header_
.max_packet_id
= 0;
96 complete
= framer_
.InsertPacket(
97 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
98 EXPECT_TRUE(complete
);
99 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
102 TEST_F(FramerTest
, DuplicatePackets
) {
103 transport::EncodedVideoFrame frame
;
104 bool next_frame
= false;
105 bool complete
= false;
106 bool duplicate
= false;
108 // Start with an incomplete key frame.
109 rtp_header_
.is_key_frame
= true;
110 rtp_header_
.max_packet_id
= 1;
112 complete
= framer_
.InsertPacket(
113 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
114 EXPECT_FALSE(complete
);
115 EXPECT_FALSE(duplicate
);
116 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
118 // Add same packet again in incomplete key frame.
120 complete
= framer_
.InsertPacket(
121 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
122 EXPECT_FALSE(complete
);
123 EXPECT_TRUE(duplicate
);
124 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
126 // Complete key frame.
127 rtp_header_
.packet_id
= 1;
129 complete
= framer_
.InsertPacket(
130 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
131 EXPECT_TRUE(complete
);
132 EXPECT_FALSE(duplicate
);
133 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
134 EXPECT_EQ(0u, frame
.frame_id
);
136 // Add same packet again in complete key frame.
138 complete
= framer_
.InsertPacket(
139 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
140 EXPECT_FALSE(complete
);
141 EXPECT_TRUE(duplicate
);
142 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
143 EXPECT_EQ(0u, frame
.frame_id
);
144 framer_
.ReleaseFrame(frame
.frame_id
);
146 // Incomplete delta frame.
147 ++rtp_header_
.frame_id
;
148 rtp_header_
.reference_frame_id
= rtp_header_
.frame_id
- 1;
149 rtp_header_
.packet_id
= 0;
150 rtp_header_
.is_key_frame
= false;
152 complete
= framer_
.InsertPacket(
153 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
154 EXPECT_FALSE(complete
);
155 EXPECT_FALSE(duplicate
);
156 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
158 // Add same packet again in incomplete delta frame.
160 complete
= framer_
.InsertPacket(
161 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
162 EXPECT_FALSE(complete
);
163 EXPECT_TRUE(duplicate
);
164 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
166 // Complete delta frame.
167 rtp_header_
.packet_id
= 1;
169 complete
= framer_
.InsertPacket(
170 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
171 EXPECT_TRUE(complete
);
172 EXPECT_FALSE(duplicate
);
173 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
174 EXPECT_EQ(1u, frame
.frame_id
);
176 // Add same packet again in complete delta frame.
178 complete
= framer_
.InsertPacket(
179 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
180 EXPECT_FALSE(complete
);
181 EXPECT_TRUE(duplicate
);
182 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
183 EXPECT_EQ(1u, frame
.frame_id
);
186 TEST_F(FramerTest
, ContinuousSequence
) {
187 transport::EncodedVideoFrame frame
;
188 bool next_frame
= false;
189 bool complete
= false;
190 bool duplicate
= false;
192 // Start with a complete key frame.
193 rtp_header_
.is_key_frame
= true;
194 complete
= framer_
.InsertPacket(
195 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
196 EXPECT_TRUE(complete
);
197 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
198 EXPECT_TRUE(next_frame
);
199 EXPECT_EQ(0u, frame
.frame_id
);
200 EXPECT_TRUE(frame
.key_frame
);
201 framer_
.ReleaseFrame(frame
.frame_id
);
203 // Complete - not continuous.
204 rtp_header_
.frame_id
= 2;
205 rtp_header_
.reference_frame_id
= rtp_header_
.frame_id
- 1;
206 rtp_header_
.is_key_frame
= false;
207 complete
= framer_
.InsertPacket(
208 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
209 EXPECT_TRUE(complete
);
210 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
213 TEST_F(FramerTest
, Wrap
) {
214 // Insert key frame, frame_id = 255 (will jump to that)
215 transport::EncodedVideoFrame frame
;
216 bool next_frame
= false;
217 bool duplicate
= false;
219 // Start with a complete key frame.
220 rtp_header_
.is_key_frame
= true;
221 rtp_header_
.frame_id
= 255;
222 rtp_header_
.reference_frame_id
= 255;
223 framer_
.InsertPacket(
224 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
225 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
226 EXPECT_TRUE(next_frame
);
227 EXPECT_EQ(255u, frame
.frame_id
);
228 framer_
.ReleaseFrame(frame
.frame_id
);
230 // Insert wrapped delta frame - should be continuous.
231 rtp_header_
.is_key_frame
= false;
232 rtp_header_
.frame_id
= 256;
233 framer_
.InsertPacket(
234 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
235 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
236 EXPECT_TRUE(next_frame
);
237 EXPECT_EQ(256u, frame
.frame_id
);
238 framer_
.ReleaseFrame(frame
.frame_id
);
241 TEST_F(FramerTest
, Reset
) {
242 transport::EncodedVideoFrame frame
;
243 bool next_frame
= false;
244 bool complete
= false;
245 bool duplicate
= false;
247 // Start with a complete key frame.
248 rtp_header_
.is_key_frame
= true;
249 complete
= framer_
.InsertPacket(
250 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
251 EXPECT_TRUE(complete
);
253 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
256 TEST_F(FramerTest
, RequireKeyAfterReset
) {
257 transport::EncodedVideoFrame frame
;
258 bool next_frame
= false;
259 bool duplicate
= false;
263 // Start with a complete key frame.
264 rtp_header_
.is_key_frame
= false;
265 rtp_header_
.frame_id
= 0;
266 framer_
.InsertPacket(
267 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
268 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
269 rtp_header_
.frame_id
= 1;
270 rtp_header_
.reference_frame_id
= 1;
271 rtp_header_
.is_key_frame
= true;
272 framer_
.InsertPacket(
273 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
274 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
275 EXPECT_TRUE(next_frame
);
278 TEST_F(FramerTest
, BasicNonLastReferenceId
) {
279 transport::EncodedVideoFrame frame
;
280 bool next_frame
= false;
281 bool duplicate
= false;
283 rtp_header_
.is_key_frame
= true;
284 rtp_header_
.frame_id
= 0;
285 framer_
.InsertPacket(
286 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
288 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
289 framer_
.ReleaseFrame(frame
.frame_id
);
291 rtp_header_
.is_key_frame
= false;
292 rtp_header_
.reference_frame_id
= 0;
293 rtp_header_
.frame_id
= 5;
294 framer_
.InsertPacket(
295 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
297 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
298 EXPECT_FALSE(next_frame
);
301 TEST_F(FramerTest
, InOrderReferenceFrameSelection
) {
302 // Create pattern: 0, 1, 4, 5.
303 transport::EncodedVideoFrame frame
;
304 bool next_frame
= false;
305 bool duplicate
= false;
307 rtp_header_
.is_key_frame
= true;
308 rtp_header_
.frame_id
= 0;
309 framer_
.InsertPacket(
310 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
311 rtp_header_
.is_key_frame
= false;
312 rtp_header_
.frame_id
= 1;
313 framer_
.InsertPacket(
314 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
316 // Insert frame #2 partially.
317 rtp_header_
.frame_id
= 2;
318 rtp_header_
.max_packet_id
= 1;
319 framer_
.InsertPacket(
320 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
321 rtp_header_
.frame_id
= 4;
322 rtp_header_
.max_packet_id
= 0;
323 rtp_header_
.reference_frame_id
= 0;
324 framer_
.InsertPacket(
325 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
326 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
327 EXPECT_EQ(0u, frame
.frame_id
);
328 framer_
.ReleaseFrame(frame
.frame_id
);
329 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
330 EXPECT_TRUE(next_frame
);
331 EXPECT_EQ(1u, frame
.frame_id
);
332 framer_
.ReleaseFrame(frame
.frame_id
);
333 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
334 EXPECT_FALSE(next_frame
);
335 EXPECT_EQ(4u, frame
.frame_id
);
336 framer_
.ReleaseFrame(frame
.frame_id
);
337 // Insert remaining packet of frame #2 - should no be continuous.
338 rtp_header_
.frame_id
= 2;
339 rtp_header_
.packet_id
= 1;
340 framer_
.InsertPacket(
341 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
342 EXPECT_FALSE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
343 rtp_header_
.frame_id
= 5;
344 rtp_header_
.reference_frame_id
= rtp_header_
.frame_id
- 1;
345 rtp_header_
.packet_id
= 0;
346 rtp_header_
.max_packet_id
= 0;
347 framer_
.InsertPacket(
348 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
349 EXPECT_TRUE(framer_
.GetEncodedVideoFrame(&frame
, &next_frame
));
350 EXPECT_TRUE(next_frame
);
351 EXPECT_EQ(5u, frame
.frame_id
);
354 TEST_F(FramerTest
, AudioWrap
) {
355 // All audio frames are marked as key frames.
356 transport::EncodedAudioFrame frame
;
357 bool next_frame
= false;
358 bool duplicate
= false;
360 rtp_header_
.is_key_frame
= true;
361 rtp_header_
.frame_id
= 254;
362 rtp_header_
.reference_frame_id
= 254;
364 framer_
.InsertPacket(
365 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
366 EXPECT_TRUE(framer_
.GetEncodedAudioFrame(&frame
, &next_frame
));
367 EXPECT_TRUE(next_frame
);
368 EXPECT_EQ(254u, frame
.frame_id
);
369 framer_
.ReleaseFrame(frame
.frame_id
);
371 rtp_header_
.frame_id
= 255;
372 rtp_header_
.reference_frame_id
= 255;
373 framer_
.InsertPacket(
374 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
376 // Insert wrapped frame - should be continuous.
377 rtp_header_
.frame_id
= 256;
378 rtp_header_
.reference_frame_id
= 256;
379 framer_
.InsertPacket(
380 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
382 EXPECT_TRUE(framer_
.GetEncodedAudioFrame(&frame
, &next_frame
));
383 EXPECT_TRUE(next_frame
);
384 EXPECT_EQ(255u, frame
.frame_id
);
385 framer_
.ReleaseFrame(frame
.frame_id
);
387 EXPECT_TRUE(framer_
.GetEncodedAudioFrame(&frame
, &next_frame
));
388 EXPECT_TRUE(next_frame
);
389 EXPECT_EQ(256u, frame
.frame_id
);
390 framer_
.ReleaseFrame(frame
.frame_id
);
393 TEST_F(FramerTest
, AudioWrapWithMissingFrame
) {
394 // All audio frames are marked as key frames.
395 transport::EncodedAudioFrame frame
;
396 bool next_frame
= false;
397 bool duplicate
= false;
399 // Insert and get first packet.
400 rtp_header_
.is_key_frame
= true;
401 rtp_header_
.frame_id
= 253;
402 rtp_header_
.reference_frame_id
= 253;
403 framer_
.InsertPacket(
404 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
405 EXPECT_TRUE(framer_
.GetEncodedAudioFrame(&frame
, &next_frame
));
406 EXPECT_TRUE(next_frame
);
407 EXPECT_EQ(253u, frame
.frame_id
);
408 framer_
.ReleaseFrame(frame
.frame_id
);
410 // Insert third and fourth packets.
411 rtp_header_
.frame_id
= 255;
412 rtp_header_
.reference_frame_id
= 255;
413 framer_
.InsertPacket(
414 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
415 rtp_header_
.frame_id
= 256;
416 rtp_header_
.reference_frame_id
= 256;
417 framer_
.InsertPacket(
418 payload_
.data(), payload_
.size(), rtp_header_
, &duplicate
);
420 // Get third and fourth packets.
421 EXPECT_TRUE(framer_
.GetEncodedAudioFrame(&frame
, &next_frame
));
422 EXPECT_FALSE(next_frame
);
423 EXPECT_EQ(255u, frame
.frame_id
);
424 framer_
.ReleaseFrame(frame
.frame_id
);
425 EXPECT_TRUE(framer_
.GetEncodedAudioFrame(&frame
, &next_frame
));
426 EXPECT_TRUE(next_frame
);
427 EXPECT_EQ(256u, frame
.frame_id
);
428 framer_
.ReleaseFrame(frame
.frame_id
);