File: webrtc_event_log_data.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (75 lines) | stat: -rw-r--r-- 2,878 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_PROTOCOL_WEBRTC_EVENT_LOG_DATA_H_
#define REMOTING_PROTOCOL_WEBRTC_EVENT_LOG_DATA_H_

#include <cstdint>
#include <string_view>
#include <vector>

#include "base/containers/circular_deque.h"

namespace remoting::protocol {

// A data store which records the most recent RTC event log data. This is
// written to by an RTCEventLogOutput instance, which is owned by the
// PeerConnection (but the data itself is owned by the caller). In order to
// keep the most recent events (but keep the amount of memory-allocations
// reasonable), the data is maintained as a circular list of sections.
class WebrtcEventLogData {
 public:
  using LogSection = std::vector<std::uint8_t>;

  WebrtcEventLogData();
  ~WebrtcEventLogData();

  WebrtcEventLogData(const WebrtcEventLogData&) = delete;
  WebrtcEventLogData& operator=(const WebrtcEventLogData&) = delete;

  // Allows tests to lower the storage limits.
  void SetMaxSectionSizeForTest(int max_section_size);
  void SetMaxSectionsForTest(int max_sections);

  // Transfers the current log data to the caller and clears the data. This can
  // safely be used without stopping the event logging.
  base::circular_deque<LogSection> TakeLogData();

  // Adds a new log event. This may discard old events, and may invalidate
  // iterators even if nothing is discarded. Note that the log_event may
  // theoretically exceed the maximum size of a section (though this should
  // never occur because these are packet-headers which are small, no bigger
  // than RTCP packets). If that ever happens, the log_event will be stored in
  // a new section anyway - the buffer's reserved capacity may be exceeded and
  // re-allocation may occur.
  void Write(std::string_view log_event);

  // Removes all event data, so the instance can be reused.
  void Clear();

 private:
  // Returns true if a new section must be created to store the event.
  bool NeedNewSection(size_t log_event_size) const;

  // Appends a new section of zero size to the end of the list, removing the
  // oldest one if necessary. On return, the section at the end (the list's
  // "back") will be empty, ready to accept the new data.
  void CreateNewSection();

  base::circular_deque<LogSection> sections_;

  // Value chosen to keep the memory-usage within reasonable limits, but also
  // allow for recording "most" sessions entirely.
  const int kMaxSections = 1000;
  int max_sections_ = kMaxSections;

  // A larger value will reduce memory-allocations at the cost of discarding a
  // larger chunk of the event log.
  const int kMaxSectionSize = 102400;  // 100K
  int max_section_size_ = kMaxSectionSize;
};

}  // namespace remoting::protocol

#endif  // REMOTING_PROTOCOL_WEBRTC_EVENT_LOG_DATA_H_