blob: dd7a3b661b44eb3a91f7b34584e88465d8fffe93 [file] [log] [blame]
Bjorn Terelius36411852015-07-30 12:45:18 +02001/*
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
Peter Boström5c389d32015-09-25 13:58:30 +020011#include "webrtc/call/rtc_event_log.h"
Bjorn Terelius36411852015-07-30 12:45:18 +020012
13#include <deque>
terelius1adce142015-10-16 08:51:08 -070014#include <vector>
Bjorn Terelius36411852015-07-30 12:45:18 +020015
16#include "webrtc/base/checks.h"
17#include "webrtc/base/criticalsection.h"
18#include "webrtc/base/thread_annotations.h"
19#include "webrtc/call.h"
terelius2f9fd5d2015-09-04 03:39:42 -070020#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
Bjorn Terelius36411852015-07-30 12:45:18 +020021#include "webrtc/system_wrappers/interface/clock.h"
22#include "webrtc/system_wrappers/interface/file_wrapper.h"
23
24#ifdef ENABLE_RTC_EVENT_LOG
25// Files generated at build-time by the protobuf compiler.
26#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
Peter Boström5c389d32015-09-25 13:58:30 +020027#include "external/webrtc/webrtc/call/rtc_event_log.pb.h"
Bjorn Terelius36411852015-07-30 12:45:18 +020028#else
Peter Boström5c389d32015-09-25 13:58:30 +020029#include "webrtc/call/rtc_event_log.pb.h"
Bjorn Terelius36411852015-07-30 12:45:18 +020030#endif
31#endif
32
33namespace webrtc {
34
35#ifndef ENABLE_RTC_EVENT_LOG
36
37// No-op implementation if flag is not set.
38class RtcEventLogImpl final : public RtcEventLog {
39 public:
terelius1adce142015-10-16 08:51:08 -070040 void SetBufferDuration(int64_t buffer_duration_us) override {}
Bjorn Terelius36411852015-07-30 12:45:18 +020041 void StartLogging(const std::string& file_name, int duration_ms) override {}
ivoc112a3d82015-10-16 02:22:18 -070042 bool StartLogging(rtc::PlatformFile log_file) override { return false; }
Bjorn Terelius36411852015-07-30 12:45:18 +020043 void StopLogging(void) override {}
44 void LogVideoReceiveStreamConfig(
45 const VideoReceiveStream::Config& config) override {}
46 void LogVideoSendStreamConfig(
47 const VideoSendStream::Config& config) override {}
48 void LogRtpHeader(bool incoming,
49 MediaType media_type,
50 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -070051 size_t packet_length) override {}
Bjorn Terelius36411852015-07-30 12:45:18 +020052 void LogRtcpPacket(bool incoming,
53 MediaType media_type,
54 const uint8_t* packet,
55 size_t length) override {}
Ivo Creusenae856f22015-09-17 16:30:16 +020056 void LogAudioPlayout(uint32_t ssrc) override {}
Bjorn Terelius36411852015-07-30 12:45:18 +020057};
58
59#else // ENABLE_RTC_EVENT_LOG is defined
60
61class RtcEventLogImpl final : public RtcEventLog {
62 public:
terelius1adce142015-10-16 08:51:08 -070063 RtcEventLogImpl();
64
65 void SetBufferDuration(int64_t buffer_duration_us) override;
Bjorn Terelius36411852015-07-30 12:45:18 +020066 void StartLogging(const std::string& file_name, int duration_ms) override;
ivoc112a3d82015-10-16 02:22:18 -070067 bool StartLogging(rtc::PlatformFile log_file) override;
Bjorn Terelius36411852015-07-30 12:45:18 +020068 void StopLogging() override;
69 void LogVideoReceiveStreamConfig(
70 const VideoReceiveStream::Config& config) override;
71 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override;
72 void LogRtpHeader(bool incoming,
73 MediaType media_type,
74 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -070075 size_t packet_length) override;
Bjorn Terelius36411852015-07-30 12:45:18 +020076 void LogRtcpPacket(bool incoming,
77 MediaType media_type,
78 const uint8_t* packet,
79 size_t length) override;
Ivo Creusenae856f22015-09-17 16:30:16 +020080 void LogAudioPlayout(uint32_t ssrc) override;
Bjorn Terelius36411852015-07-30 12:45:18 +020081
82 private:
ivoc112a3d82015-10-16 02:22:18 -070083 // Starts logging. This function assumes the file_ has been opened succesfully
84 // and that the start_time_us_ and _duration_us_ have been set.
85 void StartLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
Bjorn Terelius36411852015-07-30 12:45:18 +020086 // Stops logging and clears the stored data and buffers.
87 void StopLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
88 // Adds a new event to the logfile if logging is active, or adds it to the
89 // list of recent log events otherwise.
90 void HandleEvent(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_);
91 // Writes the event to the file. Note that this will destroy the state of the
92 // input argument.
93 void StoreToFile(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_);
94 // Adds the event to the list of recent events, and removes any events that
95 // are too old and no longer fall in the time window.
96 void AddRecentEvent(const rtclog::Event& event)
97 EXCLUSIVE_LOCKS_REQUIRED(crit_);
98
Bjorn Terelius36411852015-07-30 12:45:18 +020099 rtc::CriticalSection crit_;
ivoc112a3d82015-10-16 02:22:18 -0700100 rtc::scoped_ptr<FileWrapper> file_ GUARDED_BY(crit_) =
101 rtc::scoped_ptr<FileWrapper>(FileWrapper::Create());
102 rtc::PlatformFile platform_file_ GUARDED_BY(crit_) =
103 rtc::kInvalidPlatformFileValue;
Bjorn Terelius36411852015-07-30 12:45:18 +0200104 rtclog::EventStream stream_ GUARDED_BY(crit_);
105 std::deque<rtclog::Event> recent_log_events_ GUARDED_BY(crit_);
terelius1adce142015-10-16 08:51:08 -0700106 std::vector<rtclog::Event> config_events_ GUARDED_BY(crit_);
107
108 // Microseconds to record log events, before starting the actual log.
109 int64_t buffer_duration_us_ GUARDED_BY(crit_);
110 bool currently_logging_ GUARDED_BY(crit_);
111 int64_t start_time_us_ GUARDED_BY(crit_);
112 int64_t duration_us_ GUARDED_BY(crit_);
113 const Clock* const clock_;
Bjorn Terelius36411852015-07-30 12:45:18 +0200114};
115
116namespace {
117// The functions in this namespace convert enums from the runtime format
118// that the rest of the WebRtc project can use, to the corresponding
119// serialized enum which is defined by the protobuf.
120
121// Do not add default return values to the conversion functions in this
122// unnamed namespace. The intention is to make the compiler warn if anyone
123// adds unhandled new events/modes/etc.
124
pbosda903ea2015-10-02 02:36:56 -0700125rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) {
Bjorn Terelius36411852015-07-30 12:45:18 +0200126 switch (rtcp_mode) {
pbosda903ea2015-10-02 02:36:56 -0700127 case RtcpMode::kCompound:
Bjorn Terelius36411852015-07-30 12:45:18 +0200128 return rtclog::VideoReceiveConfig::RTCP_COMPOUND;
pbosda903ea2015-10-02 02:36:56 -0700129 case RtcpMode::kReducedSize:
Bjorn Terelius36411852015-07-30 12:45:18 +0200130 return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE;
pbosda903ea2015-10-02 02:36:56 -0700131 case RtcpMode::kOff:
132 RTC_NOTREACHED();
133 return rtclog::VideoReceiveConfig::RTCP_COMPOUND;
Bjorn Terelius36411852015-07-30 12:45:18 +0200134 }
135 RTC_NOTREACHED();
136 return rtclog::VideoReceiveConfig::RTCP_COMPOUND;
137}
138
139rtclog::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} // namespace
155
terelius1adce142015-10-16 08:51:08 -0700156namespace {
157bool IsConfigEvent(const rtclog::Event& event) {
158 rtclog::Event_EventType event_type = event.type();
159 return event_type == rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT ||
160 event_type == rtclog::Event::VIDEO_SENDER_CONFIG_EVENT ||
161 event_type == rtclog::Event::AUDIO_RECEIVER_CONFIG_EVENT ||
162 event_type == rtclog::Event::AUDIO_SENDER_CONFIG_EVENT;
163}
164} // namespace
165
Bjorn Terelius36411852015-07-30 12:45:18 +0200166// RtcEventLogImpl member functions.
terelius1adce142015-10-16 08:51:08 -0700167RtcEventLogImpl::RtcEventLogImpl()
168 : file_(FileWrapper::Create()),
169 stream_(),
170 buffer_duration_us_(10000000),
171 currently_logging_(false),
172 start_time_us_(0),
173 duration_us_(0),
174 clock_(Clock::GetRealTimeClock()) {
175}
176
177void RtcEventLogImpl::SetBufferDuration(int64_t buffer_duration_us) {
178 rtc::CritScope lock(&crit_);
179 buffer_duration_us_ = buffer_duration_us;
180}
Bjorn Terelius36411852015-07-30 12:45:18 +0200181
182void RtcEventLogImpl::StartLogging(const std::string& file_name,
183 int duration_ms) {
184 rtc::CritScope lock(&crit_);
185 if (currently_logging_) {
186 StopLoggingLocked();
187 }
188 if (file_->OpenFile(file_name.c_str(), false) != 0) {
189 return;
190 }
Bjorn Terelius36411852015-07-30 12:45:18 +0200191 start_time_us_ = clock_->TimeInMicroseconds();
192 duration_us_ = static_cast<int64_t>(duration_ms) * 1000;
ivoc112a3d82015-10-16 02:22:18 -0700193 StartLoggingLocked();
194}
195
196bool RtcEventLogImpl::StartLogging(rtc::PlatformFile log_file) {
197 rtc::CritScope lock(&crit_);
198
199 if (currently_logging_) {
200 StopLoggingLocked();
201 }
202 RTC_DCHECK(platform_file_ == rtc::kInvalidPlatformFileValue);
203
204 FILE* file_stream = rtc::FdopenPlatformFileForWriting(log_file);
205 if (!file_stream) {
206 rtc::ClosePlatformFile(log_file);
207 return false;
208 }
209
210 if (file_->OpenFromFileHandle(file_stream, true, false) != 0) {
211 rtc::ClosePlatformFile(log_file);
212 return false;
213 }
214 platform_file_ = log_file;
215 // Set the start time and duration to keep logging for 10 minutes.
216 start_time_us_ = clock_->TimeInMicroseconds();
217 duration_us_ = 10 * 60 * 1000000;
218 StartLoggingLocked();
219 return true;
220}
221
222void RtcEventLogImpl::StartLoggingLocked() {
223 currently_logging_ = true;
terelius1adce142015-10-16 08:51:08 -0700224
225 // Write all old configuration events to the log file.
226 for (auto& event : config_events_) {
227 StoreToFile(&event);
228 }
229 // Write all recent configuration events to the log file, and
230 // write all other recent events to the log file, ignoring any old events.
Bjorn Terelius36411852015-07-30 12:45:18 +0200231 for (auto& event : recent_log_events_) {
terelius1adce142015-10-16 08:51:08 -0700232 if (IsConfigEvent(event)) {
233 StoreToFile(&event);
234 config_events_.push_back(event);
235 } else if (event.timestamp_us() >= start_time_us_ - buffer_duration_us_) {
Bjorn Terelius36411852015-07-30 12:45:18 +0200236 StoreToFile(&event);
237 }
238 }
239 recent_log_events_.clear();
240 // Write a LOG_START event to the file.
241 rtclog::Event start_event;
242 start_event.set_timestamp_us(start_time_us_);
Ivo Creusen301aaed2015-10-08 18:07:41 +0200243 start_event.set_type(rtclog::Event::LOG_START);
Bjorn Terelius36411852015-07-30 12:45:18 +0200244 StoreToFile(&start_event);
245}
246
247void RtcEventLogImpl::StopLogging() {
248 rtc::CritScope lock(&crit_);
249 StopLoggingLocked();
250}
251
252void RtcEventLogImpl::LogVideoReceiveStreamConfig(
253 const VideoReceiveStream::Config& config) {
254 rtc::CritScope lock(&crit_);
255
256 rtclog::Event event;
257 const int64_t timestamp = clock_->TimeInMicroseconds();
258 event.set_timestamp_us(timestamp);
259 event.set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT);
260
261 rtclog::VideoReceiveConfig* receiver_config =
262 event.mutable_video_receiver_config();
263 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc);
264 receiver_config->set_local_ssrc(config.rtp.local_ssrc);
265
266 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode));
267
268 receiver_config->set_receiver_reference_time_report(
269 config.rtp.rtcp_xr.receiver_reference_time_report);
270 receiver_config->set_remb(config.rtp.remb);
271
272 for (const auto& kv : config.rtp.rtx) {
273 rtclog::RtxMap* rtx = receiver_config->add_rtx_map();
274 rtx->set_payload_type(kv.first);
275 rtx->mutable_config()->set_rtx_ssrc(kv.second.ssrc);
276 rtx->mutable_config()->set_rtx_payload_type(kv.second.payload_type);
277 }
278
279 for (const auto& e : config.rtp.extensions) {
280 rtclog::RtpHeaderExtension* extension =
281 receiver_config->add_header_extensions();
282 extension->set_name(e.name);
283 extension->set_id(e.id);
284 }
285
286 for (const auto& d : config.decoders) {
287 rtclog::DecoderConfig* decoder = receiver_config->add_decoders();
288 decoder->set_name(d.payload_name);
289 decoder->set_payload_type(d.payload_type);
290 }
Bjorn Terelius36411852015-07-30 12:45:18 +0200291 HandleEvent(&event);
292}
293
294void RtcEventLogImpl::LogVideoSendStreamConfig(
295 const VideoSendStream::Config& config) {
296 rtc::CritScope lock(&crit_);
297
298 rtclog::Event event;
299 const int64_t timestamp = clock_->TimeInMicroseconds();
300 event.set_timestamp_us(timestamp);
301 event.set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT);
302
303 rtclog::VideoSendConfig* sender_config = event.mutable_video_sender_config();
304
305 for (const auto& ssrc : config.rtp.ssrcs) {
306 sender_config->add_ssrcs(ssrc);
307 }
308
309 for (const auto& e : config.rtp.extensions) {
310 rtclog::RtpHeaderExtension* extension =
311 sender_config->add_header_extensions();
312 extension->set_name(e.name);
313 extension->set_id(e.id);
314 }
315
316 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) {
317 sender_config->add_rtx_ssrcs(rtx_ssrc);
318 }
319 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type);
320
321 sender_config->set_c_name(config.rtp.c_name);
322
323 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder();
324 encoder->set_name(config.encoder_settings.payload_name);
325 encoder->set_payload_type(config.encoder_settings.payload_type);
Bjorn Terelius36411852015-07-30 12:45:18 +0200326 HandleEvent(&event);
327}
328
Bjorn Terelius36411852015-07-30 12:45:18 +0200329void RtcEventLogImpl::LogRtpHeader(bool incoming,
330 MediaType media_type,
331 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -0700332 size_t packet_length) {
333 // Read header length (in bytes) from packet data.
334 if (packet_length < 12u) {
335 return; // Don't read outside the packet.
336 }
337 const bool x = (header[0] & 0x10) != 0;
338 const uint8_t cc = header[0] & 0x0f;
339 size_t header_length = 12u + cc * 4u;
340
341 if (x) {
342 if (packet_length < 12u + cc * 4u + 4u) {
343 return; // Don't read outside the packet.
344 }
345 size_t x_len = ByteReader<uint16_t>::ReadBigEndian(header + 14 + cc * 4);
346 header_length += (x_len + 1) * 4;
347 }
348
Bjorn Terelius36411852015-07-30 12:45:18 +0200349 rtc::CritScope lock(&crit_);
350 rtclog::Event rtp_event;
351 const int64_t timestamp = clock_->TimeInMicroseconds();
352 rtp_event.set_timestamp_us(timestamp);
353 rtp_event.set_type(rtclog::Event::RTP_EVENT);
354 rtp_event.mutable_rtp_packet()->set_incoming(incoming);
355 rtp_event.mutable_rtp_packet()->set_type(ConvertMediaType(media_type));
terelius2f9fd5d2015-09-04 03:39:42 -0700356 rtp_event.mutable_rtp_packet()->set_packet_length(packet_length);
Bjorn Terelius36411852015-07-30 12:45:18 +0200357 rtp_event.mutable_rtp_packet()->set_header(header, header_length);
358 HandleEvent(&rtp_event);
359}
360
361void RtcEventLogImpl::LogRtcpPacket(bool incoming,
362 MediaType media_type,
363 const uint8_t* packet,
364 size_t length) {
365 rtc::CritScope lock(&crit_);
366 rtclog::Event rtcp_event;
367 const int64_t timestamp = clock_->TimeInMicroseconds();
368 rtcp_event.set_timestamp_us(timestamp);
369 rtcp_event.set_type(rtclog::Event::RTCP_EVENT);
370 rtcp_event.mutable_rtcp_packet()->set_incoming(incoming);
371 rtcp_event.mutable_rtcp_packet()->set_type(ConvertMediaType(media_type));
372 rtcp_event.mutable_rtcp_packet()->set_packet_data(packet, length);
373 HandleEvent(&rtcp_event);
374}
375
Ivo Creusenae856f22015-09-17 16:30:16 +0200376void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) {
Bjorn Terelius36411852015-07-30 12:45:18 +0200377 rtc::CritScope lock(&crit_);
378 rtclog::Event event;
379 const int64_t timestamp = clock_->TimeInMicroseconds();
380 event.set_timestamp_us(timestamp);
Ivo Creusen301aaed2015-10-08 18:07:41 +0200381 event.set_type(rtclog::Event::AUDIO_PLAYOUT_EVENT);
382 auto playout_event = event.mutable_audio_playout_event();
383 playout_event->set_local_ssrc(ssrc);
Bjorn Terelius36411852015-07-30 12:45:18 +0200384 HandleEvent(&event);
385}
386
387void RtcEventLogImpl::StopLoggingLocked() {
388 if (currently_logging_) {
389 currently_logging_ = false;
Ivo Creusen301aaed2015-10-08 18:07:41 +0200390 // Create a LogEnd event
Bjorn Terelius36411852015-07-30 12:45:18 +0200391 rtclog::Event event;
392 int64_t timestamp = clock_->TimeInMicroseconds();
393 event.set_timestamp_us(timestamp);
Ivo Creusen301aaed2015-10-08 18:07:41 +0200394 event.set_type(rtclog::Event::LOG_END);
Bjorn Terelius36411852015-07-30 12:45:18 +0200395 // Store the event and close the file
henrikg91d6ede2015-09-17 00:24:34 -0700396 RTC_DCHECK(file_->Open());
Bjorn Terelius36411852015-07-30 12:45:18 +0200397 StoreToFile(&event);
398 file_->CloseFile();
ivoc112a3d82015-10-16 02:22:18 -0700399 if (platform_file_ != rtc::kInvalidPlatformFileValue) {
400 rtc::ClosePlatformFile(platform_file_);
401 platform_file_ = rtc::kInvalidPlatformFileValue;
402 }
Bjorn Terelius36411852015-07-30 12:45:18 +0200403 }
henrikg91d6ede2015-09-17 00:24:34 -0700404 RTC_DCHECK(!file_->Open());
Bjorn Terelius36411852015-07-30 12:45:18 +0200405 stream_.Clear();
406}
407
408void RtcEventLogImpl::HandleEvent(rtclog::Event* event) {
409 if (currently_logging_) {
410 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) {
411 StoreToFile(event);
412 return;
413 }
414 StopLoggingLocked();
415 }
416 AddRecentEvent(*event);
417}
418
419void RtcEventLogImpl::StoreToFile(rtclog::Event* event) {
420 // Reuse the same object at every log event.
421 if (stream_.stream_size() < 1) {
422 stream_.add_stream();
423 }
henrikg91d6ede2015-09-17 00:24:34 -0700424 RTC_DCHECK_EQ(stream_.stream_size(), 1);
Bjorn Terelius36411852015-07-30 12:45:18 +0200425 stream_.mutable_stream(0)->Swap(event);
426 // TODO(terelius): Doesn't this create a new EventStream per event?
427 // Is this guaranteed to work e.g. in future versions of protobuf?
428 std::string dump_buffer;
429 stream_.SerializeToString(&dump_buffer);
430 file_->Write(dump_buffer.data(), dump_buffer.size());
431}
432
433void RtcEventLogImpl::AddRecentEvent(const rtclog::Event& event) {
434 recent_log_events_.push_back(event);
435 while (recent_log_events_.front().timestamp_us() <
terelius1adce142015-10-16 08:51:08 -0700436 event.timestamp_us() - buffer_duration_us_) {
437 if (IsConfigEvent(recent_log_events_.front())) {
438 config_events_.push_back(recent_log_events_.front());
439 }
Bjorn Terelius36411852015-07-30 12:45:18 +0200440 recent_log_events_.pop_front();
441 }
442}
443
444bool RtcEventLog::ParseRtcEventLog(const std::string& file_name,
445 rtclog::EventStream* result) {
446 char tmp_buffer[1024];
447 int bytes_read = 0;
448 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create());
449 if (dump_file->OpenFile(file_name.c_str(), true) != 0) {
450 return false;
451 }
452 std::string dump_buffer;
453 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) {
454 dump_buffer.append(tmp_buffer, bytes_read);
455 }
456 dump_file->CloseFile();
457 return result->ParseFromString(dump_buffer);
458}
459
460#endif // ENABLE_RTC_EVENT_LOG
461
462// RtcEventLog member functions.
463rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() {
464 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl());
465}
terelius1adce142015-10-16 08:51:08 -0700466
Bjorn Terelius36411852015-07-30 12:45:18 +0200467} // namespace webrtc