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 |
(...skipping 19 matching lines...) Expand all Loading... | |
30 #endif | 30 #endif |
31 | 31 |
32 namespace webrtc { | 32 namespace webrtc { |
33 | 33 |
34 #ifndef ENABLE_RTC_EVENT_LOG | 34 #ifndef ENABLE_RTC_EVENT_LOG |
35 | 35 |
36 // No-op implementation if flag is not set. | 36 // No-op implementation if flag is not set. |
37 class RtcEventLogImpl final : public RtcEventLog { | 37 class RtcEventLogImpl final : public RtcEventLog { |
38 public: | 38 public: |
39 void StartLogging(const std::string& file_name, int duration_ms) override {} | 39 void StartLogging(const std::string& file_name, int duration_ms) override {} |
40 int StartLogging(FILE* log_file) override { return -1; } | |
41 int StartLogging(rtc::PlatformFile& log_file) override { return -1; } | |
40 void StopLogging(void) override {} | 42 void StopLogging(void) override {} |
41 void LogVideoReceiveStreamConfig( | 43 void LogVideoReceiveStreamConfig( |
42 const VideoReceiveStream::Config& config) override {} | 44 const VideoReceiveStream::Config& config) override {} |
43 void LogVideoSendStreamConfig( | 45 void LogVideoSendStreamConfig( |
44 const VideoSendStream::Config& config) override {} | 46 const VideoSendStream::Config& config) override {} |
45 void LogRtpHeader(bool incoming, | 47 void LogRtpHeader(bool incoming, |
46 MediaType media_type, | 48 MediaType media_type, |
47 const uint8_t* header, | 49 const uint8_t* header, |
48 size_t packet_length) override {} | 50 size_t packet_length) override {} |
49 void LogRtcpPacket(bool incoming, | 51 void LogRtcpPacket(bool incoming, |
50 MediaType media_type, | 52 MediaType media_type, |
51 const uint8_t* packet, | 53 const uint8_t* packet, |
52 size_t length) override {} | 54 size_t length) override {} |
53 void LogAudioPlayout(uint32_t ssrc) override {} | 55 void LogAudioPlayout(uint32_t ssrc) override {} |
54 }; | 56 }; |
55 | 57 |
56 #else // ENABLE_RTC_EVENT_LOG is defined | 58 #else // ENABLE_RTC_EVENT_LOG is defined |
57 | 59 |
58 class RtcEventLogImpl final : public RtcEventLog { | 60 class RtcEventLogImpl final : public RtcEventLog { |
59 public: | 61 public: |
60 RtcEventLogImpl(); | 62 RtcEventLogImpl(); |
61 | 63 |
62 void StartLogging(const std::string& file_name, int duration_ms) override; | 64 void StartLogging(const std::string& file_name, int duration_ms) override; |
65 int StartLogging(FILE* log_file) override; | |
66 int StartLogging(rtc::PlatformFile& log_file) override; | |
63 void StopLogging() override; | 67 void StopLogging() override; |
64 void LogVideoReceiveStreamConfig( | 68 void LogVideoReceiveStreamConfig( |
65 const VideoReceiveStream::Config& config) override; | 69 const VideoReceiveStream::Config& config) override; |
66 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override; | 70 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override; |
67 void LogRtpHeader(bool incoming, | 71 void LogRtpHeader(bool incoming, |
68 MediaType media_type, | 72 MediaType media_type, |
69 const uint8_t* header, | 73 const uint8_t* header, |
70 size_t packet_length) override; | 74 size_t packet_length) override; |
71 void LogRtcpPacket(bool incoming, | 75 void LogRtcpPacket(bool incoming, |
72 MediaType media_type, | 76 MediaType media_type, |
73 const uint8_t* packet, | 77 const uint8_t* packet, |
74 size_t length) override; | 78 size_t length) override; |
75 void LogAudioPlayout(uint32_t ssrc) override; | 79 void LogAudioPlayout(uint32_t ssrc) override; |
76 | 80 |
77 private: | 81 private: |
82 // Starts logging. This function assumes the file_ has been opened succesfully | |
83 // and that the start_time_us_ and _duration_us_ have been set. | |
84 void StartLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
78 // Stops logging and clears the stored data and buffers. | 85 // Stops logging and clears the stored data and buffers. |
79 void StopLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_); | 86 void StopLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
80 // Adds a new event to the logfile if logging is active, or adds it to the | 87 // Adds a new event to the logfile if logging is active, or adds it to the |
81 // list of recent log events otherwise. | 88 // list of recent log events otherwise. |
82 void HandleEvent(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | 89 void HandleEvent(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
83 // Writes the event to the file. Note that this will destroy the state of the | 90 // Writes the event to the file. Note that this will destroy the state of the |
84 // input argument. | 91 // input argument. |
85 void StoreToFile(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | 92 void StoreToFile(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
86 // Adds the event to the list of recent events, and removes any events that | 93 // Adds the event to the list of recent events, and removes any events that |
87 // are too old and no longer fall in the time window. | 94 // are too old and no longer fall in the time window. |
88 void AddRecentEvent(const rtclog::Event& event) | 95 void AddRecentEvent(const rtclog::Event& event) |
89 EXCLUSIVE_LOCKS_REQUIRED(crit_); | 96 EXCLUSIVE_LOCKS_REQUIRED(crit_); |
90 | 97 |
91 // Amount of time in microseconds to record log events, before starting the | 98 // Amount of time in microseconds to record log events, before starting the |
92 // actual log. | 99 // actual log. |
93 const int recent_log_duration_us = 10000000; | 100 const int recent_log_duration_us = 10000000; |
94 | 101 |
95 rtc::CriticalSection crit_; | 102 rtc::CriticalSection crit_; |
96 rtc::scoped_ptr<FileWrapper> file_ GUARDED_BY(crit_); | 103 rtc::scoped_ptr<FileWrapper> file_ GUARDED_BY(crit_); |
104 rtc::PlatformFile platform_file_ GUARDED_BY(crit_); | |
105 bool using_platform_file_ GUARDED_BY(crit_); | |
97 rtclog::EventStream stream_ GUARDED_BY(crit_); | 106 rtclog::EventStream stream_ GUARDED_BY(crit_); |
98 std::deque<rtclog::Event> recent_log_events_ GUARDED_BY(crit_); | 107 std::deque<rtclog::Event> recent_log_events_ GUARDED_BY(crit_); |
99 bool currently_logging_ GUARDED_BY(crit_); | 108 bool currently_logging_ GUARDED_BY(crit_); |
100 int64_t start_time_us_ GUARDED_BY(crit_); | 109 int64_t start_time_us_ GUARDED_BY(crit_); |
101 int64_t duration_us_ GUARDED_BY(crit_); | 110 int64_t duration_us_ GUARDED_BY(crit_); |
102 const Clock* const clock_; | 111 const Clock* const clock_; |
103 }; | 112 }; |
104 | 113 |
105 namespace { | 114 namespace { |
106 // The functions in this namespace convert enums from the runtime format | 115 // The functions in this namespace convert enums from the runtime format |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 } | 147 } |
139 RTC_NOTREACHED(); | 148 RTC_NOTREACHED(); |
140 return rtclog::ANY; | 149 return rtclog::ANY; |
141 } | 150 } |
142 | 151 |
143 } // namespace | 152 } // namespace |
144 | 153 |
145 // RtcEventLogImpl member functions. | 154 // RtcEventLogImpl member functions. |
146 RtcEventLogImpl::RtcEventLogImpl() | 155 RtcEventLogImpl::RtcEventLogImpl() |
147 : file_(FileWrapper::Create()), | 156 : file_(FileWrapper::Create()), |
157 platform_file_(static_cast<rtc::PlatformFile>(0)), | |
the sun
2015/10/14 09:29:09
Use C++11 initialization (at member declaration).
ivoc
2015/10/14 12:23:47
Nice, was not familiar with this. I changed it for
| |
158 using_platform_file_(false), | |
148 stream_(), | 159 stream_(), |
149 currently_logging_(false), | 160 currently_logging_(false), |
150 start_time_us_(0), | 161 start_time_us_(0), |
151 duration_us_(0), | 162 duration_us_(0), |
152 clock_(Clock::GetRealTimeClock()) { | 163 clock_(Clock::GetRealTimeClock()) { |
153 } | 164 } |
154 | 165 |
155 void RtcEventLogImpl::StartLogging(const std::string& file_name, | 166 void RtcEventLogImpl::StartLogging(const std::string& file_name, |
156 int duration_ms) { | 167 int duration_ms) { |
157 rtc::CritScope lock(&crit_); | 168 rtc::CritScope lock(&crit_); |
158 if (currently_logging_) { | 169 if (currently_logging_) { |
159 StopLoggingLocked(); | 170 StopLoggingLocked(); |
160 } | 171 } |
161 if (file_->OpenFile(file_name.c_str(), false) != 0) { | 172 if (file_->OpenFile(file_name.c_str(), false) != 0) { |
162 return; | 173 return; |
163 } | 174 } |
164 currently_logging_ = true; | |
165 start_time_us_ = clock_->TimeInMicroseconds(); | 175 start_time_us_ = clock_->TimeInMicroseconds(); |
166 duration_us_ = static_cast<int64_t>(duration_ms) * 1000; | 176 duration_us_ = static_cast<int64_t>(duration_ms) * 1000; |
177 StartLoggingLocked(); | |
178 } | |
179 | |
180 int RtcEventLogImpl::StartLogging(FILE* log_file) { | |
the sun
2015/10/14 09:29:09
Used?
ivoc
2015/10/14 12:23:47
Not currently, I left it in because I thought it m
| |
181 rtc::CritScope lock(&crit_); | |
182 | |
183 if (currently_logging_) | |
184 StopLoggingLocked(); | |
185 RTC_DCHECK(log_file); | |
186 if (file_->OpenFromFileHandle(log_file, true, false) != 0) { | |
187 return -1; | |
188 } | |
189 // Set the start time and duration to keep logging for 10 minutes. | |
190 start_time_us_ = clock_->TimeInMicroseconds(); | |
191 duration_us_ = 10 * 60 * 1000000; | |
192 StartLoggingLocked(); | |
193 return 0; | |
194 } | |
195 | |
196 int RtcEventLogImpl::StartLogging(rtc::PlatformFile& log_file) { | |
197 rtc::CritScope lock(&crit_); | |
198 | |
199 if (currently_logging_) | |
the sun
2015/10/14 09:29:09
please, always use braces
ivoc
2015/10/14 12:23:47
Done.
| |
200 StopLoggingLocked(); | |
201 | |
the sun
2015/10/14 09:29:09
RTC_DCHECK(!using_platform_file)
ivoc
2015/10/14 12:23:47
Done.
| |
202 FILE* file_stream = rtc::FdopenPlatformFileForWriting(log_file); | |
203 if (!file_stream) { | |
204 rtc::ClosePlatformFile(log_file); | |
205 return -1; | |
206 } | |
207 | |
208 if (file_->OpenFromFileHandle(file_stream, true, false) != 0) { | |
209 rtc::ClosePlatformFile(log_file); | |
210 return -1; | |
211 } | |
212 using_platform_file_ = true; | |
213 // Set the start time and duration to keep logging for 10 minutes. | |
214 start_time_us_ = clock_->TimeInMicroseconds(); | |
215 duration_us_ = 10 * 60 * 1000000; | |
216 StartLoggingLocked(); | |
217 return 0; | |
218 } | |
219 | |
220 void RtcEventLogImpl::StartLoggingLocked() { | |
221 currently_logging_ = true; | |
167 // Write all the recent events to the log file, ignoring any old events. | 222 // Write all the recent events to the log file, ignoring any old events. |
168 for (auto& event : recent_log_events_) { | 223 for (auto& event : recent_log_events_) { |
169 if (event.timestamp_us() >= start_time_us_ - recent_log_duration_us) { | 224 if (event.timestamp_us() >= start_time_us_ - recent_log_duration_us) { |
170 StoreToFile(&event); | 225 StoreToFile(&event); |
171 } | 226 } |
172 } | 227 } |
173 recent_log_events_.clear(); | 228 recent_log_events_.clear(); |
174 // Write a LOG_START event to the file. | 229 // Write a LOG_START event to the file. |
175 rtclog::Event start_event; | 230 rtclog::Event start_event; |
176 start_event.set_timestamp_us(start_time_us_); | 231 start_event.set_timestamp_us(start_time_us_); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 currently_logging_ = false; | 387 currently_logging_ = false; |
333 // Create a LogEnd event | 388 // Create a LogEnd event |
334 rtclog::Event event; | 389 rtclog::Event event; |
335 int64_t timestamp = clock_->TimeInMicroseconds(); | 390 int64_t timestamp = clock_->TimeInMicroseconds(); |
336 event.set_timestamp_us(timestamp); | 391 event.set_timestamp_us(timestamp); |
337 event.set_type(rtclog::Event::LOG_END); | 392 event.set_type(rtclog::Event::LOG_END); |
338 // Store the event and close the file | 393 // Store the event and close the file |
339 RTC_DCHECK(file_->Open()); | 394 RTC_DCHECK(file_->Open()); |
340 StoreToFile(&event); | 395 StoreToFile(&event); |
341 file_->CloseFile(); | 396 file_->CloseFile(); |
397 if (using_platform_file_) { | |
398 rtc::ClosePlatformFile(platform_file_); | |
the sun
2015/10/14 09:29:09
platform_file_ is never set. Also, you don't need
ivoc
2015/10/14 12:23:47
Good points, have updated.
| |
399 using_platform_file_ = false; | |
400 } | |
342 } | 401 } |
343 RTC_DCHECK(!file_->Open()); | 402 RTC_DCHECK(!file_->Open()); |
344 stream_.Clear(); | 403 stream_.Clear(); |
345 } | 404 } |
346 | 405 |
347 void RtcEventLogImpl::HandleEvent(rtclog::Event* event) { | 406 void RtcEventLogImpl::HandleEvent(rtclog::Event* event) { |
348 if (currently_logging_) { | 407 if (currently_logging_) { |
349 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) { | 408 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) { |
350 StoreToFile(event); | 409 StoreToFile(event); |
351 return; | 410 return; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
393 return result->ParseFromString(dump_buffer); | 452 return result->ParseFromString(dump_buffer); |
394 } | 453 } |
395 | 454 |
396 #endif // ENABLE_RTC_EVENT_LOG | 455 #endif // ENABLE_RTC_EVENT_LOG |
397 | 456 |
398 // RtcEventLog member functions. | 457 // RtcEventLog member functions. |
399 rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() { | 458 rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() { |
400 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl()); | 459 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl()); |
401 } | 460 } |
402 } // namespace webrtc | 461 } // namespace webrtc |
OLD | NEW |