Sebastian Jansson | 1175ae0 | 2019-03-13 08:56:58 +0100 | [diff] [blame] | 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 | #include "rtc_tools/event_log_visualizer/log_simulation.h" |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <utility> |
| 14 | |
| 15 | #include "logging/rtc_event_log/rtc_event_processor.h" |
| 16 | #include "modules/rtp_rtcp/source/time_util.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | LogBasedNetworkControllerSimulation::LogBasedNetworkControllerSimulation( |
| 21 | std::unique_ptr<NetworkControllerFactoryInterface> factory, |
| 22 | std::function<void(const NetworkControlUpdate&, Timestamp)> update_handler) |
| 23 | : update_handler_(update_handler), factory_(std::move(factory)) {} |
| 24 | |
| 25 | LogBasedNetworkControllerSimulation::~LogBasedNetworkControllerSimulation() {} |
| 26 | |
| 27 | void LogBasedNetworkControllerSimulation::HandleStateUpdate( |
| 28 | const NetworkControlUpdate& update) { |
| 29 | update_handler_(update, current_time_); |
| 30 | } |
| 31 | |
| 32 | void LogBasedNetworkControllerSimulation::ProcessUntil(Timestamp to_time) { |
| 33 | if (last_process_.IsInfinite()) { |
| 34 | NetworkControllerConfig config; |
| 35 | config.constraints.at_time = to_time; |
| 36 | config.constraints.min_data_rate = DataRate::kbps(30); |
| 37 | config.constraints.starting_rate = DataRate::kbps(300); |
Sebastian Jansson | 2db5fc0 | 2019-04-30 14:17:45 +0200 | [diff] [blame] | 38 | config.event_log = &null_event_log_; |
Sebastian Jansson | 1175ae0 | 2019-03-13 08:56:58 +0100 | [diff] [blame] | 39 | controller_ = factory_->Create(config); |
| 40 | } |
| 41 | if (last_process_.IsInfinite() || |
| 42 | to_time - last_process_ > TimeDelta::seconds(1)) { |
| 43 | last_process_ = to_time; |
| 44 | current_time_ = to_time; |
| 45 | ProcessInterval msg; |
| 46 | msg.at_time = to_time; |
| 47 | HandleStateUpdate(controller_->OnProcessInterval(msg)); |
| 48 | } else { |
| 49 | while (last_process_ + factory_->GetProcessInterval() <= to_time) { |
| 50 | last_process_ += factory_->GetProcessInterval(); |
| 51 | current_time_ = last_process_; |
| 52 | ProcessInterval msg; |
| 53 | msg.at_time = current_time_; |
| 54 | HandleStateUpdate(controller_->OnProcessInterval(msg)); |
| 55 | } |
| 56 | current_time_ = to_time; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void LogBasedNetworkControllerSimulation::OnProbeCreated( |
| 61 | const LoggedBweProbeClusterCreatedEvent& probe_cluster) { |
| 62 | pending_probes_.push_back({probe_cluster, 0, 0}); |
| 63 | } |
| 64 | |
| 65 | void LogBasedNetworkControllerSimulation::OnPacketSent( |
| 66 | const LoggedPacketInfo& packet) { |
| 67 | ProcessUntil(packet.log_packet_time); |
| 68 | if (packet.has_transport_seq_no) { |
| 69 | PacedPacketInfo probe_info; |
| 70 | if (!pending_probes_.empty() && |
| 71 | packet.media_type == LoggedMediaType::kVideo) { |
| 72 | auto& probe = pending_probes_.front(); |
| 73 | probe_info.probe_cluster_id = probe.event.id; |
| 74 | probe_info.send_bitrate_bps = probe.event.bitrate_bps; |
| 75 | probe_info.probe_cluster_min_bytes = probe.event.min_bytes; |
| 76 | probe_info.probe_cluster_min_probes = probe.event.min_packets; |
| 77 | probe.packets_sent++; |
| 78 | probe.bytes_sent += packet.size + packet.overhead; |
| 79 | if (probe.bytes_sent >= probe.event.min_bytes && |
| 80 | probe.packets_sent >= probe.event.min_packets) { |
| 81 | pending_probes_.pop_front(); |
| 82 | } |
| 83 | } |
Erik Språng | 30a276b | 2019-04-23 12:00:11 +0200 | [diff] [blame] | 84 | |
| 85 | RtpPacketSendInfo packet_info; |
| 86 | packet_info.ssrc = packet.ssrc; |
| 87 | packet_info.transport_sequence_number = packet.transport_seq_no; |
| 88 | packet_info.rtp_sequence_number = packet.stream_seq_no; |
| 89 | packet_info.has_rtp_sequence_number = true; |
| 90 | packet_info.length = packet.size; |
| 91 | packet_info.pacing_info = probe_info; |
| 92 | transport_feedback_.AddPacket(packet_info, packet.overhead, |
Sebastian Jansson | 1175ae0 | 2019-03-13 08:56:58 +0100 | [diff] [blame] | 93 | packet.log_packet_time); |
| 94 | } |
| 95 | rtc::SentPacket sent_packet; |
| 96 | sent_packet.send_time_ms = packet.log_packet_time.ms(); |
| 97 | sent_packet.info.included_in_allocation = true; |
| 98 | sent_packet.info.packet_size_bytes = packet.size + packet.overhead; |
| 99 | if (packet.has_transport_seq_no) { |
| 100 | sent_packet.packet_id = packet.transport_seq_no; |
| 101 | sent_packet.info.included_in_feedback = true; |
| 102 | } |
| 103 | auto msg = transport_feedback_.ProcessSentPacket(sent_packet); |
| 104 | if (msg) |
| 105 | HandleStateUpdate(controller_->OnSentPacket(*msg)); |
| 106 | } |
| 107 | |
| 108 | void LogBasedNetworkControllerSimulation::OnFeedback( |
| 109 | const LoggedRtcpPacketTransportFeedback& feedback) { |
| 110 | auto feedback_time = Timestamp::ms(feedback.log_time_ms()); |
| 111 | ProcessUntil(feedback_time); |
| 112 | auto msg = transport_feedback_.ProcessTransportFeedback( |
| 113 | feedback.transport_feedback, feedback_time); |
| 114 | if (msg) |
| 115 | HandleStateUpdate(controller_->OnTransportPacketsFeedback(*msg)); |
| 116 | } |
| 117 | |
| 118 | void LogBasedNetworkControllerSimulation::OnReceiverReport( |
| 119 | const LoggedRtcpPacketReceiverReport& report) { |
| 120 | if (report.rr.report_blocks().empty()) |
| 121 | return; |
| 122 | auto report_time = Timestamp::ms(report.log_time_ms()); |
| 123 | ProcessUntil(report_time); |
| 124 | int packets_delta = 0; |
| 125 | int lost_delta = 0; |
| 126 | for (auto& block : report.rr.report_blocks()) { |
| 127 | auto it = last_report_blocks_.find(block.source_ssrc()); |
| 128 | if (it != last_report_blocks_.end()) { |
| 129 | packets_delta += |
| 130 | block.extended_high_seq_num() - it->second.extended_high_seq_num(); |
| 131 | lost_delta += block.cumulative_lost() - it->second.cumulative_lost(); |
| 132 | } |
| 133 | last_report_blocks_[block.source_ssrc()] = block; |
| 134 | } |
| 135 | if (packets_delta > lost_delta) { |
| 136 | TransportLossReport msg; |
| 137 | msg.packets_lost_delta = lost_delta; |
| 138 | msg.packets_received_delta = packets_delta - lost_delta; |
| 139 | msg.receive_time = report_time; |
| 140 | msg.start_time = last_report_block_time_; |
| 141 | msg.end_time = report_time; |
| 142 | last_report_block_time_ = report_time; |
| 143 | HandleStateUpdate(controller_->OnTransportLossReport(msg)); |
| 144 | } |
| 145 | |
| 146 | TimeDelta rtt = TimeDelta::PlusInfinity(); |
| 147 | for (auto& rb : report.rr.report_blocks()) { |
| 148 | if (rb.last_sr()) { |
| 149 | uint32_t receive_time_ntp = |
| 150 | CompactNtp(TimeMicrosToNtp(report.log_time_us())); |
| 151 | uint32_t rtt_ntp = |
| 152 | receive_time_ntp - rb.delay_since_last_sr() - rb.last_sr(); |
| 153 | rtt = std::min(rtt, TimeDelta::ms(CompactNtpRttToMs(rtt_ntp))); |
| 154 | } |
| 155 | } |
| 156 | if (rtt.IsFinite()) { |
| 157 | RoundTripTimeUpdate msg; |
| 158 | msg.receive_time = report_time; |
| 159 | msg.round_trip_time = rtt; |
| 160 | HandleStateUpdate(controller_->OnRoundTripTimeUpdate(msg)); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void LogBasedNetworkControllerSimulation::OnIceConfig( |
| 165 | const LoggedIceCandidatePairConfig& candidate) { |
| 166 | if (candidate.type == IceCandidatePairConfigType::kSelected) { |
| 167 | auto log_time = Timestamp::us(candidate.log_time_us()); |
| 168 | ProcessUntil(log_time); |
| 169 | NetworkRouteChange msg; |
| 170 | msg.at_time = log_time; |
| 171 | msg.constraints.min_data_rate = DataRate::kbps(30); |
| 172 | msg.constraints.starting_rate = DataRate::kbps(300); |
| 173 | msg.constraints.at_time = log_time; |
| 174 | HandleStateUpdate(controller_->OnNetworkRouteChange(msg)); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void LogBasedNetworkControllerSimulation::ProcessEventsInLog( |
| 179 | const ParsedRtcEventLog& parsed_log_) { |
| 180 | auto packet_infos = parsed_log_.GetOutgoingPacketInfos(); |
| 181 | RtcEventProcessor processor; |
| 182 | processor.AddEvents( |
| 183 | parsed_log_.bwe_probe_cluster_created_events(), |
| 184 | [this](const LoggedBweProbeClusterCreatedEvent& probe_cluster) { |
| 185 | OnProbeCreated(probe_cluster); |
| 186 | }); |
| 187 | processor.AddEvents(packet_infos, [this](const LoggedPacketInfo& packet) { |
| 188 | OnPacketSent(packet); |
| 189 | }); |
| 190 | processor.AddEvents( |
| 191 | parsed_log_.transport_feedbacks(PacketDirection::kIncomingPacket), |
| 192 | [this](const LoggedRtcpPacketTransportFeedback& feedback) { |
| 193 | OnFeedback(feedback); |
| 194 | }); |
| 195 | processor.AddEvents( |
| 196 | parsed_log_.receiver_reports(PacketDirection::kIncomingPacket), |
| 197 | [this](const LoggedRtcpPacketReceiverReport& report) { |
| 198 | OnReceiverReport(report); |
| 199 | }); |
| 200 | processor.AddEvents(parsed_log_.ice_candidate_pair_configs(), |
| 201 | [this](const LoggedIceCandidatePairConfig& candidate) { |
| 202 | OnIceConfig(candidate); |
| 203 | }); |
| 204 | processor.ProcessEventsInOrder(); |
| 205 | } |
| 206 | |
| 207 | } // namespace webrtc |