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_RTC_EVENT_LOG_PARSER_H_
11 #define LOGGING_RTC_EVENT_LOG_RTC_EVENT_LOG_PARSER_H_
12 
13 #include <iterator>
14 #include <limits>
15 #include <map>
16 #include <set>
17 #include <sstream>  // no-presubmit-check TODO(webrtc:8982)
18 #include <string>
19 #include <utility>  // pair
20 #include <vector>
21 
22 #include "api/rtc_event_log/rtc_event_log.h"
23 #include "call/video_receive_stream.h"
24 #include "call/video_send_stream.h"
25 #include "logging/rtc_event_log/logged_events.h"
26 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
27 #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
28 #include "rtc_base/ignore_wundef.h"
29 
30 // Files generated at build-time by the protobuf compiler.
31 RTC_PUSH_IGNORING_WUNDEF()
32 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD
33 #include "external/webrtc/webrtc/logging/rtc_event_log/rtc_event_log.pb.h"
34 #include "external/webrtc/webrtc/logging/rtc_event_log/rtc_event_log2.pb.h"
35 #else
36 #include "logging/rtc_event_log/rtc_event_log.pb.h"
37 #include "logging/rtc_event_log/rtc_event_log2.pb.h"
38 #endif
RTC_POP_IGNORING_WUNDEF()39 RTC_POP_IGNORING_WUNDEF()
40 
41 namespace webrtc {
42 
43 enum PacketDirection { kIncomingPacket = 0, kOutgoingPacket };
44 
45 template <typename T>
46 class PacketView;
47 
48 template <typename T>
49 class PacketIterator {
50   friend class PacketView<T>;
51 
52  public:
53   // Standard iterator traits.
54   using difference_type = std::ptrdiff_t;
55   using value_type = T;
56   using pointer = T*;
57   using reference = T&;
58   using iterator_category = std::bidirectional_iterator_tag;
59 
60   // The default-contructed iterator is meaningless, but is required by the
61   // ForwardIterator concept.
62   PacketIterator() : ptr_(nullptr), element_size_(0) {}
63   PacketIterator(const PacketIterator& other)
64       : ptr_(other.ptr_), element_size_(other.element_size_) {}
65   PacketIterator(const PacketIterator&& other)
66       : ptr_(other.ptr_), element_size_(other.element_size_) {}
67   ~PacketIterator() = default;
68 
69   PacketIterator& operator=(const PacketIterator& other) {
70     ptr_ = other.ptr_;
71     element_size_ = other.element_size_;
72     return *this;
73   }
74   PacketIterator& operator=(const PacketIterator&& other) {
75     ptr_ = other.ptr_;
76     element_size_ = other.element_size_;
77     return *this;
78   }
79 
80   bool operator==(const PacketIterator<T>& other) const {
81     RTC_DCHECK_EQ(element_size_, other.element_size_);
82     return ptr_ == other.ptr_;
83   }
84   bool operator!=(const PacketIterator<T>& other) const {
85     RTC_DCHECK_EQ(element_size_, other.element_size_);
86     return ptr_ != other.ptr_;
87   }
88 
89   PacketIterator& operator++() {
90     ptr_ += element_size_;
91     return *this;
92   }
93   PacketIterator& operator--() {
94     ptr_ -= element_size_;
95     return *this;
96   }
97   PacketIterator operator++(int) {
98     PacketIterator iter_copy(ptr_, element_size_);
99     ptr_ += element_size_;
100     return iter_copy;
101   }
102   PacketIterator operator--(int) {
103     PacketIterator iter_copy(ptr_, element_size_);
104     ptr_ -= element_size_;
105     return iter_copy;
106   }
107 
108   T& operator*() { return *reinterpret_cast<T*>(ptr_); }
109   const T& operator*() const { return *reinterpret_cast<const T*>(ptr_); }
110 
111   T* operator->() { return reinterpret_cast<T*>(ptr_); }
112   const T* operator->() const { return reinterpret_cast<const T*>(ptr_); }
113 
114  private:
115   PacketIterator(typename std::conditional<std::is_const<T>::value,
116                                            const void*,
117                                            void*>::type p,
118                  size_t s)
119       : ptr_(reinterpret_cast<decltype(ptr_)>(p)), element_size_(s) {}
120 
121   typename std::conditional<std::is_const<T>::value, const char*, char*>::type
122       ptr_;
123   size_t element_size_;
124 };
125 
126 // Suppose that we have a struct S where we are only interested in a specific
127 // member M. Given an array of S, PacketView can be used to treat the array
128 // as an array of M, without exposing the type S to surrounding code and without
129 // accessing the member through a virtual function. In this case, we want to
130 // have a common view for incoming and outgoing RtpPackets, hence the PacketView
131 // name.
132 // Note that constructing a PacketView bypasses the typesystem, so the caller
133 // has to take extra care when constructing these objects. The implementation
134 // also requires that the containing struct is standard-layout (e.g. POD).
135 //
136 // Usage example:
137 // struct A {...};
138 // struct B { A a; ...};
139 // struct C { A a; ...};
140 // size_t len = 10;
141 // B* array1 = new B[len];
142 // C* array2 = new C[len];
143 //
144 // PacketView<A> view1 = PacketView<A>::Create<B>(array1, len, offsetof(B, a));
145 // PacketView<A> view2 = PacketView<A>::Create<C>(array2, len, offsetof(C, a));
146 //
147 // The following code works with either view1 or view2.
148 // void f(PacketView<A> view)
149 // for (A& a : view) {
150 //   DoSomething(a);
151 // }
152 template <typename T>
153 class PacketView {
154  public:
155   template <typename U>
156   static PacketView Create(U* ptr, size_t num_elements, size_t offset) {
157     static_assert(std::is_standard_layout<U>::value,
158                   "PacketView can only be created for standard layout types.");
159     static_assert(std::is_standard_layout<T>::value,
160                   "PacketView can only be created for standard layout types.");
161     return PacketView(ptr, num_elements, offset, sizeof(U));
162   }
163 
164   using value_type = T;
165   using reference = value_type&;
166   using const_reference = const value_type&;
167 
168   using iterator = PacketIterator<T>;
169   using const_iterator = PacketIterator<const T>;
170   using reverse_iterator = std::reverse_iterator<iterator>;
171   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
172 
173   iterator begin() { return iterator(data_, element_size_); }
174   iterator end() {
175     auto end_ptr = data_ + num_elements_ * element_size_;
176     return iterator(end_ptr, element_size_);
177   }
178 
179   const_iterator begin() const { return const_iterator(data_, element_size_); }
180   const_iterator end() const {
181     auto end_ptr = data_ + num_elements_ * element_size_;
182     return const_iterator(end_ptr, element_size_);
183   }
184 
185   reverse_iterator rbegin() { return reverse_iterator(end()); }
186   reverse_iterator rend() { return reverse_iterator(begin()); }
187 
188   const_reverse_iterator rbegin() const {
189     return const_reverse_iterator(end());
190   }
191   const_reverse_iterator rend() const {
192     return const_reverse_iterator(begin());
193   }
194 
195   size_t size() const { return num_elements_; }
196 
197   bool empty() const { return num_elements_ == 0; }
198 
199   T& operator[](size_t i) {
200     auto elem_ptr = data_ + i * element_size_;
201     return *reinterpret_cast<T*>(elem_ptr);
202   }
203 
204   const T& operator[](size_t i) const {
205     auto elem_ptr = data_ + i * element_size_;
206     return *reinterpret_cast<const T*>(elem_ptr);
207   }
208 
209  private:
210   PacketView(typename std::conditional<std::is_const<T>::value,
211                                        const void*,
212                                        void*>::type data,
213              size_t num_elements,
214              size_t offset,
215              size_t element_size)
216       : data_(reinterpret_cast<decltype(data_)>(data) + offset),
217         num_elements_(num_elements),
218         element_size_(element_size) {}
219 
220   typename std::conditional<std::is_const<T>::value, const char*, char*>::type
221       data_;
222   size_t num_elements_;
223   size_t element_size_;
224 };
225 
226 // Conversion functions for version 2 of the wire format.
227 BandwidthUsage GetRuntimeDetectorState(
228     rtclog2::DelayBasedBweUpdates::DetectorState detector_state);
229 
230 ProbeFailureReason GetRuntimeProbeFailureReason(
231     rtclog2::BweProbeResultFailure::FailureReason failure);
232 
233 DtlsTransportState GetRuntimeDtlsTransportState(
234     rtclog2::DtlsTransportStateEvent::DtlsTransportState state);
235 
236 IceCandidatePairConfigType GetRuntimeIceCandidatePairConfigType(
237     rtclog2::IceCandidatePairConfig::IceCandidatePairConfigType type);
238 
239 IceCandidateType GetRuntimeIceCandidateType(
240     rtclog2::IceCandidatePairConfig::IceCandidateType type);
241 
242 IceCandidatePairProtocol GetRuntimeIceCandidatePairProtocol(
243     rtclog2::IceCandidatePairConfig::Protocol protocol);
244 
245 IceCandidatePairAddressFamily GetRuntimeIceCandidatePairAddressFamily(
246     rtclog2::IceCandidatePairConfig::AddressFamily address_family);
247 
248 IceCandidateNetworkType GetRuntimeIceCandidateNetworkType(
249     rtclog2::IceCandidatePairConfig::NetworkType network_type);
250 
251 IceCandidatePairEventType GetRuntimeIceCandidatePairEventType(
252     rtclog2::IceCandidatePairEvent::IceCandidatePairEventType type);
253 
254 std::vector<RtpExtension> GetRuntimeRtpHeaderExtensionConfig(
255     const rtclog2::RtpHeaderExtensionConfig& proto_header_extensions);
256 // End of conversion functions.
257 
258 class ParsedRtcEventLog {
259  public:
260   enum class MediaType { ANY, AUDIO, VIDEO, DATA };
261   enum class UnconfiguredHeaderExtensions {
262     kDontParse,
263     kAttemptWebrtcDefaultConfig
264   };
265   class ParseStatus {
266    public:
267     static ParseStatus Success() { return ParseStatus(); }
268     static ParseStatus Error(std::string error, std::string file, int line) {
269       return ParseStatus(error, file, line);
270     }
271 
272     bool ok() const { return error_.empty() && file_.empty() && line_ == 0; }
273     std::string message() const {
274       return error_ + " failed at " + file_ + " line " + std::to_string(line_);
275     }
276 
277     RTC_DEPRECATED operator bool() const { return ok(); }
278 
279    private:
280     ParseStatus() : error_(), file_(), line_(0) {}
281     ParseStatus(std::string error, std::string file, int line)
282         : error_(error), file_(file), line_(line) {}
283     std::string error_;
284     std::string file_;
285     int line_;
286   };
287 
288   template <typename T>
289   class ParseStatusOr {
290    public:
291     ParseStatusOr(const ParseStatus& error)  // NOLINT
292         : status_(error), value_() {}
293     ParseStatusOr(const T& value)  // NOLINT
294         : status_(ParseStatus::Success()), value_(value) {}
295     bool ok() const { return status_.ok(); }
296     const T& value() const& {
297       RTC_DCHECK(status_.ok());
298       return value_;
299     }
300     std::string message() const { return status_.message(); }
301     const ParseStatus& status() const { return status_; }
302 
303    private:
304     ParseStatus status_;
305     T value_;
306   };
307 
308   struct LoggedRtpStreamIncoming {
309     LoggedRtpStreamIncoming();
310     LoggedRtpStreamIncoming(const LoggedRtpStreamIncoming&);
311     ~LoggedRtpStreamIncoming();
312     uint32_t ssrc;
313     std::vector<LoggedRtpPacketIncoming> incoming_packets;
314   };
315 
316   struct LoggedRtpStreamOutgoing {
317     LoggedRtpStreamOutgoing();
318     LoggedRtpStreamOutgoing(const LoggedRtpStreamOutgoing&);
319     ~LoggedRtpStreamOutgoing();
320     uint32_t ssrc;
321     std::vector<LoggedRtpPacketOutgoing> outgoing_packets;
322   };
323 
324   struct LoggedRtpStreamView {
325     LoggedRtpStreamView(uint32_t ssrc,
326                         const LoggedRtpPacketIncoming* ptr,
327                         size_t num_elements);
328     LoggedRtpStreamView(uint32_t ssrc,
329                         const LoggedRtpPacketOutgoing* ptr,
330                         size_t num_elements);
331     LoggedRtpStreamView(const LoggedRtpStreamView&);
332     uint32_t ssrc;
333     PacketView<const LoggedRtpPacket> packet_view;
334   };
335 
336   class LogSegment {
337    public:
338     LogSegment(int64_t start_time_us, int64_t stop_time_us)
339         : start_time_us_(start_time_us), stop_time_us_(stop_time_us) {}
340     int64_t start_time_ms() const { return start_time_us_ / 1000; }
341     int64_t start_time_us() const { return start_time_us_; }
342     int64_t stop_time_ms() const { return stop_time_us_ / 1000; }
343     int64_t stop_time_us() const { return stop_time_us_; }
344 
345    private:
346     int64_t start_time_us_;
347     int64_t stop_time_us_;
348   };
349 
350   static webrtc::RtpHeaderExtensionMap GetDefaultHeaderExtensionMap();
351 
352   explicit ParsedRtcEventLog(
353       UnconfiguredHeaderExtensions parse_unconfigured_header_extensions =
354           UnconfiguredHeaderExtensions::kDontParse,
355       bool allow_incomplete_log = false);
356 
357   ~ParsedRtcEventLog();
358 
359   // Clears previously parsed events and resets the ParsedRtcEventLogNew to an
360   // empty state.
361   void Clear();
362 
363   // Reads an RtcEventLog file and returns success if parsing was successful.
364   ParseStatus ParseFile(const std::string& file_name);
365 
366   // Reads an RtcEventLog from a string and returns success if successful.
367   ParseStatus ParseString(const std::string& s);
368 
369   // Reads an RtcEventLog from an istream and returns success if successful.
370   ParseStatus ParseStream(
371       std::istream& stream);  // no-presubmit-check TODO(webrtc:8982)
372 
373   MediaType GetMediaType(uint32_t ssrc, PacketDirection direction) const;
374 
375   // Configured SSRCs.
376   const std::set<uint32_t>& incoming_rtx_ssrcs() const {
377     return incoming_rtx_ssrcs_;
378   }
379 
380   const std::set<uint32_t>& incoming_video_ssrcs() const {
381     return incoming_video_ssrcs_;
382   }
383 
384   const std::set<uint32_t>& incoming_audio_ssrcs() const {
385     return incoming_audio_ssrcs_;
386   }
387 
388   const std::set<uint32_t>& outgoing_rtx_ssrcs() const {
389     return outgoing_rtx_ssrcs_;
390   }
391 
392   const std::set<uint32_t>& outgoing_video_ssrcs() const {
393     return outgoing_video_ssrcs_;
394   }
395 
396   const std::set<uint32_t>& outgoing_audio_ssrcs() const {
397     return outgoing_audio_ssrcs_;
398   }
399 
400   // Stream configurations.
401   const std::vector<LoggedAudioRecvConfig>& audio_recv_configs() const {
402     return audio_recv_configs_;
403   }
404 
405   const std::vector<LoggedAudioSendConfig>& audio_send_configs() const {
406     return audio_send_configs_;
407   }
408 
409   const std::vector<LoggedVideoRecvConfig>& video_recv_configs() const {
410     return video_recv_configs_;
411   }
412 
413   const std::vector<LoggedVideoSendConfig>& video_send_configs() const {
414     return video_send_configs_;
415   }
416 
417   // Beginning and end of log segments.
418   const std::vector<LoggedStartEvent>& start_log_events() const {
419     return start_log_events_;
420   }
421 
422   const std::vector<LoggedStopEvent>& stop_log_events() const {
423     return stop_log_events_;
424   }
425 
426   const std::vector<LoggedAlrStateEvent>& alr_state_events() const {
427     return alr_state_events_;
428   }
429 
430   // Audio
431   const std::map<uint32_t, std::vector<LoggedAudioPlayoutEvent>>&
432   audio_playout_events() const {
433     return audio_playout_events_;
434   }
435 
436   const std::vector<LoggedAudioNetworkAdaptationEvent>&
437   audio_network_adaptation_events() const {
438     return audio_network_adaptation_events_;
439   }
440 
441   // Bandwidth estimation
442   const std::vector<LoggedBweProbeClusterCreatedEvent>&
443   bwe_probe_cluster_created_events() const {
444     return bwe_probe_cluster_created_events_;
445   }
446 
447   const std::vector<LoggedBweProbeFailureEvent>& bwe_probe_failure_events()
448       const {
449     return bwe_probe_failure_events_;
450   }
451 
452   const std::vector<LoggedBweProbeSuccessEvent>& bwe_probe_success_events()
453       const {
454     return bwe_probe_success_events_;
455   }
456 
457   const std::vector<LoggedBweDelayBasedUpdate>& bwe_delay_updates() const {
458     return bwe_delay_updates_;
459   }
460 
461   const std::vector<LoggedBweLossBasedUpdate>& bwe_loss_updates() const {
462     return bwe_loss_updates_;
463   }
464 
465   // DTLS
466   const std::vector<LoggedDtlsTransportState>& dtls_transport_states() const {
467     return dtls_transport_states_;
468   }
469 
470   const std::vector<LoggedDtlsWritableState>& dtls_writable_states() const {
471     return dtls_writable_states_;
472   }
473 
474   // ICE events
475   const std::vector<LoggedIceCandidatePairConfig>& ice_candidate_pair_configs()
476       const {
477     return ice_candidate_pair_configs_;
478   }
479 
480   const std::vector<LoggedIceCandidatePairEvent>& ice_candidate_pair_events()
481       const {
482     return ice_candidate_pair_events_;
483   }
484 
485   const std::vector<LoggedRouteChangeEvent>& route_change_events() const {
486     return route_change_events_;
487   }
488 
489   const std::vector<LoggedRemoteEstimateEvent>& remote_estimate_events() const {
490     return remote_estimate_events_;
491   }
492 
493   // RTP
494   const std::vector<LoggedRtpStreamIncoming>& incoming_rtp_packets_by_ssrc()
495       const {
496     return incoming_rtp_packets_by_ssrc_;
497   }
498 
499   const std::vector<LoggedRtpStreamOutgoing>& outgoing_rtp_packets_by_ssrc()
500       const {
501     return outgoing_rtp_packets_by_ssrc_;
502   }
503 
504   const std::vector<LoggedRtpStreamView>& rtp_packets_by_ssrc(
505       PacketDirection direction) const {
506     if (direction == kIncomingPacket)
507       return incoming_rtp_packet_views_by_ssrc_;
508     else
509       return outgoing_rtp_packet_views_by_ssrc_;
510   }
511 
512   // RTCP
513   const std::vector<LoggedRtcpPacketIncoming>& incoming_rtcp_packets() const {
514     return incoming_rtcp_packets_;
515   }
516 
517   const std::vector<LoggedRtcpPacketOutgoing>& outgoing_rtcp_packets() const {
518     return outgoing_rtcp_packets_;
519   }
520 
521   const std::vector<LoggedRtcpPacketReceiverReport>& receiver_reports(
522       PacketDirection direction) const {
523     if (direction == kIncomingPacket) {
524       return incoming_rr_;
525     } else {
526       return outgoing_rr_;
527     }
528   }
529 
530   const std::vector<LoggedRtcpPacketSenderReport>& sender_reports(
531       PacketDirection direction) const {
532     if (direction == kIncomingPacket) {
533       return incoming_sr_;
534     } else {
535       return outgoing_sr_;
536     }
537   }
538 
539   const std::vector<LoggedRtcpPacketExtendedReports>& extended_reports(
540       PacketDirection direction) const {
541     if (direction == kIncomingPacket) {
542       return incoming_xr_;
543     } else {
544       return outgoing_xr_;
545     }
546   }
547 
548   const std::vector<LoggedRtcpPacketNack>& nacks(
549       PacketDirection direction) const {
550     if (direction == kIncomingPacket) {
551       return incoming_nack_;
552     } else {
553       return outgoing_nack_;
554     }
555   }
556 
557   const std::vector<LoggedRtcpPacketRemb>& rembs(
558       PacketDirection direction) const {
559     if (direction == kIncomingPacket) {
560       return incoming_remb_;
561     } else {
562       return outgoing_remb_;
563     }
564   }
565 
566   const std::vector<LoggedRtcpPacketFir>& firs(
567       PacketDirection direction) const {
568     if (direction == kIncomingPacket) {
569       return incoming_fir_;
570     } else {
571       return outgoing_fir_;
572     }
573   }
574 
575   const std::vector<LoggedRtcpPacketPli>& plis(
576       PacketDirection direction) const {
577     if (direction == kIncomingPacket) {
578       return incoming_pli_;
579     } else {
580       return outgoing_pli_;
581     }
582   }
583 
584   const std::vector<LoggedRtcpPacketTransportFeedback>& transport_feedbacks(
585       PacketDirection direction) const {
586     if (direction == kIncomingPacket) {
587       return incoming_transport_feedback_;
588     } else {
589       return outgoing_transport_feedback_;
590     }
591   }
592 
593   const std::vector<LoggedRtcpPacketLossNotification>& loss_notifications(
594       PacketDirection direction) {
595     if (direction == kIncomingPacket) {
596       return incoming_loss_notification_;
597     } else {
598       return outgoing_loss_notification_;
599     }
600   }
601 
602   const std::vector<LoggedGenericPacketReceived>& generic_packets_received()
603       const {
604     return generic_packets_received_;
605   }
606   const std::vector<LoggedGenericPacketSent>& generic_packets_sent() const {
607     return generic_packets_sent_;
608   }
609 
610   const std::vector<LoggedGenericAckReceived>& generic_acks_received() const {
611     return generic_acks_received_;
612   }
613 
614   int64_t first_timestamp() const { return first_timestamp_; }
615   int64_t last_timestamp() const { return last_timestamp_; }
616 
617   const LogSegment& first_log_segment() const { return first_log_segment_; }
618 
619   std::vector<LoggedPacketInfo> GetPacketInfos(PacketDirection direction) const;
620   std::vector<LoggedPacketInfo> GetIncomingPacketInfos() const {
621     return GetPacketInfos(kIncomingPacket);
622   }
623   std::vector<LoggedPacketInfo> GetOutgoingPacketInfos() const {
624     return GetPacketInfos(kOutgoingPacket);
625   }
626   std::vector<LoggedIceCandidatePairConfig> GetIceCandidates() const;
627   std::vector<LoggedIceEvent> GetIceEvents() const;
628 
629   std::vector<InferredRouteChangeEvent> GetRouteChanges() const;
630 
631  private:
632   ABSL_MUST_USE_RESULT ParseStatus ParseStreamInternal(
633       std::istream& stream);  // no-presubmit-check TODO(webrtc:8982)
634 
635   ABSL_MUST_USE_RESULT ParseStatus
636   StoreParsedLegacyEvent(const rtclog::Event& event);
637 
638   template <typename T>
639   void StoreFirstAndLastTimestamp(const std::vector<T>& v);
640 
641   // Reads the header, direction, header length and packet length from the RTP
642   // event at |index|, and stores the values in the corresponding output
643   // parameters. Each output parameter can be set to nullptr if that value
644   // isn't needed.
645   // NB: The header must have space for at least IP_PACKET_SIZE bytes.
646   ParseStatus GetRtpHeader(const rtclog::Event& event,
647                            PacketDirection* incoming,
648                            uint8_t* header,
649                            size_t* header_length,
650                            size_t* total_length,
651                            int* probe_cluster_id) const;
652 
653   // Returns: a pointer to a header extensions map acquired from parsing
654   // corresponding Audio/Video Sender/Receiver config events.
655   // Warning: if the same SSRC is reused by both video and audio streams during
656   // call, extensions maps may be incorrect (the last one would be returned).
657   const RtpHeaderExtensionMap* GetRtpHeaderExtensionMap(
658       PacketDirection direction,
659       uint32_t ssrc);
660 
661   // Reads packet, direction and packet length from the RTCP event at |index|,
662   // and stores the values in the corresponding output parameters.
663   // Each output parameter can be set to nullptr if that value isn't needed.
664   // NB: The packet must have space for at least IP_PACKET_SIZE bytes.
665   ParseStatus GetRtcpPacket(const rtclog::Event& event,
666                             PacketDirection* incoming,
667                             uint8_t* packet,
668                             size_t* length) const;
669 
670   ParseStatusOr<rtclog::StreamConfig> GetVideoReceiveConfig(
671       const rtclog::Event& event) const;
672   ParseStatusOr<rtclog::StreamConfig> GetVideoSendConfig(
673       const rtclog::Event& event) const;
674   ParseStatusOr<rtclog::StreamConfig> GetAudioReceiveConfig(
675       const rtclog::Event& event) const;
676   ParseStatusOr<rtclog::StreamConfig> GetAudioSendConfig(
677       const rtclog::Event& event) const;
678 
679   ParsedRtcEventLog::ParseStatusOr<LoggedAudioPlayoutEvent> GetAudioPlayout(
680       const rtclog::Event& event) const;
681 
682   ParsedRtcEventLog::ParseStatusOr<LoggedBweLossBasedUpdate>
683   GetLossBasedBweUpdate(const rtclog::Event& event) const;
684 
685   ParsedRtcEventLog::ParseStatusOr<LoggedBweDelayBasedUpdate>
686   GetDelayBasedBweUpdate(const rtclog::Event& event) const;
687 
688   ParsedRtcEventLog::ParseStatusOr<LoggedAudioNetworkAdaptationEvent>
689   GetAudioNetworkAdaptation(const rtclog::Event& event) const;
690 
691   ParsedRtcEventLog::ParseStatusOr<LoggedBweProbeClusterCreatedEvent>
692   GetBweProbeClusterCreated(const rtclog::Event& event) const;
693 
694   ParsedRtcEventLog::ParseStatusOr<LoggedBweProbeFailureEvent>
695   GetBweProbeFailure(const rtclog::Event& event) const;
696 
697   ParsedRtcEventLog::ParseStatusOr<LoggedBweProbeSuccessEvent>
698   GetBweProbeSuccess(const rtclog::Event& event) const;
699 
700   ParsedRtcEventLog::ParseStatusOr<LoggedAlrStateEvent> GetAlrState(
701       const rtclog::Event& event) const;
702 
703   ParsedRtcEventLog::ParseStatusOr<LoggedIceCandidatePairConfig>
704   GetIceCandidatePairConfig(const rtclog::Event& event) const;
705 
706   ParsedRtcEventLog::ParseStatusOr<LoggedIceCandidatePairEvent>
707   GetIceCandidatePairEvent(const rtclog::Event& event) const;
708 
709   // Parsing functions for new format.
710   ParseStatus StoreAlrStateEvent(const rtclog2::AlrState& proto);
711   ParseStatus StoreAudioNetworkAdaptationEvent(
712       const rtclog2::AudioNetworkAdaptations& proto);
713   ParseStatus StoreAudioPlayoutEvent(const rtclog2::AudioPlayoutEvents& proto);
714   ParseStatus StoreAudioRecvConfig(const rtclog2::AudioRecvStreamConfig& proto);
715   ParseStatus StoreAudioSendConfig(const rtclog2::AudioSendStreamConfig& proto);
716   ParseStatus StoreBweDelayBasedUpdate(
717       const rtclog2::DelayBasedBweUpdates& proto);
718   ParseStatus StoreBweLossBasedUpdate(
719       const rtclog2::LossBasedBweUpdates& proto);
720   ParseStatus StoreBweProbeClusterCreated(
721       const rtclog2::BweProbeCluster& proto);
722   ParseStatus StoreBweProbeFailureEvent(
723       const rtclog2::BweProbeResultFailure& proto);
724   ParseStatus StoreBweProbeSuccessEvent(
725       const rtclog2::BweProbeResultSuccess& proto);
726   ParseStatus StoreDtlsTransportState(
727       const rtclog2::DtlsTransportStateEvent& proto);
728   ParseStatus StoreDtlsWritableState(const rtclog2::DtlsWritableState& proto);
729   ParseStatus StoreGenericAckReceivedEvent(
730       const rtclog2::GenericAckReceived& proto);
731   ParseStatus StoreGenericPacketReceivedEvent(
732       const rtclog2::GenericPacketReceived& proto);
733   ParseStatus StoreGenericPacketSentEvent(
734       const rtclog2::GenericPacketSent& proto);
735   ParseStatus StoreIceCandidateEvent(
736       const rtclog2::IceCandidatePairEvent& proto);
737   ParseStatus StoreIceCandidatePairConfig(
738       const rtclog2::IceCandidatePairConfig& proto);
739   ParseStatus StoreIncomingRtcpPackets(
740       const rtclog2::IncomingRtcpPackets& proto);
741   ParseStatus StoreIncomingRtpPackets(const rtclog2::IncomingRtpPackets& proto);
742   ParseStatus StoreOutgoingRtcpPackets(
743       const rtclog2::OutgoingRtcpPackets& proto);
744   ParseStatus StoreOutgoingRtpPackets(const rtclog2::OutgoingRtpPackets& proto);
745   ParseStatus StoreParsedNewFormatEvent(const rtclog2::EventStream& event);
746   ParseStatus StoreRouteChangeEvent(const rtclog2::RouteChange& proto);
747   ParseStatus StoreRemoteEstimateEvent(const rtclog2::RemoteEstimates& proto);
748   ParseStatus StoreStartEvent(const rtclog2::BeginLogEvent& proto);
749   ParseStatus StoreStopEvent(const rtclog2::EndLogEvent& proto);
750   ParseStatus StoreVideoRecvConfig(const rtclog2::VideoRecvStreamConfig& proto);
751   ParseStatus StoreVideoSendConfig(const rtclog2::VideoSendStreamConfig& proto);
752   // End of new parsing functions.
753 
754   struct Stream {
755     Stream(uint32_t ssrc,
756            MediaType media_type,
757            PacketDirection direction,
758            webrtc::RtpHeaderExtensionMap map)
759         : ssrc(ssrc),
760           media_type(media_type),
761           direction(direction),
762           rtp_extensions_map(map) {}
763     uint32_t ssrc;
764     MediaType media_type;
765     PacketDirection direction;
766     webrtc::RtpHeaderExtensionMap rtp_extensions_map;
767   };
768 
769   const UnconfiguredHeaderExtensions parse_unconfigured_header_extensions_;
770   const bool allow_incomplete_logs_;
771 
772   // Make a default extension map for streams without configuration information.
773   // TODO(ivoc): Once configuration of audio streams is stored in the event log,
774   //             this can be removed. Tracking bug: webrtc:6399
775   RtpHeaderExtensionMap default_extension_map_;
776 
777   // Tracks what each stream is configured for. Note that a single SSRC can be
778   // in several sets. For example, the SSRC used for sending video over RTX
779   // will appear in both video_ssrcs_ and rtx_ssrcs_. In the unlikely case that
780   // an SSRC is reconfigured to a different media type mid-call, it will also
781   // appear in multiple sets.
782   std::set<uint32_t> incoming_rtx_ssrcs_;
783   std::set<uint32_t> incoming_video_ssrcs_;
784   std::set<uint32_t> incoming_audio_ssrcs_;
785   std::set<uint32_t> outgoing_rtx_ssrcs_;
786   std::set<uint32_t> outgoing_video_ssrcs_;
787   std::set<uint32_t> outgoing_audio_ssrcs_;
788 
789   // Maps an SSRC to the parsed  RTP headers in that stream. Header extensions
790   // are parsed if the stream has been configured. This is only used for
791   // grouping the events by SSRC during parsing; the events are moved to
792   // incoming_rtp_packets_by_ssrc_ once the parsing is done.
793   std::map<uint32_t, std::vector<LoggedRtpPacketIncoming>>
794       incoming_rtp_packets_map_;
795   std::map<uint32_t, std::vector<LoggedRtpPacketOutgoing>>
796       outgoing_rtp_packets_map_;
797 
798   // RTP headers.
799   std::vector<LoggedRtpStreamIncoming> incoming_rtp_packets_by_ssrc_;
800   std::vector<LoggedRtpStreamOutgoing> outgoing_rtp_packets_by_ssrc_;
801   std::vector<LoggedRtpStreamView> incoming_rtp_packet_views_by_ssrc_;
802   std::vector<LoggedRtpStreamView> outgoing_rtp_packet_views_by_ssrc_;
803 
804   // Raw RTCP packets.
805   std::vector<LoggedRtcpPacketIncoming> incoming_rtcp_packets_;
806   std::vector<LoggedRtcpPacketOutgoing> outgoing_rtcp_packets_;
807 
808   // Parsed RTCP messages. Currently not separated based on SSRC.
809   std::vector<LoggedRtcpPacketReceiverReport> incoming_rr_;
810   std::vector<LoggedRtcpPacketReceiverReport> outgoing_rr_;
811   std::vector<LoggedRtcpPacketSenderReport> incoming_sr_;
812   std::vector<LoggedRtcpPacketSenderReport> outgoing_sr_;
813   std::vector<LoggedRtcpPacketExtendedReports> incoming_xr_;
814   std::vector<LoggedRtcpPacketExtendedReports> outgoing_xr_;
815   std::vector<LoggedRtcpPacketNack> incoming_nack_;
816   std::vector<LoggedRtcpPacketNack> outgoing_nack_;
817   std::vector<LoggedRtcpPacketRemb> incoming_remb_;
818   std::vector<LoggedRtcpPacketRemb> outgoing_remb_;
819   std::vector<LoggedRtcpPacketFir> incoming_fir_;
820   std::vector<LoggedRtcpPacketFir> outgoing_fir_;
821   std::vector<LoggedRtcpPacketPli> incoming_pli_;
822   std::vector<LoggedRtcpPacketPli> outgoing_pli_;
823   std::vector<LoggedRtcpPacketTransportFeedback> incoming_transport_feedback_;
824   std::vector<LoggedRtcpPacketTransportFeedback> outgoing_transport_feedback_;
825   std::vector<LoggedRtcpPacketLossNotification> incoming_loss_notification_;
826   std::vector<LoggedRtcpPacketLossNotification> outgoing_loss_notification_;
827 
828   std::vector<LoggedStartEvent> start_log_events_;
829   std::vector<LoggedStopEvent> stop_log_events_;
830 
831   std::vector<LoggedAlrStateEvent> alr_state_events_;
832 
833   std::map<uint32_t, std::vector<LoggedAudioPlayoutEvent>>
834       audio_playout_events_;
835 
836   std::vector<LoggedAudioNetworkAdaptationEvent>
837       audio_network_adaptation_events_;
838 
839   std::vector<LoggedBweProbeClusterCreatedEvent>
840       bwe_probe_cluster_created_events_;
841 
842   std::vector<LoggedBweProbeFailureEvent> bwe_probe_failure_events_;
843   std::vector<LoggedBweProbeSuccessEvent> bwe_probe_success_events_;
844 
845   std::vector<LoggedBweDelayBasedUpdate> bwe_delay_updates_;
846   std::vector<LoggedBweLossBasedUpdate> bwe_loss_updates_;
847 
848   std::vector<LoggedDtlsTransportState> dtls_transport_states_;
849   std::vector<LoggedDtlsWritableState> dtls_writable_states_;
850 
851   std::vector<LoggedIceCandidatePairConfig> ice_candidate_pair_configs_;
852   std::vector<LoggedIceCandidatePairEvent> ice_candidate_pair_events_;
853 
854   std::vector<LoggedAudioRecvConfig> audio_recv_configs_;
855   std::vector<LoggedAudioSendConfig> audio_send_configs_;
856   std::vector<LoggedVideoRecvConfig> video_recv_configs_;
857   std::vector<LoggedVideoSendConfig> video_send_configs_;
858 
859   std::vector<LoggedGenericPacketReceived> generic_packets_received_;
860   std::vector<LoggedGenericPacketSent> generic_packets_sent_;
861   std::vector<LoggedGenericAckReceived> generic_acks_received_;
862 
863   std::vector<LoggedRouteChangeEvent> route_change_events_;
864   std::vector<LoggedRemoteEstimateEvent> remote_estimate_events_;
865 
866   uint8_t last_incoming_rtcp_packet_[IP_PACKET_SIZE];
867   uint8_t last_incoming_rtcp_packet_length_;
868 
869   int64_t first_timestamp_;
870   int64_t last_timestamp_;
871 
872   LogSegment first_log_segment_ =
873       LogSegment(0, std::numeric_limits<int64_t>::max());
874 
875   // The extension maps are mutable to allow us to insert the default
876   // configuration when parsing an RTP header for an unconfigured stream.
877   // TODO(terelius): This is only used for the legacy format. Remove once we've
878   // fully transitioned to the new format.
879   mutable std::map<uint32_t, webrtc::RtpHeaderExtensionMap>
880       incoming_rtp_extensions_maps_;
881   mutable std::map<uint32_t, webrtc::RtpHeaderExtensionMap>
882       outgoing_rtp_extensions_maps_;
883 };
884 
885 struct MatchedSendArrivalTimes {
886   static constexpr int64_t kNotReceived = -1;
887 
888   MatchedSendArrivalTimes(int64_t fb, int64_t tx, int64_t rx, int64_t ps)
889       : feedback_arrival_time_ms(fb),
890         send_time_ms(tx),
891         arrival_time_ms(rx),
892         payload_size(ps) {}
893 
894   int64_t feedback_arrival_time_ms;
895   int64_t send_time_ms;
896   int64_t arrival_time_ms;  // kNotReceived for lost packets.
897   int64_t payload_size;
898 };
899 const std::vector<MatchedSendArrivalTimes> GetNetworkTrace(
900     const ParsedRtcEventLog& parsed_log);
901 
902 }  // namespace webrtc
903 
904 #endif  // LOGGING_RTC_EVENT_LOG_RTC_EVENT_LOG_PARSER_H_
905