1 /*
2  *  Copyright (c) 2016 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 <map>
14 #include <string>
15 #include <utility>  // pair
16 #include <vector>
17 
18 #include "call/video_receive_stream.h"
19 #include "call/video_send_stream.h"
20 #include "logging/rtc_event_log/events/rtc_event_probe_result_failure.h"
21 #include "logging/rtc_event_log/rtc_event_log.h"
22 #include "logging/rtc_event_log/rtc_stream_config.h"
23 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
24 #include "modules/rtp_rtcp/source/byte_io.h"
25 #include "rtc_base/ignore_wundef.h"
26 
27 // Files generated at build-time by the protobuf compiler.
28 RTC_PUSH_IGNORING_WUNDEF()
29 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD
30 #include "external/webrtc/webrtc/logging/rtc_event_log/rtc_event_log.pb.h"
31 #else
32 #include "logging/rtc_event_log/rtc_event_log.pb.h"
33 #endif
RTC_POP_IGNORING_WUNDEF()34 RTC_POP_IGNORING_WUNDEF()
35 
36 namespace webrtc {
37 
38 enum class BandwidthUsage;
39 enum class MediaType;
40 
41 struct AudioEncoderRuntimeConfig;
42 
43 class ParsedRtcEventLog {
44   friend class RtcEventLogTestHelper;
45 
46  public:
47   struct BweProbeClusterCreatedEvent {
48     uint64_t timestamp;
49     uint32_t id;
50     uint64_t bitrate_bps;
51     uint32_t min_packets;
52     uint32_t min_bytes;
53   };
54 
55   struct BweProbeResultEvent {
56     uint64_t timestamp;
57     uint32_t id;
58     rtc::Optional<uint64_t> bitrate_bps;
59     rtc::Optional<ProbeFailureReason> failure_reason;
60   };
61 
62   struct BweDelayBasedUpdate {
63     uint64_t timestamp;
64     int32_t bitrate_bps;
65     BandwidthUsage detector_state;
66   };
67 
68   enum EventType {
69     UNKNOWN_EVENT = 0,
70     LOG_START = 1,
71     LOG_END = 2,
72     RTP_EVENT = 3,
73     RTCP_EVENT = 4,
74     AUDIO_PLAYOUT_EVENT = 5,
75     LOSS_BASED_BWE_UPDATE = 6,
76     DELAY_BASED_BWE_UPDATE = 7,
77     VIDEO_RECEIVER_CONFIG_EVENT = 8,
78     VIDEO_SENDER_CONFIG_EVENT = 9,
79     AUDIO_RECEIVER_CONFIG_EVENT = 10,
80     AUDIO_SENDER_CONFIG_EVENT = 11,
81     AUDIO_NETWORK_ADAPTATION_EVENT = 16,
82     BWE_PROBE_CLUSTER_CREATED_EVENT = 17,
83     BWE_PROBE_RESULT_EVENT = 18
84   };
85 
86   enum class MediaType { ANY, AUDIO, VIDEO, DATA };
87 
88   // Reads an RtcEventLog file and returns true if parsing was successful.
89   bool ParseFile(const std::string& file_name);
90 
91   // Reads an RtcEventLog from a string and returns true if successful.
92   bool ParseString(const std::string& s);
93 
94   // Reads an RtcEventLog from an istream and returns true if successful.
95   bool ParseStream(std::istream& stream);
96 
97   // Returns the number of events in an EventStream.
98   size_t GetNumberOfEvents() const;
99 
100   // Reads the arrival timestamp (in microseconds) from a rtclog::Event.
101   int64_t GetTimestamp(size_t index) const;
102 
103   // Reads the event type of the rtclog::Event at |index|.
104   EventType GetEventType(size_t index) const;
105 
106   // Reads the header, direction, header length and packet length from the RTP
107   // event at |index|, and stores the values in the corresponding output
108   // parameters. Each output parameter can be set to nullptr if that value
109   // isn't needed.
110   // NB: The header must have space for at least IP_PACKET_SIZE bytes.
111   // Returns: a pointer to a header extensions map acquired from parsing
112   // corresponding Audio/Video Sender/Receiver config events.
113   // Warning: if the same SSRC is reused by both video and audio streams during
114   // call, extensions maps may be incorrect (the last one would be returned).
115   webrtc::RtpHeaderExtensionMap* GetRtpHeader(size_t index,
116                                               PacketDirection* incoming,
117                                               uint8_t* header,
118                                               size_t* header_length,
119                                               size_t* total_length,
120                                               int* probe_cluster_id) const;
121 
122   // Reads packet, direction and packet length from the RTCP event at |index|,
123   // and stores the values in the corresponding output parameters.
124   // Each output parameter can be set to nullptr if that value isn't needed.
125   // NB: The packet must have space for at least IP_PACKET_SIZE bytes.
126   void GetRtcpPacket(size_t index,
127                      PacketDirection* incoming,
128                      uint8_t* packet,
129                      size_t* length) const;
130 
131   // Reads a video receive config event to a StreamConfig struct.
132   // Only the fields that are stored in the protobuf will be written.
133   rtclog::StreamConfig GetVideoReceiveConfig(size_t index) const;
134 
135   // Reads a video send config event to a StreamConfig struct. If the proto
136   // contains multiple SSRCs and RTX SSRCs (this used to be the case for
137   // simulcast streams) then we return one StreamConfig per SSRC,RTX_SSRC pair.
138   // Only the fields that are stored in the protobuf will be written.
139   std::vector<rtclog::StreamConfig> GetVideoSendConfig(size_t index) const;
140 
141   // Reads a audio receive config event to a StreamConfig struct.
142   // Only the fields that are stored in the protobuf will be written.
143   rtclog::StreamConfig GetAudioReceiveConfig(size_t index) const;
144 
145   // Reads a config event to a StreamConfig struct.
146   // Only the fields that are stored in the protobuf will be written.
147   rtclog::StreamConfig GetAudioSendConfig(size_t index) const;
148 
149   // Reads the SSRC from the audio playout event at |index|. The SSRC is stored
150   // in the output parameter ssrc. The output parameter can be set to nullptr
151   // and in that case the function only asserts that the event is well formed.
152   void GetAudioPlayout(size_t index, uint32_t* ssrc) const;
153 
154   // Reads bitrate, fraction loss (as defined in RFC 1889) and total number of
155   // expected packets from the loss based BWE event at |index| and stores the
156   // values in
157   // the corresponding output parameters. Each output parameter can be set to
158   // nullptr if that
159   // value isn't needed.
160   void GetLossBasedBweUpdate(size_t index,
161                              int32_t* bitrate_bps,
162                              uint8_t* fraction_loss,
163                              int32_t* total_packets) const;
164 
165   // Reads bitrate and detector_state from the delay based BWE event at |index|
166   // and stores the values in the corresponding output parameters. Each output
167   // parameter can be set to nullptr if that
168   // value isn't needed.
169   BweDelayBasedUpdate GetDelayBasedBweUpdate(size_t index) const;
170 
171   // Reads a audio network adaptation event to a (non-NULL)
172   // AudioEncoderRuntimeConfig struct. Only the fields that are
173   // stored in the protobuf will be written.
174   void GetAudioNetworkAdaptation(size_t index,
175                                  AudioEncoderRuntimeConfig* config) const;
176 
177   BweProbeClusterCreatedEvent GetBweProbeClusterCreated(size_t index) const;
178 
179   BweProbeResultEvent GetBweProbeResult(size_t index) const;
180 
181   MediaType GetMediaType(uint32_t ssrc, PacketDirection direction) const;
182 
183  private:
184   rtclog::StreamConfig GetVideoReceiveConfig(const rtclog::Event& event) const;
185   std::vector<rtclog::StreamConfig> GetVideoSendConfig(
186       const rtclog::Event& event) const;
187   rtclog::StreamConfig GetAudioReceiveConfig(const rtclog::Event& event) const;
188   rtclog::StreamConfig GetAudioSendConfig(const rtclog::Event& event) const;
189 
190   std::vector<rtclog::Event> events_;
191 
192   struct Stream {
193     Stream(uint32_t ssrc,
194            MediaType media_type,
195            webrtc::PacketDirection direction,
196            webrtc::RtpHeaderExtensionMap map)
197         : ssrc(ssrc),
198           media_type(media_type),
199           direction(direction),
200           rtp_extensions_map(map) {}
201     uint32_t ssrc;
202     MediaType media_type;
203     webrtc::PacketDirection direction;
204     webrtc::RtpHeaderExtensionMap rtp_extensions_map;
205   };
206 
207   // All configured streams found in the event log.
208   std::vector<Stream> streams_;
209 
210   // To find configured extensions map for given stream, what are needed to
211   // parse a header.
212   typedef std::pair<uint32_t, webrtc::PacketDirection> StreamId;
213   std::map<StreamId, webrtc::RtpHeaderExtensionMap*> rtp_extensions_maps_;
214 };
215 
216 }  // namespace webrtc
217 
218 #endif  // LOGGING_RTC_EVENT_LOG_RTC_EVENT_LOG_PARSER_H_
219