OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/video/rtc_event_log.h" | |
12 | |
13 #include <deque> | |
14 | |
15 #include "webrtc/base/checks.h" | |
16 #include "webrtc/base/criticalsection.h" | |
17 #include "webrtc/base/thread_annotations.h" | |
18 #include "webrtc/call.h" | |
19 #include "webrtc/system_wrappers/interface/clock.h" | |
20 #include "webrtc/system_wrappers/interface/file_wrapper.h" | |
21 | |
22 #ifdef ENABLE_RTC_EVENT_LOG | |
23 // Files generated at build-time by the protobuf compiler. | |
24 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD | |
25 #include "external/webrtc/webrtc/video/rtc_event_log.pb.h" | |
26 #else | |
27 #include "webrtc/video/rtc_event_log.pb.h" | |
28 #endif | |
29 #endif | |
30 | |
31 namespace webrtc { | |
32 | |
33 // No-op implementation if flag is not set. | |
34 #ifndef ENABLE_RTC_EVENT_LOG | |
35 class RtcEventLogImpl final : public RtcEventLog { | |
36 public: | |
37 void StartLogging(const std::string& file_name, int duration_ms) override{}; | |
stefan-webrtc
2015/07/28 08:15:34
Space after override. Same below. Also no ; after
terelius
2015/07/28 11:52:08
Done.
| |
38 void StopLogging(void) override{}; | |
39 void LogVideoReceiveStreamConfig( | |
40 const VideoReceiveStream::Config& config) override{}; | |
41 void LogVideoSendStreamConfig( | |
42 const VideoSendStream::Config& config) override{}; | |
43 void LogRtpHeader(bool incoming, | |
44 MediaType media_type, | |
45 const uint8_t* header, | |
46 size_t header_length, | |
47 size_t total_length) override{}; | |
48 void LogRtcpPacket(bool incoming, | |
49 MediaType media_type, | |
50 const uint8_t* packet, | |
51 size_t length) override{}; | |
52 void LogDebugEvent(DebugEvent event_type) override{}; | |
53 }; | |
54 #else | |
stefan-webrtc
2015/07/28 08:15:34
new line above, possibly
terelius
2015/07/28 11:52:09
Done.
| |
55 | |
56 class RtcEventLogImpl final : public RtcEventLog { | |
57 public: | |
58 RtcEventLogImpl(); | |
59 | |
60 void StartLogging(const std::string& file_name, int duration_ms) override; | |
61 void StopLogging() override; | |
62 void LogVideoReceiveStreamConfig( | |
63 const VideoReceiveStream::Config& config) override; | |
64 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override; | |
65 void LogRtpHeader(bool incoming, | |
66 MediaType media_type, | |
67 const uint8_t* header, | |
68 size_t header_length, | |
69 size_t total_length) override; | |
70 void LogRtcpPacket(bool incoming, | |
71 MediaType media_type, | |
72 const uint8_t* packet, | |
73 size_t length) override; | |
74 void LogDebugEvent(DebugEvent event_type) override; | |
75 | |
76 private: | |
77 // Stops logging and clears the stored data and buffers. TODO | |
stefan-webrtc
2015/07/28 08:15:34
Owner for TODO
terelius
2015/07/28 11:52:08
Done. Removed empty TODO.
| |
78 void StopLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
79 // Adds a new event to the logfile if logging is active, or adds it to the | |
80 // list of recent log events otherwise. | |
81 void HandleEvent(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
82 // Writes the event to the file. Note that this will destroy the state of the | |
83 // input argument. | |
84 void StoreToFile(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
85 // Adds the event to the list of recent events, and removes any events that | |
86 // are too old and no longer fall in the time window. | |
87 void AddRecentEvent(const rtclog::Event& event) | |
88 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
89 | |
90 // Amount of time in microseconds to record log events, before starting the | |
91 // actual log. | |
92 const int recent_log_duration_us = 10000000; | |
93 | |
94 rtc::CriticalSection crit_; | |
95 rtc::scoped_ptr<FileWrapper> file_ GUARDED_BY(crit_); | |
96 rtclog::EventStream stream_ GUARDED_BY(crit_); | |
97 std::deque<rtclog::Event> recent_log_events_ GUARDED_BY(crit_); | |
98 bool currently_logging_ GUARDED_BY(crit_); | |
99 int64_t start_time_us_ GUARDED_BY(crit_); | |
100 int64_t duration_us_ GUARDED_BY(crit_); | |
101 const Clock* const clock_; | |
102 }; | |
103 | |
104 namespace { | |
105 // The functions in this namespace convert enums from the runtime format | |
106 // that the rest of the WebRtc project can use, to the corresponding | |
107 // serialized enum which is defined by the protobuf. | |
108 | |
109 // Do not add default return values to the conversion functions in this | |
110 // unnamed namespace. The intention is to make the compiler warn if anyone | |
111 // adds unhandled new events/modes/etc. | |
112 | |
113 rtclog::DebugEvent_EventType ConvertDebugEvent( | |
114 RtcEventLog::DebugEvent event_type) { | |
115 switch (event_type) { | |
116 case RtcEventLog::DebugEvent::kLogStart: | |
117 return rtclog::DebugEvent::LOG_START; | |
118 case RtcEventLog::DebugEvent::kLogEnd: | |
119 return rtclog::DebugEvent::LOG_END; | |
120 case RtcEventLog::DebugEvent::kAudioPlayout: | |
121 return rtclog::DebugEvent::AUDIO_PLAYOUT; | |
122 } | |
123 RTC_NOTREACHED(); | |
124 return rtclog::DebugEvent::UNKNOWN_EVENT; | |
125 } | |
126 | |
127 rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode( | |
128 newapi::RtcpMode rtcp_mode) { | |
129 switch (rtcp_mode) { | |
130 case newapi::kRtcpCompound: | |
131 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; | |
132 case newapi::kRtcpReducedSize: | |
133 return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE; | |
134 } | |
135 RTC_NOTREACHED(); | |
136 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; | |
137 } | |
138 | |
139 rtclog::MediaType ConvertMediaType(MediaType media_type) { | |
140 switch (media_type) { | |
141 case MediaType::ANY: | |
142 return rtclog::MediaType::ANY; | |
143 case MediaType::AUDIO: | |
144 return rtclog::MediaType::AUDIO; | |
145 case MediaType::VIDEO: | |
146 return rtclog::MediaType::VIDEO; | |
147 case MediaType::DATA: | |
148 return rtclog::MediaType::DATA; | |
149 } | |
150 RTC_NOTREACHED(); | |
151 return rtclog::ANY; | |
152 } | |
153 | |
154 } // Anonymous namespace. | |
stefan-webrtc
2015/07/28 08:15:34
Remove Anonymous (at least that's how I think we t
terelius
2015/07/28 11:52:09
Done.
| |
155 | |
156 // RtcEventLogImpl member functions. | |
157 RtcEventLogImpl::RtcEventLogImpl() | |
158 : file_(FileWrapper::Create()), | |
159 stream_(), | |
160 currently_logging_(false), | |
161 start_time_us_(0), | |
162 duration_us_(0), | |
163 clock_(Clock::GetRealTimeClock()) { | |
164 } | |
165 | |
166 void RtcEventLogImpl::StartLogging(const std::string& file_name, | |
167 int duration_ms) { | |
168 rtc::CritScope lock(&crit_); | |
169 if (currently_logging_) { | |
170 StopLoggingLocked(); | |
171 } | |
172 if (file_->OpenFile(file_name.c_str(), false) != 0) { | |
173 return; | |
174 } | |
175 currently_logging_ = true; | |
176 start_time_us_ = clock_->TimeInMicroseconds(); | |
177 duration_us_ = static_cast<int64_t>(duration_ms) * 1000; | |
178 // Write all the recent events to the log file, ignoring any old events. | |
179 for (auto& event : recent_log_events_) { | |
180 if (event.timestamp_us() >= start_time_us_ - recent_log_duration_us) { | |
181 StoreToFile(&event); | |
182 } | |
183 } | |
184 recent_log_events_.clear(); | |
185 // Write a LOG_START event to the file. | |
186 rtclog::Event start_event; | |
187 start_event.set_timestamp_us(start_time_us_); | |
188 start_event.set_type(rtclog::Event::DEBUG_EVENT); | |
189 auto debug_event = start_event.mutable_debug_event(); | |
190 debug_event->set_type(ConvertDebugEvent(DebugEvent::kLogStart)); | |
191 StoreToFile(&start_event); | |
192 } | |
193 | |
194 void RtcEventLogImpl::StopLogging() { | |
195 rtc::CritScope lock(&crit_); | |
196 StopLoggingLocked(); | |
197 } | |
198 | |
199 void RtcEventLogImpl::LogVideoReceiveStreamConfig( | |
200 const VideoReceiveStream::Config& config) { | |
201 rtc::CritScope lock(&crit_); | |
202 | |
203 rtclog::Event event; | |
204 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
205 event.set_timestamp_us(timestamp); | |
206 event.set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); | |
207 | |
208 rtclog::VideoReceiveConfig* receiver_config = | |
209 event.mutable_video_receiver_config(); | |
210 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc); | |
211 receiver_config->set_local_ssrc(config.rtp.local_ssrc); | |
212 | |
213 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode)); | |
214 | |
215 receiver_config->set_receiver_reference_time_report( | |
216 config.rtp.rtcp_xr.receiver_reference_time_report); | |
217 receiver_config->set_remb(config.rtp.remb); | |
218 | |
219 for (const auto& kv : config.rtp.rtx) { | |
220 rtclog::RtxMap* rtx = receiver_config->add_rtx_map(); | |
221 rtx->set_payload_type(kv.first); | |
222 rtx->mutable_config()->set_rtx_ssrc(kv.second.ssrc); | |
223 rtx->mutable_config()->set_rtx_payload_type(kv.second.payload_type); | |
224 } | |
225 | |
226 for (const auto& e : config.rtp.extensions) { | |
227 rtclog::RtpHeaderExtension* extension = | |
228 receiver_config->add_header_extensions(); | |
229 extension->set_name(e.name); | |
230 extension->set_id(e.id); | |
231 } | |
232 | |
233 for (const auto& d : config.decoders) { | |
234 rtclog::DecoderConfig* decoder = receiver_config->add_decoders(); | |
235 decoder->set_name(d.payload_name); | |
236 decoder->set_payload_type(d.payload_type); | |
237 } | |
238 // TODO(terelius): We should use a separate event queue for config events. | |
239 // The current approach of storing the configuration together with the | |
240 // RTP events causes the configuration information to be removed 10s | |
241 // after the ReceiveStream is created. | |
242 HandleEvent(&event); | |
243 } | |
244 | |
245 void RtcEventLogImpl::LogVideoSendStreamConfig( | |
246 const VideoSendStream::Config& config) { | |
247 rtc::CritScope lock(&crit_); | |
248 | |
249 rtclog::Event event; | |
250 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
251 event.set_timestamp_us(timestamp); | |
252 event.set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT); | |
253 | |
254 rtclog::VideoSendConfig* sender_config = event.mutable_video_sender_config(); | |
255 | |
256 for (const auto& ssrc : config.rtp.ssrcs) { | |
257 sender_config->add_ssrcs(ssrc); | |
258 } | |
259 | |
260 for (const auto& e : config.rtp.extensions) { | |
261 rtclog::RtpHeaderExtension* extension = | |
262 sender_config->add_header_extensions(); | |
263 extension->set_name(e.name); | |
264 extension->set_id(e.id); | |
265 } | |
266 | |
267 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) { | |
268 sender_config->add_rtx_ssrcs(rtx_ssrc); | |
269 } | |
270 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type); | |
271 | |
272 sender_config->set_c_name(config.rtp.c_name); | |
273 | |
274 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder(); | |
275 encoder->set_name(config.encoder_settings.payload_name); | |
276 encoder->set_payload_type(config.encoder_settings.payload_type); | |
277 | |
278 // TODO(terelius): We should use a separate event queue for config events. | |
279 // The current approach of storing the configuration together with the | |
280 // RTP events causes the configuration information to be removed 10s | |
281 // after the ReceiveStream is created. | |
282 HandleEvent(&event); | |
283 } | |
284 | |
285 // TODO(terelius): It is more convenient and less error prone to parse the | |
286 // header length from the packet instead of relying on the caller to provide it. | |
287 void RtcEventLogImpl::LogRtpHeader(bool incoming, | |
288 MediaType media_type, | |
289 const uint8_t* header, | |
290 size_t header_length, | |
291 size_t total_length) { | |
292 rtc::CritScope lock(&crit_); | |
293 rtclog::Event rtp_event; | |
294 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
295 rtp_event.set_timestamp_us(timestamp); | |
296 rtp_event.set_type(rtclog::Event::RTP_EVENT); | |
297 rtp_event.mutable_rtp_packet()->set_incoming(incoming); | |
298 rtp_event.mutable_rtp_packet()->set_type(ConvertMediaType(media_type)); | |
299 rtp_event.mutable_rtp_packet()->set_packet_length(total_length); | |
300 rtp_event.mutable_rtp_packet()->set_header(header, header_length); | |
301 HandleEvent(&rtp_event); | |
302 } | |
303 | |
304 void RtcEventLogImpl::LogRtcpPacket(bool incoming, | |
305 MediaType media_type, | |
306 const uint8_t* packet, | |
307 size_t length) { | |
308 rtc::CritScope lock(&crit_); | |
309 rtclog::Event rtcp_event; | |
310 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
311 rtcp_event.set_timestamp_us(timestamp); | |
312 rtcp_event.set_type(rtclog::Event::RTCP_EVENT); | |
313 rtcp_event.mutable_rtcp_packet()->set_incoming(incoming); | |
314 rtcp_event.mutable_rtcp_packet()->set_type(ConvertMediaType(media_type)); | |
315 rtcp_event.mutable_rtcp_packet()->set_packet_data(packet, length); | |
316 HandleEvent(&rtcp_event); | |
317 } | |
318 | |
319 void RtcEventLogImpl::LogDebugEvent(DebugEvent event_type) { | |
320 rtc::CritScope lock(&crit_); | |
321 rtclog::Event event; | |
322 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
323 event.set_timestamp_us(timestamp); | |
324 event.set_type(rtclog::Event::DEBUG_EVENT); | |
325 auto debug_event = event.mutable_debug_event(); | |
326 debug_event->set_type(ConvertDebugEvent(event_type)); | |
327 HandleEvent(&event); | |
328 } | |
329 | |
330 void RtcEventLogImpl::StopLoggingLocked() { | |
331 if (currently_logging_) { | |
332 currently_logging_ = false; | |
333 // Create a LogEnd debug event | |
334 rtclog::Event event; | |
335 int64_t timestamp = clock_->TimeInMicroseconds(); | |
336 event.set_timestamp_us(timestamp); | |
337 event.set_type(rtclog::Event::DEBUG_EVENT); | |
338 auto debug_event = event.mutable_debug_event(); | |
339 debug_event->set_type(ConvertDebugEvent(DebugEvent::kLogEnd)); | |
340 // Store the event and close the file | |
341 DCHECK(file_->Open()); | |
342 StoreToFile(&event); | |
343 file_->CloseFile(); | |
344 } | |
345 DCHECK(!file_->Open()); | |
346 stream_.Clear(); | |
347 } | |
348 | |
349 void RtcEventLogImpl::HandleEvent(rtclog::Event* event) { | |
350 if (currently_logging_) { | |
351 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) { | |
352 StoreToFile(event); | |
353 return; | |
354 } | |
355 StopLoggingLocked(); | |
356 } | |
357 AddRecentEvent(*event); | |
358 } | |
359 | |
360 void RtcEventLogImpl::StoreToFile(rtclog::Event* event) { | |
361 // Reuse the same object at every log event. | |
362 if (stream_.stream_size() < 1) { | |
363 stream_.add_stream(); | |
364 } | |
365 DCHECK_EQ(stream_.stream_size(), 1); | |
366 stream_.mutable_stream(0)->Swap(event); | |
367 // TODO(terelius): Doesn't this create a new EventStream per event? | |
368 // Is this guaranteed to work e.g. in future versions of protobuf? | |
369 std::string dump_buffer; | |
370 stream_.SerializeToString(&dump_buffer); | |
371 file_->Write(dump_buffer.data(), dump_buffer.size()); | |
372 } | |
373 | |
374 void RtcEventLogImpl::AddRecentEvent(const rtclog::Event& event) { | |
375 recent_log_events_.push_back(event); | |
376 while (recent_log_events_.front().timestamp_us() < | |
377 event.timestamp_us() - recent_log_duration_us) { | |
378 recent_log_events_.pop_front(); | |
379 } | |
380 } | |
381 | |
382 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, | |
383 rtclog::EventStream* result) { | |
384 char tmp_buffer[1024]; | |
385 int bytes_read = 0; | |
386 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create()); | |
387 if (dump_file->OpenFile(file_name.c_str(), true) != 0) { | |
388 return false; | |
389 } | |
390 std::string dump_buffer; | |
391 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { | |
392 dump_buffer.append(tmp_buffer, bytes_read); | |
393 } | |
394 dump_file->CloseFile(); | |
395 return result->ParseFromString(dump_buffer); | |
396 } | |
397 | |
398 #endif // ENABLE_RTC_EVENT_LOG | |
399 | |
400 // RtcEventLog member functions. | |
401 rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() { | |
402 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl()); | |
403 } | |
404 } // namespace webrtc | |
OLD | NEW |