tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

rtc_event_processor.cc (2219B)


      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 "logging/rtc_event_log/rtc_event_processor.h"
     11 
     12 #include <algorithm>
     13 #include <cstdint>
     14 #include <optional>
     15 
     16 #include "rtc_base/numerics/sequence_number_util.h"
     17 
     18 namespace webrtc {
     19 
     20 RtcEventProcessor::RtcEventProcessor() = default;
     21 RtcEventProcessor::~RtcEventProcessor() = default;
     22 
     23 void RtcEventProcessor::ProcessEventsInOrder() {
     24  // `event_lists_` is a min-heap of lists ordered by the timestamp of the
     25  // first element in the list. We therefore process the first element of the
     26  // first list, then reinsert the remainder of that list into the heap
     27  // if the list still contains unprocessed elements.
     28  std::make_heap(event_lists_.begin(), event_lists_.end(), Cmp);
     29 
     30  while (!event_lists_.empty()) {
     31    event_lists_.front()->ProcessNext();
     32    std::pop_heap(event_lists_.begin(), event_lists_.end(), Cmp);
     33    if (event_lists_.back()->IsEmpty()) {
     34      event_lists_.pop_back();
     35    } else {
     36      std::push_heap(event_lists_.begin(), event_lists_.end(), Cmp);
     37    }
     38  }
     39 }
     40 
     41 bool RtcEventProcessor::Cmp(const RtcEventProcessor::ListPtrType& a,
     42                            const RtcEventProcessor::ListPtrType& b) {
     43  int64_t time_diff = a->GetNextTime() - b->GetNextTime();
     44  if (time_diff != 0)
     45    return time_diff > 0;
     46 
     47  if (a->GetTypeOrder() != b->GetTypeOrder())
     48    return a->GetTypeOrder() > b->GetTypeOrder();
     49 
     50  std::optional<uint16_t> wrapped_seq_num_a = a->GetTransportSeqNum();
     51  std::optional<uint16_t> wrapped_seq_num_b = b->GetTransportSeqNum();
     52  if (wrapped_seq_num_a && wrapped_seq_num_b) {
     53    return AheadOf<uint16_t>(*wrapped_seq_num_a, *wrapped_seq_num_b);
     54  } else if (wrapped_seq_num_a.has_value() != wrapped_seq_num_b.has_value()) {
     55    return wrapped_seq_num_a.has_value();
     56  }
     57 
     58  return a->GetInsertionOrder() > b->GetInsertionOrder();
     59 }
     60 
     61 }  // namespace webrtc