1 /*
2  *  Copyright 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #ifndef LOGGING_RTC_EVENT_LOG_LOGGED_EVENTS_H_
11 #define LOGGING_RTC_EVENT_LOG_LOGGED_EVENTS_H_
12 
13 #include <string>
14 #include <vector>
15 
16 #include "absl/types/optional.h"
17 #include "api/rtp_headers.h"
18 #include "api/units/time_delta.h"
19 #include "api/units/timestamp.h"
20 #include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
21 #include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
22 #include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
23 #include "modules/rtp_rtcp/source/rtcp_packet/loss_notification.h"
24 #include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
25 #include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
26 #include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
27 #include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
28 #include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
29 #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
30 
31 namespace webrtc {
32 
33 // The different event types are deliberately POD. Analysis of large logs is
34 // already resource intensive. The code simplifications that would be possible
35 // possible by having a base class (containing e.g. the log time) are not
36 // considered to outweigh the added memory and runtime overhead incurred by
37 // adding a vptr.
38 
39 struct LoggedRtpPacket {
LoggedRtpPacketLoggedRtpPacket40   LoggedRtpPacket(int64_t timestamp_us,
41                   RTPHeader header,
42                   size_t header_length,
43                   size_t total_length)
44       : timestamp_us(timestamp_us),
45         header(header),
46         header_length(header_length),
47         total_length(total_length) {}
48 
log_time_usLoggedRtpPacket49   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtpPacket50   int64_t log_time_ms() const { return timestamp_us / 1000; }
51 
52   int64_t timestamp_us;
53   // TODO(terelius): This allocates space for 15 CSRCs even if none are used.
54   RTPHeader header;
55   size_t header_length;
56   size_t total_length;
57 };
58 
59 struct LoggedRtpPacketIncoming {
LoggedRtpPacketIncomingLoggedRtpPacketIncoming60   LoggedRtpPacketIncoming(int64_t timestamp_us,
61                           RTPHeader header,
62                           size_t header_length,
63                           size_t total_length)
64       : rtp(timestamp_us, header, header_length, total_length) {}
log_time_usLoggedRtpPacketIncoming65   int64_t log_time_us() const { return rtp.timestamp_us; }
log_time_msLoggedRtpPacketIncoming66   int64_t log_time_ms() const { return rtp.timestamp_us / 1000; }
67 
68   LoggedRtpPacket rtp;
69 };
70 
71 struct LoggedRtpPacketOutgoing {
LoggedRtpPacketOutgoingLoggedRtpPacketOutgoing72   LoggedRtpPacketOutgoing(int64_t timestamp_us,
73                           RTPHeader header,
74                           size_t header_length,
75                           size_t total_length)
76       : rtp(timestamp_us, header, header_length, total_length) {}
log_time_usLoggedRtpPacketOutgoing77   int64_t log_time_us() const { return rtp.timestamp_us; }
log_time_msLoggedRtpPacketOutgoing78   int64_t log_time_ms() const { return rtp.timestamp_us / 1000; }
79 
80   LoggedRtpPacket rtp;
81 };
82 
83 struct LoggedRtcpPacket {
84   LoggedRtcpPacket(int64_t timestamp_us, const std::vector<uint8_t>& packet);
85   LoggedRtcpPacket(int64_t timestamp_us, const std::string& packet);
86   LoggedRtcpPacket(const LoggedRtcpPacket&);
87   ~LoggedRtcpPacket();
88 
log_time_usLoggedRtcpPacket89   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtcpPacket90   int64_t log_time_ms() const { return timestamp_us / 1000; }
91 
92   int64_t timestamp_us;
93   std::vector<uint8_t> raw_data;
94 };
95 
96 struct LoggedRtcpPacketIncoming {
LoggedRtcpPacketIncomingLoggedRtcpPacketIncoming97   LoggedRtcpPacketIncoming(int64_t timestamp_us,
98                            const std::vector<uint8_t>& packet)
99       : rtcp(timestamp_us, packet) {}
LoggedRtcpPacketIncomingLoggedRtcpPacketIncoming100   LoggedRtcpPacketIncoming(uint64_t timestamp_us, const std::string& packet)
101       : rtcp(timestamp_us, packet) {}
102 
log_time_usLoggedRtcpPacketIncoming103   int64_t log_time_us() const { return rtcp.timestamp_us; }
log_time_msLoggedRtcpPacketIncoming104   int64_t log_time_ms() const { return rtcp.timestamp_us / 1000; }
105 
106   LoggedRtcpPacket rtcp;
107 };
108 
109 struct LoggedRtcpPacketOutgoing {
LoggedRtcpPacketOutgoingLoggedRtcpPacketOutgoing110   LoggedRtcpPacketOutgoing(int64_t timestamp_us,
111                            const std::vector<uint8_t>& packet)
112       : rtcp(timestamp_us, packet) {}
LoggedRtcpPacketOutgoingLoggedRtcpPacketOutgoing113   LoggedRtcpPacketOutgoing(uint64_t timestamp_us, const std::string& packet)
114       : rtcp(timestamp_us, packet) {}
115 
log_time_usLoggedRtcpPacketOutgoing116   int64_t log_time_us() const { return rtcp.timestamp_us; }
log_time_msLoggedRtcpPacketOutgoing117   int64_t log_time_ms() const { return rtcp.timestamp_us / 1000; }
118 
119   LoggedRtcpPacket rtcp;
120 };
121 
122 struct LoggedRtcpPacketReceiverReport {
123   LoggedRtcpPacketReceiverReport() = default;
LoggedRtcpPacketReceiverReportLoggedRtcpPacketReceiverReport124   LoggedRtcpPacketReceiverReport(int64_t timestamp_us,
125                                  const rtcp::ReceiverReport& rr)
126       : timestamp_us(timestamp_us), rr(rr) {}
127 
log_time_usLoggedRtcpPacketReceiverReport128   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtcpPacketReceiverReport129   int64_t log_time_ms() const { return timestamp_us / 1000; }
130 
131   int64_t timestamp_us;
132   rtcp::ReceiverReport rr;
133 };
134 
135 struct LoggedRtcpPacketSenderReport {
136   LoggedRtcpPacketSenderReport() = default;
LoggedRtcpPacketSenderReportLoggedRtcpPacketSenderReport137   LoggedRtcpPacketSenderReport(int64_t timestamp_us,
138                                const rtcp::SenderReport& sr)
139       : timestamp_us(timestamp_us), sr(sr) {}
140 
log_time_usLoggedRtcpPacketSenderReport141   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtcpPacketSenderReport142   int64_t log_time_ms() const { return timestamp_us / 1000; }
143 
144   int64_t timestamp_us;
145   rtcp::SenderReport sr;
146 };
147 
148 struct LoggedRtcpPacketExtendedReports {
149   LoggedRtcpPacketExtendedReports() = default;
150 
log_time_usLoggedRtcpPacketExtendedReports151   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtcpPacketExtendedReports152   int64_t log_time_ms() const { return timestamp_us / 1000; }
153 
154   int64_t timestamp_us;
155   rtcp::ExtendedReports xr;
156 };
157 
158 struct LoggedRtcpPacketRemb {
159   LoggedRtcpPacketRemb() = default;
LoggedRtcpPacketRembLoggedRtcpPacketRemb160   LoggedRtcpPacketRemb(int64_t timestamp_us, const rtcp::Remb& remb)
161       : timestamp_us(timestamp_us), remb(remb) {}
162 
log_time_usLoggedRtcpPacketRemb163   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtcpPacketRemb164   int64_t log_time_ms() const { return timestamp_us / 1000; }
165 
166   int64_t timestamp_us;
167   rtcp::Remb remb;
168 };
169 
170 struct LoggedRtcpPacketNack {
171   LoggedRtcpPacketNack() = default;
LoggedRtcpPacketNackLoggedRtcpPacketNack172   LoggedRtcpPacketNack(int64_t timestamp_us, const rtcp::Nack& nack)
173       : timestamp_us(timestamp_us), nack(nack) {}
174 
log_time_usLoggedRtcpPacketNack175   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtcpPacketNack176   int64_t log_time_ms() const { return timestamp_us / 1000; }
177 
178   int64_t timestamp_us;
179   rtcp::Nack nack;
180 };
181 
182 struct LoggedRtcpPacketFir {
183   LoggedRtcpPacketFir() = default;
184 
log_time_usLoggedRtcpPacketFir185   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtcpPacketFir186   int64_t log_time_ms() const { return timestamp_us / 1000; }
187 
188   int64_t timestamp_us;
189   rtcp::Fir fir;
190 };
191 
192 struct LoggedRtcpPacketPli {
193   LoggedRtcpPacketPli() = default;
194 
log_time_usLoggedRtcpPacketPli195   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtcpPacketPli196   int64_t log_time_ms() const { return timestamp_us / 1000; }
197 
198   int64_t timestamp_us;
199   rtcp::Pli pli;
200 };
201 
202 struct LoggedRtcpPacketTransportFeedback {
LoggedRtcpPacketTransportFeedbackLoggedRtcpPacketTransportFeedback203   LoggedRtcpPacketTransportFeedback()
204       : transport_feedback(/*include_timestamps=*/true, /*include_lost*/ true) {
205   }
LoggedRtcpPacketTransportFeedbackLoggedRtcpPacketTransportFeedback206   LoggedRtcpPacketTransportFeedback(
207       int64_t timestamp_us,
208       const rtcp::TransportFeedback& transport_feedback)
209       : timestamp_us(timestamp_us), transport_feedback(transport_feedback) {}
210 
log_time_usLoggedRtcpPacketTransportFeedback211   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtcpPacketTransportFeedback212   int64_t log_time_ms() const { return timestamp_us / 1000; }
213 
214   int64_t timestamp_us;
215   rtcp::TransportFeedback transport_feedback;
216 };
217 
218 struct LoggedRtcpPacketLossNotification {
219   LoggedRtcpPacketLossNotification() = default;
LoggedRtcpPacketLossNotificationLoggedRtcpPacketLossNotification220   LoggedRtcpPacketLossNotification(
221       int64_t timestamp_us,
222       const rtcp::LossNotification& loss_notification)
223       : timestamp_us(timestamp_us), loss_notification(loss_notification) {}
224 
log_time_usLoggedRtcpPacketLossNotification225   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtcpPacketLossNotification226   int64_t log_time_ms() const { return timestamp_us / 1000; }
227 
228   int64_t timestamp_us;
229   rtcp::LossNotification loss_notification;
230 };
231 
232 struct LoggedRtcpPacketBye {
233   LoggedRtcpPacketBye() = default;
234 
log_time_usLoggedRtcpPacketBye235   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedRtcpPacketBye236   int64_t log_time_ms() const { return timestamp_us / 1000; }
237 
238   int64_t timestamp_us;
239   rtcp::Bye bye;
240 };
241 
242 struct LoggedStartEvent {
LoggedStartEventLoggedStartEvent243   explicit LoggedStartEvent(int64_t timestamp_us)
244       : LoggedStartEvent(timestamp_us, timestamp_us / 1000) {}
245 
LoggedStartEventLoggedStartEvent246   LoggedStartEvent(int64_t timestamp_us, int64_t utc_start_time_ms)
247       : timestamp_us(timestamp_us), utc_start_time_ms(utc_start_time_ms) {}
248 
log_time_usLoggedStartEvent249   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedStartEvent250   int64_t log_time_ms() const { return timestamp_us / 1000; }
251 
252   int64_t timestamp_us;
253   int64_t utc_start_time_ms;
254 };
255 
256 struct LoggedStopEvent {
LoggedStopEventLoggedStopEvent257   explicit LoggedStopEvent(int64_t timestamp_us) : timestamp_us(timestamp_us) {}
258 
log_time_usLoggedStopEvent259   int64_t log_time_us() const { return timestamp_us; }
log_time_msLoggedStopEvent260   int64_t log_time_ms() const { return timestamp_us / 1000; }
261 
262   int64_t timestamp_us;
263 };
264 
265 struct InferredRouteChangeEvent {
log_time_msInferredRouteChangeEvent266   int64_t log_time_ms() const { return log_time.ms(); }
log_time_usInferredRouteChangeEvent267   int64_t log_time_us() const { return log_time.us(); }
268   uint32_t route_id;
269   Timestamp log_time = Timestamp::MinusInfinity();
270   uint16_t send_overhead;
271   uint16_t return_overhead;
272 };
273 
274 enum class LoggedMediaType : uint8_t { kUnknown, kAudio, kVideo };
275 
276 struct LoggedPacketInfo {
277   LoggedPacketInfo(const LoggedRtpPacket& rtp,
278                    LoggedMediaType media_type,
279                    bool rtx,
280                    Timestamp capture_time);
281   LoggedPacketInfo(const LoggedPacketInfo&);
282   ~LoggedPacketInfo();
log_time_msLoggedPacketInfo283   int64_t log_time_ms() const { return log_packet_time.ms(); }
log_time_usLoggedPacketInfo284   int64_t log_time_us() const { return log_packet_time.us(); }
285   uint32_t ssrc;
286   uint16_t stream_seq_no;
287   uint16_t size;
288   uint16_t payload_size;
289   uint16_t padding_size;
290   uint16_t overhead = 0;
291   uint8_t payload_type;
292   LoggedMediaType media_type = LoggedMediaType::kUnknown;
293   bool rtx = false;
294   bool marker_bit = false;
295   bool has_transport_seq_no = false;
296   bool last_in_feedback = false;
297   uint16_t transport_seq_no = 0;
298   // The RTP header timestamp unwrapped and converted from tick count to seconds
299   // based timestamp.
300   Timestamp capture_time;
301   // The time the packet was logged. This is the receive time for incoming
302   // packets and send time for outgoing.
303   Timestamp log_packet_time;
304   // Send time as reported by abs-send-time extension, For outgoing packets this
305   // corresponds to log_packet_time, but might be measured using another clock.
306   Timestamp reported_send_time;
307   // The receive time that was reported in feedback. For incoming packets this
308   // corresponds to log_packet_time, but might be measured using another clock.
309   // PlusInfinity indicates that the packet was lost.
310   Timestamp reported_recv_time = Timestamp::MinusInfinity();
311   // The time feedback message was logged. This is the feedback send time for
312   // incoming packets and feedback receive time for outgoing.
313   // PlusInfinity indicates that feedback was expected but not received.
314   Timestamp log_feedback_time = Timestamp::MinusInfinity();
315   // The delay betweeen receiving an RTP packet and sending feedback for
316   // incoming packets. For outgoing packets we don't know the feedback send
317   // time, and this is instead calculated as the difference in reported receive
318   // time between this packet and the last packet in the same feedback message.
319   TimeDelta feedback_hold_duration = TimeDelta::MinusInfinity();
320 };
321 
322 enum class LoggedIceEventType {
323   kAdded,
324   kUpdated,
325   kDestroyed,
326   kSelected,
327   kCheckSent,
328   kCheckReceived,
329   kCheckResponseSent,
330   kCheckResponseReceived,
331 };
332 
333 struct LoggedIceEvent {
334   uint32_t candidate_pair_id;
335   Timestamp log_time;
336   LoggedIceEventType event_type;
337 };
338 
339 
340 
341 
342 
343 }  // namespace webrtc
344 #endif  // LOGGING_RTC_EVENT_LOG_LOGGED_EVENTS_H_
345