summaryrefslogtreecommitdiffstats
path: root/media/cast/logging/encoding_event_subscriber.cc
blob: 5a7333a70893b3d3f6c0caaadfadc207fac1de0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/cast/logging/encoding_event_subscriber.h"

#include <utility>

#include "base/logging.h"
#include "media/cast/logging/proto/proto_utils.h"

using google::protobuf::RepeatedPtrField;
using media::cast::proto::AggregatedFrameEvent;
using media::cast::proto::AggregatedGenericEvent;
using media::cast::proto::AggregatedPacketEvent;
using media::cast::proto::BasePacketEvent;

namespace media {
namespace cast {

EncodingEventSubscriber::EncodingEventSubscriber() {}

EncodingEventSubscriber::~EncodingEventSubscriber() {
  DCHECK(thread_checker_.CalledOnValidThread());
}

void EncodingEventSubscriber::OnReceiveFrameEvent(
    const FrameEvent& frame_event) {
  DCHECK(thread_checker_.CalledOnValidThread());

  FrameEventMap::iterator it = frame_event_map_.find(frame_event.rtp_timestamp);
  linked_ptr<AggregatedFrameEvent> event_proto;

  // Look up existing entry. If not found, create a new entry and add to map.
  if (it == frame_event_map_.end()) {
    event_proto.reset(new AggregatedFrameEvent);
    event_proto->set_rtp_timestamp(frame_event.rtp_timestamp);
    frame_event_map_.insert(
        std::make_pair(frame_event.rtp_timestamp, event_proto));
  } else {
    event_proto = it->second;
  }

  event_proto->add_event_type(ToProtoEventType(frame_event.type));
  event_proto->add_event_timestamp_micros(
      frame_event.timestamp.ToInternalValue());

  if (frame_event.type == kAudioFrameEncoded ||
      frame_event.type == kVideoFrameEncoded) {
    event_proto->set_encoded_frame_size(frame_event.size);
  } else if (frame_event.type == kAudioPlayoutDelay ||
             frame_event.type == kVideoRenderDelay) {
    event_proto->set_delay_millis(frame_event.delay_delta.InMilliseconds());
  }
}

void EncodingEventSubscriber::OnReceivePacketEvent(
    const PacketEvent& packet_event) {
  DCHECK(thread_checker_.CalledOnValidThread());

  PacketEventMap::iterator it =
      packet_event_map_.find(packet_event.rtp_timestamp);
  linked_ptr<AggregatedPacketEvent> event_proto;
  BasePacketEvent* base_packet_event_proto = NULL;

  // Look up existing entry. If not found, create a new entry and add to map.
  if (it == packet_event_map_.end()) {
    event_proto.reset(new AggregatedPacketEvent);
    event_proto->set_rtp_timestamp(packet_event.rtp_timestamp);
    packet_event_map_.insert(
        std::make_pair(packet_event.rtp_timestamp, event_proto));
    base_packet_event_proto = event_proto->add_base_packet_event();
    base_packet_event_proto->set_packet_id(packet_event.packet_id);
  } else {
    // Found existing entry, now look up existing BasePacketEvent using packet
    // ID. If not found, create a new entry and add to proto.
    event_proto = it->second;
    RepeatedPtrField<BasePacketEvent>* field =
        event_proto->mutable_base_packet_event();
    for (RepeatedPtrField<BasePacketEvent>::pointer_iterator it =
             field->pointer_begin();
         it != field->pointer_end(); ++it) {
      if ((*it)->packet_id() == packet_event.packet_id) {
        base_packet_event_proto = *it;
        break;
      }
    }
    if (!base_packet_event_proto) {
      base_packet_event_proto = event_proto->add_base_packet_event();
      base_packet_event_proto->set_packet_id(packet_event.packet_id);
    }
  }

  base_packet_event_proto->add_event_type(ToProtoEventType(packet_event.type));
  base_packet_event_proto->add_event_timestamp_micros(
      packet_event.timestamp.ToInternalValue());
}

void EncodingEventSubscriber::OnReceiveGenericEvent(
    const GenericEvent& generic_event) {
  DCHECK(thread_checker_.CalledOnValidThread());

  GenericEventMap::iterator it = generic_event_map_.find(generic_event.type);
  linked_ptr<AggregatedGenericEvent> event_proto;
  if (it == generic_event_map_.end()) {
    event_proto.reset(new AggregatedGenericEvent);
    event_proto->set_event_type(ToProtoEventType(generic_event.type));
    generic_event_map_.insert(std::make_pair(generic_event.type, event_proto));
  } else {
    event_proto = it->second;
  }

  event_proto->add_event_timestamp_micros(
      generic_event.timestamp.ToInternalValue());
  event_proto->add_value(generic_event.value);
}

void EncodingEventSubscriber::GetFrameEventsAndReset(
    FrameEventMap* frame_event_map) {
  DCHECK(thread_checker_.CalledOnValidThread());
  frame_event_map->swap(frame_event_map_);
  frame_event_map_.clear();
}

void EncodingEventSubscriber::GetPacketEventsAndReset(
    PacketEventMap* packet_event_map) {
  DCHECK(thread_checker_.CalledOnValidThread());
  packet_event_map->swap(packet_event_map_);
  packet_event_map_.clear();
}

void EncodingEventSubscriber::GetGenericEventsAndReset(
    GenericEventMap* generic_event_map) {
  DCHECK(thread_checker_.CalledOnValidThread());
  generic_event_map->swap(generic_event_map_);
  generic_event_map_.clear();
}

}  // namespace cast
}  // namespace media