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