blob: 60ed912b65163a22971272c8b9ab05cd4fcc5e89 [file] [log] [blame]
terelius4311ba52016-04-22 12:40:37 -07001/*
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
39namespace webrtc {
40
41class 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,
70 rtc::Event* wake_up,
71 rtc::Event* file_finished,
72 const Clock* const clock);
73 ~RtcEventLogHelperThread();
74
75 private:
76 static bool ThreadOutputFunction(void* obj);
77
78 void TerminateThread();
79 bool AppendEventToString(rtclog::Event* event);
80 void AppendEventToHistory(const rtclog::Event& event);
81 void LogToMemory();
82 void StartLogFile();
83 void LogToFile();
84 void StopLogFile();
85 void WriteLog();
86
87 // Message queues for passing events to the logging thread.
88 SwapQueue<ControlMessage>* message_queue_;
89 SwapQueue<std::unique_ptr<rtclog::Event>>* event_queue_;
90
91 // History containing the most recent events (~ 10 s).
92 RingBuffer<std::unique_ptr<rtclog::Event>> history_;
93
94 // History containing all past configuration events.
95 std::vector<std::unique_ptr<rtclog::Event>> config_history_;
96
97 std::unique_ptr<FileWrapper> file_;
98 rtc::PlatformThread thread_;
99
100 int64_t max_size_bytes_;
101 int64_t written_bytes_;
102 int64_t start_time_;
103 int64_t stop_time_;
104
105 bool has_recent_event_;
106 std::unique_ptr<rtclog::Event> most_recent_event_;
107
108 // Temporary space for serializing profobuf data.
109 std::string output_string_;
110
111 rtc::Event* wake_up_;
112 rtc::Event* stopped_;
113
114 const Clock* const clock_;
115
116 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtcEventLogHelperThread);
117};
118
119} // namespace webrtc
120
121#endif // ENABLE_RTC_EVENT_LOG
122
123#endif // WEBRTC_CALL_RTC_EVENT_LOG_HELPER_THREAD_H_