OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/call/rtc_event_log.h" | 11 #include "webrtc/call/rtc_event_log.h" |
12 | 12 |
13 #include <deque> | |
14 #include <vector> | 13 #include <vector> |
14 #include <limits> | |
15 | 15 |
16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
17 #include "webrtc/base/constructormagic.h" | |
17 #include "webrtc/base/criticalsection.h" | 18 #include "webrtc/base/criticalsection.h" |
19 #include "webrtc/base/event.h" | |
20 #include "webrtc/base/platform_thread.h" | |
18 #include "webrtc/base/thread_annotations.h" | 21 #include "webrtc/base/thread_annotations.h" |
19 #include "webrtc/call.h" | 22 #include "webrtc/call.h" |
23 #include "webrtc/call/ringbuffer.h" | |
24 #include "webrtc/common_audio/swap_queue.h" | |
20 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | 25 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
21 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 26 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
22 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" | 27 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" |
23 #include "webrtc/system_wrappers/include/clock.h" | 28 #include "webrtc/system_wrappers/include/clock.h" |
24 #include "webrtc/system_wrappers/include/file_wrapper.h" | 29 #include "webrtc/system_wrappers/include/file_wrapper.h" |
30 #include "webrtc/system_wrappers/include/logging.h" | |
25 | 31 |
26 #ifdef ENABLE_RTC_EVENT_LOG | 32 #ifdef ENABLE_RTC_EVENT_LOG |
27 // Files generated at build-time by the protobuf compiler. | 33 // Files generated at build-time by the protobuf compiler. |
28 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD | 34 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
29 #include "external/webrtc/webrtc/call/rtc_event_log.pb.h" | 35 #include "external/webrtc/webrtc/call/rtc_event_log.pb.h" |
30 #else | 36 #else |
31 #include "webrtc/call/rtc_event_log.pb.h" | 37 #include "webrtc/call/rtc_event_log.pb.h" |
32 #endif | 38 #endif |
33 #endif | 39 #endif |
34 | 40 |
35 namespace webrtc { | 41 namespace webrtc { |
36 | 42 |
37 #ifndef ENABLE_RTC_EVENT_LOG | 43 #ifndef ENABLE_RTC_EVENT_LOG |
38 | 44 |
39 // No-op implementation if flag is not set. | 45 // No-op implementation if flag is not set. |
40 class RtcEventLogImpl final : public RtcEventLog { | 46 class RtcEventLogNullImpl final : public RtcEventLog { |
41 public: | 47 public: |
42 void SetBufferDuration(int64_t buffer_duration_us) override {} | 48 bool StartLogging(const std::string& file_name, |
43 void StartLogging(const std::string& file_name, int duration_ms) override {} | 49 int64_t max_size_bytes) override { |
44 bool StartLogging(rtc::PlatformFile log_file) override { return false; } | 50 return false; |
45 void StopLogging(void) override {} | 51 } |
52 bool StartLogging(rtc::PlatformFile platform_file, | |
53 int64_t max_size_bytes) override { | |
54 return false; | |
55 } | |
56 void StopLogging() override {} | |
46 void LogVideoReceiveStreamConfig( | 57 void LogVideoReceiveStreamConfig( |
47 const VideoReceiveStream::Config& config) override {} | 58 const VideoReceiveStream::Config& config) override {} |
48 void LogVideoSendStreamConfig( | 59 void LogVideoSendStreamConfig( |
49 const VideoSendStream::Config& config) override {} | 60 const VideoSendStream::Config& config) override {} |
50 void LogRtpHeader(PacketDirection direction, | 61 void LogRtpHeader(PacketDirection direction, |
51 MediaType media_type, | 62 MediaType media_type, |
52 const uint8_t* header, | 63 const uint8_t* header, |
53 size_t packet_length) override {} | 64 size_t packet_length) override {} |
54 void LogRtcpPacket(PacketDirection direction, | 65 void LogRtcpPacket(PacketDirection direction, |
55 MediaType media_type, | 66 MediaType media_type, |
56 const uint8_t* packet, | 67 const uint8_t* packet, |
57 size_t length) override {} | 68 size_t length) override {} |
58 void LogAudioPlayout(uint32_t ssrc) override {} | 69 void LogAudioPlayout(uint32_t ssrc) override {} |
59 void LogBwePacketLossEvent(int32_t bitrate, | 70 void LogBwePacketLossEvent(int32_t bitrate, |
60 uint8_t fraction_loss, | 71 uint8_t fraction_loss, |
61 int32_t total_packets) override {} | 72 int32_t total_packets) override {} |
62 }; | 73 }; |
63 | 74 |
64 #else // ENABLE_RTC_EVENT_LOG is defined | 75 #else // ENABLE_RTC_EVENT_LOG is defined |
65 | 76 |
66 class RtcEventLogImpl final : public RtcEventLog { | 77 class RtcEventLogImpl final : public RtcEventLog { |
67 public: | 78 public: |
68 RtcEventLogImpl(); | 79 explicit RtcEventLogImpl(const Clock* clock); |
80 ~RtcEventLogImpl() override; | |
69 | 81 |
70 void SetBufferDuration(int64_t buffer_duration_us) override; | 82 bool StartLogging(const std::string& file_name, |
71 void StartLogging(const std::string& file_name, int duration_ms) override; | 83 int64_t max_size_bytes) override; |
72 bool StartLogging(rtc::PlatformFile log_file) override; | 84 bool StartLogging(rtc::PlatformFile platform_file, |
85 int64_t max_size_bytes) override; | |
73 void StopLogging() override; | 86 void StopLogging() override; |
74 void LogVideoReceiveStreamConfig( | 87 void LogVideoReceiveStreamConfig( |
75 const VideoReceiveStream::Config& config) override; | 88 const VideoReceiveStream::Config& config) override; |
76 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override; | 89 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override; |
77 void LogRtpHeader(PacketDirection direction, | 90 void LogRtpHeader(PacketDirection direction, |
78 MediaType media_type, | 91 MediaType media_type, |
79 const uint8_t* header, | 92 const uint8_t* header, |
80 size_t packet_length) override; | 93 size_t packet_length) override; |
81 void LogRtcpPacket(PacketDirection direction, | 94 void LogRtcpPacket(PacketDirection direction, |
82 MediaType media_type, | 95 MediaType media_type, |
83 const uint8_t* packet, | 96 const uint8_t* packet, |
84 size_t length) override; | 97 size_t length) override; |
85 void LogAudioPlayout(uint32_t ssrc) override; | 98 void LogAudioPlayout(uint32_t ssrc) override; |
86 void LogBwePacketLossEvent(int32_t bitrate, | 99 void LogBwePacketLossEvent(int32_t bitrate, |
87 uint8_t fraction_loss, | 100 uint8_t fraction_loss, |
88 int32_t total_packets) override; | 101 int32_t total_packets) override; |
89 | 102 |
90 private: | 103 private: |
91 // Starts logging. This function assumes the file_ has been opened succesfully | 104 // The normal state progression is |
92 // and that the start_time_us_ and _duration_us_ have been set. | 105 // TO_MEMORY -> STARTING_FILE -> TO_FILE -> STOPPING_FILE -> TO_MEMORY |
93 void StartLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_); | 106 // but it can also go directly from STARTING_FILE to STOPPING_FILE. |
94 // Stops logging and clears the stored data and buffers. | 107 enum LogState { |
95 void StopLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_); | 108 TO_MEMORY, |
96 // Adds a new event to the logfile if logging is active, or adds it to the | 109 STARTING_FILE, |
97 // list of recent log events otherwise. | 110 TO_FILE, |
98 void HandleEvent(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | 111 STOPPING_FILE, |
99 // Writes the event to the file. Note that this will destroy the state of the | 112 TERMINATE_THREAD, |
100 // input argument. | 113 }; |
101 void StoreToFile(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | 114 |
102 // Adds the event to the list of recent events, and removes any events that | 115 static bool ThreadOutputFunction(void* obj); |
103 // are too old and no longer fall in the time window. | 116 void TerminateThread(); |
104 void AddRecentEvent(const rtclog::Event& event) | 117 |
118 bool AppendEventToString(rtclog::Event* event) | |
105 EXCLUSIVE_LOCKS_REQUIRED(crit_); | 119 EXCLUSIVE_LOCKS_REQUIRED(crit_); |
120 void AppendEventToHistory(const rtclog::Event& event) | |
121 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
122 bool ProcessInOrder(bool memory, int64_t current_time) | |
123 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
124 void LogToMemory() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
the sun
2016/02/10 10:47:23
To make things even clearer, the parts of this cla
terelius
2016/02/11 21:27:28
One side effect of moving the state to a separate
the sun
2016/02/12 09:37:32
For StartLogging() it would likely make sense to o
terelius
2016/02/12 14:13:38
But even if StartLogging opens the file we wouldn'
the sun
2016/02/12 15:37:27
You probably want to process the control messages
terelius
2016/02/12 16:22:24
Though if we process the control messages before t
the sun
2016/02/16 13:29:15
I thought a START_FILE message would contain the f
terelius
2016/02/16 14:36:58
Oh, so you are saying that you would always proces
the sun
2016/02/17 09:44:20
No, I would handle all the control messages first,
| |
125 void StartLogFile() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
126 void LogToFile() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
127 void StopLogFile() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
128 void WriteLog(); | |
129 | |
130 // Message queues for passing events to the logging thread. | |
131 SwapQueue<rtclog::Event> config_buffer_; | |
the sun
2016/02/10 10:47:23
I think it is better to call these config_queue_ e
terelius
2016/02/11 21:27:28
Done.
| |
132 SwapQueue<rtclog::Event> rtp_buffer_; | |
133 SwapQueue<rtclog::Event> rtcp_buffer_; | |
134 SwapQueue<rtclog::Event> acm_playout_buffer_; | |
135 SwapQueue<rtclog::Event> bwe_loss_buffer_; | |
106 | 136 |
107 rtc::CriticalSection crit_; | 137 rtc::CriticalSection crit_; |
108 rtc::scoped_ptr<FileWrapper> file_ GUARDED_BY(crit_) = | |
109 rtc::scoped_ptr<FileWrapper>(FileWrapper::Create()); | |
110 rtc::PlatformFile platform_file_ GUARDED_BY(crit_) = | |
111 rtc::kInvalidPlatformFileValue; | |
112 rtclog::EventStream stream_ GUARDED_BY(crit_); | |
113 std::deque<rtclog::Event> recent_log_events_ GUARDED_BY(crit_); | |
114 std::vector<rtclog::Event> config_events_ GUARDED_BY(crit_); | |
115 | 138 |
116 // Microseconds to record log events, before starting the actual log. | 139 // History containing the most recent events (~ 10 s). |
117 int64_t buffer_duration_us_ GUARDED_BY(crit_); | 140 RingBuffer<rtclog::Event> history_ GUARDED_BY(crit_); |
118 bool currently_logging_ GUARDED_BY(crit_); | 141 |
119 int64_t start_time_us_ GUARDED_BY(crit_); | 142 // History containing all past configuration events. |
120 int64_t duration_us_ GUARDED_BY(crit_); | 143 std::vector<rtclog::Event> config_history_ GUARDED_BY(crit_); |
144 | |
145 LogState state_ GUARDED_BY(crit_); | |
146 rtc::scoped_ptr<FileWrapper> file_ GUARDED_BY(crit_); | |
147 rtc::PlatformThread thread_ GUARDED_BY(crit_); | |
148 | |
149 int64_t max_size_bytes_ GUARDED_BY(crit_); | |
150 int64_t written_bytes_ GUARDED_BY(crit_); | |
151 int64_t start_time_ GUARDED_BY(crit_); | |
152 int64_t stop_time_ GUARDED_BY(crit_); | |
153 | |
154 // We want to extract the elements from the buffers in timestamp order | |
155 // and, because we can't peek into the SwapQueue, we need to keep the first | |
156 // elements outside. | |
157 rtclog::Event config_event_; | |
158 rtclog::Event rtp_event_; | |
159 rtclog::Event rtcp_event_; | |
160 rtclog::Event playout_event_; | |
161 rtclog::Event loss_event_; | |
162 bool valid_config_event_; | |
the sun
2016/02/10 10:47:23
You may be able to avoid these by using rtc::Optio
terelius
2016/02/11 21:27:28
There are several reasons why Optional won't work.
| |
163 bool valid_rtp_event_; | |
164 bool valid_rtcp_event_; | |
165 bool valid_playout_event_; | |
166 bool valid_loss_event_; | |
167 | |
168 // Temporary space for serializing profobuf data. | |
169 std::string output_string_ GUARDED_BY(crit_); | |
170 | |
171 rtc::Event wake_up_; | |
172 rtc::Event file_finished_; | |
173 | |
121 const Clock* const clock_; | 174 const Clock* const clock_; |
175 | |
176 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtcEventLogImpl); | |
122 }; | 177 }; |
123 | 178 |
124 namespace { | 179 namespace { |
125 // The functions in this namespace convert enums from the runtime format | 180 // The functions in this namespace convert enums from the runtime format |
126 // that the rest of the WebRtc project can use, to the corresponding | 181 // that the rest of the WebRtc project can use, to the corresponding |
127 // serialized enum which is defined by the protobuf. | 182 // serialized enum which is defined by the protobuf. |
128 | 183 |
129 // Do not add default return values to the conversion functions in this | |
130 // unnamed namespace. The intention is to make the compiler warn if anyone | |
131 // adds unhandled new events/modes/etc. | |
132 | |
133 rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) { | 184 rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) { |
134 switch (rtcp_mode) { | 185 switch (rtcp_mode) { |
135 case RtcpMode::kCompound: | 186 case RtcpMode::kCompound: |
136 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; | 187 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
137 case RtcpMode::kReducedSize: | 188 case RtcpMode::kReducedSize: |
138 return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE; | 189 return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE; |
139 case RtcpMode::kOff: | 190 case RtcpMode::kOff: |
140 RTC_NOTREACHED(); | 191 RTC_NOTREACHED(); |
141 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; | 192 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
142 } | 193 } |
143 RTC_NOTREACHED(); | 194 RTC_NOTREACHED(); |
144 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; | 195 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
145 } | 196 } |
146 | 197 |
147 rtclog::MediaType ConvertMediaType(MediaType media_type) { | 198 rtclog::MediaType ConvertMediaType(MediaType media_type) { |
148 switch (media_type) { | 199 switch (media_type) { |
149 case MediaType::ANY: | 200 case MediaType::ANY: |
150 return rtclog::MediaType::ANY; | 201 return rtclog::MediaType::ANY; |
151 case MediaType::AUDIO: | 202 case MediaType::AUDIO: |
152 return rtclog::MediaType::AUDIO; | 203 return rtclog::MediaType::AUDIO; |
153 case MediaType::VIDEO: | 204 case MediaType::VIDEO: |
154 return rtclog::MediaType::VIDEO; | 205 return rtclog::MediaType::VIDEO; |
155 case MediaType::DATA: | 206 case MediaType::DATA: |
156 return rtclog::MediaType::DATA; | 207 return rtclog::MediaType::DATA; |
157 } | 208 } |
158 RTC_NOTREACHED(); | 209 RTC_NOTREACHED(); |
159 return rtclog::ANY; | 210 return rtclog::ANY; |
160 } | 211 } |
161 | 212 |
213 // The RTP and RTCP buffers reserve space for twice the expected number of | |
214 // sent packets because they also contain received packets. | |
215 const int kStreamConfigsPerSecond = 64; // 16 clients w. 4 streams each. | |
216 const int kRtpPacketsPerSecond = 500; // 125 sent video packets/s @ 1 Mbps. | |
217 const int kRtcpPacketsPerSecond = 40; // Assume RTCP sent 20 times/s. | |
218 const int kPlayoutsPerSecond = 100; // Playout called every 10 ms. | |
219 const int kBweUpdatesPerSecond = 20; // One BWE update per RTCP packet. | |
220 const int kEventsInHistory = 10000; | |
221 | |
162 } // namespace | 222 } // namespace |
163 | 223 |
164 namespace { | |
165 bool IsConfigEvent(const rtclog::Event& event) { | |
166 rtclog::Event_EventType event_type = event.type(); | |
167 return event_type == rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT || | |
168 event_type == rtclog::Event::VIDEO_SENDER_CONFIG_EVENT || | |
169 event_type == rtclog::Event::AUDIO_RECEIVER_CONFIG_EVENT || | |
170 event_type == rtclog::Event::AUDIO_SENDER_CONFIG_EVENT; | |
171 } | |
172 } // namespace | |
173 | 224 |
174 // RtcEventLogImpl member functions. | 225 // RtcEventLogImpl member functions. |
175 RtcEventLogImpl::RtcEventLogImpl() | 226 RtcEventLogImpl::RtcEventLogImpl(const Clock* clock) |
176 : file_(FileWrapper::Create()), | 227 // Allocate buffers for roughly one second of history. |
177 stream_(), | 228 : config_buffer_(kStreamConfigsPerSecond), |
178 buffer_duration_us_(10000000), | 229 rtp_buffer_(kRtpPacketsPerSecond), |
179 currently_logging_(false), | 230 rtcp_buffer_(kRtcpPacketsPerSecond), |
180 start_time_us_(0), | 231 acm_playout_buffer_(kPlayoutsPerSecond), |
181 duration_us_(0), | 232 bwe_loss_buffer_(kBweUpdatesPerSecond), |
182 clock_(Clock::GetRealTimeClock()) { | 233 crit_(), |
234 history_(kEventsInHistory), | |
235 config_history_(), | |
236 state_(TO_MEMORY), | |
237 file_(FileWrapper::Create()), | |
238 thread_(&ThreadOutputFunction, this, "RtcEventLog thread"), | |
239 max_size_bytes_(std::numeric_limits<int64_t>::max()), | |
240 written_bytes_(0), | |
241 start_time_(0), | |
242 stop_time_(std::numeric_limits<int64_t>::max()), | |
243 config_event_(), | |
244 rtp_event_(), | |
245 rtcp_event_(), | |
246 playout_event_(), | |
247 loss_event_(), | |
248 valid_config_event_(false), | |
249 valid_rtp_event_(false), | |
250 valid_rtcp_event_(false), | |
251 valid_playout_event_(false), | |
252 valid_loss_event_(false), | |
253 output_string_(), | |
254 wake_up_(false, false), | |
255 file_finished_(false, false), | |
256 clock_(clock) { | |
257 thread_.Start(); | |
183 } | 258 } |
184 | 259 |
185 void RtcEventLogImpl::SetBufferDuration(int64_t buffer_duration_us) { | 260 RtcEventLogImpl::~RtcEventLogImpl() { |
186 rtc::CritScope lock(&crit_); | 261 TerminateThread(); |
187 buffer_duration_us_ = buffer_duration_us; | 262 thread_.Stop(); |
188 } | 263 } |
189 | 264 |
190 void RtcEventLogImpl::StartLogging(const std::string& file_name, | 265 bool RtcEventLogImpl::StartLogging(const std::string& file_name, |
191 int duration_ms) { | 266 int64_t max_size_bytes) { |
192 rtc::CritScope lock(&crit_); | 267 rtc::CritScope lock(&crit_); |
193 if (currently_logging_) { | 268 if (state_ != TO_MEMORY) { |
194 StopLoggingLocked(); | 269 return false; |
195 } | 270 } |
271 file_.reset(FileWrapper::Create()); | |
196 if (file_->OpenFile(file_name.c_str(), false) != 0) { | 272 if (file_->OpenFile(file_name.c_str(), false) != 0) { |
197 return; | |
198 } | |
199 start_time_us_ = clock_->TimeInMicroseconds(); | |
200 duration_us_ = static_cast<int64_t>(duration_ms) * 1000; | |
201 StartLoggingLocked(); | |
202 } | |
203 | |
204 bool RtcEventLogImpl::StartLogging(rtc::PlatformFile log_file) { | |
205 rtc::CritScope lock(&crit_); | |
206 | |
207 if (currently_logging_) { | |
208 StopLoggingLocked(); | |
209 } | |
210 RTC_DCHECK(platform_file_ == rtc::kInvalidPlatformFileValue); | |
211 | |
212 FILE* file_stream = rtc::FdopenPlatformFileForWriting(log_file); | |
213 if (!file_stream) { | |
214 rtc::ClosePlatformFile(log_file); | |
215 return false; | 273 return false; |
216 } | 274 } |
217 | 275 |
218 if (file_->OpenFromFileHandle(file_stream, true, false) != 0) { | 276 max_size_bytes_ = max_size_bytes; |
219 rtc::ClosePlatformFile(log_file); | 277 start_time_ = clock_->TimeInMicroseconds(); |
220 return false; | 278 stop_time_ = std::numeric_limits<int64_t>::max(); |
221 } | 279 state_ = STARTING_FILE; |
222 platform_file_ = log_file; | |
223 // Set the start time and duration to keep logging for 10 minutes. | |
224 start_time_us_ = clock_->TimeInMicroseconds(); | |
225 duration_us_ = 10 * 60 * 1000000; | |
226 StartLoggingLocked(); | |
227 return true; | 280 return true; |
228 } | 281 } |
229 | 282 |
230 void RtcEventLogImpl::StartLoggingLocked() { | 283 bool RtcEventLogImpl::StartLogging(rtc::PlatformFile platform_file, |
231 currently_logging_ = true; | 284 int64_t max_size_bytes) { |
285 rtc::CritScope lock(&crit_); | |
286 if (state_ != TO_MEMORY) { | |
287 return false; | |
288 } | |
232 | 289 |
233 // Write all old configuration events to the log file. | 290 FILE* file_handle = rtc::FdopenPlatformFileForWriting(platform_file); |
234 for (auto& event : config_events_) { | 291 if (!file_handle) { |
235 StoreToFile(&event); | 292 return false; |
236 } | 293 } |
237 // Write all recent configuration events to the log file, and | 294 file_.reset(FileWrapper::Create()); |
238 // write all other recent events to the log file, ignoring any old events. | 295 if (file_->OpenFromFileHandle(file_handle, true, false) != 0) { |
239 for (auto& event : recent_log_events_) { | 296 return false; |
240 if (IsConfigEvent(event)) { | |
241 StoreToFile(&event); | |
242 config_events_.push_back(event); | |
243 } else if (event.timestamp_us() >= start_time_us_ - buffer_duration_us_) { | |
244 StoreToFile(&event); | |
245 } | |
246 } | 297 } |
247 recent_log_events_.clear(); | 298 |
248 // Write a LOG_START event to the file. | 299 max_size_bytes_ = max_size_bytes; |
249 rtclog::Event start_event; | 300 start_time_ = clock_->TimeInMicroseconds(); |
250 start_event.set_timestamp_us(start_time_us_); | 301 stop_time_ = std::numeric_limits<int64_t>::max(); |
251 start_event.set_type(rtclog::Event::LOG_START); | 302 state_ = STARTING_FILE; |
252 StoreToFile(&start_event); | 303 return true; |
253 } | 304 } |
254 | 305 |
255 void RtcEventLogImpl::StopLogging() { | 306 void RtcEventLogImpl::StopLogging() { |
256 rtc::CritScope lock(&crit_); | 307 { |
257 StopLoggingLocked(); | 308 rtc::CritScope lock(&crit_); |
309 if (state_ == TO_FILE || state_ == STARTING_FILE) { | |
310 stop_time_ = clock_->TimeInMicroseconds(); | |
311 wake_up_.Set(); // Request the output thread to wake up. | |
312 } else { | |
313 return; | |
314 } | |
315 } | |
316 file_finished_.Wait( | |
317 rtc::Event::kForever); // Wait for the log file to be completed. | |
258 } | 318 } |
259 | 319 |
260 void RtcEventLogImpl::LogVideoReceiveStreamConfig( | 320 void RtcEventLogImpl::LogVideoReceiveStreamConfig( |
261 const VideoReceiveStream::Config& config) { | 321 const VideoReceiveStream::Config& config) { |
262 rtc::CritScope lock(&crit_); | |
263 | |
264 rtclog::Event event; | 322 rtclog::Event event; |
265 event.set_timestamp_us(clock_->TimeInMicroseconds()); | 323 event.set_timestamp_us(clock_->TimeInMicroseconds()); |
266 event.set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); | 324 event.set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); |
267 | 325 |
268 rtclog::VideoReceiveConfig* receiver_config = | 326 rtclog::VideoReceiveConfig* receiver_config = |
269 event.mutable_video_receiver_config(); | 327 event.mutable_video_receiver_config(); |
270 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc); | 328 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc); |
271 receiver_config->set_local_ssrc(config.rtp.local_ssrc); | 329 receiver_config->set_local_ssrc(config.rtp.local_ssrc); |
272 | 330 |
273 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode)); | 331 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode)); |
(...skipping 11 matching lines...) Loading... | |
285 receiver_config->add_header_extensions(); | 343 receiver_config->add_header_extensions(); |
286 extension->set_name(e.name); | 344 extension->set_name(e.name); |
287 extension->set_id(e.id); | 345 extension->set_id(e.id); |
288 } | 346 } |
289 | 347 |
290 for (const auto& d : config.decoders) { | 348 for (const auto& d : config.decoders) { |
291 rtclog::DecoderConfig* decoder = receiver_config->add_decoders(); | 349 rtclog::DecoderConfig* decoder = receiver_config->add_decoders(); |
292 decoder->set_name(d.payload_name); | 350 decoder->set_name(d.payload_name); |
293 decoder->set_payload_type(d.payload_type); | 351 decoder->set_payload_type(d.payload_type); |
294 } | 352 } |
295 HandleEvent(&event); | 353 if (!config_buffer_.Insert(&event)) { |
354 LOG(LS_WARNING) << "Config buffer full. Not logging config event."; | |
355 } | |
296 } | 356 } |
297 | 357 |
298 void RtcEventLogImpl::LogVideoSendStreamConfig( | 358 void RtcEventLogImpl::LogVideoSendStreamConfig( |
299 const VideoSendStream::Config& config) { | 359 const VideoSendStream::Config& config) { |
300 rtc::CritScope lock(&crit_); | |
301 | |
302 rtclog::Event event; | 360 rtclog::Event event; |
303 event.set_timestamp_us(clock_->TimeInMicroseconds()); | 361 event.set_timestamp_us(clock_->TimeInMicroseconds()); |
304 event.set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT); | 362 event.set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT); |
305 | 363 |
306 rtclog::VideoSendConfig* sender_config = event.mutable_video_sender_config(); | 364 rtclog::VideoSendConfig* sender_config = event.mutable_video_sender_config(); |
307 | 365 |
308 for (const auto& ssrc : config.rtp.ssrcs) { | 366 for (const auto& ssrc : config.rtp.ssrcs) { |
309 sender_config->add_ssrcs(ssrc); | 367 sender_config->add_ssrcs(ssrc); |
310 } | 368 } |
311 | 369 |
312 for (const auto& e : config.rtp.extensions) { | 370 for (const auto& e : config.rtp.extensions) { |
313 rtclog::RtpHeaderExtension* extension = | 371 rtclog::RtpHeaderExtension* extension = |
314 sender_config->add_header_extensions(); | 372 sender_config->add_header_extensions(); |
315 extension->set_name(e.name); | 373 extension->set_name(e.name); |
316 extension->set_id(e.id); | 374 extension->set_id(e.id); |
317 } | 375 } |
318 | 376 |
319 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) { | 377 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) { |
320 sender_config->add_rtx_ssrcs(rtx_ssrc); | 378 sender_config->add_rtx_ssrcs(rtx_ssrc); |
321 } | 379 } |
322 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type); | 380 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type); |
323 | 381 |
324 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder(); | 382 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder(); |
325 encoder->set_name(config.encoder_settings.payload_name); | 383 encoder->set_name(config.encoder_settings.payload_name); |
326 encoder->set_payload_type(config.encoder_settings.payload_type); | 384 encoder->set_payload_type(config.encoder_settings.payload_type); |
327 HandleEvent(&event); | 385 if (!config_buffer_.Insert(&event)) { |
386 LOG(LS_WARNING) << "Config buffer full. Not logging config event."; | |
387 } | |
328 } | 388 } |
329 | 389 |
330 void RtcEventLogImpl::LogRtpHeader(PacketDirection direction, | 390 void RtcEventLogImpl::LogRtpHeader(PacketDirection direction, |
331 MediaType media_type, | 391 MediaType media_type, |
332 const uint8_t* header, | 392 const uint8_t* header, |
333 size_t packet_length) { | 393 size_t packet_length) { |
334 // Read header length (in bytes) from packet data. | 394 // Read header length (in bytes) from packet data. |
335 if (packet_length < 12u) { | 395 if (packet_length < 12u) { |
336 return; // Don't read outside the packet. | 396 return; // Don't read outside the packet. |
337 } | 397 } |
338 const bool x = (header[0] & 0x10) != 0; | 398 const bool x = (header[0] & 0x10) != 0; |
339 const uint8_t cc = header[0] & 0x0f; | 399 const uint8_t cc = header[0] & 0x0f; |
340 size_t header_length = 12u + cc * 4u; | 400 size_t header_length = 12u + cc * 4u; |
341 | 401 |
342 if (x) { | 402 if (x) { |
343 if (packet_length < 12u + cc * 4u + 4u) { | 403 if (packet_length < 12u + cc * 4u + 4u) { |
344 return; // Don't read outside the packet. | 404 return; // Don't read outside the packet. |
345 } | 405 } |
346 size_t x_len = ByteReader<uint16_t>::ReadBigEndian(header + 14 + cc * 4); | 406 size_t x_len = ByteReader<uint16_t>::ReadBigEndian(header + 14 + cc * 4); |
347 header_length += (x_len + 1) * 4; | 407 header_length += (x_len + 1) * 4; |
348 } | 408 } |
349 | 409 |
350 rtc::CritScope lock(&crit_); | |
351 rtclog::Event rtp_event; | 410 rtclog::Event rtp_event; |
352 rtp_event.set_timestamp_us(clock_->TimeInMicroseconds()); | 411 rtp_event.set_timestamp_us(clock_->TimeInMicroseconds()); |
353 rtp_event.set_type(rtclog::Event::RTP_EVENT); | 412 rtp_event.set_type(rtclog::Event::RTP_EVENT); |
354 rtp_event.mutable_rtp_packet()->set_incoming(direction == kIncomingPacket); | 413 rtp_event.mutable_rtp_packet()->set_incoming(direction == kIncomingPacket); |
355 rtp_event.mutable_rtp_packet()->set_type(ConvertMediaType(media_type)); | 414 rtp_event.mutable_rtp_packet()->set_type(ConvertMediaType(media_type)); |
356 rtp_event.mutable_rtp_packet()->set_packet_length(packet_length); | 415 rtp_event.mutable_rtp_packet()->set_packet_length(packet_length); |
357 rtp_event.mutable_rtp_packet()->set_header(header, header_length); | 416 rtp_event.mutable_rtp_packet()->set_header(header, header_length); |
358 HandleEvent(&rtp_event); | 417 if (!rtp_buffer_.Insert(&rtp_event)) { |
418 LOG(LS_WARNING) << "RTP buffer full. Not logging RTP packet."; | |
419 } | |
359 } | 420 } |
360 | 421 |
361 void RtcEventLogImpl::LogRtcpPacket(PacketDirection direction, | 422 void RtcEventLogImpl::LogRtcpPacket(PacketDirection direction, |
362 MediaType media_type, | 423 MediaType media_type, |
363 const uint8_t* packet, | 424 const uint8_t* packet, |
364 size_t length) { | 425 size_t length) { |
365 rtc::CritScope lock(&crit_); | |
366 rtclog::Event rtcp_event; | 426 rtclog::Event rtcp_event; |
367 rtcp_event.set_timestamp_us(clock_->TimeInMicroseconds()); | 427 rtcp_event.set_timestamp_us(clock_->TimeInMicroseconds()); |
368 rtcp_event.set_type(rtclog::Event::RTCP_EVENT); | 428 rtcp_event.set_type(rtclog::Event::RTCP_EVENT); |
369 rtcp_event.mutable_rtcp_packet()->set_incoming(direction == kIncomingPacket); | 429 rtcp_event.mutable_rtcp_packet()->set_incoming(direction == kIncomingPacket); |
370 rtcp_event.mutable_rtcp_packet()->set_type(ConvertMediaType(media_type)); | 430 rtcp_event.mutable_rtcp_packet()->set_type(ConvertMediaType(media_type)); |
371 | 431 |
372 RTCPUtility::RtcpCommonHeader header; | 432 RTCPUtility::RtcpCommonHeader header; |
373 const uint8_t* block_begin = packet; | 433 const uint8_t* block_begin = packet; |
374 const uint8_t* packet_end = packet + length; | 434 const uint8_t* packet_end = packet + length; |
375 RTC_DCHECK(length <= IP_PACKET_SIZE); | 435 RTC_DCHECK(length <= IP_PACKET_SIZE); |
(...skipping 31 matching lines...) Loading... | |
407 FALLTHROUGH(); | 467 FALLTHROUGH(); |
408 default: | 468 default: |
409 // We don't log sender descriptions, application defined messages | 469 // We don't log sender descriptions, application defined messages |
410 // or message blocks of unknown type. | 470 // or message blocks of unknown type. |
411 break; | 471 break; |
412 } | 472 } |
413 | 473 |
414 block_begin += block_size; | 474 block_begin += block_size; |
415 } | 475 } |
416 rtcp_event.mutable_rtcp_packet()->set_packet_data(buffer, buffer_length); | 476 rtcp_event.mutable_rtcp_packet()->set_packet_data(buffer, buffer_length); |
417 HandleEvent(&rtcp_event); | 477 if (!rtcp_buffer_.Insert(&rtcp_event)) { |
478 LOG(LS_WARNING) << "RTCP buffer full. Not logging RTCP packet."; | |
479 } | |
418 } | 480 } |
419 | 481 |
420 void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) { | 482 void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) { |
421 rtc::CritScope lock(&crit_); | |
422 rtclog::Event event; | 483 rtclog::Event event; |
423 event.set_timestamp_us(clock_->TimeInMicroseconds()); | 484 event.set_timestamp_us(clock_->TimeInMicroseconds()); |
424 event.set_type(rtclog::Event::AUDIO_PLAYOUT_EVENT); | 485 event.set_type(rtclog::Event::AUDIO_PLAYOUT_EVENT); |
425 auto playout_event = event.mutable_audio_playout_event(); | 486 auto playout_event = event.mutable_audio_playout_event(); |
426 playout_event->set_local_ssrc(ssrc); | 487 playout_event->set_local_ssrc(ssrc); |
427 HandleEvent(&event); | 488 if (!acm_playout_buffer_.Insert(&event)) { |
489 LOG(LS_WARNING) << "Playout buffer full. Not logging ACM playout."; | |
490 } | |
428 } | 491 } |
429 | 492 |
430 void RtcEventLogImpl::LogBwePacketLossEvent(int32_t bitrate, | 493 void RtcEventLogImpl::LogBwePacketLossEvent(int32_t bitrate, |
431 uint8_t fraction_loss, | 494 uint8_t fraction_loss, |
432 int32_t total_packets) { | 495 int32_t total_packets) { |
433 rtc::CritScope lock(&crit_); | |
434 rtclog::Event event; | 496 rtclog::Event event; |
435 event.set_timestamp_us(clock_->TimeInMicroseconds()); | 497 event.set_timestamp_us(clock_->TimeInMicroseconds()); |
436 event.set_type(rtclog::Event::BWE_PACKET_LOSS_EVENT); | 498 event.set_type(rtclog::Event::BWE_PACKET_LOSS_EVENT); |
437 auto bwe_event = event.mutable_bwe_packet_loss_event(); | 499 auto bwe_event = event.mutable_bwe_packet_loss_event(); |
438 bwe_event->set_bitrate(bitrate); | 500 bwe_event->set_bitrate(bitrate); |
439 bwe_event->set_fraction_loss(fraction_loss); | 501 bwe_event->set_fraction_loss(fraction_loss); |
440 bwe_event->set_total_packets(total_packets); | 502 bwe_event->set_total_packets(total_packets); |
441 HandleEvent(&event); | 503 if (!bwe_loss_buffer_.Insert(&event)) { |
442 } | 504 LOG(LS_WARNING) << "BWE loss buffer full. Not logging BWE update."; |
443 | 505 } |
444 void RtcEventLogImpl::StopLoggingLocked() { | 506 } |
445 if (currently_logging_) { | 507 |
446 currently_logging_ = false; | 508 bool RtcEventLogImpl::AppendEventToString(rtclog::Event* event) { |
447 // Create a LogEnd event | 509 rtclog::EventStream event_stream; |
448 rtclog::Event event; | 510 event_stream.add_stream(); |
449 event.set_timestamp_us(clock_->TimeInMicroseconds()); | 511 event_stream.mutable_stream(0)->Swap(event); |
450 event.set_type(rtclog::Event::LOG_END); | 512 // TODO(terelius): We create a new event strem per event, but it will look |
451 // Store the event and close the file | 513 // like a single stream when we read it back from file. |
452 RTC_DCHECK(file_->Open()); | |
453 StoreToFile(&event); | |
454 file_->CloseFile(); | |
455 if (platform_file_ != rtc::kInvalidPlatformFileValue) { | |
456 rtc::ClosePlatformFile(platform_file_); | |
457 platform_file_ = rtc::kInvalidPlatformFileValue; | |
458 } | |
459 } | |
460 RTC_DCHECK(!file_->Open()); | |
461 stream_.Clear(); | |
462 } | |
463 | |
464 void RtcEventLogImpl::HandleEvent(rtclog::Event* event) { | |
465 if (currently_logging_) { | |
466 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) { | |
467 StoreToFile(event); | |
468 return; | |
469 } | |
470 StopLoggingLocked(); | |
471 } | |
472 AddRecentEvent(*event); | |
473 } | |
474 | |
475 void RtcEventLogImpl::StoreToFile(rtclog::Event* event) { | |
476 // Reuse the same object at every log event. | |
477 if (stream_.stream_size() < 1) { | |
478 stream_.add_stream(); | |
479 } | |
480 RTC_DCHECK_EQ(stream_.stream_size(), 1); | |
481 stream_.mutable_stream(0)->Swap(event); | |
482 // TODO(terelius): Doesn't this create a new EventStream per event? | |
483 // Is this guaranteed to work e.g. in future versions of protobuf? | 514 // Is this guaranteed to work e.g. in future versions of protobuf? |
484 std::string dump_buffer; | 515 bool stop = true; |
485 stream_.SerializeToString(&dump_buffer); | 516 if (written_bytes_ + static_cast<int64_t>(output_string_.size()) + |
486 file_->Write(dump_buffer.data(), dump_buffer.size()); | 517 event_stream.ByteSize() <= |
487 } | 518 max_size_bytes_) { |
488 | 519 event_stream.AppendToString(&output_string_); |
489 void RtcEventLogImpl::AddRecentEvent(const rtclog::Event& event) { | 520 stop = false; |
490 recent_log_events_.push_back(event); | 521 } |
491 while (recent_log_events_.front().timestamp_us() < | 522 // Swap the event back so that we don't mix event types in the queues. |
492 event.timestamp_us() - buffer_duration_us_) { | 523 event_stream.mutable_stream(0)->Swap(event); |
493 if (IsConfigEvent(recent_log_events_.front())) { | 524 return stop; |
494 config_events_.push_back(recent_log_events_.front()); | 525 } |
495 } | 526 |
496 recent_log_events_.pop_front(); | 527 void RtcEventLogImpl::AppendEventToHistory(const rtclog::Event& event) { |
497 } | 528 history_.push_back(event); |
498 } | 529 } |
499 | 530 |
531 // Traverses the SwapQueues in timestamp order and copies all events earlier | |
532 // than |current_time| either to the history or to a string that will be | |
533 // written to disc. | |
534 bool RtcEventLogImpl::ProcessInOrder(bool memory, int64_t current_time) { | |
535 bool stop = false; | |
536 enum EventType { | |
537 CONFIG_EVENT, | |
538 RTP_EVENT, | |
539 RTCP_EVENT, | |
540 PLAYOUT_EVENT, | |
541 LOSS_EVENT | |
542 }; | |
543 | |
544 // Extract the head of each queue. | |
545 if (!valid_config_event_) { | |
546 valid_config_event_ = config_buffer_.Remove(&config_event_); | |
547 } | |
548 | |
549 if (!valid_rtp_event_) { | |
550 valid_rtp_event_ = rtp_buffer_.Remove(&rtp_event_); | |
551 } | |
552 | |
553 if (!valid_rtcp_event_) { | |
554 valid_rtcp_event_ = rtcp_buffer_.Remove(&rtcp_event_); | |
555 } | |
556 | |
557 if (!valid_playout_event_) { | |
558 valid_playout_event_ = acm_playout_buffer_.Remove(&playout_event_); | |
559 } | |
560 | |
561 if (!valid_loss_event_) { | |
562 valid_loss_event_ = bwe_loss_buffer_.Remove(&loss_event_); | |
563 } | |
564 | |
565 while ((valid_config_event_ || valid_rtp_event_ || valid_rtcp_event_ || | |
566 valid_playout_event_ || valid_loss_event_) && | |
567 !stop) { | |
568 // Find the earliest event (in timestamp order). | |
569 EventType type = CONFIG_EVENT; | |
570 int64_t first_timestamp = | |
571 (valid_config_event_ ? config_event_.timestamp_us() | |
572 : std::numeric_limits<int64_t>::max()); | |
573 if (valid_rtp_event_ && (rtp_event_.timestamp_us() < first_timestamp)) { | |
574 first_timestamp = rtp_event_.timestamp_us(); | |
575 type = RTP_EVENT; | |
576 } | |
577 if (valid_rtcp_event_ && (rtcp_event_.timestamp_us() < first_timestamp)) { | |
578 first_timestamp = rtcp_event_.timestamp_us(); | |
579 type = RTCP_EVENT; | |
580 } | |
581 if (valid_playout_event_ && | |
582 playout_event_.timestamp_us() < first_timestamp) { | |
583 first_timestamp = playout_event_.timestamp_us(); | |
584 type = PLAYOUT_EVENT; | |
585 } | |
586 if (valid_loss_event_ && loss_event_.timestamp_us() < first_timestamp) { | |
587 first_timestamp = loss_event_.timestamp_us(); | |
588 type = LOSS_EVENT; | |
589 } | |
590 | |
591 if (first_timestamp > current_time) { | |
592 // We have handled all events earlier than current_time. | |
593 break; | |
594 } | |
595 // Serialize the event and fetch the next event of that type. | |
596 switch (type) { | |
597 case CONFIG_EVENT: | |
598 if (!memory) { | |
599 stop = AppendEventToString(&config_event_); | |
600 } | |
601 config_history_.push_back(config_event_); | |
602 if (!stop) { | |
603 valid_config_event_ = config_buffer_.Remove(&config_event_); | |
604 } | |
605 break; | |
606 case RTP_EVENT: | |
607 if (memory) { | |
608 AppendEventToHistory(rtp_event_); | |
609 } else { | |
610 stop = AppendEventToString(&rtp_event_); | |
611 } | |
612 if (!stop) { | |
613 valid_rtp_event_ = rtp_buffer_.Remove(&rtp_event_); | |
614 } | |
615 break; | |
616 case RTCP_EVENT: | |
617 if (memory) { | |
618 AppendEventToHistory(rtcp_event_); | |
619 } else { | |
620 stop = AppendEventToString(&rtcp_event_); | |
621 } | |
622 if (!stop) { | |
623 valid_rtcp_event_ = rtcp_buffer_.Remove(&rtcp_event_); | |
624 } | |
625 break; | |
626 case PLAYOUT_EVENT: | |
627 if (memory) { | |
628 AppendEventToHistory(playout_event_); | |
629 } else { | |
630 stop = AppendEventToString(&playout_event_); | |
631 } | |
632 if (!stop) { | |
633 valid_playout_event_ = acm_playout_buffer_.Remove(&playout_event_); | |
634 } | |
635 break; | |
636 case LOSS_EVENT: | |
637 if (memory) { | |
638 AppendEventToHistory(loss_event_); | |
639 } else { | |
640 stop = AppendEventToString(&loss_event_); | |
641 } | |
642 if (!stop) { | |
643 valid_loss_event_ = bwe_loss_buffer_.Remove(&loss_event_); | |
644 } | |
645 break; | |
646 } | |
647 } | |
648 // We want to stop logging either if we have struck the file size limit | |
649 // or if we have logged all events older than |stop_time_| | |
650 return stop || (current_time > stop_time_); | |
651 } | |
652 | |
653 void RtcEventLogImpl::LogToMemory() { | |
654 // Process each event in order and append it to the appropriate history_. | |
655 int64_t current_time = clock_->TimeInMicroseconds(); | |
656 ProcessInOrder(true, current_time); | |
657 } | |
658 | |
659 void RtcEventLogImpl::StartLogFile() { | |
660 bool stop = false; | |
661 output_string_.clear(); | |
662 | |
663 // Create and serialize the LOG_START event. | |
664 rtclog::Event start_event; | |
665 start_event.set_timestamp_us(start_time_); | |
666 start_event.set_type(rtclog::Event::LOG_START); | |
667 AppendEventToString(&start_event); | |
668 | |
669 // Serialize the config information for all old streams. | |
670 for (rtclog::Event& event : config_history_) { | |
671 AppendEventToString(&event); | |
672 } | |
673 | |
674 // Serialize the events in the event queue. | |
675 for (int i = 0; !history_.empty() && !stop; i++) { | |
676 stop = AppendEventToString(&history_.front()); | |
677 if (!stop) { | |
678 history_.pop_front(); | |
679 } | |
680 } | |
681 | |
682 // Write to file. | |
683 file_->Write(output_string_.data(), output_string_.size()); | |
684 written_bytes_ += output_string_.size(); | |
685 state_ = (stop ? STOPPING_FILE : TO_FILE); | |
686 } | |
687 | |
688 void RtcEventLogImpl::LogToFile() { | |
689 output_string_.clear(); | |
690 | |
691 // Process each event in order and append it to the output_string_. | |
692 int64_t current_time = clock_->TimeInMicroseconds(); | |
693 bool stop = ProcessInOrder(false, current_time); | |
694 if (stop || stop_time_ <= current_time) { | |
695 state_ = STOPPING_FILE; | |
696 } | |
697 | |
698 // Write string to file. | |
699 file_->Write(output_string_.data(), output_string_.size()); | |
700 written_bytes_ += output_string_.size(); | |
701 } | |
702 | |
703 void RtcEventLogImpl::StopLogFile() { | |
704 output_string_.clear(); | |
705 | |
706 rtclog::Event end_event; | |
707 end_event.set_timestamp_us(stop_time_); | |
708 end_event.set_type(rtclog::Event::LOG_END); | |
709 AppendEventToString(&end_event); | |
710 | |
711 if (written_bytes_ + static_cast<int64_t>(output_string_.size()) <= | |
712 max_size_bytes_) { | |
713 file_->Write(output_string_.data(), output_string_.size()); | |
714 written_bytes_ += output_string_.size(); | |
715 } | |
716 | |
717 state_ = TO_MEMORY; | |
718 max_size_bytes_ = std::numeric_limits<int64_t>::max(); | |
719 written_bytes_ = 0; | |
720 start_time_ = 0; | |
721 stop_time_ = std::numeric_limits<int64_t>::max(); | |
722 output_string_.clear(); | |
723 file_->CloseFile(); | |
724 file_finished_.Set(); | |
725 } | |
726 | |
727 void RtcEventLogImpl::WriteLog() { | |
728 bool sleep; | |
729 | |
730 while (true) { | |
731 { | |
732 rtc::CritScope lock(&crit_); | |
733 switch (state_) { | |
the sun
2016/02/10 10:47:23
Pull this switch out into a separate function of t
terelius
2016/02/11 21:27:28
I don't understand what logic you want to test. Wr
the sun
2016/02/12 09:37:32
I was thinking mostly about the stuff in the funct
terelius
2016/02/12 14:13:38
I still don't understand what you want to pull out
the sun
2016/02/12 15:37:27
Yes, I think you could pretty pretty much write th
| |
734 case TO_MEMORY: | |
735 LogToMemory(); | |
736 break; | |
737 case STARTING_FILE: | |
738 StartLogFile(); | |
739 break; | |
740 case TO_FILE: | |
741 LogToFile(); | |
742 break; | |
743 case STOPPING_FILE: | |
744 StopLogFile(); | |
745 break; | |
746 case TERMINATE_THREAD: | |
747 if (file_->Open()) { | |
748 RTC_DCHECK(stop_time_ < std::numeric_limits<int64_t>::max()); | |
749 StopLogFile(); | |
750 RTC_DCHECK(stop_time_ == std::numeric_limits<int64_t>::max()); | |
751 RTC_DCHECK(!file_->Open()); | |
752 state_ = TERMINATE_THREAD; | |
753 } | |
754 return; | |
755 } | |
756 int64_t current_time = clock_->TimeInMicroseconds(); | |
757 sleep = (state_ != STOPPING_FILE && state_ != TERMINATE_THREAD && | |
758 current_time <= stop_time_); | |
759 } | |
760 | |
761 // Accumulate a new batch of events instead of processing them one at a | |
762 // time. By waiting, we also release the lock so that the main thread can | |
763 // call StopLogging and StartLogging. | |
764 if (sleep) { | |
765 wake_up_.Wait(50); | |
766 } | |
767 } | |
768 } | |
769 | |
770 bool RtcEventLogImpl::ThreadOutputFunction(void* obj) { | |
771 RtcEventLogImpl* event_log = static_cast<RtcEventLogImpl*>(obj); | |
772 event_log->WriteLog(); | |
773 return false; | |
774 } | |
775 | |
776 void RtcEventLogImpl::TerminateThread() { | |
777 { | |
778 rtc::CritScope lock(&crit_); | |
779 stop_time_ = clock_->TimeInMicroseconds(); | |
780 state_ = TERMINATE_THREAD; | |
781 wake_up_.Set(); // Wake up the output thread. | |
782 } | |
783 } | |
784 | |
500 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, | 785 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, |
501 rtclog::EventStream* result) { | 786 rtclog::EventStream* result) { |
502 char tmp_buffer[1024]; | 787 char tmp_buffer[1024]; |
503 int bytes_read = 0; | 788 int bytes_read = 0; |
504 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create()); | 789 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create()); |
505 if (dump_file->OpenFile(file_name.c_str(), true) != 0) { | 790 if (dump_file->OpenFile(file_name.c_str(), true) != 0) { |
506 return false; | 791 return false; |
507 } | 792 } |
508 std::string dump_buffer; | 793 std::string dump_buffer; |
509 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { | 794 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { |
510 dump_buffer.append(tmp_buffer, bytes_read); | 795 dump_buffer.append(tmp_buffer, bytes_read); |
511 } | 796 } |
512 dump_file->CloseFile(); | 797 dump_file->CloseFile(); |
513 return result->ParseFromString(dump_buffer); | 798 return result->ParseFromString(dump_buffer); |
514 } | 799 } |
515 | 800 |
516 #endif // ENABLE_RTC_EVENT_LOG | 801 #endif // ENABLE_RTC_EVENT_LOG |
517 | 802 |
518 // RtcEventLog member functions. | 803 // RtcEventLog member functions. |
519 rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() { | 804 rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create(const Clock* clock) { |
520 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl()); | 805 #ifdef ENABLE_RTC_EVENT_LOG |
806 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl(clock)); | |
807 #else | |
808 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogNullImpl()); | |
809 #endif // ENABLE_RTC_EVENT_LOG | |
521 } | 810 } |
522 | 811 |
523 } // namespace webrtc | 812 } // namespace webrtc |
OLD | NEW |