blob: 7ea1e167a1e0a15e905c1e47bb722de84b97018f [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
terelius4311ba52016-04-22 12:40:37 -070013#include <limits>
terelius1adce142015-10-16 08:51:08 -070014#include <vector>
Bjorn Terelius36411852015-07-30 12:45:18 +020015
16#include "webrtc/base/checks.h"
terelius4311ba52016-04-22 12:40:37 -070017#include "webrtc/base/constructormagic.h"
18#include "webrtc/base/event.h"
19#include "webrtc/base/swap_queue.h"
20#include "webrtc/base/thread_checker.h"
Bjorn Terelius36411852015-07-30 12:45:18 +020021#include "webrtc/call.h"
terelius4311ba52016-04-22 12:40:37 -070022#include "webrtc/call/rtc_event_log_helper_thread.h"
tereliusd66daa22015-11-06 09:00:18 -080023#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
terelius2f9fd5d2015-09-04 03:39:42 -070024#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
tereliusd66daa22015-11-06 09:00:18 -080025#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010026#include "webrtc/system_wrappers/include/clock.h"
27#include "webrtc/system_wrappers/include/file_wrapper.h"
terelius4311ba52016-04-22 12:40:37 -070028#include "webrtc/system_wrappers/include/logging.h"
Bjorn Terelius36411852015-07-30 12:45:18 +020029
30#ifdef ENABLE_RTC_EVENT_LOG
31// Files generated at build-time by the protobuf compiler.
32#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
Peter Boström5c389d32015-09-25 13:58:30 +020033#include "external/webrtc/webrtc/call/rtc_event_log.pb.h"
Bjorn Terelius36411852015-07-30 12:45:18 +020034#else
Peter Boström5c389d32015-09-25 13:58:30 +020035#include "webrtc/call/rtc_event_log.pb.h"
Bjorn Terelius36411852015-07-30 12:45:18 +020036#endif
37#endif
38
39namespace webrtc {
40
41#ifndef ENABLE_RTC_EVENT_LOG
42
43// No-op implementation if flag is not set.
terelius4311ba52016-04-22 12:40:37 -070044class RtcEventLogNullImpl final : public RtcEventLog {
Bjorn Terelius36411852015-07-30 12:45:18 +020045 public:
terelius4311ba52016-04-22 12:40:37 -070046 bool StartLogging(const std::string& file_name,
47 int64_t max_size_bytes) override {
48 return false;
49 }
50 bool StartLogging(rtc::PlatformFile platform_file,
51 int64_t max_size_bytes) override {
terelius43587e32016-05-27 02:22:51 -070052 // The platform_file is open and needs to be closed.
53 if (!rtc::ClosePlatformFile(platform_file)) {
54 LOG(LS_ERROR) << "Can't close file.";
55 }
terelius4311ba52016-04-22 12:40:37 -070056 return false;
57 }
58 void StopLogging() override {}
Bjorn Terelius36411852015-07-30 12:45:18 +020059 void LogVideoReceiveStreamConfig(
60 const VideoReceiveStream::Config& config) override {}
61 void LogVideoSendStreamConfig(
62 const VideoSendStream::Config& config) override {}
terelius429c3452016-01-21 05:42:04 -080063 void LogRtpHeader(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +020064 MediaType media_type,
65 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -070066 size_t packet_length) override {}
terelius429c3452016-01-21 05:42:04 -080067 void LogRtcpPacket(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +020068 MediaType media_type,
69 const uint8_t* packet,
70 size_t length) override {}
Ivo Creusenae856f22015-09-17 16:30:16 +020071 void LogAudioPlayout(uint32_t ssrc) override {}
terelius006d93d2015-11-05 12:02:15 -080072 void LogBwePacketLossEvent(int32_t bitrate,
73 uint8_t fraction_loss,
74 int32_t total_packets) override {}
Bjorn Terelius36411852015-07-30 12:45:18 +020075};
76
77#else // ENABLE_RTC_EVENT_LOG is defined
78
79class RtcEventLogImpl final : public RtcEventLog {
80 public:
terelius4311ba52016-04-22 12:40:37 -070081 explicit RtcEventLogImpl(const Clock* clock);
82 ~RtcEventLogImpl() override;
terelius1adce142015-10-16 08:51:08 -070083
terelius4311ba52016-04-22 12:40:37 -070084 bool StartLogging(const std::string& file_name,
85 int64_t max_size_bytes) override;
86 bool StartLogging(rtc::PlatformFile platform_file,
87 int64_t max_size_bytes) override;
Bjorn Terelius36411852015-07-30 12:45:18 +020088 void StopLogging() override;
89 void LogVideoReceiveStreamConfig(
90 const VideoReceiveStream::Config& config) override;
91 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override;
terelius429c3452016-01-21 05:42:04 -080092 void LogRtpHeader(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +020093 MediaType media_type,
94 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -070095 size_t packet_length) override;
terelius429c3452016-01-21 05:42:04 -080096 void LogRtcpPacket(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +020097 MediaType media_type,
98 const uint8_t* packet,
99 size_t length) override;
Ivo Creusenae856f22015-09-17 16:30:16 +0200100 void LogAudioPlayout(uint32_t ssrc) override;
terelius006d93d2015-11-05 12:02:15 -0800101 void LogBwePacketLossEvent(int32_t bitrate,
102 uint8_t fraction_loss,
103 int32_t total_packets) override;
Bjorn Terelius36411852015-07-30 12:45:18 +0200104
105 private:
tereliusbea89592016-06-08 07:20:29 -0700106 void StoreEvent(std::unique_ptr<rtclog::Event>* event);
107
terelius4311ba52016-04-22 12:40:37 -0700108 // Message queue for passing control messages to the logging thread.
109 SwapQueue<RtcEventLogHelperThread::ControlMessage> message_queue_;
Bjorn Terelius36411852015-07-30 12:45:18 +0200110
terelius4311ba52016-04-22 12:40:37 -0700111 // Message queue for passing events to the logging thread.
112 SwapQueue<std::unique_ptr<rtclog::Event> > event_queue_;
terelius1adce142015-10-16 08:51:08 -0700113
terelius1adce142015-10-16 08:51:08 -0700114 const Clock* const clock_;
terelius4311ba52016-04-22 12:40:37 -0700115
116 RtcEventLogHelperThread helper_thread_;
117 rtc::ThreadChecker thread_checker_;
118
119 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtcEventLogImpl);
Bjorn Terelius36411852015-07-30 12:45:18 +0200120};
121
122namespace {
123// The functions in this namespace convert enums from the runtime format
124// that the rest of the WebRtc project can use, to the corresponding
125// serialized enum which is defined by the protobuf.
126
pbosda903ea2015-10-02 02:36:56 -0700127rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) {
Bjorn Terelius36411852015-07-30 12:45:18 +0200128 switch (rtcp_mode) {
pbosda903ea2015-10-02 02:36:56 -0700129 case RtcpMode::kCompound:
Bjorn Terelius36411852015-07-30 12:45:18 +0200130 return rtclog::VideoReceiveConfig::RTCP_COMPOUND;
pbosda903ea2015-10-02 02:36:56 -0700131 case RtcpMode::kReducedSize:
Bjorn Terelius36411852015-07-30 12:45:18 +0200132 return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE;
pbosda903ea2015-10-02 02:36:56 -0700133 case RtcpMode::kOff:
134 RTC_NOTREACHED();
135 return rtclog::VideoReceiveConfig::RTCP_COMPOUND;
Bjorn Terelius36411852015-07-30 12:45:18 +0200136 }
137 RTC_NOTREACHED();
138 return rtclog::VideoReceiveConfig::RTCP_COMPOUND;
139}
140
141rtclog::MediaType ConvertMediaType(MediaType media_type) {
142 switch (media_type) {
143 case MediaType::ANY:
144 return rtclog::MediaType::ANY;
145 case MediaType::AUDIO:
146 return rtclog::MediaType::AUDIO;
147 case MediaType::VIDEO:
148 return rtclog::MediaType::VIDEO;
149 case MediaType::DATA:
150 return rtclog::MediaType::DATA;
151 }
152 RTC_NOTREACHED();
153 return rtclog::ANY;
154}
155
terelius4311ba52016-04-22 12:40:37 -0700156// The RTP and RTCP buffers reserve space for twice the expected number of
157// sent packets because they also contain received packets.
158static const int kEventsPerSecond = 1000;
159static const int kControlMessagesPerSecond = 10;
terelius1adce142015-10-16 08:51:08 -0700160} // namespace
161
Bjorn Terelius36411852015-07-30 12:45:18 +0200162// RtcEventLogImpl member functions.
terelius4311ba52016-04-22 12:40:37 -0700163RtcEventLogImpl::RtcEventLogImpl(const Clock* clock)
164 // Allocate buffers for roughly one second of history.
165 : message_queue_(kControlMessagesPerSecond),
166 event_queue_(kEventsPerSecond),
terelius4311ba52016-04-22 12:40:37 -0700167 clock_(clock),
168 helper_thread_(&message_queue_,
169 &event_queue_,
terelius4311ba52016-04-22 12:40:37 -0700170 clock),
171 thread_checker_() {
172 thread_checker_.DetachFromThread();
terelius1adce142015-10-16 08:51:08 -0700173}
174
terelius4311ba52016-04-22 12:40:37 -0700175RtcEventLogImpl::~RtcEventLogImpl() {
176 // The RtcEventLogHelperThread destructor closes the file
177 // and waits for the thread to terminate.
terelius1adce142015-10-16 08:51:08 -0700178}
Bjorn Terelius36411852015-07-30 12:45:18 +0200179
terelius4311ba52016-04-22 12:40:37 -0700180bool RtcEventLogImpl::StartLogging(const std::string& file_name,
181 int64_t max_size_bytes) {
182 RTC_DCHECK(thread_checker_.CalledOnValidThread());
183 RtcEventLogHelperThread::ControlMessage message;
184 message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE;
ivocc1513ee2016-05-13 08:30:39 -0700185 message.max_size_bytes = max_size_bytes <= 0
186 ? std::numeric_limits<int64_t>::max()
187 : max_size_bytes;
terelius4311ba52016-04-22 12:40:37 -0700188 message.start_time = clock_->TimeInMicroseconds();
189 message.stop_time = std::numeric_limits<int64_t>::max();
190 message.file.reset(FileWrapper::Create());
tommia6219cc2016-06-15 10:30:14 -0700191 if (!message.file->OpenFile(file_name.c_str(), false)) {
terelius43587e32016-05-27 02:22:51 -0700192 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
ivoc112a3d82015-10-16 02:22:18 -0700193 return false;
194 }
terelius4311ba52016-04-22 12:40:37 -0700195 if (!message_queue_.Insert(&message)) {
terelius43587e32016-05-27 02:22:51 -0700196 LOG(LS_ERROR) << "Message queue full. Can't start logging.";
ivoc112a3d82015-10-16 02:22:18 -0700197 return false;
198 }
tereliusbea89592016-06-08 07:20:29 -0700199 helper_thread_.SignalNewEvent();
terelius43587e32016-05-27 02:22:51 -0700200 LOG(LS_INFO) << "Starting WebRTC event log.";
ivoc112a3d82015-10-16 02:22:18 -0700201 return true;
202}
203
terelius4311ba52016-04-22 12:40:37 -0700204bool RtcEventLogImpl::StartLogging(rtc::PlatformFile platform_file,
205 int64_t max_size_bytes) {
206 RTC_DCHECK(thread_checker_.CalledOnValidThread());
207 RtcEventLogHelperThread::ControlMessage message;
208 message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE;
ivocc1513ee2016-05-13 08:30:39 -0700209 message.max_size_bytes = max_size_bytes <= 0
210 ? std::numeric_limits<int64_t>::max()
211 : max_size_bytes;
terelius4311ba52016-04-22 12:40:37 -0700212 message.start_time = clock_->TimeInMicroseconds();
213 message.stop_time = std::numeric_limits<int64_t>::max();
214 message.file.reset(FileWrapper::Create());
215 FILE* file_handle = rtc::FdopenPlatformFileForWriting(platform_file);
216 if (!file_handle) {
terelius43587e32016-05-27 02:22:51 -0700217 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
218 // Even though we failed to open a FILE*, the platform_file is still open
219 // and needs to be closed.
220 if (!rtc::ClosePlatformFile(platform_file)) {
221 LOG(LS_ERROR) << "Can't close file.";
222 }
terelius4311ba52016-04-22 12:40:37 -0700223 return false;
terelius1adce142015-10-16 08:51:08 -0700224 }
tommia6219cc2016-06-15 10:30:14 -0700225 if (!message.file->OpenFromFileHandle(file_handle)) {
terelius43587e32016-05-27 02:22:51 -0700226 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
terelius4311ba52016-04-22 12:40:37 -0700227 return false;
Bjorn Terelius36411852015-07-30 12:45:18 +0200228 }
terelius4311ba52016-04-22 12:40:37 -0700229 if (!message_queue_.Insert(&message)) {
terelius43587e32016-05-27 02:22:51 -0700230 LOG(LS_ERROR) << "Message queue full. Can't start logging.";
terelius4311ba52016-04-22 12:40:37 -0700231 return false;
232 }
tereliusbea89592016-06-08 07:20:29 -0700233 helper_thread_.SignalNewEvent();
terelius43587e32016-05-27 02:22:51 -0700234 LOG(LS_INFO) << "Starting WebRTC event log.";
terelius4311ba52016-04-22 12:40:37 -0700235 return true;
Bjorn Terelius36411852015-07-30 12:45:18 +0200236}
237
238void RtcEventLogImpl::StopLogging() {
terelius4311ba52016-04-22 12:40:37 -0700239 RTC_DCHECK(thread_checker_.CalledOnValidThread());
240 RtcEventLogHelperThread::ControlMessage message;
241 message.message_type = RtcEventLogHelperThread::ControlMessage::STOP_FILE;
242 message.stop_time = clock_->TimeInMicroseconds();
243 while (!message_queue_.Insert(&message)) {
244 // TODO(terelius): We would like to have a blocking Insert function in the
245 // SwapQueue, but for the time being we will just clear any previous
246 // messages.
247 // Since StopLogging waits for the thread, it is essential that we don't
248 // clear any STOP_FILE messages. To ensure that there is only one call at a
249 // time, we require that all calls to StopLogging are made on the same
250 // thread.
terelius43587e32016-05-27 02:22:51 -0700251 LOG(LS_ERROR) << "Message queue full. Clearing queue to stop logging.";
terelius4311ba52016-04-22 12:40:37 -0700252 message_queue_.Clear();
253 }
terelius43587e32016-05-27 02:22:51 -0700254 LOG(LS_INFO) << "Stopping WebRTC event log.";
tereliusbea89592016-06-08 07:20:29 -0700255 helper_thread_.WaitForFileFinished();
Bjorn Terelius36411852015-07-30 12:45:18 +0200256}
257
258void RtcEventLogImpl::LogVideoReceiveStreamConfig(
259 const VideoReceiveStream::Config& config) {
terelius4311ba52016-04-22 12:40:37 -0700260 std::unique_ptr<rtclog::Event> event(new rtclog::Event());
261 event->set_timestamp_us(clock_->TimeInMicroseconds());
262 event->set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT);
Bjorn Terelius36411852015-07-30 12:45:18 +0200263
264 rtclog::VideoReceiveConfig* receiver_config =
terelius4311ba52016-04-22 12:40:37 -0700265 event->mutable_video_receiver_config();
Bjorn Terelius36411852015-07-30 12:45:18 +0200266 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc);
267 receiver_config->set_local_ssrc(config.rtp.local_ssrc);
268
269 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode));
Bjorn Terelius36411852015-07-30 12:45:18 +0200270 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();
isheriff6f8d6862016-05-26 11:24:55 -0700282 extension->set_name(e.uri);
Bjorn Terelius36411852015-07-30 12:45:18 +0200283 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 }
tereliusbea89592016-06-08 07:20:29 -0700291 StoreEvent(&event);
Bjorn Terelius36411852015-07-30 12:45:18 +0200292}
293
294void RtcEventLogImpl::LogVideoSendStreamConfig(
295 const VideoSendStream::Config& config) {
terelius4311ba52016-04-22 12:40:37 -0700296 std::unique_ptr<rtclog::Event> event(new rtclog::Event());
297 event->set_timestamp_us(clock_->TimeInMicroseconds());
298 event->set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT);
Bjorn Terelius36411852015-07-30 12:45:18 +0200299
terelius4311ba52016-04-22 12:40:37 -0700300 rtclog::VideoSendConfig* sender_config = event->mutable_video_sender_config();
Bjorn Terelius36411852015-07-30 12:45:18 +0200301
302 for (const auto& ssrc : config.rtp.ssrcs) {
303 sender_config->add_ssrcs(ssrc);
304 }
305
306 for (const auto& e : config.rtp.extensions) {
307 rtclog::RtpHeaderExtension* extension =
308 sender_config->add_header_extensions();
isheriff6f8d6862016-05-26 11:24:55 -0700309 extension->set_name(e.uri);
Bjorn Terelius36411852015-07-30 12:45:18 +0200310 extension->set_id(e.id);
311 }
312
313 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) {
314 sender_config->add_rtx_ssrcs(rtx_ssrc);
315 }
316 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type);
317
Bjorn Terelius36411852015-07-30 12:45:18 +0200318 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder();
319 encoder->set_name(config.encoder_settings.payload_name);
320 encoder->set_payload_type(config.encoder_settings.payload_type);
tereliusbea89592016-06-08 07:20:29 -0700321 StoreEvent(&event);
Bjorn Terelius36411852015-07-30 12:45:18 +0200322}
323
terelius429c3452016-01-21 05:42:04 -0800324void RtcEventLogImpl::LogRtpHeader(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +0200325 MediaType media_type,
326 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -0700327 size_t packet_length) {
328 // Read header length (in bytes) from packet data.
329 if (packet_length < 12u) {
330 return; // Don't read outside the packet.
331 }
332 const bool x = (header[0] & 0x10) != 0;
333 const uint8_t cc = header[0] & 0x0f;
334 size_t header_length = 12u + cc * 4u;
335
336 if (x) {
337 if (packet_length < 12u + cc * 4u + 4u) {
338 return; // Don't read outside the packet.
339 }
340 size_t x_len = ByteReader<uint16_t>::ReadBigEndian(header + 14 + cc * 4);
341 header_length += (x_len + 1) * 4;
342 }
343
terelius4311ba52016-04-22 12:40:37 -0700344 std::unique_ptr<rtclog::Event> rtp_event(new rtclog::Event());
345 rtp_event->set_timestamp_us(clock_->TimeInMicroseconds());
346 rtp_event->set_type(rtclog::Event::RTP_EVENT);
347 rtp_event->mutable_rtp_packet()->set_incoming(direction == kIncomingPacket);
348 rtp_event->mutable_rtp_packet()->set_type(ConvertMediaType(media_type));
349 rtp_event->mutable_rtp_packet()->set_packet_length(packet_length);
350 rtp_event->mutable_rtp_packet()->set_header(header, header_length);
tereliusbea89592016-06-08 07:20:29 -0700351 StoreEvent(&rtp_event);
Bjorn Terelius36411852015-07-30 12:45:18 +0200352}
353
terelius429c3452016-01-21 05:42:04 -0800354void RtcEventLogImpl::LogRtcpPacket(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +0200355 MediaType media_type,
356 const uint8_t* packet,
357 size_t length) {
terelius4311ba52016-04-22 12:40:37 -0700358 std::unique_ptr<rtclog::Event> rtcp_event(new rtclog::Event());
359 rtcp_event->set_timestamp_us(clock_->TimeInMicroseconds());
360 rtcp_event->set_type(rtclog::Event::RTCP_EVENT);
361 rtcp_event->mutable_rtcp_packet()->set_incoming(direction == kIncomingPacket);
362 rtcp_event->mutable_rtcp_packet()->set_type(ConvertMediaType(media_type));
tereliusd66daa22015-11-06 09:00:18 -0800363
364 RTCPUtility::RtcpCommonHeader header;
365 const uint8_t* block_begin = packet;
366 const uint8_t* packet_end = packet + length;
367 RTC_DCHECK(length <= IP_PACKET_SIZE);
368 uint8_t buffer[IP_PACKET_SIZE];
369 uint32_t buffer_length = 0;
370 while (block_begin < packet_end) {
371 if (!RtcpParseCommonHeader(block_begin, packet_end - block_begin,
372 &header)) {
373 break; // Incorrect message header.
374 }
375 uint32_t block_size = header.BlockSize();
376 switch (header.packet_type) {
377 case RTCPUtility::PT_SR:
378 FALLTHROUGH();
379 case RTCPUtility::PT_RR:
380 FALLTHROUGH();
381 case RTCPUtility::PT_BYE:
382 FALLTHROUGH();
383 case RTCPUtility::PT_IJ:
384 FALLTHROUGH();
385 case RTCPUtility::PT_RTPFB:
386 FALLTHROUGH();
387 case RTCPUtility::PT_PSFB:
388 FALLTHROUGH();
389 case RTCPUtility::PT_XR:
390 // We log sender reports, receiver reports, bye messages
391 // inter-arrival jitter, third-party loss reports, payload-specific
392 // feedback and extended reports.
393 memcpy(buffer + buffer_length, block_begin, block_size);
394 buffer_length += block_size;
395 break;
396 case RTCPUtility::PT_SDES:
397 FALLTHROUGH();
398 case RTCPUtility::PT_APP:
399 FALLTHROUGH();
400 default:
401 // We don't log sender descriptions, application defined messages
402 // or message blocks of unknown type.
403 break;
404 }
405
406 block_begin += block_size;
407 }
terelius4311ba52016-04-22 12:40:37 -0700408 rtcp_event->mutable_rtcp_packet()->set_packet_data(buffer, buffer_length);
tereliusbea89592016-06-08 07:20:29 -0700409 StoreEvent(&rtcp_event);
Bjorn Terelius36411852015-07-30 12:45:18 +0200410}
411
Ivo Creusenae856f22015-09-17 16:30:16 +0200412void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) {
terelius4311ba52016-04-22 12:40:37 -0700413 std::unique_ptr<rtclog::Event> event(new rtclog::Event());
414 event->set_timestamp_us(clock_->TimeInMicroseconds());
415 event->set_type(rtclog::Event::AUDIO_PLAYOUT_EVENT);
416 auto playout_event = event->mutable_audio_playout_event();
Ivo Creusen301aaed2015-10-08 18:07:41 +0200417 playout_event->set_local_ssrc(ssrc);
tereliusbea89592016-06-08 07:20:29 -0700418 StoreEvent(&event);
Bjorn Terelius36411852015-07-30 12:45:18 +0200419}
420
terelius006d93d2015-11-05 12:02:15 -0800421void RtcEventLogImpl::LogBwePacketLossEvent(int32_t bitrate,
422 uint8_t fraction_loss,
423 int32_t total_packets) {
terelius4311ba52016-04-22 12:40:37 -0700424 std::unique_ptr<rtclog::Event> event(new rtclog::Event());
425 event->set_timestamp_us(clock_->TimeInMicroseconds());
426 event->set_type(rtclog::Event::BWE_PACKET_LOSS_EVENT);
427 auto bwe_event = event->mutable_bwe_packet_loss_event();
terelius006d93d2015-11-05 12:02:15 -0800428 bwe_event->set_bitrate(bitrate);
429 bwe_event->set_fraction_loss(fraction_loss);
430 bwe_event->set_total_packets(total_packets);
tereliusbea89592016-06-08 07:20:29 -0700431 StoreEvent(&event);
432}
433
434void RtcEventLogImpl::StoreEvent(std::unique_ptr<rtclog::Event>* event) {
435 if (!event_queue_.Insert(event)) {
436 LOG(LS_ERROR) << "WebRTC event log queue full. Dropping event.";
Bjorn Terelius36411852015-07-30 12:45:18 +0200437 }
tereliusbea89592016-06-08 07:20:29 -0700438 helper_thread_.SignalNewEvent();
Bjorn Terelius36411852015-07-30 12:45:18 +0200439}
440
441bool RtcEventLog::ParseRtcEventLog(const std::string& file_name,
442 rtclog::EventStream* result) {
443 char tmp_buffer[1024];
444 int bytes_read = 0;
kwibergb25345e2016-03-12 06:10:44 -0800445 std::unique_ptr<FileWrapper> dump_file(FileWrapper::Create());
tommia6219cc2016-06-15 10:30:14 -0700446 if (!dump_file->OpenFile(file_name.c_str(), true)) {
Bjorn Terelius36411852015-07-30 12:45:18 +0200447 return false;
448 }
449 std::string dump_buffer;
450 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) {
451 dump_buffer.append(tmp_buffer, bytes_read);
452 }
453 dump_file->CloseFile();
454 return result->ParseFromString(dump_buffer);
455}
456
457#endif // ENABLE_RTC_EVENT_LOG
458
459// RtcEventLog member functions.
terelius4311ba52016-04-22 12:40:37 -0700460std::unique_ptr<RtcEventLog> RtcEventLog::Create(const Clock* clock) {
461#ifdef ENABLE_RTC_EVENT_LOG
462 return std::unique_ptr<RtcEventLog>(new RtcEventLogImpl(clock));
463#else
464 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl());
465#endif // ENABLE_RTC_EVENT_LOG
Bjorn Terelius36411852015-07-30 12:45:18 +0200466}
terelius1adce142015-10-16 08:51:08 -0700467
Bjorn Terelius36411852015-07-30 12:45:18 +0200468} // namespace webrtc