Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / media / cast / logging / encoding_event_subscriber.cc
blob49404654c9252c694918250958919242de182844
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 "media/cast/logging/encoding_event_subscriber.h"
7 #include <cstring>
8 #include <utility>
10 #include "base/logging.h"
11 #include "media/cast/logging/proto/proto_utils.h"
13 using google::protobuf::RepeatedPtrField;
14 using media::cast::proto::AggregatedFrameEvent;
15 using media::cast::proto::AggregatedPacketEvent;
16 using media::cast::proto::BasePacketEvent;
17 using media::cast::proto::LogMetadata;
19 namespace {
21 // A size limit on maps to keep lookups fast.
22 const size_t kMaxMapSize = 200;
24 // The smallest (oredered by RTP timestamp) |kNumMapEntriesToTransfer| entries
25 // will be moved when the map size reaches |kMaxMapSize|.
26 // Must be smaller than |kMaxMapSize|.
27 const size_t kNumMapEntriesToTransfer = 100;
29 template <typename ProtoPtr>
30 bool IsRtpTimestampLessThan(const ProtoPtr& lhs, const ProtoPtr& rhs) {
31 return lhs->relative_rtp_timestamp() < rhs->relative_rtp_timestamp();
34 BasePacketEvent* GetNewBasePacketEvent(AggregatedPacketEvent* event_proto,
35 int packet_id, int size) {
36 BasePacketEvent* base = event_proto->add_base_packet_event();
37 base->set_packet_id(packet_id);
38 base->set_size(size);
39 return base;
44 namespace media {
45 namespace cast {
47 EncodingEventSubscriber::EncodingEventSubscriber(
48 EventMediaType event_media_type,
49 size_t max_frames)
50 : event_media_type_(event_media_type),
51 max_frames_(max_frames),
52 frame_event_storage_index_(0),
53 packet_event_storage_index_(0),
54 seen_first_rtp_timestamp_(false),
55 first_rtp_timestamp_(0u) {}
57 EncodingEventSubscriber::~EncodingEventSubscriber() {
58 DCHECK(thread_checker_.CalledOnValidThread());
61 void EncodingEventSubscriber::OnReceiveFrameEvent(
62 const FrameEvent& frame_event) {
63 DCHECK(thread_checker_.CalledOnValidThread());
65 if (event_media_type_ != frame_event.media_type)
66 return;
68 RtpTimestamp relative_rtp_timestamp =
69 GetRelativeRtpTimestamp(frame_event.rtp_timestamp);
70 FrameEventMap::iterator it = frame_event_map_.find(relative_rtp_timestamp);
71 linked_ptr<AggregatedFrameEvent> event_proto;
73 // Look up existing entry. If not found, create a new entry and add to map.
74 if (it == frame_event_map_.end()) {
75 event_proto.reset(new AggregatedFrameEvent);
76 event_proto->set_relative_rtp_timestamp(relative_rtp_timestamp);
77 frame_event_map_.insert(
78 std::make_pair(relative_rtp_timestamp, event_proto));
79 } else {
80 event_proto = it->second;
81 if (event_proto->event_type_size() >= kMaxEventsPerProto) {
82 DVLOG(2) << "Too many events in frame " << frame_event.rtp_timestamp
83 << ". Using new frame event proto.";
84 AddFrameEventToStorage(event_proto);
85 event_proto.reset(new AggregatedFrameEvent);
86 event_proto->set_relative_rtp_timestamp(relative_rtp_timestamp);
87 it->second = event_proto;
91 event_proto->add_event_type(ToProtoEventType(frame_event.type));
92 event_proto->add_event_timestamp_ms(
93 (frame_event.timestamp - base::TimeTicks()).InMilliseconds());
95 if (frame_event.type == FRAME_ENCODED) {
96 event_proto->set_encoded_frame_size(frame_event.size);
97 if (frame_event.media_type == VIDEO_EVENT) {
98 event_proto->set_encoded_frame_size(frame_event.size);
99 event_proto->set_key_frame(frame_event.key_frame);
100 event_proto->set_target_bitrate(frame_event.target_bitrate);
102 } else if (frame_event.type == FRAME_PLAYOUT) {
103 event_proto->set_delay_millis(frame_event.delay_delta.InMilliseconds());
106 if (frame_event_map_.size() > kMaxMapSize)
107 TransferFrameEvents(kNumMapEntriesToTransfer);
109 DCHECK(frame_event_map_.size() <= kMaxMapSize);
110 DCHECK(frame_event_storage_.size() <= max_frames_);
113 void EncodingEventSubscriber::OnReceivePacketEvent(
114 const PacketEvent& packet_event) {
115 DCHECK(thread_checker_.CalledOnValidThread());
117 if (event_media_type_ != packet_event.media_type)
118 return;
119 RtpTimestamp relative_rtp_timestamp =
120 GetRelativeRtpTimestamp(packet_event.rtp_timestamp);
121 PacketEventMap::iterator it =
122 packet_event_map_.find(relative_rtp_timestamp);
123 linked_ptr<AggregatedPacketEvent> event_proto;
124 BasePacketEvent* base_packet_event_proto = NULL;
126 // Look up existing entry. If not found, create a new entry and add to map.
127 if (it == packet_event_map_.end()) {
128 event_proto.reset(new AggregatedPacketEvent);
129 event_proto->set_relative_rtp_timestamp(relative_rtp_timestamp);
130 packet_event_map_.insert(
131 std::make_pair(relative_rtp_timestamp, event_proto));
132 base_packet_event_proto = GetNewBasePacketEvent(
133 event_proto.get(), packet_event.packet_id, packet_event.size);
134 } else {
135 // Found existing entry, now look up existing BasePacketEvent using packet
136 // ID. If not found, create a new entry and add to proto.
137 event_proto = it->second;
138 RepeatedPtrField<BasePacketEvent>* field =
139 event_proto->mutable_base_packet_event();
140 for (RepeatedPtrField<BasePacketEvent>::pointer_iterator base_it =
141 field->pointer_begin();
142 base_it != field->pointer_end();
143 ++base_it) {
144 if ((*base_it)->packet_id() == packet_event.packet_id) {
145 base_packet_event_proto = *base_it;
146 break;
149 if (!base_packet_event_proto) {
150 if (event_proto->base_packet_event_size() >= kMaxPacketsPerFrame) {
151 DVLOG(3) << "Too many packets in AggregatedPacketEvent "
152 << packet_event.rtp_timestamp << ". "
153 << "Using new packet event proto.";
154 AddPacketEventToStorage(event_proto);
155 event_proto.reset(new AggregatedPacketEvent);
156 event_proto->set_relative_rtp_timestamp(relative_rtp_timestamp);
157 it->second = event_proto;
160 base_packet_event_proto = GetNewBasePacketEvent(
161 event_proto.get(), packet_event.packet_id, packet_event.size);
162 } else if (base_packet_event_proto->event_type_size() >=
163 kMaxEventsPerProto) {
164 DVLOG(3) << "Too many events in packet "
165 << packet_event.rtp_timestamp << ", "
166 << packet_event.packet_id << ". Using new packet event proto.";
167 AddPacketEventToStorage(event_proto);
168 event_proto.reset(new AggregatedPacketEvent);
169 event_proto->set_relative_rtp_timestamp(relative_rtp_timestamp);
170 it->second = event_proto;
171 base_packet_event_proto = GetNewBasePacketEvent(
172 event_proto.get(), packet_event.packet_id, packet_event.size);
176 base_packet_event_proto->add_event_type(
177 ToProtoEventType(packet_event.type));
178 base_packet_event_proto->add_event_timestamp_ms(
179 (packet_event.timestamp - base::TimeTicks()).InMilliseconds());
181 // |base_packet_event_proto| could have been created with a receiver event
182 // which does not have the packet size and we would need to overwrite it when
183 // we see a sender event, which does have the packet size.
184 if (packet_event.size > 0) {
185 base_packet_event_proto->set_size(packet_event.size);
188 if (packet_event_map_.size() > kMaxMapSize)
189 TransferPacketEvents(kNumMapEntriesToTransfer);
191 DCHECK(packet_event_map_.size() <= kMaxMapSize);
192 DCHECK(packet_event_storage_.size() <= max_frames_);
195 void EncodingEventSubscriber::GetEventsAndReset(LogMetadata* metadata,
196 FrameEventList* frame_events, PacketEventList* packet_events) {
197 DCHECK(thread_checker_.CalledOnValidThread());
199 // Flush all events.
200 TransferFrameEvents(frame_event_map_.size());
201 TransferPacketEvents(packet_event_map_.size());
202 std::sort(frame_event_storage_.begin(), frame_event_storage_.end(),
203 &IsRtpTimestampLessThan<linked_ptr<AggregatedFrameEvent> >);
204 std::sort(packet_event_storage_.begin(), packet_event_storage_.end(),
205 &IsRtpTimestampLessThan<linked_ptr<AggregatedPacketEvent> >);
207 metadata->set_is_audio(event_media_type_ == AUDIO_EVENT);
208 metadata->set_first_rtp_timestamp(first_rtp_timestamp_);
209 metadata->set_num_frame_events(frame_event_storage_.size());
210 metadata->set_num_packet_events(packet_event_storage_.size());
211 metadata->set_reference_timestamp_ms_at_unix_epoch(
212 (base::TimeTicks::UnixEpoch() - base::TimeTicks()).InMilliseconds());
213 frame_events->swap(frame_event_storage_);
214 packet_events->swap(packet_event_storage_);
215 Reset();
218 void EncodingEventSubscriber::TransferFrameEvents(size_t max_num_entries) {
219 DCHECK(frame_event_map_.size() >= max_num_entries);
221 FrameEventMap::iterator it = frame_event_map_.begin();
222 for (size_t i = 0;
223 i < max_num_entries && it != frame_event_map_.end();
224 i++, ++it) {
225 AddFrameEventToStorage(it->second);
228 frame_event_map_.erase(frame_event_map_.begin(), it);
231 void EncodingEventSubscriber::TransferPacketEvents(size_t max_num_entries) {
232 PacketEventMap::iterator it = packet_event_map_.begin();
233 for (size_t i = 0;
234 i < max_num_entries && it != packet_event_map_.end();
235 i++, ++it) {
236 AddPacketEventToStorage(it->second);
239 packet_event_map_.erase(packet_event_map_.begin(), it);
242 void EncodingEventSubscriber::AddFrameEventToStorage(
243 const linked_ptr<AggregatedFrameEvent>& frame_event_proto) {
244 if (frame_event_storage_.size() >= max_frames_) {
245 frame_event_storage_[frame_event_storage_index_] = frame_event_proto;
246 } else {
247 frame_event_storage_.push_back(frame_event_proto);
250 frame_event_storage_index_ = (frame_event_storage_index_ + 1) % max_frames_;
253 void EncodingEventSubscriber::AddPacketEventToStorage(
254 const linked_ptr<AggregatedPacketEvent>& packet_event_proto) {
255 if (packet_event_storage_.size() >= max_frames_)
256 packet_event_storage_[packet_event_storage_index_] = packet_event_proto;
257 else
258 packet_event_storage_.push_back(packet_event_proto);
260 packet_event_storage_index_ = (packet_event_storage_index_ + 1) % max_frames_;
263 RtpTimestamp EncodingEventSubscriber::GetRelativeRtpTimestamp(
264 RtpTimestamp rtp_timestamp) {
265 if (!seen_first_rtp_timestamp_) {
266 seen_first_rtp_timestamp_ = true;
267 first_rtp_timestamp_ = rtp_timestamp;
270 return rtp_timestamp - first_rtp_timestamp_;
273 void EncodingEventSubscriber::Reset() {
274 frame_event_map_.clear();
275 frame_event_storage_.clear();
276 frame_event_storage_index_ = 0;
277 packet_event_map_.clear();
278 packet_event_storage_.clear();
279 packet_event_storage_index_ = 0;
280 seen_first_rtp_timestamp_ = false;
281 first_rtp_timestamp_ = 0u;
284 } // namespace cast
285 } // namespace media