Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / media / cast / logging / encoding_event_subscriber.cc
blob2ccf731ba0aedd13ee81e37370aa0f85a6133254
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 (!ShouldProcessEvent(frame_event.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 == kAudioFrameEncoded) {
96 event_proto->set_encoded_frame_size(frame_event.size);
97 } else if (frame_event.type == kVideoFrameEncoded) {
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);
101 } else if (frame_event.type == kAudioPlayoutDelay ||
102 frame_event.type == kVideoRenderDelay) {
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 (!ShouldProcessEvent(packet_event.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 if (packet_event_map_.size() > kMaxMapSize)
182 TransferPacketEvents(kNumMapEntriesToTransfer);
184 DCHECK(packet_event_map_.size() <= kMaxMapSize);
185 DCHECK(packet_event_storage_.size() <= max_frames_);
188 void EncodingEventSubscriber::GetEventsAndReset(LogMetadata* metadata,
189 FrameEventList* frame_events, PacketEventList* packet_events) {
190 DCHECK(thread_checker_.CalledOnValidThread());
192 // Flush all events.
193 TransferFrameEvents(frame_event_map_.size());
194 TransferPacketEvents(packet_event_map_.size());
195 std::sort(frame_event_storage_.begin(), frame_event_storage_.end(),
196 &IsRtpTimestampLessThan<linked_ptr<AggregatedFrameEvent> >);
197 std::sort(packet_event_storage_.begin(), packet_event_storage_.end(),
198 &IsRtpTimestampLessThan<linked_ptr<AggregatedPacketEvent> >);
200 metadata->set_is_audio(event_media_type_ == AUDIO_EVENT);
201 metadata->set_first_rtp_timestamp(first_rtp_timestamp_);
202 metadata->set_num_frame_events(frame_event_storage_.size());
203 metadata->set_num_packet_events(packet_event_storage_.size());
204 metadata->set_reference_timestamp_ms_at_unix_epoch(
205 (base::TimeTicks::UnixEpoch() - base::TimeTicks()).InMilliseconds());
206 frame_events->swap(frame_event_storage_);
207 packet_events->swap(packet_event_storage_);
208 Reset();
211 bool EncodingEventSubscriber::ShouldProcessEvent(CastLoggingEvent event) {
212 return GetEventMediaType(event) == event_media_type_;
215 void EncodingEventSubscriber::TransferFrameEvents(size_t max_num_entries) {
216 DCHECK(frame_event_map_.size() >= max_num_entries);
218 FrameEventMap::iterator it = frame_event_map_.begin();
219 for (size_t i = 0;
220 i < max_num_entries && it != frame_event_map_.end();
221 i++, ++it) {
222 AddFrameEventToStorage(it->second);
225 frame_event_map_.erase(frame_event_map_.begin(), it);
228 void EncodingEventSubscriber::TransferPacketEvents(size_t max_num_entries) {
229 PacketEventMap::iterator it = packet_event_map_.begin();
230 for (size_t i = 0;
231 i < max_num_entries && it != packet_event_map_.end();
232 i++, ++it) {
233 AddPacketEventToStorage(it->second);
236 packet_event_map_.erase(packet_event_map_.begin(), it);
239 void EncodingEventSubscriber::AddFrameEventToStorage(
240 const linked_ptr<AggregatedFrameEvent>& frame_event_proto) {
241 if (frame_event_storage_.size() >= max_frames_) {
242 frame_event_storage_[frame_event_storage_index_] = frame_event_proto;
243 } else {
244 frame_event_storage_.push_back(frame_event_proto);
247 frame_event_storage_index_ = (frame_event_storage_index_ + 1) % max_frames_;
250 void EncodingEventSubscriber::AddPacketEventToStorage(
251 const linked_ptr<AggregatedPacketEvent>& packet_event_proto) {
252 if (packet_event_storage_.size() >= max_frames_)
253 packet_event_storage_[packet_event_storage_index_] = packet_event_proto;
254 else
255 packet_event_storage_.push_back(packet_event_proto);
257 packet_event_storage_index_ = (packet_event_storage_index_ + 1) % max_frames_;
260 RtpTimestamp EncodingEventSubscriber::GetRelativeRtpTimestamp(
261 RtpTimestamp rtp_timestamp) {
262 if (!seen_first_rtp_timestamp_) {
263 seen_first_rtp_timestamp_ = true;
264 first_rtp_timestamp_ = rtp_timestamp;
267 return rtp_timestamp - first_rtp_timestamp_;
270 void EncodingEventSubscriber::Reset() {
271 frame_event_map_.clear();
272 frame_event_storage_.clear();
273 frame_event_storage_index_ = 0;
274 packet_event_map_.clear();
275 packet_event_storage_.clear();
276 packet_event_storage_index_ = 0;
277 seen_first_rtp_timestamp_ = false;
278 first_rtp_timestamp_ = 0u;
281 } // namespace cast
282 } // namespace media