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/thread_annotations.h" | |
17 #include "webrtc/call.h" // For MediaType definition | |
pbos-webrtc
2015/07/22 11:05:27
Remove comment
terelius
2015/07/23 15:54:00
Done.
| |
18 #include "webrtc/system_wrappers/interface/clock.h" | |
19 #include "webrtc/system_wrappers/interface/critical_section_wrapper.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 // Noop implementation if flag is not set. | |
pbos-webrtc
2015/07/22 11:05:26
No-op
terelius
2015/07/23 15:54:00
Done.
| |
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 {}; | |
38 void LogVideoReceiveStreamConfig( | |
39 const webrtc::VideoReceiveStream::Config& config) override{}; | |
40 void LogVideoSendStreamConfig( | |
41 const webrtc::VideoSendStream::Config& config) override{}; | |
42 void LogRtpHeader(bool incoming, | |
43 MediaType media_type, | |
44 const uint8_t* header, | |
45 size_t header_length, | |
46 size_t total_length) override{}; | |
47 void LogRtcpPacket(bool incoming, | |
48 MediaType media_type, | |
49 const uint8_t* packet, | |
50 size_t length) override{}; | |
51 void LogDebugEvent(DebugEvent event_type, | |
52 const std::string& event_message) override{}; | |
53 void LogDebugEvent(DebugEvent event_type) override{}; | |
54 }; | |
55 #else | |
56 | |
57 class RtcEventLogImpl final : public RtcEventLog { | |
58 public: | |
59 RtcEventLogImpl(); | |
60 | |
61 void StartLogging(const std::string& file_name, int duration_ms) override; | |
62 void LogVideoReceiveStreamConfig( | |
63 const webrtc::VideoReceiveStream::Config& config) override; | |
64 void LogVideoSendStreamConfig( | |
65 const webrtc::VideoSendStream::Config& config) override; | |
66 void LogRtpHeader(bool incoming, | |
67 MediaType media_type, | |
68 const uint8_t* header, | |
69 size_t header_length, | |
70 size_t total_length) override; | |
71 void LogRtcpPacket(bool incoming, | |
72 MediaType media_type, | |
73 const uint8_t* packet, | |
74 size_t length) override; | |
75 void LogDebugEvent(DebugEvent event_type, | |
76 const std::string& event_message) override; | |
77 void LogDebugEvent(DebugEvent event_type) override; | |
78 | |
79 private: | |
80 // This function is identical to LogDebugEvent, but requires holding the lock. | |
81 void LogDebugEventLocked(DebugEvent event_type, | |
82 const std::string& event_message) | |
83 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
84 // Stops logging and clears the stored data and buffers. | |
85 void Clear() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
86 // Adds a new event to the logfile if logging is active, or adds it to the | |
87 // list of recent log events otherwise. | |
88 void HandleEvent(RelEvent* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
89 // Writes the event to the file. Note that this will destroy the state of the | |
90 // input argument. | |
91 void StoreToFile(RelEvent* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
92 // Adds the event to the list of recent events, and removes any events that | |
93 // are too old and no longer fall in the time window. | |
94 void AddRecentEvent(const RelEvent& event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
95 | |
96 // Amount of time in microseconds to record log events, before starting the | |
97 // actual log. | |
98 const int recent_log_duration_us = 10000000; | |
99 | |
100 rtc::scoped_ptr<webrtc::CriticalSectionWrapper> crit_; | |
101 rtc::scoped_ptr<webrtc::FileWrapper> file_ GUARDED_BY(crit_); | |
102 rtc::scoped_ptr<RelEventStream> stream_ GUARDED_BY(crit_); | |
103 std::deque<RelEvent> recent_log_events_ GUARDED_BY(crit_); | |
104 bool currently_logging_ GUARDED_BY(crit_); | |
105 int64_t start_time_us_ GUARDED_BY(crit_); | |
106 int64_t duration_us_ GUARDED_BY(crit_); | |
107 const webrtc::Clock* const clock_; | |
108 }; | |
109 | |
110 namespace { | |
111 // The functions in this namespace convert enums from the runtime format | |
112 // that the rest of the WebRtc project can use, to the corresponding | |
113 // serialized enum which is defined by the protobuf. | |
114 | |
115 // Do not add default return values to the conversion functions in this | |
116 // unnamed namespace. The intention is to make the compiler warn if anyone | |
117 // adds unhandled new events/modes/etc. | |
118 | |
119 RelDebugEvent_EventType ConvertDebugEvent(RtcEventLog::DebugEvent event_type) { | |
120 switch (event_type) { | |
121 case RtcEventLog::DebugEvent::kLogStart: | |
122 return RelDebugEvent::LOG_START; | |
123 case RtcEventLog::DebugEvent::kLogEnd: | |
124 return RelDebugEvent::LOG_END; | |
125 case RtcEventLog::DebugEvent::kAudioPlayout: | |
126 return RelDebugEvent::AUDIO_PLAYOUT; | |
127 } | |
128 RTC_NOTREACHED(); // Return something only to avoid certain compiler warnings | |
pbos-webrtc
2015/07/22 11:05:27
Remove comment, it's very obvious (conveyed by RTC
terelius
2015/07/23 15:54:01
Done.
| |
129 return RelDebugEvent::UNKNOWN_EVENT; | |
130 } | |
131 | |
132 RelVideoReceiveConfig_RtcpMode ConvertRtcpMode(newapi::RtcpMode rtcp_mode) { | |
133 switch (rtcp_mode) { | |
134 case newapi::kRtcpCompound: | |
135 return RelVideoReceiveConfig::RTCP_COMPOUND; | |
136 case newapi::kRtcpReducedSize: | |
137 return RelVideoReceiveConfig::RTCP_REDUCEDSIZE; | |
138 } | |
139 RTC_NOTREACHED(); // Return something only to avoid certain compiler warnings | |
140 return RelVideoReceiveConfig::RTCP_COMPOUND; | |
141 } | |
142 | |
143 | |
144 RelRtpPacket_PayloadType ConvertRtpPayloadType(MediaType media_type) { | |
pbos-webrtc
2015/07/22 11:05:26
This is not a payload type. RTP payload types have
terelius
2015/07/23 15:54:01
This is not really related to my CL, and since I w
| |
145 switch (media_type) { | |
146 case MediaType::VIDEO: | |
147 return RelRtpPacket::VIDEO; | |
148 case MediaType::AUDIO: | |
149 return RelRtpPacket::AUDIO; | |
150 case MediaType::DATA: // Fall through | |
151 case MediaType::ANY: | |
152 return RelRtpPacket::UNKNOWN_TYPE; | |
pbos-webrtc
2015/07/22 11:05:27
Should any "ANY" or "DATA" stamped packets enter h
terelius
2015/07/23 15:54:00
It was unclear what UNKNOWN_TYPE was for, but pres
ivoc
2015/07/24 08:06:04
The reason for UNKNOWN_TYPE is that when you add a
pbos-webrtc
2015/07/24 11:43:40
Don't understand this one, are you expecting to pa
terelius
2015/07/24 11:58:41
By reading the generated protobuf code I've found
ivoc
2015/07/24 12:07:27
Well, not planning on it, but it might happen. It
| |
153 } | |
154 RTC_NOTREACHED(); // Return something only to avoid certain compiler warnings | |
155 return RelRtpPacket::UNKNOWN_TYPE; | |
156 } | |
157 | |
158 | |
159 RelRtcpPacket_PayloadType ConvertRtcpPayloadType(MediaType media_type) { | |
pbos-webrtc
2015/07/22 11:05:26
Same here, payload type is "kind-of reserved".
terelius
2015/07/23 15:54:00
Done.
| |
160 switch (media_type) { | |
161 case MediaType::VIDEO: | |
162 return RelRtcpPacket::VIDEO; | |
163 case MediaType::AUDIO: | |
164 return RelRtcpPacket::AUDIO; | |
165 case MediaType::DATA: // Fall through | |
166 case MediaType::ANY: | |
167 return RelRtcpPacket::UNKNOWN_TYPE; | |
168 } | |
169 RTC_NOTREACHED(); // Return something only to avoid certain compiler warnings | |
170 return RelRtcpPacket::UNKNOWN_TYPE; | |
171 } | |
172 | |
173 } // Anonymous namespace. | |
174 | |
175 // RtcEventLogImpl member functions. | |
176 RtcEventLogImpl::RtcEventLogImpl() | |
177 : crit_(webrtc::CriticalSectionWrapper::CreateCriticalSection()), | |
pbos-webrtc
2015/07/22 11:05:27
Use a rtc::CriticalSection (this doesn't require a
terelius
2015/07/23 15:54:00
Done. Please review the locking changes extra care
pbos-webrtc
2015/07/24 11:43:40
The GUARDED_BY macros above are your friend (they
| |
178 file_(webrtc::FileWrapper::Create()), | |
179 stream_(new webrtc::RelEventStream()), | |
180 currently_logging_(false), | |
181 start_time_us_(0), | |
182 duration_us_(0), | |
183 clock_(webrtc::Clock::GetRealTimeClock()) { | |
184 } | |
185 | |
186 void RtcEventLogImpl::StartLogging(const std::string& file_name, | |
187 int duration_ms) { | |
188 CriticalSectionScoped lock(crit_.get()); | |
189 Clear(); | |
190 if (file_->OpenFile(file_name.c_str(), false) != 0) { | |
191 return; | |
192 } | |
193 | |
194 // Add LOG_START event to the recent event list. This call will also remove | |
195 // any events that are too old from the recent event list. | |
196 LogDebugEventLocked(DebugEvent::kLogStart, ""); | |
197 currently_logging_ = true; | |
198 start_time_us_ = clock_->TimeInMicroseconds(); | |
199 duration_us_ = static_cast<int64_t>(duration_ms) * 1000; | |
200 // Write all the recent events to the log file. | |
201 for (auto& event : recent_log_events_) { | |
pbos-webrtc
2015/07/22 11:05:26
Remove {}s
terelius
2015/07/23 15:54:00
All other loops and if-statements use braces, even
| |
202 StoreToFile(&event); | |
203 } | |
204 recent_log_events_.clear(); | |
205 } | |
206 | |
207 void RtcEventLogImpl::LogVideoReceiveStreamConfig( | |
208 const webrtc::VideoReceiveStream::Config& config) { | |
pbos-webrtc
2015/07/22 11:05:26
Remove all webrtc::, you're under the webrtc names
terelius
2015/07/23 15:54:00
Done.
| |
209 CriticalSectionScoped lock(crit_.get()); | |
210 | |
211 RelEvent event; | |
212 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
213 event.set_timestamp_us(timestamp); | |
214 event.set_type(webrtc::RelEvent::RECEIVER_CONFIG_EVENT); | |
215 | |
216 RelVideoReceiveConfig* receiver_config = event.mutable_receiver_config(); | |
217 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc); | |
218 receiver_config->set_local_ssrc(config.rtp.local_ssrc); | |
219 | |
220 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode)); | |
221 | |
222 receiver_config->set_receiver_reference_time_report( | |
223 config.rtp.rtcp_xr.receiver_reference_time_report); | |
224 receiver_config->set_remb(config.rtp.remb); | |
225 | |
226 for (const auto& config_rtx : config.rtp.rtx) { | |
pbos-webrtc
2015/07/22 11:05:27
s/config_rtx/kv, I think. We do this in a bunch of
terelius
2015/07/23 15:54:00
Done.
| |
227 RtxMap* rtx = receiver_config->add_rtx_map(); | |
228 rtx->set_payload_type(config_rtx.first); | |
229 rtx->mutable_config()->set_rtx_ssrc(config_rtx.second.ssrc); | |
230 rtx->mutable_config()->set_rtx_payload_type(config_rtx.second.payload_type); | |
231 } | |
232 | |
233 for (const auto& config_extension : config.rtp.extensions) { | |
pbos-webrtc
2015/07/22 11:05:26
extension
terelius
2015/07/23 15:54:00
extension is already used. I'll rename the loop va
| |
234 RtpHeaderExtension* extension = receiver_config->add_header_extensions(); | |
235 extension->set_name(config_extension.name); | |
236 extension->set_id(config_extension.id); | |
237 } | |
238 | |
239 for (const auto& config_decoder : config.decoders) { | |
pbos-webrtc
2015/07/22 11:05:26
decoder, and switch below DecoderConfig to decoder
terelius
2015/07/23 15:54:01
decoder is already used. I'll rename the loop vari
| |
240 DecoderConfig* decoder = receiver_config->add_decoders(); | |
241 decoder->set_name(config_decoder.payload_name); | |
242 decoder->set_payload_type(config_decoder.payload_type); | |
243 } | |
244 // TODO(terelius): We should use a separate event queue for config events. | |
245 // The current approach of storing the configuration together with the | |
246 // RTP events causes the configuration information to be removed 10s | |
247 // after the ReceiveStream is created. | |
248 HandleEvent(&event); | |
249 } | |
250 | |
251 void RtcEventLogImpl::LogVideoSendStreamConfig( | |
252 const webrtc::VideoSendStream::Config& config) { | |
253 CriticalSectionScoped lock(crit_.get()); | |
254 | |
255 RelEvent event; | |
pbos-webrtc
2015/07/22 11:05:26
What's the "Rel" in "RelEvent" supposed to mean? D
terelius
2015/07/23 15:54:00
It originated as an abbreviation of RtcEventLog. N
| |
256 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
257 event.set_timestamp_us(timestamp); | |
258 event.set_type(webrtc::RelEvent::SENDER_CONFIG_EVENT); | |
259 | |
260 RelVideoSendConfig* sender_config = event.mutable_sender_config(); | |
261 | |
262 for (const auto& ssrc : config.rtp.ssrcs) { | |
pbos-webrtc
2015/07/22 11:05:26
Remove {}s
terelius
2015/07/23 15:54:00
All other loops and if-statements use braces, even
| |
263 sender_config->add_ssrcs(ssrc); | |
264 } | |
265 | |
266 for (const auto& config_extension : config.rtp.extensions) { | |
267 RtpHeaderExtension* extension = sender_config->add_header_extensions(); | |
268 extension->set_name(config_extension.name); | |
269 extension->set_id(config_extension.id); | |
270 } | |
271 | |
272 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) { | |
273 sender_config->add_rtx_ssrcs(rtx_ssrc); | |
274 } | |
275 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type); | |
276 | |
277 sender_config->set_c_name(config.rtp.c_name); | |
278 | |
279 EncoderConfig* encoder = sender_config->mutable_encoder(); | |
280 encoder->set_name(config.encoder_settings.payload_name); | |
281 encoder->set_payload_type(config.encoder_settings.payload_type); | |
282 | |
283 // TODO(terelius): We should use a separate event queue for config events. | |
284 // The current approach of storing the configuration together with the | |
285 // RTP events causes the configuration information to be removed 10s | |
286 // after the ReceiveStream is created. | |
287 HandleEvent(&event); | |
288 } | |
289 | |
290 void RtcEventLogImpl::LogRtpHeader(bool incoming, | |
291 MediaType media_type, | |
292 const uint8_t* header, | |
293 size_t header_length, | |
294 size_t total_length) { | |
295 CriticalSectionScoped lock(crit_.get()); | |
296 RelEvent rtp_event; | |
297 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
298 rtp_event.set_timestamp_us(timestamp); | |
299 rtp_event.set_type(webrtc::RelEvent::RTP_EVENT); | |
300 rtp_event.mutable_rtp_packet()->set_direction( | |
301 incoming ? RelRtpPacket::INCOMING : RelRtpPacket::OUTGOING); | |
pbos-webrtc
2015/07/22 11:05:26
Do we need separate direction types for RTP and RT
terelius
2015/07/23 15:54:00
I changed to use a bool instead. I have a hard tim
| |
302 rtp_event.mutable_rtp_packet()->set_type(ConvertRtpPayloadType(media_type)); | |
pbos-webrtc
2015/07/22 11:05:26
Not PayloadType
terelius
2015/07/23 15:54:01
Changed to ConvertMediaType
| |
303 rtp_event.mutable_rtp_packet()->set_packet_length(total_length); | |
304 rtp_event.mutable_rtp_packet()->set_header(header, header_length); | |
305 HandleEvent(&rtp_event); | |
306 } | |
307 | |
308 void RtcEventLogImpl::LogRtcpPacket(bool incoming, | |
309 MediaType media_type, | |
310 const uint8_t* packet, | |
311 size_t length) { | |
312 CriticalSectionScoped lock(crit_.get()); | |
313 RelEvent rtcp_event; | |
314 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
315 rtcp_event.set_timestamp_us(timestamp); | |
316 rtcp_event.set_type(webrtc::RelEvent::RTCP_EVENT); | |
317 rtcp_event.mutable_rtcp_packet()->set_direction( | |
318 incoming ? RelRtcpPacket::INCOMING : RelRtcpPacket::OUTGOING); | |
319 rtcp_event.mutable_rtcp_packet()->set_type( | |
320 ConvertRtcpPayloadType(media_type)); | |
321 rtcp_event.mutable_rtcp_packet()->set_data(packet, length); | |
322 HandleEvent(&rtcp_event); | |
323 } | |
324 | |
325 void RtcEventLogImpl::LogDebugEvent(DebugEvent event_type, | |
326 const std::string& event_message) { | |
327 CriticalSectionScoped lock(crit_.get()); | |
328 LogDebugEventLocked(event_type, event_message); | |
329 } | |
330 | |
331 void RtcEventLogImpl::LogDebugEvent(DebugEvent event_type) { | |
332 CriticalSectionScoped lock(crit_.get()); | |
333 LogDebugEventLocked(event_type, ""); | |
334 } | |
335 | |
336 void RtcEventLogImpl::LogDebugEventLocked(DebugEvent event_type, | |
337 const std::string& event_message) { | |
338 RelEvent event; | |
339 int64_t timestamp = clock_->TimeInMicroseconds(); | |
340 event.set_timestamp_us(timestamp); | |
341 event.set_type(webrtc::RelEvent::DEBUG_EVENT); | |
342 auto debug_event = event.mutable_debug_event(); | |
343 debug_event->set_type(ConvertDebugEvent(event_type)); | |
344 debug_event->set_message(event_message); | |
345 HandleEvent(&event); | |
346 } | |
347 | |
348 void RtcEventLogImpl::Clear() { | |
349 if (file_->Open()) { | |
350 file_->CloseFile(); | |
351 } | |
352 currently_logging_ = false; | |
353 stream_->Clear(); | |
354 } | |
355 | |
356 void RtcEventLogImpl::HandleEvent(RelEvent* event) { | |
357 if (currently_logging_) { | |
358 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) { | |
359 StoreToFile(event); | |
360 } else { | |
361 LogDebugEventLocked(DebugEvent::kLogEnd, ""); | |
362 Clear(); | |
363 AddRecentEvent(*event); | |
364 } | |
365 } else { | |
366 AddRecentEvent(*event); | |
367 } | |
368 } | |
369 | |
370 void RtcEventLogImpl::StoreToFile(RelEvent* event) { | |
371 // Reuse the same object at every log event. | |
372 if (stream_->stream_size() < 1) { | |
373 stream_->add_stream(); | |
374 } | |
375 DCHECK_EQ(stream_->stream_size(), 1); | |
376 stream_->mutable_stream(0)->Swap(event); | |
377 | |
378 std::string dump_buffer; | |
379 stream_->SerializeToString(&dump_buffer); | |
380 file_->Write(dump_buffer.data(), dump_buffer.size()); | |
381 } | |
382 | |
383 void RtcEventLogImpl::AddRecentEvent(const RelEvent& event) { | |
384 recent_log_events_.push_back(event); | |
385 while (recent_log_events_.front().timestamp_us() < | |
386 event.timestamp_us() - recent_log_duration_us) { | |
387 recent_log_events_.pop_front(); | |
388 } | |
389 } | |
390 | |
391 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, | |
392 RelEventStream* result) { | |
393 char tmp_buffer[1024]; | |
394 int bytes_read = 0; | |
395 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create()); | |
396 if (dump_file->OpenFile(file_name.c_str(), true) != 0) { | |
397 return false; | |
398 } | |
399 std::string dump_buffer; | |
400 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { | |
401 dump_buffer.append(tmp_buffer, bytes_read); | |
402 } | |
403 dump_file->CloseFile(); | |
404 return result->ParseFromString(dump_buffer); | |
405 } | |
406 | |
407 #endif // ENABLE_RTC_EVENT_LOG | |
408 | |
409 // RtcEventLog member functions. | |
410 rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() { | |
411 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl()); | |
412 } | |
413 } // namespace webrtc | |
OLD | NEW |