Home | History | Annotate | Download | only in logging
      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.
      4 
      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"
     14 
     15 using media::cast::proto::AggregatedFrameEvent;
     16 using media::cast::proto::AggregatedPacketEvent;
     17 using media::cast::proto::BasePacketEvent;
     18 using media::cast::proto::LogMetadata;
     19 
     20 namespace {
     21 
     22 int64 InMilliseconds(base::TimeTicks event_time) {
     23   return (event_time - base::TimeTicks()).InMilliseconds();
     24 }
     25 
     26 }
     27 
     28 namespace media {
     29 namespace cast {
     30 
     31 class EncodingEventSubscriberTest : public ::testing::Test {
     32  protected:
     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(),
     38             task_runner_,
     39             task_runner_,
     40             task_runner_)),
     41         first_rtp_timestamp_(0) {}
     42 
     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());
     48   }
     49 
     50   virtual ~EncodingEventSubscriberTest() {
     51     if (event_subscriber_) {
     52       cast_environment_->Logging()->RemoveRawEventSubscriber(
     53           event_subscriber_.get());
     54     }
     55   }
     56 
     57   void GetEventsAndReset() {
     58     event_subscriber_->GetEventsAndReset(
     59         &metadata_, &frame_events_, &packet_events_);
     60     first_rtp_timestamp_ = metadata_.first_rtp_timestamp();
     61   }
     62 
     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_;
     71 };
     72 
     73 TEST_F(EncodingEventSubscriberTest, FrameEventTruncating) {
     74   Init(VIDEO_EVENT);
     75 
     76   base::TimeTicks now(testing_clock_->NowTicks());
     77 
     78   // Entry with RTP timestamp 0 should get dropped.
     79   for (int i = 0; i < 11; i++) {
     80     cast_environment_->Logging()->InsertFrameEvent(now,
     81                                                    FRAME_CAPTURE_BEGIN,
     82                                                    VIDEO_EVENT,
     83                                                    i * 100,
     84                                                    /*frame_id*/ 0);
     85     cast_environment_->Logging()->InsertFrameEvent(now,
     86                                                    FRAME_DECODED,
     87                                                    VIDEO_EVENT,
     88                                                    i * 100,
     89                                                    /*frame_id*/ 0);
     90   }
     91 
     92   GetEventsAndReset();
     93 
     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());
     97 }
     98 
     99 TEST_F(EncodingEventSubscriberTest, PacketEventTruncating) {
    100   Init(AUDIO_EVENT);
    101 
    102   base::TimeTicks now(testing_clock_->NowTicks());
    103 
    104   // Entry with RTP timestamp 0 should get dropped.
    105   for (int i = 0; i < 11; i++) {
    106     cast_environment_->Logging()->InsertPacketEvent(now,
    107                                                     PACKET_RECEIVED,
    108                                                     AUDIO_EVENT,
    109                                                     /*rtp_timestamp*/ i * 100,
    110                                                     /*frame_id*/ 0,
    111                                                     /*packet_id*/ i,
    112                                                     /*max_packet_id*/ 10,
    113                                                     /*size*/ 123);
    114   }
    115 
    116   GetEventsAndReset();
    117 
    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());
    121 }
    122 
    123 TEST_F(EncodingEventSubscriberTest, EventFiltering) {
    124   Init(VIDEO_EVENT);
    125 
    126   base::TimeTicks now(testing_clock_->NowTicks());
    127   RtpTimestamp rtp_timestamp = 100;
    128   cast_environment_->Logging()->InsertFrameEvent(now,
    129                                                  FRAME_DECODED,
    130                                                  VIDEO_EVENT,
    131                                                  rtp_timestamp,
    132                                                  /*frame_id*/ 0);
    133 
    134   // This is an AUDIO_EVENT and shouldn't be processed by the subscriber.
    135   cast_environment_->Logging()->InsertFrameEvent(now,
    136                                                  FRAME_DECODED,
    137                                                  AUDIO_EVENT,
    138                                                  rtp_timestamp,
    139                                                  /*frame_id*/ 0);
    140 
    141   GetEventsAndReset();
    142 
    143   ASSERT_EQ(1u, frame_events_.size());
    144   FrameEventList::iterator it = frame_events_.begin();
    145 
    146   linked_ptr<AggregatedFrameEvent> frame_event = *it;
    147 
    148   ASSERT_EQ(1, frame_event->event_type_size());
    149   EXPECT_EQ(media::cast::proto::FRAME_DECODED,
    150             frame_event->event_type(0));
    151 
    152   GetEventsAndReset();
    153 
    154   EXPECT_TRUE(packet_events_.empty());
    155 }
    156 
    157 TEST_F(EncodingEventSubscriberTest, FrameEvent) {
    158   Init(VIDEO_EVENT);
    159   base::TimeTicks now(testing_clock_->NowTicks());
    160   RtpTimestamp rtp_timestamp = 100;
    161   cast_environment_->Logging()->InsertFrameEvent(now, FRAME_DECODED,
    162                                                  VIDEO_EVENT,
    163                                                  rtp_timestamp,
    164                                                  /*frame_id*/ 0);
    165 
    166   GetEventsAndReset();
    167 
    168   ASSERT_EQ(1u, frame_events_.size());
    169 
    170   RtpTimestamp relative_rtp_timestamp = rtp_timestamp - first_rtp_timestamp_;
    171   FrameEventList::iterator it = frame_events_.begin();
    172 
    173   linked_ptr<AggregatedFrameEvent> event = *it;
    174 
    175   EXPECT_EQ(relative_rtp_timestamp, event->relative_rtp_timestamp());
    176 
    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));
    181 
    182   EXPECT_EQ(0, event->encoded_frame_size());
    183   EXPECT_EQ(0, event->delay_millis());
    184 
    185   GetEventsAndReset();
    186   EXPECT_TRUE(frame_events_.empty());
    187 }
    188 
    189 TEST_F(EncodingEventSubscriberTest, FrameEventDelay) {
    190   Init(AUDIO_EVENT);
    191   base::TimeTicks now(testing_clock_->NowTicks());
    192   RtpTimestamp rtp_timestamp = 100;
    193   int delay_ms = 100;
    194   cast_environment_->Logging()->InsertFrameEventWithDelay(
    195       now, FRAME_PLAYOUT, AUDIO_EVENT, rtp_timestamp,
    196       /*frame_id*/ 0, base::TimeDelta::FromMilliseconds(delay_ms));
    197 
    198   GetEventsAndReset();
    199 
    200   ASSERT_EQ(1u, frame_events_.size());
    201 
    202   RtpTimestamp relative_rtp_timestamp = rtp_timestamp - first_rtp_timestamp_;
    203   FrameEventList::iterator it = frame_events_.begin();
    204 
    205   linked_ptr<AggregatedFrameEvent> event = *it;
    206 
    207   EXPECT_EQ(relative_rtp_timestamp, event->relative_rtp_timestamp());
    208 
    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));
    213 
    214   EXPECT_EQ(0, event->encoded_frame_size());
    215   EXPECT_EQ(100, event->delay_millis());
    216   EXPECT_FALSE(event->has_key_frame());
    217 }
    218 
    219 TEST_F(EncodingEventSubscriberTest, FrameEventSize) {
    220   Init(VIDEO_EVENT);
    221   base::TimeTicks now(testing_clock_->NowTicks());
    222   RtpTimestamp rtp_timestamp = 100;
    223   int size = 123;
    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);
    229 
    230   GetEventsAndReset();
    231 
    232   ASSERT_EQ(1u, frame_events_.size());
    233 
    234   RtpTimestamp relative_rtp_timestamp = rtp_timestamp - first_rtp_timestamp_;
    235   FrameEventList::iterator it = frame_events_.begin();
    236 
    237   linked_ptr<AggregatedFrameEvent> event = *it;
    238 
    239   EXPECT_EQ(relative_rtp_timestamp, event->relative_rtp_timestamp());
    240 
    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));
    245 
    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());
    251 }
    252 
    253 TEST_F(EncodingEventSubscriberTest, MultipleFrameEvents) {
    254   Init(AUDIO_EVENT);
    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));
    261 
    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);
    268 
    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);
    273 
    274   GetEventsAndReset();
    275 
    276   ASSERT_EQ(2u, frame_events_.size());
    277 
    278   RtpTimestamp relative_rtp_timestamp = rtp_timestamp1 - first_rtp_timestamp_;
    279   FrameEventList::iterator it = frame_events_.begin();
    280 
    281   linked_ptr<AggregatedFrameEvent> event = *it;
    282 
    283   EXPECT_EQ(relative_rtp_timestamp, event->relative_rtp_timestamp());
    284 
    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));
    288 
    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));
    292 
    293   EXPECT_FALSE(event->has_key_frame());
    294 
    295   relative_rtp_timestamp = rtp_timestamp2 - first_rtp_timestamp_;
    296   ++it;
    297 
    298   event = *it;
    299 
    300   EXPECT_EQ(relative_rtp_timestamp, event->relative_rtp_timestamp());
    301 
    302   ASSERT_EQ(1, event->event_type_size());
    303   EXPECT_EQ(media::cast::proto::FRAME_ENCODED, event->event_type(0));
    304 
    305   ASSERT_EQ(1, event->event_timestamp_ms_size());
    306   EXPECT_EQ(InMilliseconds(now2), event->event_timestamp_ms(0));
    307 
    308   EXPECT_FALSE(event->has_key_frame());
    309 }
    310 
    311 TEST_F(EncodingEventSubscriberTest, PacketEvent) {
    312   Init(AUDIO_EVENT);
    313   base::TimeTicks now(testing_clock_->NowTicks());
    314   RtpTimestamp rtp_timestamp = 100;
    315   int packet_id = 2;
    316   int size = 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);
    321 
    322   GetEventsAndReset();
    323 
    324   ASSERT_EQ(1u, packet_events_.size());
    325 
    326   RtpTimestamp relative_rtp_timestamp = rtp_timestamp - first_rtp_timestamp_;
    327   PacketEventList::iterator it = packet_events_.begin();
    328 
    329   linked_ptr<AggregatedPacketEvent> event = *it;
    330 
    331   EXPECT_EQ(relative_rtp_timestamp, event->relative_rtp_timestamp());
    332 
    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());
    342 
    343   GetEventsAndReset();
    344   EXPECT_TRUE(packet_events_.empty());
    345 }
    346 
    347 TEST_F(EncodingEventSubscriberTest, MultiplePacketEventsForPacket) {
    348   Init(VIDEO_EVENT);
    349   base::TimeTicks now1(testing_clock_->NowTicks());
    350   RtpTimestamp rtp_timestamp = 100;
    351   int packet_id = 2;
    352   int size = 100;
    353   cast_environment_->Logging()->InsertPacketEvent(now1,
    354                                                   PACKET_SENT_TO_NETWORK,
    355                                                   VIDEO_EVENT,
    356                                                   rtp_timestamp,
    357                                                   /*frame_id*/ 0,
    358                                                   packet_id,
    359                                                   /*max_packet_id*/ 10,
    360                                                   size);
    361 
    362   testing_clock_->Advance(base::TimeDelta::FromMilliseconds(20));
    363   base::TimeTicks now2(testing_clock_->NowTicks());
    364   cast_environment_->Logging()->InsertPacketEvent(now2,
    365                                                   PACKET_RETRANSMITTED,
    366                                                   VIDEO_EVENT,
    367                                                   rtp_timestamp,
    368                                                   /*frame_id*/ 0,
    369                                                   packet_id,
    370                                                   /*max_packet_id*/ 10,
    371                                                   size);
    372 
    373   GetEventsAndReset();
    374 
    375   ASSERT_EQ(1u, packet_events_.size());
    376 
    377   RtpTimestamp relative_rtp_timestamp = rtp_timestamp - first_rtp_timestamp_;
    378   PacketEventList::iterator it = packet_events_.begin();
    379 
    380   linked_ptr<AggregatedPacketEvent> event = *it;
    381 
    382   EXPECT_EQ(relative_rtp_timestamp, event->relative_rtp_timestamp());
    383 
    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));
    395 }
    396 
    397 TEST_F(EncodingEventSubscriberTest, MultiplePacketEventsForFrame) {
    398   Init(VIDEO_EVENT);
    399   base::TimeTicks now1(testing_clock_->NowTicks());
    400   RtpTimestamp rtp_timestamp = 100;
    401   int packet_id_1 = 2;
    402   int packet_id_2 = 3;
    403   int size = 100;
    404   cast_environment_->Logging()->InsertPacketEvent(now1,
    405                                                   PACKET_SENT_TO_NETWORK,
    406                                                   VIDEO_EVENT,
    407                                                   rtp_timestamp,
    408                                                   /*frame_id*/ 0,
    409                                                   packet_id_1,
    410                                                   /*max_packet_id*/ 10,
    411                                                   size);
    412 
    413   testing_clock_->Advance(base::TimeDelta::FromMilliseconds(20));
    414   base::TimeTicks now2(testing_clock_->NowTicks());
    415   cast_environment_->Logging()->InsertPacketEvent(now2,
    416                                                   PACKET_RETRANSMITTED,
    417                                                   VIDEO_EVENT,
    418                                                   rtp_timestamp,
    419                                                   /*frame_id*/ 0,
    420                                                   packet_id_2,
    421                                                   /*max_packet_id*/ 10,
    422                                                   size);
    423 
    424   GetEventsAndReset();
    425 
    426   ASSERT_EQ(1u, packet_events_.size());
    427 
    428   RtpTimestamp relative_rtp_timestamp = rtp_timestamp - first_rtp_timestamp_;
    429   PacketEventList::iterator it = packet_events_.begin();
    430 
    431   linked_ptr<AggregatedPacketEvent> event = *it;
    432 
    433   EXPECT_EQ(relative_rtp_timestamp, event->relative_rtp_timestamp());
    434 
    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));
    443 
    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));
    451 }
    452 
    453 TEST_F(EncodingEventSubscriberTest, MultiplePacketEvents) {
    454   Init(VIDEO_EVENT);
    455   base::TimeTicks now1(testing_clock_->NowTicks());
    456   RtpTimestamp rtp_timestamp_1 = 100;
    457   RtpTimestamp rtp_timestamp_2 = 200;
    458   int packet_id_1 = 2;
    459   int packet_id_2 = 3;
    460   int size = 100;
    461   cast_environment_->Logging()->InsertPacketEvent(now1,
    462                                                   PACKET_SENT_TO_NETWORK,
    463                                                   VIDEO_EVENT,
    464                                                   rtp_timestamp_1,
    465                                                   /*frame_id*/ 0,
    466                                                   packet_id_1,
    467                                                   /*max_packet_id*/ 10,
    468                                                   size);
    469 
    470   testing_clock_->Advance(base::TimeDelta::FromMilliseconds(20));
    471   base::TimeTicks now2(testing_clock_->NowTicks());
    472   cast_environment_->Logging()->InsertPacketEvent(now2,
    473                                                   PACKET_RETRANSMITTED,
    474                                                   VIDEO_EVENT,
    475                                                   rtp_timestamp_2,
    476                                                   /*frame_id*/ 0,
    477                                                   packet_id_2,
    478                                                   /*max_packet_id*/ 10,
    479                                                   size);
    480 
    481   GetEventsAndReset();
    482 
    483   ASSERT_EQ(2u, packet_events_.size());
    484 
    485   RtpTimestamp relative_rtp_timestamp = rtp_timestamp_1 - first_rtp_timestamp_;
    486   PacketEventList::iterator it = packet_events_.begin();
    487 
    488   linked_ptr<AggregatedPacketEvent> event = *it;
    489 
    490   EXPECT_EQ(relative_rtp_timestamp, event->relative_rtp_timestamp());
    491 
    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));
    500 
    501   relative_rtp_timestamp = rtp_timestamp_2 - first_rtp_timestamp_;
    502   ++it;
    503   ASSERT_TRUE(it != packet_events_.end());
    504 
    505   event = *it;
    506   EXPECT_EQ(relative_rtp_timestamp, event->relative_rtp_timestamp());
    507 
    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));
    516 }
    517 
    518 TEST_F(EncodingEventSubscriberTest, FirstRtpTimestamp) {
    519   Init(VIDEO_EVENT);
    520   RtpTimestamp rtp_timestamp = 12345;
    521   base::TimeTicks now(testing_clock_->NowTicks());
    522 
    523   cast_environment_->Logging()->InsertFrameEvent(now,
    524                                                  FRAME_CAPTURE_BEGIN,
    525                                                  VIDEO_EVENT,
    526                                                  rtp_timestamp,
    527                                                  /*frame_id*/ 0);
    528 
    529   cast_environment_->Logging()->InsertFrameEvent(now,
    530                                                  FRAME_CAPTURE_END,
    531                                                  VIDEO_EVENT,
    532                                                  rtp_timestamp + 30,
    533                                                  /*frame_id*/ 1);
    534 
    535   GetEventsAndReset();
    536 
    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());
    541 
    542   ++it;
    543   ASSERT_NE(frame_events_.end(), it);
    544   EXPECT_EQ(30u, (*it)->relative_rtp_timestamp());
    545 
    546   rtp_timestamp = 67890;
    547 
    548   cast_environment_->Logging()->InsertFrameEvent(now,
    549                                                  FRAME_CAPTURE_BEGIN,
    550                                                  VIDEO_EVENT,
    551                                                  rtp_timestamp,
    552                                                  /*frame_id*/ 0);
    553   GetEventsAndReset();
    554 
    555   EXPECT_EQ(rtp_timestamp, first_rtp_timestamp_);
    556 }
    557 
    558 TEST_F(EncodingEventSubscriberTest, RelativeRtpTimestampWrapAround) {
    559   Init(VIDEO_EVENT);
    560   RtpTimestamp rtp_timestamp = 0xffffffff - 20;
    561   base::TimeTicks now(testing_clock_->NowTicks());
    562 
    563   cast_environment_->Logging()->InsertFrameEvent(now,
    564                                                  FRAME_CAPTURE_BEGIN,
    565                                                  VIDEO_EVENT,
    566                                                  rtp_timestamp,
    567                                                  /*frame_id*/ 0);
    568 
    569   // RtpTimestamp has now wrapped around.
    570   cast_environment_->Logging()->InsertFrameEvent(now,
    571                                                  FRAME_CAPTURE_END,
    572                                                  VIDEO_EVENT,
    573                                                  rtp_timestamp + 30,
    574                                                  /*frame_id*/ 1);
    575 
    576   GetEventsAndReset();
    577 
    578   FrameEventList::iterator it = frame_events_.begin();
    579   ASSERT_NE(frame_events_.end(), it);
    580   EXPECT_EQ(0u, (*it)->relative_rtp_timestamp());
    581 
    582   ++it;
    583   ASSERT_NE(frame_events_.end(), it);
    584   EXPECT_EQ(30u, (*it)->relative_rtp_timestamp());
    585 }
    586 
    587 TEST_F(EncodingEventSubscriberTest, MaxEventsPerProto) {
    588   Init(VIDEO_EVENT);
    589   RtpTimestamp rtp_timestamp = 100;
    590   for (int i = 0; i < kMaxEventsPerProto + 1; i++) {
    591     cast_environment_->Logging()->InsertFrameEvent(testing_clock_->NowTicks(),
    592                                                    FRAME_ACK_RECEIVED,
    593                                                    VIDEO_EVENT,
    594                                                    rtp_timestamp,
    595                                                    /*frame_id*/ 0);
    596     testing_clock_->Advance(base::TimeDelta::FromMilliseconds(30));
    597   }
    598 
    599   GetEventsAndReset();
    600 
    601   ASSERT_EQ(2u, frame_events_.size());
    602   FrameEventList::iterator frame_it = frame_events_.begin();
    603   ASSERT_TRUE(frame_it != frame_events_.end());
    604 
    605   linked_ptr<AggregatedFrameEvent> frame_event = *frame_it;
    606 
    607   EXPECT_EQ(kMaxEventsPerProto, frame_event->event_type_size());
    608 
    609   for (int i = 0; i < kMaxPacketsPerFrame + 1; i++) {
    610     cast_environment_->Logging()->InsertPacketEvent(
    611         testing_clock_->NowTicks(),
    612         PACKET_SENT_TO_NETWORK,
    613         VIDEO_EVENT,
    614         rtp_timestamp,
    615         /*frame_id*/ 0,
    616         i,
    617         kMaxPacketsPerFrame,
    618         123);
    619     testing_clock_->Advance(base::TimeDelta::FromMilliseconds(30));
    620   }
    621 
    622   GetEventsAndReset();
    623 
    624   EXPECT_EQ(2u, packet_events_.size());
    625 
    626   PacketEventList::iterator packet_it = packet_events_.begin();
    627   ASSERT_TRUE(packet_it != packet_events_.end());
    628 
    629   linked_ptr<AggregatedPacketEvent> packet_event = *packet_it;
    630 
    631   EXPECT_EQ(kMaxPacketsPerFrame,
    632       packet_event->base_packet_event_size());
    633 
    634   ++packet_it;
    635   packet_event = *packet_it;
    636   EXPECT_EQ(1, packet_event->base_packet_event_size());
    637 
    638   for (int j = 0; j < kMaxEventsPerProto + 1; j++) {
    639     cast_environment_->Logging()->InsertPacketEvent(
    640         testing_clock_->NowTicks(),
    641         PACKET_SENT_TO_NETWORK,
    642         VIDEO_EVENT,
    643         rtp_timestamp,
    644         /*frame_id*/ 0,
    645         0,
    646         0,
    647         123);
    648     testing_clock_->Advance(base::TimeDelta::FromMilliseconds(30));
    649   }
    650 
    651   GetEventsAndReset();
    652 
    653   EXPECT_EQ(2u, packet_events_.size());
    654   packet_it = packet_events_.begin();
    655   ASSERT_TRUE(packet_it != packet_events_.end());
    656 
    657   packet_event = *packet_it;
    658 
    659   EXPECT_EQ(kMaxEventsPerProto,
    660       packet_event->base_packet_event(0).event_type_size());
    661 
    662   ++packet_it;
    663   packet_event = *packet_it;
    664   EXPECT_EQ(1, packet_event->base_packet_event(0).event_type_size());
    665 }
    666 
    667 }  // namespace cast
    668 }  // namespace media
    669