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