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/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/test/simple_test_tick_clock.h"
8 #include "base/time/tick_clock.h"
9 #include "media/cast/cast_environment.h"
10 #include "media/cast/logging/encoding_event_subscriber.h"
11 #include "media/cast/logging/logging_defines.h"
12 #include "media/cast/test/fake_single_thread_task_runner.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using media::cast::proto::AggregatedFrameEvent
;
16 using media::cast::proto::AggregatedPacketEvent
;
17 using media::cast::proto::BasePacketEvent
;
18 using media::cast::proto::LogMetadata
;
22 int64
InMilliseconds(base::TimeTicks event_time
) {
23 return (event_time
- base::TimeTicks()).InMilliseconds();
31 class EncodingEventSubscriberTest
: public ::testing::Test
{
33 EncodingEventSubscriberTest()
34 : testing_clock_(new base::SimpleTestTickClock()),
35 task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_
)),
36 cast_environment_(new CastEnvironment(
37 scoped_ptr
<base::TickClock
>(testing_clock_
).Pass(),
41 first_rtp_timestamp_(0) {}
43 void Init(EventMediaType event_media_type
) {
44 DCHECK(!event_subscriber_
);
45 event_subscriber_
.reset(new EncodingEventSubscriber(event_media_type
, 10));
46 cast_environment_
->Logging()->AddRawEventSubscriber(
47 event_subscriber_
.get());
50 virtual ~EncodingEventSubscriberTest() {
51 if (event_subscriber_
) {
52 cast_environment_
->Logging()->RemoveRawEventSubscriber(
53 event_subscriber_
.get());
57 void GetEventsAndReset() {
58 event_subscriber_
->GetEventsAndReset(
59 &metadata_
, &frame_events_
, &packet_events_
);
60 first_rtp_timestamp_
= metadata_
.first_rtp_timestamp();
63 base::SimpleTestTickClock
* testing_clock_
; // Owned by CastEnvironment.
64 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
65 scoped_refptr
<CastEnvironment
> cast_environment_
;
66 scoped_ptr
<EncodingEventSubscriber
> event_subscriber_
;
67 FrameEventList frame_events_
;
68 PacketEventList packet_events_
;
69 LogMetadata metadata_
;
70 RtpTimestamp first_rtp_timestamp_
;
73 TEST_F(EncodingEventSubscriberTest
, FrameEventTruncating
) {
76 base::TimeTicks
now(testing_clock_
->NowTicks());
78 // Entry with RTP timestamp 0 should get dropped.
79 for (int i
= 0; i
< 11; i
++) {
80 cast_environment_
->Logging()->InsertFrameEvent(now
,
85 cast_environment_
->Logging()->InsertFrameEvent(now
,
94 ASSERT_EQ(10u, frame_events_
.size());
95 EXPECT_EQ(100u, frame_events_
.front()->relative_rtp_timestamp());
96 EXPECT_EQ(1000u, frame_events_
.back()->relative_rtp_timestamp());
99 TEST_F(EncodingEventSubscriberTest
, PacketEventTruncating
) {
102 base::TimeTicks
now(testing_clock_
->NowTicks());
104 // Entry with RTP timestamp 0 should get dropped.
105 for (int i
= 0; i
< 11; i
++) {
106 cast_environment_
->Logging()->InsertPacketEvent(now
,
109 /*rtp_timestamp*/ i
* 100,
112 /*max_packet_id*/ 10,
118 ASSERT_EQ(10u, packet_events_
.size());
119 EXPECT_EQ(100u, packet_events_
.front()->relative_rtp_timestamp());
120 EXPECT_EQ(1000u, packet_events_
.back()->relative_rtp_timestamp());
123 TEST_F(EncodingEventSubscriberTest
, EventFiltering
) {
126 base::TimeTicks
now(testing_clock_
->NowTicks());
127 RtpTimestamp rtp_timestamp
= 100;
128 cast_environment_
->Logging()->InsertFrameEvent(now
,
134 // This is an AUDIO_EVENT and shouldn't be processed by the subscriber.
135 cast_environment_
->Logging()->InsertFrameEvent(now
,
143 ASSERT_EQ(1u, frame_events_
.size());
144 FrameEventList::iterator it
= frame_events_
.begin();
146 linked_ptr
<AggregatedFrameEvent
> frame_event
= *it
;
148 ASSERT_EQ(1, frame_event
->event_type_size());
149 EXPECT_EQ(media::cast::proto::FRAME_DECODED
,
150 frame_event
->event_type(0));
154 EXPECT_TRUE(packet_events_
.empty());
157 TEST_F(EncodingEventSubscriberTest
, FrameEvent
) {
159 base::TimeTicks
now(testing_clock_
->NowTicks());
160 RtpTimestamp rtp_timestamp
= 100;
161 cast_environment_
->Logging()->InsertFrameEvent(now
, FRAME_DECODED
,
168 ASSERT_EQ(1u, frame_events_
.size());
170 RtpTimestamp relative_rtp_timestamp
= rtp_timestamp
- first_rtp_timestamp_
;
171 FrameEventList::iterator it
= frame_events_
.begin();
173 linked_ptr
<AggregatedFrameEvent
> event
= *it
;
175 EXPECT_EQ(relative_rtp_timestamp
, event
->relative_rtp_timestamp());
177 ASSERT_EQ(1, event
->event_type_size());
178 EXPECT_EQ(media::cast::proto::FRAME_DECODED
, event
->event_type(0));
179 ASSERT_EQ(1, event
->event_timestamp_ms_size());
180 EXPECT_EQ(InMilliseconds(now
), event
->event_timestamp_ms(0));
182 EXPECT_EQ(0, event
->encoded_frame_size());
183 EXPECT_EQ(0, event
->delay_millis());
186 EXPECT_TRUE(frame_events_
.empty());
189 TEST_F(EncodingEventSubscriberTest
, FrameEventDelay
) {
191 base::TimeTicks
now(testing_clock_
->NowTicks());
192 RtpTimestamp rtp_timestamp
= 100;
194 cast_environment_
->Logging()->InsertFrameEventWithDelay(
195 now
, FRAME_PLAYOUT
, AUDIO_EVENT
, rtp_timestamp
,
196 /*frame_id*/ 0, base::TimeDelta::FromMilliseconds(delay_ms
));
200 ASSERT_EQ(1u, frame_events_
.size());
202 RtpTimestamp relative_rtp_timestamp
= rtp_timestamp
- first_rtp_timestamp_
;
203 FrameEventList::iterator it
= frame_events_
.begin();
205 linked_ptr
<AggregatedFrameEvent
> event
= *it
;
207 EXPECT_EQ(relative_rtp_timestamp
, event
->relative_rtp_timestamp());
209 ASSERT_EQ(1, event
->event_type_size());
210 EXPECT_EQ(media::cast::proto::FRAME_PLAYOUT
, event
->event_type(0));
211 ASSERT_EQ(1, event
->event_timestamp_ms_size());
212 EXPECT_EQ(InMilliseconds(now
), event
->event_timestamp_ms(0));
214 EXPECT_EQ(0, event
->encoded_frame_size());
215 EXPECT_EQ(100, event
->delay_millis());
216 EXPECT_FALSE(event
->has_key_frame());
219 TEST_F(EncodingEventSubscriberTest
, FrameEventSize
) {
221 base::TimeTicks
now(testing_clock_
->NowTicks());
222 RtpTimestamp rtp_timestamp
= 100;
224 bool key_frame
= true;
225 int target_bitrate
= 1024;
226 cast_environment_
->Logging()->InsertEncodedFrameEvent(
227 now
, FRAME_ENCODED
, VIDEO_EVENT
, rtp_timestamp
,
228 /*frame_id*/ 0, size
, key_frame
, target_bitrate
);
232 ASSERT_EQ(1u, frame_events_
.size());
234 RtpTimestamp relative_rtp_timestamp
= rtp_timestamp
- first_rtp_timestamp_
;
235 FrameEventList::iterator it
= frame_events_
.begin();
237 linked_ptr
<AggregatedFrameEvent
> event
= *it
;
239 EXPECT_EQ(relative_rtp_timestamp
, event
->relative_rtp_timestamp());
241 ASSERT_EQ(1, event
->event_type_size());
242 EXPECT_EQ(media::cast::proto::FRAME_ENCODED
, event
->event_type(0));
243 ASSERT_EQ(1, event
->event_timestamp_ms_size());
244 EXPECT_EQ(InMilliseconds(now
), event
->event_timestamp_ms(0));
246 EXPECT_EQ(size
, event
->encoded_frame_size());
247 EXPECT_EQ(0, event
->delay_millis());
248 EXPECT_TRUE(event
->has_key_frame());
249 EXPECT_EQ(key_frame
, event
->key_frame());
250 EXPECT_EQ(target_bitrate
, event
->target_bitrate());
253 TEST_F(EncodingEventSubscriberTest
, MultipleFrameEvents
) {
255 RtpTimestamp rtp_timestamp1
= 100;
256 RtpTimestamp rtp_timestamp2
= 200;
257 base::TimeTicks
now1(testing_clock_
->NowTicks());
258 cast_environment_
->Logging()->InsertFrameEventWithDelay(
259 now1
, FRAME_PLAYOUT
, AUDIO_EVENT
, rtp_timestamp1
,
260 /*frame_id*/ 0, /*delay*/ base::TimeDelta::FromMilliseconds(100));
262 testing_clock_
->Advance(base::TimeDelta::FromMilliseconds(20));
263 base::TimeTicks
now2(testing_clock_
->NowTicks());
264 cast_environment_
->Logging()->InsertEncodedFrameEvent(
265 now2
, FRAME_ENCODED
, AUDIO_EVENT
, rtp_timestamp2
,
266 /*frame_id*/ 0, /*size*/ 123, /* key_frame - unused */ false,
267 /*target_bitrate - unused*/ 0);
269 testing_clock_
->Advance(base::TimeDelta::FromMilliseconds(20));
270 base::TimeTicks
now3(testing_clock_
->NowTicks());
271 cast_environment_
->Logging()->InsertFrameEvent(
272 now3
, FRAME_DECODED
, AUDIO_EVENT
, rtp_timestamp1
, /*frame_id*/ 0);
276 ASSERT_EQ(2u, frame_events_
.size());
278 RtpTimestamp relative_rtp_timestamp
= rtp_timestamp1
- first_rtp_timestamp_
;
279 FrameEventList::iterator it
= frame_events_
.begin();
281 linked_ptr
<AggregatedFrameEvent
> event
= *it
;
283 EXPECT_EQ(relative_rtp_timestamp
, event
->relative_rtp_timestamp());
285 ASSERT_EQ(2, event
->event_type_size());
286 EXPECT_EQ(media::cast::proto::FRAME_PLAYOUT
, event
->event_type(0));
287 EXPECT_EQ(media::cast::proto::FRAME_DECODED
, event
->event_type(1));
289 ASSERT_EQ(2, event
->event_timestamp_ms_size());
290 EXPECT_EQ(InMilliseconds(now1
), event
->event_timestamp_ms(0));
291 EXPECT_EQ(InMilliseconds(now3
), event
->event_timestamp_ms(1));
293 EXPECT_FALSE(event
->has_key_frame());
295 relative_rtp_timestamp
= rtp_timestamp2
- first_rtp_timestamp_
;
300 EXPECT_EQ(relative_rtp_timestamp
, event
->relative_rtp_timestamp());
302 ASSERT_EQ(1, event
->event_type_size());
303 EXPECT_EQ(media::cast::proto::FRAME_ENCODED
, event
->event_type(0));
305 ASSERT_EQ(1, event
->event_timestamp_ms_size());
306 EXPECT_EQ(InMilliseconds(now2
), event
->event_timestamp_ms(0));
308 EXPECT_FALSE(event
->has_key_frame());
311 TEST_F(EncodingEventSubscriberTest
, PacketEvent
) {
313 base::TimeTicks
now(testing_clock_
->NowTicks());
314 RtpTimestamp rtp_timestamp
= 100;
317 cast_environment_
->Logging()->InsertPacketEvent(
318 now
, PACKET_RECEIVED
, AUDIO_EVENT
,
319 rtp_timestamp
, /*frame_id*/ 0, packet_id
,
320 /*max_packet_id*/ 10, size
);
324 ASSERT_EQ(1u, packet_events_
.size());
326 RtpTimestamp relative_rtp_timestamp
= rtp_timestamp
- first_rtp_timestamp_
;
327 PacketEventList::iterator it
= packet_events_
.begin();
329 linked_ptr
<AggregatedPacketEvent
> event
= *it
;
331 EXPECT_EQ(relative_rtp_timestamp
, event
->relative_rtp_timestamp());
333 ASSERT_EQ(1, event
->base_packet_event_size());
334 const BasePacketEvent
& base_event
= event
->base_packet_event(0);
335 EXPECT_EQ(packet_id
, base_event
.packet_id());
336 ASSERT_EQ(1, base_event
.event_type_size());
337 EXPECT_EQ(media::cast::proto::PACKET_RECEIVED
,
338 base_event
.event_type(0));
339 ASSERT_EQ(1, base_event
.event_timestamp_ms_size());
340 EXPECT_EQ(InMilliseconds(now
), base_event
.event_timestamp_ms(0));
341 EXPECT_EQ(size
, base_event
.size());
344 EXPECT_TRUE(packet_events_
.empty());
347 TEST_F(EncodingEventSubscriberTest
, MultiplePacketEventsForPacket
) {
349 base::TimeTicks
now1(testing_clock_
->NowTicks());
350 RtpTimestamp rtp_timestamp
= 100;
353 cast_environment_
->Logging()->InsertPacketEvent(now1
,
354 PACKET_SENT_TO_NETWORK
,
359 /*max_packet_id*/ 10,
362 testing_clock_
->Advance(base::TimeDelta::FromMilliseconds(20));
363 base::TimeTicks
now2(testing_clock_
->NowTicks());
364 cast_environment_
->Logging()->InsertPacketEvent(now2
,
365 PACKET_RETRANSMITTED
,
370 /*max_packet_id*/ 10,
375 ASSERT_EQ(1u, packet_events_
.size());
377 RtpTimestamp relative_rtp_timestamp
= rtp_timestamp
- first_rtp_timestamp_
;
378 PacketEventList::iterator it
= packet_events_
.begin();
380 linked_ptr
<AggregatedPacketEvent
> event
= *it
;
382 EXPECT_EQ(relative_rtp_timestamp
, event
->relative_rtp_timestamp());
384 ASSERT_EQ(1, event
->base_packet_event_size());
385 const BasePacketEvent
& base_event
= event
->base_packet_event(0);
386 EXPECT_EQ(packet_id
, base_event
.packet_id());
387 ASSERT_EQ(2, base_event
.event_type_size());
388 EXPECT_EQ(media::cast::proto::PACKET_SENT_TO_NETWORK
,
389 base_event
.event_type(0));
390 EXPECT_EQ(media::cast::proto::PACKET_RETRANSMITTED
,
391 base_event
.event_type(1));
392 ASSERT_EQ(2, base_event
.event_timestamp_ms_size());
393 EXPECT_EQ(InMilliseconds(now1
), base_event
.event_timestamp_ms(0));
394 EXPECT_EQ(InMilliseconds(now2
), base_event
.event_timestamp_ms(1));
397 TEST_F(EncodingEventSubscriberTest
, MultiplePacketEventsForFrame
) {
399 base::TimeTicks
now1(testing_clock_
->NowTicks());
400 RtpTimestamp rtp_timestamp
= 100;
404 cast_environment_
->Logging()->InsertPacketEvent(now1
,
405 PACKET_SENT_TO_NETWORK
,
410 /*max_packet_id*/ 10,
413 testing_clock_
->Advance(base::TimeDelta::FromMilliseconds(20));
414 base::TimeTicks
now2(testing_clock_
->NowTicks());
415 cast_environment_
->Logging()->InsertPacketEvent(now2
,
416 PACKET_RETRANSMITTED
,
421 /*max_packet_id*/ 10,
426 ASSERT_EQ(1u, packet_events_
.size());
428 RtpTimestamp relative_rtp_timestamp
= rtp_timestamp
- first_rtp_timestamp_
;
429 PacketEventList::iterator it
= packet_events_
.begin();
431 linked_ptr
<AggregatedPacketEvent
> event
= *it
;
433 EXPECT_EQ(relative_rtp_timestamp
, event
->relative_rtp_timestamp());
435 ASSERT_EQ(2, event
->base_packet_event_size());
436 const BasePacketEvent
& base_event
= event
->base_packet_event(0);
437 EXPECT_EQ(packet_id_1
, base_event
.packet_id());
438 ASSERT_EQ(1, base_event
.event_type_size());
439 EXPECT_EQ(media::cast::proto::PACKET_SENT_TO_NETWORK
,
440 base_event
.event_type(0));
441 ASSERT_EQ(1, base_event
.event_timestamp_ms_size());
442 EXPECT_EQ(InMilliseconds(now1
), base_event
.event_timestamp_ms(0));
444 const BasePacketEvent
& base_event_2
= event
->base_packet_event(1);
445 EXPECT_EQ(packet_id_2
, base_event_2
.packet_id());
446 ASSERT_EQ(1, base_event_2
.event_type_size());
447 EXPECT_EQ(media::cast::proto::PACKET_RETRANSMITTED
,
448 base_event_2
.event_type(0));
449 ASSERT_EQ(1, base_event_2
.event_timestamp_ms_size());
450 EXPECT_EQ(InMilliseconds(now2
), base_event_2
.event_timestamp_ms(0));
453 TEST_F(EncodingEventSubscriberTest
, MultiplePacketEvents
) {
455 base::TimeTicks
now1(testing_clock_
->NowTicks());
456 RtpTimestamp rtp_timestamp_1
= 100;
457 RtpTimestamp rtp_timestamp_2
= 200;
461 cast_environment_
->Logging()->InsertPacketEvent(now1
,
462 PACKET_SENT_TO_NETWORK
,
467 /*max_packet_id*/ 10,
470 testing_clock_
->Advance(base::TimeDelta::FromMilliseconds(20));
471 base::TimeTicks
now2(testing_clock_
->NowTicks());
472 cast_environment_
->Logging()->InsertPacketEvent(now2
,
473 PACKET_RETRANSMITTED
,
478 /*max_packet_id*/ 10,
483 ASSERT_EQ(2u, packet_events_
.size());
485 RtpTimestamp relative_rtp_timestamp
= rtp_timestamp_1
- first_rtp_timestamp_
;
486 PacketEventList::iterator it
= packet_events_
.begin();
488 linked_ptr
<AggregatedPacketEvent
> event
= *it
;
490 EXPECT_EQ(relative_rtp_timestamp
, event
->relative_rtp_timestamp());
492 ASSERT_EQ(1, event
->base_packet_event_size());
493 const BasePacketEvent
& base_event
= event
->base_packet_event(0);
494 EXPECT_EQ(packet_id_1
, base_event
.packet_id());
495 ASSERT_EQ(1, base_event
.event_type_size());
496 EXPECT_EQ(media::cast::proto::PACKET_SENT_TO_NETWORK
,
497 base_event
.event_type(0));
498 ASSERT_EQ(1, base_event
.event_timestamp_ms_size());
499 EXPECT_EQ(InMilliseconds(now1
), base_event
.event_timestamp_ms(0));
501 relative_rtp_timestamp
= rtp_timestamp_2
- first_rtp_timestamp_
;
503 ASSERT_TRUE(it
!= packet_events_
.end());
506 EXPECT_EQ(relative_rtp_timestamp
, event
->relative_rtp_timestamp());
508 ASSERT_EQ(1, event
->base_packet_event_size());
509 const BasePacketEvent
& base_event_2
= event
->base_packet_event(0);
510 EXPECT_EQ(packet_id_2
, base_event_2
.packet_id());
511 ASSERT_EQ(1, base_event_2
.event_type_size());
512 EXPECT_EQ(media::cast::proto::PACKET_RETRANSMITTED
,
513 base_event_2
.event_type(0));
514 ASSERT_EQ(1, base_event_2
.event_timestamp_ms_size());
515 EXPECT_EQ(InMilliseconds(now2
), base_event_2
.event_timestamp_ms(0));
518 TEST_F(EncodingEventSubscriberTest
, FirstRtpTimestamp
) {
520 RtpTimestamp rtp_timestamp
= 12345;
521 base::TimeTicks
now(testing_clock_
->NowTicks());
523 cast_environment_
->Logging()->InsertFrameEvent(now
,
529 cast_environment_
->Logging()->InsertFrameEvent(now
,
537 EXPECT_EQ(rtp_timestamp
, first_rtp_timestamp_
);
538 FrameEventList::iterator it
= frame_events_
.begin();
539 ASSERT_NE(frame_events_
.end(), it
);
540 EXPECT_EQ(0u, (*it
)->relative_rtp_timestamp());
543 ASSERT_NE(frame_events_
.end(), it
);
544 EXPECT_EQ(30u, (*it
)->relative_rtp_timestamp());
546 rtp_timestamp
= 67890;
548 cast_environment_
->Logging()->InsertFrameEvent(now
,
555 EXPECT_EQ(rtp_timestamp
, first_rtp_timestamp_
);
558 TEST_F(EncodingEventSubscriberTest
, RelativeRtpTimestampWrapAround
) {
560 RtpTimestamp rtp_timestamp
= 0xffffffff - 20;
561 base::TimeTicks
now(testing_clock_
->NowTicks());
563 cast_environment_
->Logging()->InsertFrameEvent(now
,
569 // RtpTimestamp has now wrapped around.
570 cast_environment_
->Logging()->InsertFrameEvent(now
,
578 FrameEventList::iterator it
= frame_events_
.begin();
579 ASSERT_NE(frame_events_
.end(), it
);
580 EXPECT_EQ(0u, (*it
)->relative_rtp_timestamp());
583 ASSERT_NE(frame_events_
.end(), it
);
584 EXPECT_EQ(30u, (*it
)->relative_rtp_timestamp());
587 TEST_F(EncodingEventSubscriberTest
, MaxEventsPerProto
) {
589 RtpTimestamp rtp_timestamp
= 100;
590 for (int i
= 0; i
< kMaxEventsPerProto
+ 1; i
++) {
591 cast_environment_
->Logging()->InsertFrameEvent(testing_clock_
->NowTicks(),
596 testing_clock_
->Advance(base::TimeDelta::FromMilliseconds(30));
601 ASSERT_EQ(2u, frame_events_
.size());
602 FrameEventList::iterator frame_it
= frame_events_
.begin();
603 ASSERT_TRUE(frame_it
!= frame_events_
.end());
605 linked_ptr
<AggregatedFrameEvent
> frame_event
= *frame_it
;
607 EXPECT_EQ(kMaxEventsPerProto
, frame_event
->event_type_size());
609 for (int i
= 0; i
< kMaxPacketsPerFrame
+ 1; i
++) {
610 cast_environment_
->Logging()->InsertPacketEvent(
611 testing_clock_
->NowTicks(),
612 PACKET_SENT_TO_NETWORK
,
619 testing_clock_
->Advance(base::TimeDelta::FromMilliseconds(30));
624 EXPECT_EQ(2u, packet_events_
.size());
626 PacketEventList::iterator packet_it
= packet_events_
.begin();
627 ASSERT_TRUE(packet_it
!= packet_events_
.end());
629 linked_ptr
<AggregatedPacketEvent
> packet_event
= *packet_it
;
631 EXPECT_EQ(kMaxPacketsPerFrame
,
632 packet_event
->base_packet_event_size());
635 packet_event
= *packet_it
;
636 EXPECT_EQ(1, packet_event
->base_packet_event_size());
638 for (int j
= 0; j
< kMaxEventsPerProto
+ 1; j
++) {
639 cast_environment_
->Logging()->InsertPacketEvent(
640 testing_clock_
->NowTicks(),
641 PACKET_SENT_TO_NETWORK
,
648 testing_clock_
->Advance(base::TimeDelta::FromMilliseconds(30));
653 EXPECT_EQ(2u, packet_events_
.size());
654 packet_it
= packet_events_
.begin();
655 ASSERT_TRUE(packet_it
!= packet_events_
.end());
657 packet_event
= *packet_it
;
659 EXPECT_EQ(kMaxEventsPerProto
,
660 packet_event
->base_packet_event(0).event_type_size());
663 packet_event
= *packet_it
;
664 EXPECT_EQ(1, packet_event
->base_packet_event(0).event_type_size());