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