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
|
// 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 <stdint.h>
#include <algorithm>
#include <cstring>
#include <utility>
#include "base/logging.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 media {
namespace cast {
EncodingEventSubscriber::EncodingEventSubscriber(
EventMediaType event_media_type,
size_t max_frames)
: event_media_type_(event_media_type),
max_frames_(max_frames),
frame_event_storage_index_(0),
packet_event_storage_index_(0),
seen_first_rtp_timestamp_(false),
first_rtp_timestamp_(0u) {}
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;
RtpTimestamp relative_rtp_timestamp =
GetRelativeRtpTimestamp(frame_event.rtp_timestamp);
FrameEventMap::iterator it = frame_event_map_.find(relative_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_relative_rtp_timestamp(relative_rtp_timestamp);
frame_event_map_.insert(
std::make_pair(relative_rtp_timestamp, event_proto));
} else {
event_proto = it->second;
if (event_proto->event_type_size() >= kMaxEventsPerProto) {
DVLOG(2) << "Too many events in frame " << frame_event.rtp_timestamp
<< ". Using new frame event proto.";
AddFrameEventToStorage(event_proto);
event_proto.reset(new AggregatedFrameEvent);
event_proto->set_relative_rtp_timestamp(relative_rtp_timestamp);
it->second = event_proto;
}
}
event_proto->add_event_type(ToProtoEventType(frame_event.type));
event_proto->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->set_width(frame_event.width);
event_proto->set_height(frame_event.height);
}
} else if (frame_event.type == FRAME_ENCODED) {
event_proto->set_encoded_frame_size(frame_event.size);
if (frame_event.encoder_cpu_utilization >= 0.0) {
event_proto->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->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->set_key_frame(frame_event.key_frame);
event_proto->set_target_bitrate(frame_event.target_bitrate);
}
} else if (frame_event.type == FRAME_PLAYOUT) {
event_proto->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;
RtpTimestamp relative_rtp_timestamp =
GetRelativeRtpTimestamp(packet_event.rtp_timestamp);
PacketEventMap::iterator it =
packet_event_map_.find(relative_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_relative_rtp_timestamp(relative_rtp_timestamp);
packet_event_map_.insert(
std::make_pair(relative_rtp_timestamp, event_proto));
base_packet_event_proto = GetNewBasePacketEvent(
event_proto.get(), packet_event.packet_id, packet_event.size);
} 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 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 (event_proto->base_packet_event_size() >= kMaxPacketsPerFrame) {
DVLOG(3) << "Too many packets in AggregatedPacketEvent "
<< packet_event.rtp_timestamp << ". "
<< "Using new packet event proto.";
AddPacketEventToStorage(event_proto);
event_proto.reset(new AggregatedPacketEvent);
event_proto->set_relative_rtp_timestamp(relative_rtp_timestamp);
it->second = event_proto;
}
base_packet_event_proto = GetNewBasePacketEvent(
event_proto.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(event_proto);
event_proto.reset(new AggregatedPacketEvent);
event_proto->set_relative_rtp_timestamp(relative_rtp_timestamp);
it->second = event_proto;
base_packet_event_proto = GetNewBasePacketEvent(
event_proto.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<linked_ptr<AggregatedFrameEvent> >);
std::sort(packet_event_storage_.begin(), packet_event_storage_.end(),
&IsRtpTimestampLessThan<linked_ptr<AggregatedPacketEvent> >);
metadata->set_is_audio(event_media_type_ == AUDIO_EVENT);
metadata->set_first_rtp_timestamp(first_rtp_timestamp_);
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);
FrameEventMap::iterator it = frame_event_map_.begin();
for (size_t i = 0;
i < max_num_entries && it != frame_event_map_.end();
i++, ++it) {
AddFrameEventToStorage(it->second);
}
frame_event_map_.erase(frame_event_map_.begin(), it);
}
void EncodingEventSubscriber::TransferPacketEvents(size_t max_num_entries) {
PacketEventMap::iterator it = packet_event_map_.begin();
for (size_t i = 0;
i < max_num_entries && it != packet_event_map_.end();
i++, ++it) {
AddPacketEventToStorage(it->second);
}
packet_event_map_.erase(packet_event_map_.begin(), it);
}
void EncodingEventSubscriber::AddFrameEventToStorage(
const linked_ptr<AggregatedFrameEvent>& frame_event_proto) {
if (frame_event_storage_.size() >= max_frames_) {
frame_event_storage_[frame_event_storage_index_] = frame_event_proto;
} else {
frame_event_storage_.push_back(frame_event_proto);
}
frame_event_storage_index_ = (frame_event_storage_index_ + 1) % max_frames_;
}
void EncodingEventSubscriber::AddPacketEventToStorage(
const linked_ptr<AggregatedPacketEvent>& packet_event_proto) {
if (packet_event_storage_.size() >= max_frames_)
packet_event_storage_[packet_event_storage_index_] = packet_event_proto;
else
packet_event_storage_.push_back(packet_event_proto);
packet_event_storage_index_ = (packet_event_storage_index_ + 1) % max_frames_;
}
RtpTimestamp EncodingEventSubscriber::GetRelativeRtpTimestamp(
RtpTimestamp 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;
seen_first_rtp_timestamp_ = false;
first_rtp_timestamp_ = 0u;
}
} // namespace cast
} // namespace media
|