xref: /aosp_15_r20/external/webrtc/logging/rtc_event_log/rtc_event_log_impl.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 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 
11 #ifndef LOGGING_RTC_EVENT_LOG_RTC_EVENT_LOG_IMPL_H_
12 #define LOGGING_RTC_EVENT_LOG_RTC_EVENT_LOG_IMPL_H_
13 
14 #include <cstddef>
15 #include <cstdint>
16 #include <deque>
17 #include <memory>
18 #include <string>
19 
20 #include "absl/strings/string_view.h"
21 #include "absl/types/optional.h"
22 #include "api/rtc_event_log/rtc_event.h"
23 #include "api/rtc_event_log/rtc_event_log.h"
24 #include "api/rtc_event_log_output.h"
25 #include "api/sequence_checker.h"
26 #include "api/task_queue/task_queue_factory.h"
27 #include "logging/rtc_event_log/encoder/rtc_event_log_encoder.h"
28 #include "rtc_base/system/no_unique_address.h"
29 #include "rtc_base/task_queue.h"
30 #include "rtc_base/thread_annotations.h"
31 
32 namespace webrtc {
33 
34 class RtcEventLogImpl final : public RtcEventLog {
35  public:
36   RtcEventLogImpl(EncodingType encoding_type,
37                   TaskQueueFactory* task_queue_factory);
38   RtcEventLogImpl(const RtcEventLogImpl&) = delete;
39   RtcEventLogImpl& operator=(const RtcEventLogImpl&) = delete;
40 
41   ~RtcEventLogImpl() override;
42 
43   // TODO(eladalon): We should change these name to reflect that what we're
44   // actually starting/stopping is the output of the log, not the log itself.
45   bool StartLogging(std::unique_ptr<RtcEventLogOutput> output,
46                     int64_t output_period_ms) override;
47   void StopLogging() override;
48   void StopLogging(std::function<void()> callback) override;
49 
50   void Log(std::unique_ptr<RtcEvent> event) override;
51 
52  private:
53   void LogToMemory(std::unique_ptr<RtcEvent> event) RTC_RUN_ON(task_queue_);
54   void LogEventsFromMemoryToOutput() RTC_RUN_ON(task_queue_);
55 
56   void StopOutput() RTC_RUN_ON(task_queue_);
57 
58   void WriteConfigsAndHistoryToOutput(absl::string_view encoded_configs,
59                                       absl::string_view encoded_history)
60       RTC_RUN_ON(task_queue_);
61   void WriteToOutput(absl::string_view output_string) RTC_RUN_ON(task_queue_);
62 
63   void StopLoggingInternal() RTC_RUN_ON(task_queue_);
64 
65   void ScheduleOutput() RTC_RUN_ON(task_queue_);
66 
67   // History containing all past configuration events.
68   std::deque<std::unique_ptr<RtcEvent>> config_history_
69       RTC_GUARDED_BY(*task_queue_);
70 
71   // History containing the most recent (non-configuration) events (~10s).
72   std::deque<std::unique_ptr<RtcEvent>> history_ RTC_GUARDED_BY(*task_queue_);
73 
74   std::unique_ptr<RtcEventLogEncoder> event_encoder_
75       RTC_GUARDED_BY(*task_queue_);
76   std::unique_ptr<RtcEventLogOutput> event_output_ RTC_GUARDED_BY(*task_queue_);
77 
78   size_t num_config_events_written_ RTC_GUARDED_BY(*task_queue_);
79   absl::optional<int64_t> output_period_ms_ RTC_GUARDED_BY(*task_queue_);
80   int64_t last_output_ms_ RTC_GUARDED_BY(*task_queue_);
81   bool output_scheduled_ RTC_GUARDED_BY(*task_queue_);
82 
83   RTC_NO_UNIQUE_ADDRESS SequenceChecker logging_state_checker_;
84   bool logging_state_started_ RTC_GUARDED_BY(logging_state_checker_);
85 
86   // Since we are posting tasks bound to `this`,  it is critical that the event
87   // log and its members outlive `task_queue_`. Keep the `task_queue_`
88   // last to ensure it destructs first, or else tasks living on the queue might
89   // access other members after they've been torn down.
90   std::unique_ptr<rtc::TaskQueue> task_queue_;
91 };
92 
93 }  // namespace webrtc
94 
95 #endif  //  LOGGING_RTC_EVENT_LOG_RTC_EVENT_LOG_IMPL_H_
96