xref: /aosp_15_r20/external/webrtc/logging/rtc_event_log/encoder/rtc_event_log_encoder_v3.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 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 
11 #include "logging/rtc_event_log/encoder/rtc_event_log_encoder_v3.h"
12 
13 #include <string>
14 #include <vector>
15 
16 #include "absl/types/optional.h"
17 #include "logging/rtc_event_log/encoder/rtc_event_log_encoder_common.h"
18 #include "logging/rtc_event_log/encoder/var_int.h"
19 #include "logging/rtc_event_log/events/rtc_event_alr_state.h"
20 #include "logging/rtc_event_log/events/rtc_event_audio_network_adaptation.h"
21 #include "logging/rtc_event_log/events/rtc_event_audio_playout.h"
22 #include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h"
23 #include "logging/rtc_event_log/events/rtc_event_audio_send_stream_config.h"
24 #include "logging/rtc_event_log/events/rtc_event_begin_log.h"
25 #include "logging/rtc_event_log/events/rtc_event_bwe_update_delay_based.h"
26 #include "logging/rtc_event_log/events/rtc_event_bwe_update_loss_based.h"
27 #include "logging/rtc_event_log/events/rtc_event_dtls_transport_state.h"
28 #include "logging/rtc_event_log/events/rtc_event_dtls_writable_state.h"
29 #include "logging/rtc_event_log/events/rtc_event_end_log.h"
30 #include "logging/rtc_event_log/events/rtc_event_frame_decoded.h"
31 #include "logging/rtc_event_log/events/rtc_event_generic_ack_received.h"
32 #include "logging/rtc_event_log/events/rtc_event_generic_packet_received.h"
33 #include "logging/rtc_event_log/events/rtc_event_generic_packet_sent.h"
34 #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair.h"
35 #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h"
36 #include "logging/rtc_event_log/events/rtc_event_probe_cluster_created.h"
37 #include "logging/rtc_event_log/events/rtc_event_probe_result_failure.h"
38 #include "logging/rtc_event_log/events/rtc_event_probe_result_success.h"
39 #include "logging/rtc_event_log/events/rtc_event_remote_estimate.h"
40 #include "logging/rtc_event_log/events/rtc_event_route_change.h"
41 #include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h"
42 #include "logging/rtc_event_log/events/rtc_event_rtcp_packet_outgoing.h"
43 #include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h"
44 #include "logging/rtc_event_log/events/rtc_event_rtp_packet_outgoing.h"
45 #include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h"
46 #include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h"
47 #include "rtc_base/checks.h"
48 #include "rtc_base/logging.h"
49 
50 namespace webrtc {
51 
EncodeLogStart(int64_t timestamp_us,int64_t utc_time_us)52 std::string RtcEventLogEncoderV3::EncodeLogStart(int64_t timestamp_us,
53                                                  int64_t utc_time_us) {
54   std::unique_ptr<RtcEventBeginLog> begin_log =
55       std::make_unique<RtcEventBeginLog>(Timestamp::Micros(timestamp_us),
56                                          Timestamp::Micros(utc_time_us));
57   std::vector<const RtcEvent*> batch;
58   batch.push_back(begin_log.get());
59 
60   std::string encoded_event = RtcEventBeginLog::Encode(batch);
61 
62   return encoded_event;
63 }
64 
EncodeLogEnd(int64_t timestamp_us)65 std::string RtcEventLogEncoderV3::EncodeLogEnd(int64_t timestamp_us) {
66   std::unique_ptr<RtcEventEndLog> end_log =
67       std::make_unique<RtcEventEndLog>(Timestamp::Micros(timestamp_us));
68   std::vector<const RtcEvent*> batch;
69   batch.push_back(end_log.get());
70 
71   std::string encoded_event = RtcEventEndLog::Encode(batch);
72 
73   return encoded_event;
74 }
75 
RtcEventLogEncoderV3()76 RtcEventLogEncoderV3::RtcEventLogEncoderV3() {
77   encoders_[RtcEvent::Type::AlrStateEvent] = RtcEventAlrState::Encode;
78   encoders_[RtcEvent::Type::AudioNetworkAdaptation] =
79       RtcEventAudioNetworkAdaptation::Encode;
80   encoders_[RtcEvent::Type::AudioPlayout] = RtcEventAudioPlayout::Encode;
81   encoders_[RtcEvent::Type::AudioReceiveStreamConfig] =
82       RtcEventAudioReceiveStreamConfig::Encode;
83   encoders_[RtcEvent::Type::AudioSendStreamConfig] =
84       RtcEventAudioSendStreamConfig::Encode;
85   encoders_[RtcEvent::Type::BweUpdateDelayBased] =
86       RtcEventBweUpdateDelayBased::Encode;
87   encoders_[RtcEvent::Type::BweUpdateLossBased] =
88       RtcEventBweUpdateLossBased::Encode;
89   encoders_[RtcEvent::Type::DtlsTransportState] =
90       RtcEventDtlsTransportState::Encode;
91   encoders_[RtcEvent::Type::DtlsWritableState] =
92       RtcEventDtlsWritableState::Encode;
93   encoders_[RtcEvent::Type::FrameDecoded] = RtcEventFrameDecoded::Encode;
94   encoders_[RtcEvent::Type::GenericAckReceived] =
95       RtcEventGenericAckReceived::Encode;
96   encoders_[RtcEvent::Type::GenericPacketReceived] =
97       RtcEventGenericPacketReceived::Encode;
98   encoders_[RtcEvent::Type::GenericPacketSent] =
99       RtcEventGenericPacketSent::Encode;
100   encoders_[RtcEvent::Type::IceCandidatePairConfig] =
101       RtcEventIceCandidatePairConfig::Encode;
102   encoders_[RtcEvent::Type::IceCandidatePairEvent] =
103       RtcEventIceCandidatePair::Encode;
104   encoders_[RtcEvent::Type::ProbeClusterCreated] =
105       RtcEventProbeClusterCreated::Encode;
106   encoders_[RtcEvent::Type::ProbeResultFailure] =
107       RtcEventProbeResultFailure::Encode;
108   encoders_[RtcEvent::Type::ProbeResultSuccess] =
109       RtcEventProbeResultSuccess::Encode;
110   encoders_[RtcEvent::Type::RemoteEstimateEvent] =
111       RtcEventRemoteEstimate::Encode;
112   encoders_[RtcEvent::Type::RouteChangeEvent] = RtcEventRouteChange::Encode;
113   encoders_[RtcEvent::Type::RtcpPacketIncoming] =
114       RtcEventRtcpPacketIncoming::Encode;
115   encoders_[RtcEvent::Type::RtcpPacketOutgoing] =
116       RtcEventRtcpPacketOutgoing::Encode;
117   encoders_[RtcEvent::Type::RtpPacketIncoming] =
118       RtcEventRtpPacketIncoming::Encode;
119   encoders_[RtcEvent::Type::RtpPacketOutgoing] =
120       RtcEventRtpPacketOutgoing::Encode;
121   encoders_[RtcEvent::Type::VideoReceiveStreamConfig] =
122       RtcEventVideoReceiveStreamConfig::Encode;
123   encoders_[RtcEvent::Type::VideoSendStreamConfig] =
124       RtcEventVideoSendStreamConfig::Encode;
125 }
126 
EncodeBatch(std::deque<std::unique_ptr<RtcEvent>>::const_iterator begin,std::deque<std::unique_ptr<RtcEvent>>::const_iterator end)127 std::string RtcEventLogEncoderV3::EncodeBatch(
128     std::deque<std::unique_ptr<RtcEvent>>::const_iterator begin,
129     std::deque<std::unique_ptr<RtcEvent>>::const_iterator end) {
130   struct EventGroupKey {
131     // Events are grouped by event type. For compression efficiency,
132     // events can optionally have a secondary key, in most cases the
133     // SSRC.
134     RtcEvent::Type type;
135     uint32_t secondary_group_key;
136 
137     bool operator<(EventGroupKey other) const {
138       return type < other.type ||
139              (type == other.type &&
140               secondary_group_key < other.secondary_group_key);
141     }
142   };
143 
144   std::map<EventGroupKey, std::vector<const RtcEvent*>> event_groups;
145 
146   for (auto it = begin; it != end; ++it) {
147     event_groups[{(*it)->GetType(), (*it)->GetGroupKey()}].push_back(it->get());
148   }
149 
150   std::string encoded_output;
151   for (auto& kv : event_groups) {
152     auto it = encoders_.find(kv.first.type);
153     RTC_DCHECK(it != encoders_.end());
154     if (it != encoders_.end()) {
155       auto& encoder = it->second;
156       // TODO(terelius): Use some "string builder" or preallocate?
157       encoded_output += encoder(kv.second);
158     }
159   }
160 
161   return encoded_output;
162 }
163 
164 }  // namespace webrtc
165