| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2016 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 WEBRTC_CALL_RTC_EVENT_LOG_HELPER_THREAD_H_ | |
| 12 #define WEBRTC_CALL_RTC_EVENT_LOG_HELPER_THREAD_H_ | |
| 13 | |
| 14 #include <limits> | |
| 15 #include <memory> | |
| 16 #include <string> | |
| 17 #include <utility> | |
| 18 #include <vector> | |
| 19 | |
| 20 #include "webrtc/base/constructormagic.h" | |
| 21 #include "webrtc/base/event.h" | |
| 22 #include "webrtc/base/ignore_wundef.h" | |
| 23 #include "webrtc/base/platform_thread.h" | |
| 24 #include "webrtc/base/swap_queue.h" | |
| 25 #include "webrtc/call/ringbuffer.h" | |
| 26 #include "webrtc/system_wrappers/include/clock.h" | |
| 27 #include "webrtc/system_wrappers/include/file_wrapper.h" | |
| 28 | |
| 29 #ifdef ENABLE_RTC_EVENT_LOG | |
| 30 // Files generated at build-time by the protobuf compiler. | |
| 31 RTC_PUSH_IGNORING_WUNDEF() | |
| 32 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD | |
| 33 #include "external/webrtc/webrtc/call/rtc_event_log.pb.h" | |
| 34 #else | |
| 35 #include "webrtc/call/rtc_event_log.pb.h" | |
| 36 #endif | |
| 37 RTC_POP_IGNORING_WUNDEF() | |
| 38 #endif | |
| 39 | |
| 40 #ifdef ENABLE_RTC_EVENT_LOG | |
| 41 | |
| 42 namespace webrtc { | |
| 43 | |
| 44 class RtcEventLogHelperThread final { | |
| 45 public: | |
| 46 struct ControlMessage { | |
| 47 ControlMessage() | |
| 48 : message_type(STOP_FILE), | |
| 49 file(nullptr), | |
| 50 max_size_bytes(0), | |
| 51 start_time(0), | |
| 52 stop_time(0) {} | |
| 53 enum { START_FILE, STOP_FILE, TERMINATE_THREAD } message_type; | |
| 54 | |
| 55 std::unique_ptr<FileWrapper> file; // Only used with START_FILE. | |
| 56 int64_t max_size_bytes; // Only used with START_FILE. | |
| 57 int64_t start_time; // Only used with START_FILE. | |
| 58 int64_t stop_time; // Used with all 3 message types. | |
| 59 | |
| 60 friend void swap(ControlMessage& lhs, ControlMessage& rhs) { | |
| 61 using std::swap; | |
| 62 swap(lhs.message_type, rhs.message_type); | |
| 63 lhs.file.swap(rhs.file); | |
| 64 swap(lhs.max_size_bytes, rhs.max_size_bytes); | |
| 65 swap(lhs.start_time, rhs.start_time); | |
| 66 swap(lhs.stop_time, rhs.stop_time); | |
| 67 } | |
| 68 }; | |
| 69 | |
| 70 RtcEventLogHelperThread( | |
| 71 SwapQueue<ControlMessage>* message_queue, | |
| 72 SwapQueue<std::unique_ptr<rtclog::Event>>* event_queue, | |
| 73 const Clock* const clock); | |
| 74 ~RtcEventLogHelperThread(); | |
| 75 | |
| 76 // This function MUST be called once a STOP_FILE message is added to the | |
| 77 // signalling queue. The function will make sure that the output thread | |
| 78 // wakes up to read the message, and it blocks until the output thread has | |
| 79 // finished writing to the file. | |
| 80 void WaitForFileFinished(); | |
| 81 | |
| 82 // This fuction MUST be called once an event is added to the event queue. | |
| 83 void SignalNewEvent(); | |
| 84 | |
| 85 private: | |
| 86 static bool ThreadOutputFunction(void* obj); | |
| 87 | |
| 88 bool AppendEventToString(rtclog::Event* event); | |
| 89 bool LogToMemory(); | |
| 90 void StartLogFile(); | |
| 91 bool LogToFile(); | |
| 92 void StopLogFile(); | |
| 93 void ProcessEvents(); | |
| 94 | |
| 95 // Message queues for passing events to the logging thread. | |
| 96 SwapQueue<ControlMessage>* message_queue_; | |
| 97 SwapQueue<std::unique_ptr<rtclog::Event>>* event_queue_; | |
| 98 | |
| 99 // History containing the most recent events (~ 10 s). | |
| 100 RingBuffer<std::unique_ptr<rtclog::Event>> history_; | |
| 101 | |
| 102 // History containing all past configuration events. | |
| 103 std::vector<std::unique_ptr<rtclog::Event>> config_history_; | |
| 104 | |
| 105 std::unique_ptr<FileWrapper> file_; | |
| 106 rtc::PlatformThread thread_; | |
| 107 | |
| 108 int64_t max_size_bytes_; | |
| 109 int64_t written_bytes_; | |
| 110 int64_t start_time_; | |
| 111 int64_t stop_time_; | |
| 112 | |
| 113 bool has_recent_event_; | |
| 114 std::unique_ptr<rtclog::Event> most_recent_event_; | |
| 115 | |
| 116 // Temporary space for serializing profobuf data. | |
| 117 std::string output_string_; | |
| 118 | |
| 119 rtc::Event wake_periodically_; | |
| 120 rtc::Event wake_from_hibernation_; | |
| 121 rtc::Event file_finished_; | |
| 122 | |
| 123 const Clock* const clock_; | |
| 124 | |
| 125 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtcEventLogHelperThread); | |
| 126 }; | |
| 127 | |
| 128 } // namespace webrtc | |
| 129 | |
| 130 #endif // ENABLE_RTC_EVENT_LOG | |
| 131 | |
| 132 #endif // WEBRTC_CALL_RTC_EVENT_LOG_HELPER_THREAD_H_ | |
| OLD | NEW |