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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
media / cast / logging / encoding_event_subscriber.cc [blame]
// Copyright 2014 The Chromium Authors
// 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 <stdint.h>
#include <algorithm>
#include <cstring>
#include <utility>
#include "base/logging.h"
#include "base/not_fatal_until.h"
#include "base/numerics/safe_conversions.h"
#include "media/cast/logging/proto/proto_utils.h"
using google::protobuf::RepeatedPtrField;
using media::cast::proto::AggregatedFrameEvent;
using media::cast::proto::AggregatedPacketEvent;
using media::cast::proto::BasePacketEvent;
using media::cast::proto::LogMetadata;
namespace {
// A size limit on maps to keep lookups fast.
const size_t kMaxMapSize = 200;
// The smallest (oredered by RTP timestamp) |kNumMapEntriesToTransfer| entries
// will be moved when the map size reaches |kMaxMapSize|.
// Must be smaller than |kMaxMapSize|.
const size_t kNumMapEntriesToTransfer = 100;
template <typename ProtoPtr>
bool IsRtpTimestampLessThan(const ProtoPtr& lhs, const ProtoPtr& rhs) {
return lhs->relative_rtp_timestamp() < rhs->relative_rtp_timestamp();
}
BasePacketEvent* GetNewBasePacketEvent(AggregatedPacketEvent* event_proto,
int packet_id,
int size) {
BasePacketEvent* base = event_proto->add_base_packet_event();
base->set_packet_id(packet_id);
base->set_size(size);
return base;
}
} // namespace
namespace media {
namespace cast {
EncodingEventSubscriber::EncodingEventSubscriber(
EventMediaType event_media_type,
size_t max_frames)
: event_media_type_(event_media_type), max_frames_(max_frames) {
Reset();
}
EncodingEventSubscriber::~EncodingEventSubscriber() {
DCHECK(thread_checker_.CalledOnValidThread());
}
void EncodingEventSubscriber::OnReceiveFrameEvent(
const FrameEvent& frame_event) {
DCHECK(thread_checker_.CalledOnValidThread());
if (event_media_type_ != frame_event.media_type) {
return;
}
const RtpTimeDelta relative_rtp_timestamp =
GetRelativeRtpTimestamp(frame_event.rtp_timestamp);
const uint32_t lower_32_bits = relative_rtp_timestamp.lower_32_bits();
AggregatedFrameEvent* event_proto_ptr = nullptr;
// Look up existing entry. If not found, create a new entry and add to map.
auto it = frame_event_map_.find(relative_rtp_timestamp);
if (it == frame_event_map_.end()) {
if (!ShouldCreateNewProto(lower_32_bits)) {
return;
}
IncrementStoredProtoCount(lower_32_bits);
auto event_proto = std::make_unique<AggregatedFrameEvent>();
event_proto->set_relative_rtp_timestamp(lower_32_bits);
event_proto_ptr = event_proto.get();
frame_event_map_.insert(
std::make_pair(relative_rtp_timestamp, std::move(event_proto)));
} else {
if (it->second->event_type_size() >= kMaxEventsPerProto) {
DVLOG(2) << "Too many events in frame " << frame_event.rtp_timestamp
<< ". Using new frame event proto.";
AddFrameEventToStorage(std::move(it->second));
if (!ShouldCreateNewProto(lower_32_bits)) {
frame_event_map_.erase(it);
return;
}
IncrementStoredProtoCount(lower_32_bits);
it->second = std::make_unique<AggregatedFrameEvent>();
it->second->set_relative_rtp_timestamp(lower_32_bits);
}
event_proto_ptr = it->second.get();
}
event_proto_ptr->add_event_type(ToProtoEventType(frame_event.type));
event_proto_ptr->add_event_timestamp_ms(
(frame_event.timestamp - base::TimeTicks()).InMilliseconds());
if (frame_event.type == FRAME_CAPTURE_END) {
if (frame_event.media_type == VIDEO_EVENT && frame_event.width > 0 &&
frame_event.height > 0) {
event_proto_ptr->set_width(frame_event.width);
event_proto_ptr->set_height(frame_event.height);
}
} else if (frame_event.type == FRAME_ENCODED) {
event_proto_ptr->set_encoded_frame_size(frame_event.size);
if (frame_event.encoder_cpu_utilization >= 0.0) {
event_proto_ptr->set_encoder_cpu_percent_utilized(
base::saturated_cast<int32_t>(
frame_event.encoder_cpu_utilization * 100.0 + 0.5));
}
if (frame_event.idealized_bitrate_utilization >= 0.0) {
event_proto_ptr->set_idealized_bitrate_percent_utilized(
base::saturated_cast<int32_t>(
frame_event.idealized_bitrate_utilization * 100.0 + 0.5));
}
if (frame_event.media_type == VIDEO_EVENT) {
event_proto_ptr->set_key_frame(frame_event.key_frame);
event_proto_ptr->set_target_bitrate(frame_event.target_bitrate);
}
} else if (frame_event.type == FRAME_PLAYOUT) {
event_proto_ptr->set_delay_millis(frame_event.delay_delta.InMilliseconds());
}
if (frame_event_map_.size() > kMaxMapSize) {
TransferFrameEvents(kNumMapEntriesToTransfer);
}
DCHECK(frame_event_map_.size() <= kMaxMapSize);
DCHECK(frame_event_storage_.size() <= max_frames_);
}
void EncodingEventSubscriber::OnReceivePacketEvent(
const PacketEvent& packet_event) {
DCHECK(thread_checker_.CalledOnValidThread());
if (event_media_type_ != packet_event.media_type) {
return;
}
const RtpTimeDelta relative_rtp_timestamp =
GetRelativeRtpTimestamp(packet_event.rtp_timestamp);
uint32_t lower_32_bits = relative_rtp_timestamp.lower_32_bits();
auto it = packet_event_map_.find(relative_rtp_timestamp);
BasePacketEvent* base_packet_event_proto = nullptr;
// Look up existing entry. If not found, create a new entry and add to map.
if (it == packet_event_map_.end()) {
if (!ShouldCreateNewProto(lower_32_bits)) {
return;
}
IncrementStoredProtoCount(lower_32_bits);
auto event_proto = std::make_unique<AggregatedPacketEvent>();
event_proto->set_relative_rtp_timestamp(lower_32_bits);
base_packet_event_proto = GetNewBasePacketEvent(
event_proto.get(), packet_event.packet_id, packet_event.size);
packet_event_map_.insert(
std::make_pair(relative_rtp_timestamp, std::move(event_proto)));
} else {
// Found existing entry, now look up existing BasePacketEvent using packet
// ID. If not found, create a new entry and add to proto.
RepeatedPtrField<BasePacketEvent>* field =
it->second->mutable_base_packet_event();
for (RepeatedPtrField<BasePacketEvent>::pointer_iterator base_it =
field->pointer_begin();
base_it != field->pointer_end(); ++base_it) {
if ((*base_it)->packet_id() == packet_event.packet_id) {
base_packet_event_proto = *base_it;
break;
}
}
if (!base_packet_event_proto) {
if (it->second->base_packet_event_size() >= kMaxPacketsPerFrame) {
DVLOG(3) << "Too many packets in AggregatedPacketEvent "
<< packet_event.rtp_timestamp << ". "
<< "Using new packet event proto.";
AddPacketEventToStorage(std::move(it->second));
if (!ShouldCreateNewProto(lower_32_bits)) {
packet_event_map_.erase(it);
return;
}
IncrementStoredProtoCount(lower_32_bits);
it->second = std::make_unique<AggregatedPacketEvent>();
it->second->set_relative_rtp_timestamp(lower_32_bits);
}
base_packet_event_proto = GetNewBasePacketEvent(
it->second.get(), packet_event.packet_id, packet_event.size);
} else if (base_packet_event_proto->event_type_size() >=
kMaxEventsPerProto) {
DVLOG(3) << "Too many events in packet " << packet_event.rtp_timestamp
<< ", " << packet_event.packet_id
<< ". Using new packet event proto.";
AddPacketEventToStorage(std::move(it->second));
if (!ShouldCreateNewProto(lower_32_bits)) {
packet_event_map_.erase(it);
return;
}
IncrementStoredProtoCount(lower_32_bits);
it->second = std::make_unique<AggregatedPacketEvent>();
it->second->set_relative_rtp_timestamp(lower_32_bits);
base_packet_event_proto = GetNewBasePacketEvent(
it->second.get(), packet_event.packet_id, packet_event.size);
}
}
base_packet_event_proto->add_event_type(ToProtoEventType(packet_event.type));
base_packet_event_proto->add_event_timestamp_ms(
(packet_event.timestamp - base::TimeTicks()).InMilliseconds());
// |base_packet_event_proto| could have been created with a receiver event
// which does not have the packet size and we would need to overwrite it when
// we see a sender event, which does have the packet size.
if (packet_event.size > 0) {
base_packet_event_proto->set_size(packet_event.size);
}
if (packet_event_map_.size() > kMaxMapSize) {
TransferPacketEvents(kNumMapEntriesToTransfer);
}
DCHECK(packet_event_map_.size() <= kMaxMapSize);
DCHECK(packet_event_storage_.size() <= max_frames_);
}
void EncodingEventSubscriber::GetEventsAndReset(
LogMetadata* metadata,
FrameEventList* frame_events,
PacketEventList* packet_events) {
DCHECK(thread_checker_.CalledOnValidThread());
// Flush all events.
TransferFrameEvents(frame_event_map_.size());
TransferPacketEvents(packet_event_map_.size());
std::sort(frame_event_storage_.begin(), frame_event_storage_.end(),
&IsRtpTimestampLessThan<std::unique_ptr<AggregatedFrameEvent>>);
std::sort(packet_event_storage_.begin(), packet_event_storage_.end(),
&IsRtpTimestampLessThan<std::unique_ptr<AggregatedPacketEvent>>);
metadata->set_is_audio(event_media_type_ == AUDIO_EVENT);
metadata->set_first_rtp_timestamp(first_rtp_timestamp_.lower_32_bits());
metadata->set_num_frame_events(frame_event_storage_.size());
metadata->set_num_packet_events(packet_event_storage_.size());
metadata->set_reference_timestamp_ms_at_unix_epoch(
(base::TimeTicks::UnixEpoch() - base::TimeTicks()).InMilliseconds());
frame_events->swap(frame_event_storage_);
packet_events->swap(packet_event_storage_);
Reset();
}
void EncodingEventSubscriber::TransferFrameEvents(size_t max_num_entries) {
DCHECK(frame_event_map_.size() >= max_num_entries);
auto it = frame_event_map_.begin();
for (size_t i = 0; i < max_num_entries && it != frame_event_map_.end();
i++, ++it) {
AddFrameEventToStorage(std::move(it->second));
}
frame_event_map_.erase(frame_event_map_.begin(), it);
}
void EncodingEventSubscriber::TransferPacketEvents(size_t max_num_entries) {
auto it = packet_event_map_.begin();
for (size_t i = 0; i < max_num_entries && it != packet_event_map_.end();
i++, ++it) {
AddPacketEventToStorage(std::move(it->second));
}
packet_event_map_.erase(packet_event_map_.begin(), it);
}
void EncodingEventSubscriber::AddFrameEventToStorage(
std::unique_ptr<AggregatedFrameEvent> frame_event_proto) {
if (frame_event_storage_.size() >= max_frames_) {
auto& entry = frame_event_storage_[frame_event_storage_index_];
DecrementStoredProtoCount(entry->relative_rtp_timestamp());
entry = std::move(frame_event_proto);
} else {
frame_event_storage_.push_back(std::move(frame_event_proto));
}
frame_event_storage_index_ = (frame_event_storage_index_ + 1) % max_frames_;
}
void EncodingEventSubscriber::AddPacketEventToStorage(
std::unique_ptr<AggregatedPacketEvent> packet_event_proto) {
if (packet_event_storage_.size() >= max_frames_) {
auto& entry = packet_event_storage_[packet_event_storage_index_];
DecrementStoredProtoCount(entry->relative_rtp_timestamp());
entry = std::move(packet_event_proto);
} else {
packet_event_storage_.push_back(std::move(packet_event_proto));
}
packet_event_storage_index_ = (packet_event_storage_index_ + 1) % max_frames_;
}
bool EncodingEventSubscriber::ShouldCreateNewProto(
uint32_t relative_rtp_timestamp_lower_32_bits) const {
auto it = stored_proto_counts_.find(relative_rtp_timestamp_lower_32_bits);
int proto_count = it == stored_proto_counts_.end() ? 0 : it->second;
DVLOG_IF(2, proto_count >= kMaxProtosPerFrame)
<< relative_rtp_timestamp_lower_32_bits
<< " already reached max number of protos.";
return proto_count < kMaxProtosPerFrame;
}
void EncodingEventSubscriber::IncrementStoredProtoCount(
uint32_t relative_rtp_timestamp_lower_32_bits) {
stored_proto_counts_[relative_rtp_timestamp_lower_32_bits]++;
DCHECK_LE(stored_proto_counts_[relative_rtp_timestamp_lower_32_bits],
kMaxProtosPerFrame)
<< relative_rtp_timestamp_lower_32_bits
<< " exceeded max number of event protos.";
}
void EncodingEventSubscriber::DecrementStoredProtoCount(
uint32_t relative_rtp_timestamp_lower_32_bits) {
auto it = stored_proto_counts_.find(relative_rtp_timestamp_lower_32_bits);
CHECK(it != stored_proto_counts_.end(), base::NotFatalUntil::M130)
<< "no event protos for " << relative_rtp_timestamp_lower_32_bits;
if (it->second > 1) {
it->second--;
} else {
stored_proto_counts_.erase(it);
}
}
RtpTimeDelta EncodingEventSubscriber::GetRelativeRtpTimestamp(
RtpTimeTicks rtp_timestamp) {
if (!seen_first_rtp_timestamp_) {
seen_first_rtp_timestamp_ = true;
first_rtp_timestamp_ = rtp_timestamp;
}
return rtp_timestamp - first_rtp_timestamp_;
}
void EncodingEventSubscriber::Reset() {
frame_event_map_.clear();
frame_event_storage_.clear();
frame_event_storage_index_ = 0;
packet_event_map_.clear();
packet_event_storage_.clear();
packet_event_storage_index_ = 0;
stored_proto_counts_.clear();
seen_first_rtp_timestamp_ = false;
first_rtp_timestamp_ = RtpTimeTicks();
}
} // namespace cast
} // namespace media