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