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/logging/rtc_event_log/rtc_event_log.h" | 11 #include "webrtc/logging/rtc_event_log/rtc_event_log.h" |
12 | 12 |
13 #include <atomic> | |
14 #include <deque> | |
15 #include <functional> | |
13 #include <limits> | 16 #include <limits> |
17 #include <memory> | |
14 #include <utility> | 18 #include <utility> |
15 #include <vector> | 19 #include <vector> |
16 | 20 |
17 #include "webrtc/logging/rtc_event_log/rtc_event_log_helper_thread.h" | |
18 #include "webrtc/modules/audio_coding/audio_network_adaptor/include/audio_networ k_adaptor.h" | 21 #include "webrtc/modules/audio_coding/audio_network_adaptor/include/audio_networ k_adaptor.h" |
19 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" | 22 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" |
20 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | 23 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
21 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 24 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
22 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/app.h" | 25 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/app.h" |
23 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h" | 26 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h" |
24 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h" | 27 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h" |
25 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_jitter_report.h" | 28 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_jitter_report.h" |
26 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h" | 29 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h" |
27 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/psfb.h" | 30 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/psfb.h" |
28 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h" | 31 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h" |
29 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rtpfb.h" | 32 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rtpfb.h" |
30 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h" | 33 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h" |
31 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h" | 34 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h" |
32 #include "webrtc/rtc_base/atomicops.h" | |
33 #include "webrtc/rtc_base/checks.h" | 35 #include "webrtc/rtc_base/checks.h" |
34 #include "webrtc/rtc_base/constructormagic.h" | 36 #include "webrtc/rtc_base/constructormagic.h" |
35 #include "webrtc/rtc_base/event.h" | 37 #include "webrtc/rtc_base/event.h" |
38 #include "webrtc/rtc_base/ignore_wundef.h" | |
36 #include "webrtc/rtc_base/logging.h" | 39 #include "webrtc/rtc_base/logging.h" |
37 #include "webrtc/rtc_base/protobuf_utils.h" | 40 #include "webrtc/rtc_base/protobuf_utils.h" |
38 #include "webrtc/rtc_base/swap_queue.h" | 41 #include "webrtc/rtc_base/ptr_util.h" |
39 #include "webrtc/rtc_base/thread_checker.h" | 42 #include "webrtc/rtc_base/sequenced_task_checker.h" |
43 #include "webrtc/rtc_base/task_queue.h" | |
44 #include "webrtc/rtc_base/thread_annotations.h" | |
40 #include "webrtc/rtc_base/timeutils.h" | 45 #include "webrtc/rtc_base/timeutils.h" |
41 #include "webrtc/system_wrappers/include/file_wrapper.h" | 46 #include "webrtc/system_wrappers/include/file_wrapper.h" |
47 #include "webrtc/typedefs.h" | |
42 | 48 |
43 #ifdef ENABLE_RTC_EVENT_LOG | 49 #ifdef ENABLE_RTC_EVENT_LOG |
44 // *.pb.h files are generated at build-time by the protobuf compiler. | 50 // *.ph.h files are generated at build-time by the protobuf compiler. |
51 RTC_PUSH_IGNORING_WUNDEF() | |
45 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD | 52 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
46 #include "external/webrtc/webrtc/logging/rtc_event_log/rtc_event_log.pb.h" | 53 #include "external/webrtc/webrtc/logging/rtc_event_log/rtc_event_log.pb.h" |
47 #else | 54 #else |
48 #include "webrtc/logging/rtc_event_log/rtc_event_log.pb.h" | 55 #include "webrtc/logging/rtc_event_log/rtc_event_log.pb.h" |
49 #endif | 56 #endif |
57 RTC_POP_IGNORING_WUNDEF() | |
50 #endif | 58 #endif |
51 | 59 |
52 namespace webrtc { | 60 namespace webrtc { |
53 | 61 |
54 #ifdef ENABLE_RTC_EVENT_LOG | 62 #ifdef ENABLE_RTC_EVENT_LOG |
55 | 63 |
64 namespace { | |
65 const int kEventsInHistory = 10000; | |
66 | |
67 bool IsConfigEvent(const rtclog::Event& event) { | |
68 rtclog::Event_EventType event_type = event.type(); | |
69 return event_type == rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT || | |
70 event_type == rtclog::Event::VIDEO_SENDER_CONFIG_EVENT || | |
71 event_type == rtclog::Event::AUDIO_RECEIVER_CONFIG_EVENT || | |
72 event_type == rtclog::Event::AUDIO_SENDER_CONFIG_EVENT; | |
73 } | |
74 | |
75 // This class exists because C++11 doesn't allow transferring a unique_ptr to | |
76 // a lambda (a copy constructor is required). | |
77 class RtcEvent final : public rtc::QueuedTask { | |
terelius
2017/08/28 13:35:55
Maybe RtcEventTask or RtcEventLogTask to make the
eladalon
2017/08/29 13:36:55
Done.
| |
78 public: | |
79 RtcEvent(std::unique_ptr<rtclog::Event> event, | |
80 std::function<void(std::unique_ptr<rtclog::Event>)> handler) | |
81 : event_(std::move(event)), handler_(handler) {} | |
82 | |
83 bool Run() override { | |
84 handler_(std::move(event_)); | |
85 return true; | |
86 } | |
87 | |
88 private: | |
89 std::unique_ptr<rtclog::Event> event_; | |
90 std::function<void(std::unique_ptr<rtclog::Event>)> handler_; | |
91 }; | |
92 } // namespace | |
93 | |
56 class RtcEventLogImpl final : public RtcEventLog { | 94 class RtcEventLogImpl final : public RtcEventLog { |
57 friend std::unique_ptr<RtcEventLog> RtcEventLog::Create(); | 95 friend std::unique_ptr<RtcEventLog> RtcEventLog::Create(); |
58 | 96 |
59 public: | 97 public: |
60 ~RtcEventLogImpl() override; | 98 ~RtcEventLogImpl() override; |
61 | 99 |
62 bool StartLogging(const std::string& file_name, | 100 bool StartLogging(const std::string& file_name, |
63 int64_t max_size_bytes) override; | 101 int64_t max_size_bytes) override; |
64 bool StartLogging(rtc::PlatformFile platform_file, | 102 bool StartLogging(rtc::PlatformFile platform_file, |
65 int64_t max_size_bytes) override; | 103 int64_t max_size_bytes) override; |
(...skipping 22 matching lines...) Expand all Loading... | |
88 const AudioEncoderRuntimeConfig& config) override; | 126 const AudioEncoderRuntimeConfig& config) override; |
89 void LogProbeClusterCreated(int id, | 127 void LogProbeClusterCreated(int id, |
90 int bitrate_bps, | 128 int bitrate_bps, |
91 int min_probes, | 129 int min_probes, |
92 int min_bytes) override; | 130 int min_bytes) override; |
93 void LogProbeResultSuccess(int id, int bitrate_bps) override; | 131 void LogProbeResultSuccess(int id, int bitrate_bps) override; |
94 void LogProbeResultFailure(int id, | 132 void LogProbeResultFailure(int id, |
95 ProbeFailureReason failure_reason) override; | 133 ProbeFailureReason failure_reason) override; |
96 | 134 |
97 private: | 135 private: |
98 // Private constructor to ensure that creation is done by RtcEventLog::Create. | 136 void StartLoggingInternal(std::unique_ptr<FileWrapper> file, |
99 RtcEventLogImpl(); | 137 int64_t max_size_bytes); |
138 | |
139 RtcEventLogImpl(); // Creation is done by RtcEventLog::Create. | |
100 | 140 |
101 void StoreEvent(std::unique_ptr<rtclog::Event> event); | 141 void StoreEvent(std::unique_ptr<rtclog::Event> event); |
142 void LogEvent(std::unique_ptr<rtclog::Event> rtclog_event); | |
102 void LogProbeResult(int id, | 143 void LogProbeResult(int id, |
103 rtclog::BweProbeResult::ResultType result, | 144 rtclog::BweProbeResult::ResultType result, |
104 int bitrate_bps); | 145 int bitrate_bps); |
105 | 146 |
106 static volatile int log_count_; | 147 // Attempts to append an event to the output protobuf string, while observing |
148 // the (potential) limitation on file size. (False is returned when observing | |
149 // the limitation disallows appending the event.) | |
150 bool AppendEventToString(rtclog::Event* event, | |
151 ProtoString* output_string) RTC_WARN_UNUSED_RESULT; | |
107 | 152 |
108 // Message queue for passing control messages to the logging thread. | 153 void LogToMemory(std::unique_ptr<rtclog::Event> event); |
109 SwapQueue<RtcEventLogHelperThread::ControlMessage> message_queue_; | |
110 | 154 |
111 // Message queue for passing events to the logging thread. | 155 void StartLogFile(int64_t start_time); |
112 SwapQueue<std::unique_ptr<rtclog::Event> > event_queue_; | 156 void LogToFile(std::unique_ptr<rtclog::Event> event); |
157 void StopLogFile(int64_t stop_time); | |
113 | 158 |
114 RtcEventLogHelperThread helper_thread_; | 159 // TODO(eladalon): Known issue - there's a race over |log_count_|. |
115 rtc::ThreadChecker thread_checker_; | 160 static std::atomic<int> log_count_; |
161 | |
162 rtc::SequencedTaskChecker worker_sequence_checker_; | |
163 rtc::SequencedTaskChecker owner_sequence_checker_; | |
164 | |
165 // History containing all past configuration events. | |
166 std::vector<std::unique_ptr<rtclog::Event>> config_history_ | |
167 GUARDED_BY(worker_sequence_checker_); | |
168 | |
169 // History containing the most recent (non-configuration) events (~ 10 s). | |
170 std::deque<std::unique_ptr<rtclog::Event>> history_ | |
171 GUARDED_BY(worker_sequence_checker_); | |
172 | |
173 std::unique_ptr<FileWrapper> file_ GUARDED_BY(worker_sequence_checker_); | |
174 | |
175 size_t max_size_bytes_ GUARDED_BY(worker_sequence_checker_); | |
176 size_t written_bytes_ GUARDED_BY(worker_sequence_checker_); | |
177 | |
178 // Keep this last to ensure it destructs first, or else tasks living on the | |
179 // queue might access other members after they've been torn down. | |
180 rtc::TaskQueue task_queue_; | |
116 | 181 |
117 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogImpl); | 182 RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogImpl); |
118 }; | 183 }; |
119 | 184 |
120 namespace { | 185 namespace { |
121 // The functions in this namespace convert enums from the runtime format | 186 // The functions in this namespace convert enums from the runtime format |
122 // that the rest of the WebRtc project can use, to the corresponding | 187 // that the rest of the WebRtc project can use, to the corresponding |
123 // serialized enum which is defined by the protobuf. | 188 // serialized enum which is defined by the protobuf. |
124 | 189 |
125 rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) { | 190 rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 return rtclog::BweProbeResult::INVALID_SEND_RECEIVE_INTERVAL; | 222 return rtclog::BweProbeResult::INVALID_SEND_RECEIVE_INTERVAL; |
158 case kInvalidSendReceiveRatio: | 223 case kInvalidSendReceiveRatio: |
159 return rtclog::BweProbeResult::INVALID_SEND_RECEIVE_RATIO; | 224 return rtclog::BweProbeResult::INVALID_SEND_RECEIVE_RATIO; |
160 case kTimeout: | 225 case kTimeout: |
161 return rtclog::BweProbeResult::TIMEOUT; | 226 return rtclog::BweProbeResult::TIMEOUT; |
162 } | 227 } |
163 RTC_NOTREACHED(); | 228 RTC_NOTREACHED(); |
164 return rtclog::BweProbeResult::SUCCESS; | 229 return rtclog::BweProbeResult::SUCCESS; |
165 } | 230 } |
166 | 231 |
167 // The RTP and RTCP buffers reserve space for twice the expected number of | |
168 // sent packets because they also contain received packets. | |
169 static const int kEventsPerSecond = 1000; | |
170 static const int kControlMessagesPerSecond = 10; | |
171 } // namespace | 232 } // namespace |
172 | 233 |
173 volatile int RtcEventLogImpl::log_count_ = 0; | 234 std::atomic<int> RtcEventLogImpl::log_count_(0); |
174 | 235 |
175 // RtcEventLogImpl member functions. | |
176 RtcEventLogImpl::RtcEventLogImpl() | 236 RtcEventLogImpl::RtcEventLogImpl() |
177 // Allocate buffers for roughly one second of history. | 237 : file_(FileWrapper::Create()), |
178 : message_queue_(kControlMessagesPerSecond), | 238 max_size_bytes_(std::numeric_limits<decltype(max_size_bytes_)>::max()), |
179 event_queue_(kEventsPerSecond), | 239 written_bytes_(0), |
180 helper_thread_(&message_queue_, &event_queue_), | 240 task_queue_("rtc_event_log") { |
181 thread_checker_() { | 241 worker_sequence_checker_.Detach(); |
182 thread_checker_.DetachFromThread(); | |
183 } | 242 } |
184 | 243 |
185 RtcEventLogImpl::~RtcEventLogImpl() { | 244 RtcEventLogImpl::~RtcEventLogImpl() { |
186 // The RtcEventLogHelperThread destructor closes the file | 245 RTC_DCHECK_CALLED_SEQUENTIALLY(&owner_sequence_checker_); |
187 // and waits for the thread to terminate. | 246 |
188 int count = rtc::AtomicOps::Decrement(&RtcEventLogImpl::log_count_); | 247 // If we're logging to the file, this will stop that. Blocking function. |
248 StopLogging(); | |
249 | |
250 int count = std::atomic_fetch_sub(&RtcEventLogImpl::log_count_, 1) - 1; | |
189 RTC_DCHECK_GE(count, 0); | 251 RTC_DCHECK_GE(count, 0); |
190 } | 252 } |
191 | 253 |
192 bool RtcEventLogImpl::StartLogging(const std::string& file_name, | 254 bool RtcEventLogImpl::StartLogging(const std::string& file_name, |
193 int64_t max_size_bytes) { | 255 int64_t max_size_bytes) { |
194 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 256 RTC_DCHECK_CALLED_SEQUENTIALLY(&owner_sequence_checker_); |
195 RtcEventLogHelperThread::ControlMessage message; | 257 |
196 message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE; | 258 auto file = rtc::WrapUnique<FileWrapper>(FileWrapper::Create()); |
197 message.max_size_bytes = max_size_bytes <= 0 | 259 if (!file->OpenFile(file_name.c_str(), false)) { |
198 ? std::numeric_limits<int64_t>::max() | |
199 : max_size_bytes; | |
200 message.start_time = rtc::TimeMicros(); | |
201 message.stop_time = std::numeric_limits<int64_t>::max(); | |
202 message.file.reset(FileWrapper::Create()); | |
203 if (!message.file->OpenFile(file_name.c_str(), false)) { | |
204 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; | 260 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; |
205 return false; | 261 return false; |
206 } | 262 } |
207 if (!message_queue_.Insert(&message)) { | 263 |
208 LOG(LS_ERROR) << "Message queue full. Can't start logging."; | 264 StartLoggingInternal(std::move(file), max_size_bytes); |
209 return false; | 265 |
210 } | |
211 helper_thread_.SignalNewEvent(); | |
212 LOG(LS_INFO) << "Starting WebRTC event log."; | |
213 return true; | 266 return true; |
214 } | 267 } |
215 | 268 |
216 bool RtcEventLogImpl::StartLogging(rtc::PlatformFile platform_file, | 269 bool RtcEventLogImpl::StartLogging(rtc::PlatformFile platform_file, |
217 int64_t max_size_bytes) { | 270 int64_t max_size_bytes) { |
218 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 271 RTC_DCHECK_CALLED_SEQUENTIALLY(&owner_sequence_checker_); |
219 RtcEventLogHelperThread::ControlMessage message; | 272 |
220 message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE; | 273 auto file = rtc::WrapUnique<FileWrapper>(FileWrapper::Create()); |
221 message.max_size_bytes = max_size_bytes <= 0 | |
222 ? std::numeric_limits<int64_t>::max() | |
223 : max_size_bytes; | |
224 message.start_time = rtc::TimeMicros(); | |
225 message.stop_time = std::numeric_limits<int64_t>::max(); | |
226 message.file.reset(FileWrapper::Create()); | |
227 FILE* file_handle = rtc::FdopenPlatformFileForWriting(platform_file); | 274 FILE* file_handle = rtc::FdopenPlatformFileForWriting(platform_file); |
228 if (!file_handle) { | 275 if (!file_handle) { |
229 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; | 276 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; |
230 // Even though we failed to open a FILE*, the platform_file is still open | 277 // Even though we failed to open a FILE*, the platform_file is still open |
231 // and needs to be closed. | 278 // and needs to be closed. |
232 if (!rtc::ClosePlatformFile(platform_file)) { | 279 if (!rtc::ClosePlatformFile(platform_file)) { |
233 LOG(LS_ERROR) << "Can't close file."; | 280 LOG(LS_ERROR) << "Can't close file."; |
234 } | 281 } |
235 return false; | 282 return false; |
236 } | 283 } |
237 if (!message.file->OpenFromFileHandle(file_handle)) { | 284 if (!file->OpenFromFileHandle(file_handle)) { |
238 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; | 285 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; |
239 return false; | 286 return false; |
240 } | 287 } |
241 if (!message_queue_.Insert(&message)) { | 288 |
242 LOG(LS_ERROR) << "Message queue full. Can't start logging."; | 289 StartLoggingInternal(std::move(file), max_size_bytes); |
243 return false; | 290 |
244 } | |
245 helper_thread_.SignalNewEvent(); | |
246 LOG(LS_INFO) << "Starting WebRTC event log."; | |
247 return true; | 291 return true; |
248 } | 292 } |
249 | 293 |
250 void RtcEventLogImpl::StopLogging() { | 294 void RtcEventLogImpl::StopLogging() { |
251 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 295 RTC_DCHECK_CALLED_SEQUENTIALLY(&owner_sequence_checker_); |
252 RtcEventLogHelperThread::ControlMessage message; | 296 |
253 message.message_type = RtcEventLogHelperThread::ControlMessage::STOP_FILE; | |
254 message.stop_time = rtc::TimeMicros(); | |
255 while (!message_queue_.Insert(&message)) { | |
256 // TODO(terelius): We would like to have a blocking Insert function in the | |
257 // SwapQueue, but for the time being we will just clear any previous | |
258 // messages. | |
259 // Since StopLogging waits for the thread, it is essential that we don't | |
260 // clear any STOP_FILE messages. To ensure that there is only one call at a | |
261 // time, we require that all calls to StopLogging are made on the same | |
262 // thread. | |
263 LOG(LS_ERROR) << "Message queue full. Clearing queue to stop logging."; | |
264 message_queue_.Clear(); | |
265 } | |
266 LOG(LS_INFO) << "Stopping WebRTC event log."; | 297 LOG(LS_INFO) << "Stopping WebRTC event log."; |
267 helper_thread_.WaitForFileFinished(); | 298 |
299 const int64_t stop_time = rtc::TimeMicros(); | |
300 | |
301 task_queue_.PostTask([this, stop_time]() { | |
302 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_); | |
303 if (file_->is_open()) { | |
304 StopLogFile(stop_time); | |
305 } | |
306 }); | |
307 | |
308 // Wait for an indication that the previous task, which stops the log file, | |
309 // has been executed to completion. | |
310 rtc::Event file_finished(true, false); | |
311 task_queue_.PostTask([this, &file_finished]() { | |
312 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_); | |
313 file_finished.Set(); | |
314 }); | |
315 file_finished.Wait(rtc::Event::kForever); | |
316 | |
317 LOG(LS_INFO) << "WebRTC event log successfully stopped."; | |
268 } | 318 } |
269 | 319 |
270 void RtcEventLogImpl::LogVideoReceiveStreamConfig( | 320 void RtcEventLogImpl::LogVideoReceiveStreamConfig( |
271 const rtclog::StreamConfig& config) { | 321 const rtclog::StreamConfig& config) { |
272 std::unique_ptr<rtclog::Event> event(new rtclog::Event()); | 322 std::unique_ptr<rtclog::Event> event(new rtclog::Event()); |
273 event->set_timestamp_us(rtc::TimeMicros()); | 323 event->set_timestamp_us(rtc::TimeMicros()); |
274 event->set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); | 324 event->set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); |
275 | 325 |
276 rtclog::VideoReceiveConfig* receiver_config = | 326 rtclog::VideoReceiveConfig* receiver_config = |
277 event->mutable_video_receiver_config(); | 327 event->mutable_video_receiver_config(); |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
558 event->set_type(rtclog::Event::BWE_PROBE_RESULT_EVENT); | 608 event->set_type(rtclog::Event::BWE_PROBE_RESULT_EVENT); |
559 | 609 |
560 auto probe_result = event->mutable_probe_result(); | 610 auto probe_result = event->mutable_probe_result(); |
561 probe_result->set_id(id); | 611 probe_result->set_id(id); |
562 probe_result->set_result(result); | 612 probe_result->set_result(result); |
563 if (result == rtclog::BweProbeResult::SUCCESS) | 613 if (result == rtclog::BweProbeResult::SUCCESS) |
564 probe_result->set_bitrate_bps(bitrate_bps); | 614 probe_result->set_bitrate_bps(bitrate_bps); |
565 StoreEvent(std::move(event)); | 615 StoreEvent(std::move(event)); |
566 } | 616 } |
567 | 617 |
618 void RtcEventLogImpl::StartLoggingInternal(std::unique_ptr<FileWrapper> file, | |
619 int64_t max_size_bytes) { | |
620 LOG(LS_INFO) << "Starting WebRTC event log."; | |
621 | |
622 int64_t start_time = rtc::TimeMicros(); | |
623 max_size_bytes = (max_size_bytes <= 0) ? std::numeric_limits<int64_t>::max() | |
624 : max_size_bytes; | |
625 | |
626 // Kept as a raw pointer due to limitations C++11 imposes on lambdas; namely, | |
627 // the necessity of a copy-constructor. | |
628 FileWrapper* file_raw = file.release(); | |
629 | |
630 task_queue_.PostTask([this, start_time, max_size_bytes, file_raw]() { | |
terelius
2017/08/28 13:35:55
Are tasks guaranteed to be executed or is there an
eladalon
2017/08/29 13:36:55
You're right in theory (thanks for catching!), bec
| |
631 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_); | |
632 if (!file_->is_open()) { | |
633 max_size_bytes_ = max_size_bytes; | |
634 file_.reset(file_raw); | |
635 StartLogFile(start_time); | |
636 } else { | |
637 // Already started. Ignore message and close file handle. | |
638 file_raw->CloseFile(); | |
639 delete file_raw; | |
640 } | |
641 }); | |
642 } | |
643 | |
568 void RtcEventLogImpl::StoreEvent(std::unique_ptr<rtclog::Event> event) { | 644 void RtcEventLogImpl::StoreEvent(std::unique_ptr<rtclog::Event> event) { |
569 RTC_DCHECK(event.get() != nullptr); | 645 RTC_DCHECK(event); |
570 if (!event_queue_.Insert(&event)) { | 646 // LogEvent is necessary because lambdas in C++11 need their captured |
571 LOG(LS_ERROR) << "WebRTC event log queue full. Dropping event."; | 647 // arguments to have a copy-constructor. |
648 auto event_handler = [this](std::unique_ptr<rtclog::Event> rtclog_event) { | |
649 LogEvent(std::move(rtclog_event)); | |
650 }; | |
651 auto task = rtc::WrapUnique<rtc::QueuedTask>( | |
652 new RtcEvent(std::move(event), event_handler)); | |
653 task_queue_.PostTask(std::move(task)); | |
654 } | |
655 | |
656 void RtcEventLogImpl::LogEvent(std::unique_ptr<rtclog::Event> event) { | |
657 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_); | |
658 if (file_->is_open()) { | |
659 LogToFile(std::move(event)); | |
660 } else { | |
661 LogToMemory(std::move(event)); | |
572 } | 662 } |
573 helper_thread_.SignalNewEvent(); | 663 } |
664 | |
665 bool RtcEventLogImpl::AppendEventToString(rtclog::Event* event, | |
666 ProtoString* output_string) { | |
667 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_); | |
668 rtclog::EventStream event_stream; | |
669 event_stream.add_stream(); | |
670 event_stream.mutable_stream(0)->Swap(event); | |
671 // We create a new event stream per event but because of the way protobufs | |
672 // are encoded, events can be merged by concatenating them. Therefore, | |
673 // it will look like a single stream when we read it back from file. | |
674 bool appended; | |
675 size_t potential_new_size = | |
676 written_bytes_ + output_string->size() + event_stream.ByteSize(); | |
677 if (potential_new_size <= max_size_bytes_) { | |
678 event_stream.AppendToString(output_string); | |
679 appended = true; | |
680 } else { | |
681 appended = false; | |
682 } | |
683 // Swap the event back so that we don't mix event types in the queues. | |
terelius
2017/08/28 13:35:55
I still think we should do the Swap(), but this co
eladalon
2017/08/29 13:36:55
I've freshened up the comments.
| |
684 event_stream.mutable_stream(0)->Swap(event); | |
685 return appended; | |
686 } | |
687 | |
688 void RtcEventLogImpl::LogToMemory(std::unique_ptr<rtclog::Event> event) { | |
689 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_); | |
690 RTC_DCHECK(!file_->is_open()); | |
691 | |
692 if (IsConfigEvent(*event.get())) { | |
693 config_history_.push_back(std::move(event)); | |
694 } else { | |
695 history_.push_back(std::move(event)); | |
696 if (history_.size() > kEventsInHistory) { | |
697 history_.pop_front(); | |
698 } | |
699 } | |
700 } | |
701 | |
702 void RtcEventLogImpl::StartLogFile(int64_t start_time) { | |
703 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_); | |
704 RTC_DCHECK(file_->is_open()); | |
705 | |
706 ProtoString output_string; | |
707 | |
708 // Create and serialize the LOG_START event. | |
709 rtclog::Event start_event; | |
710 // TODO(eladalon): !!! This time precedes the timestamps in history_. Is that | |
eladalon
2017/08/24 09:23:25
Björn, what do you think?
terelius
2017/08/28 13:35:55
You mean that it is written to file before seriali
eladalon
2017/08/29 13:36:55
For posterity - we've discussed this some more off
| |
711 // a problem for us? | |
712 start_event.set_timestamp_us(start_time); | |
713 start_event.set_type(rtclog::Event::LOG_START); | |
714 bool appended = AppendEventToString(&start_event, &output_string); | |
715 | |
716 // Serialize the config information for all old streams, including streams | |
717 // which were already logged to previous files. | |
718 for (auto& event : config_history_) { | |
719 if (!appended) { | |
720 break; | |
721 } | |
722 appended = AppendEventToString(event.get(), &output_string); | |
723 } | |
724 | |
725 // Serialize the events in the event queue. | |
726 while (appended && !history_.empty()) { | |
727 appended = AppendEventToString(history_.front().get(), &output_string); | |
728 if (appended) { | |
729 // TODO(eladalon): !!! Do we really want to pop before the write to | |
eladalon
2017/08/24 09:23:26
Björn, what do you think?
terelius
2017/08/28 13:35:54
I don't think this matters one way or the other. T
eladalon
2017/08/29 13:36:55
I can document it as a known-potential-unlikely-is
| |
730 // the file proves successful? | |
731 history_.pop_front(); | |
732 } | |
733 } | |
734 | |
735 // Write to file. | |
736 if (!file_->Write(output_string.data(), output_string.size())) { | |
737 LOG(LS_ERROR) << "FileWrapper failed to write WebRtcEventLog file."; | |
738 // The current FileWrapper implementation closes the file on error. | |
739 RTC_DCHECK(!file_->is_open()); | |
740 return; | |
741 } | |
742 written_bytes_ += output_string.size(); | |
743 | |
744 if (!appended) { | |
745 RTC_DCHECK(file_->is_open()); | |
746 StopLogFile(rtc::TimeMicros()); | |
747 } | |
748 } | |
749 | |
750 void RtcEventLogImpl::LogToFile(std::unique_ptr<rtclog::Event> event) { | |
751 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_); | |
752 RTC_DCHECK(file_->is_open()); | |
753 | |
754 ProtoString output_string; | |
755 | |
756 bool appended = AppendEventToString(event.get(), &output_string); | |
757 | |
758 if (IsConfigEvent(*event.get())) { | |
759 config_history_.push_back(std::move(event)); | |
760 } | |
761 | |
762 if (!appended) { | |
763 RTC_DCHECK(file_->is_open()); | |
764 StopLogFile(rtc::TimeMicros()); | |
765 return; | |
766 } | |
767 | |
768 // Write string to file. | |
769 if (file_->Write(output_string.data(), output_string.size())) { | |
770 written_bytes_ += output_string.size(); | |
771 } else { | |
772 LOG(LS_ERROR) << "FileWrapper failed to write WebRtcEventLog file."; | |
773 // The current FileWrapper implementation closes the file on error. | |
774 RTC_DCHECK(!file_->is_open()); | |
775 } | |
776 } | |
777 | |
778 void RtcEventLogImpl::StopLogFile(int64_t stop_time) { | |
779 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_); | |
780 RTC_DCHECK(file_->is_open()); | |
781 | |
782 ProtoString output_string; | |
783 | |
784 rtclog::Event end_event; | |
785 end_event.set_timestamp_us(stop_time); | |
786 end_event.set_type(rtclog::Event::LOG_END); | |
787 bool appended = AppendEventToString(&end_event, &output_string); | |
788 | |
789 if (appended) { | |
790 if (!file_->Write(output_string.data(), output_string.size())) { | |
791 LOG(LS_ERROR) << "FileWrapper failed to write WebRtcEventLog file."; | |
792 // The current FileWrapper implementation closes the file on error. | |
793 RTC_DCHECK(!file_->is_open()); | |
794 } | |
795 written_bytes_ += output_string.size(); | |
796 } | |
797 | |
798 max_size_bytes_ = std::numeric_limits<int64_t>::max(); | |
799 written_bytes_ = 0; | |
800 | |
801 file_->CloseFile(); | |
802 RTC_DCHECK(!file_->is_open()); | |
574 } | 803 } |
575 | 804 |
576 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, | 805 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, |
577 rtclog::EventStream* result) { | 806 rtclog::EventStream* result) { |
578 char tmp_buffer[1024]; | 807 char tmp_buffer[1024]; |
579 int bytes_read = 0; | 808 int bytes_read = 0; |
580 std::unique_ptr<FileWrapper> dump_file(FileWrapper::Create()); | 809 std::unique_ptr<FileWrapper> dump_file(FileWrapper::Create()); |
581 if (!dump_file->OpenFile(file_name.c_str(), true)) { | 810 if (!dump_file->OpenFile(file_name.c_str(), true)) { |
582 return false; | 811 return false; |
583 } | 812 } |
584 ProtoString dump_buffer; | 813 ProtoString dump_buffer; |
585 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { | 814 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { |
586 dump_buffer.append(tmp_buffer, bytes_read); | 815 dump_buffer.append(tmp_buffer, bytes_read); |
587 } | 816 } |
588 dump_file->CloseFile(); | 817 dump_file->CloseFile(); |
589 return result->ParseFromString(dump_buffer); | 818 return result->ParseFromString(dump_buffer); |
590 } | 819 } |
591 | 820 |
592 #endif // ENABLE_RTC_EVENT_LOG | 821 #endif // ENABLE_RTC_EVENT_LOG |
593 | 822 |
594 // RtcEventLog member functions. | 823 // RtcEventLog member functions. |
595 std::unique_ptr<RtcEventLog> RtcEventLog::Create() { | 824 std::unique_ptr<RtcEventLog> RtcEventLog::Create() { |
596 #ifdef ENABLE_RTC_EVENT_LOG | 825 #ifdef ENABLE_RTC_EVENT_LOG |
826 // TODO(eladalon): Known issue - there's a race over |log_count_| here. | |
597 constexpr int kMaxLogCount = 5; | 827 constexpr int kMaxLogCount = 5; |
598 int count = rtc::AtomicOps::Increment(&RtcEventLogImpl::log_count_); | 828 int count = 1 + std::atomic_fetch_add(&RtcEventLogImpl::log_count_, 1); |
599 if (count > kMaxLogCount) { | 829 if (count > kMaxLogCount) { |
600 LOG(LS_WARNING) << "Denied creation of additional WebRTC event logs. " | 830 LOG(LS_WARNING) << "Denied creation of additional WebRTC event logs. " |
601 << count - 1 << " logs open already."; | 831 << count - 1 << " logs open already."; |
602 rtc::AtomicOps::Decrement(&RtcEventLogImpl::log_count_); | 832 std::atomic_fetch_sub(&RtcEventLogImpl::log_count_, 1); |
603 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl()); | 833 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl()); |
604 } | 834 } |
605 return std::unique_ptr<RtcEventLog>(new RtcEventLogImpl()); | 835 return std::unique_ptr<RtcEventLog>(new RtcEventLogImpl()); |
606 #else | 836 #else |
607 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl()); | 837 return CreateNull(); |
608 #endif // ENABLE_RTC_EVENT_LOG | 838 #endif // ENABLE_RTC_EVENT_LOG |
609 } | 839 } |
610 | 840 |
611 std::unique_ptr<RtcEventLog> RtcEventLog::CreateNull() { | 841 std::unique_ptr<RtcEventLog> RtcEventLog::CreateNull() { |
612 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl()); | 842 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl()); |
613 } | 843 } |
614 | 844 |
615 } // namespace webrtc | 845 } // namespace webrtc |
OLD | NEW |