blob: 840b210d15378dbbdf1ee079886674fe6064d27b [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
ivoc14d5dbe2016-07-04 07:06:55 -070041// No-op implementation is used if flag is not set, or in tests.
terelius4311ba52016-04-22 12:40:37 -070042class RtcEventLogNullImpl final : public RtcEventLog {
Bjorn Terelius36411852015-07-30 12:45:18 +020043 public:
terelius4311ba52016-04-22 12:40:37 -070044 bool StartLogging(const std::string& file_name,
45 int64_t max_size_bytes) override {
46 return false;
47 }
48 bool StartLogging(rtc::PlatformFile platform_file,
49 int64_t max_size_bytes) override {
terelius43587e32016-05-27 02:22:51 -070050 // The platform_file is open and needs to be closed.
51 if (!rtc::ClosePlatformFile(platform_file)) {
52 LOG(LS_ERROR) << "Can't close file.";
53 }
terelius4311ba52016-04-22 12:40:37 -070054 return false;
55 }
56 void StopLogging() override {}
Bjorn Terelius36411852015-07-30 12:45:18 +020057 void LogVideoReceiveStreamConfig(
58 const VideoReceiveStream::Config& config) override {}
59 void LogVideoSendStreamConfig(
60 const VideoSendStream::Config& config) override {}
terelius429c3452016-01-21 05:42:04 -080061 void LogRtpHeader(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +020062 MediaType media_type,
63 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -070064 size_t packet_length) override {}
terelius429c3452016-01-21 05:42:04 -080065 void LogRtcpPacket(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +020066 MediaType media_type,
67 const uint8_t* packet,
68 size_t length) override {}
Ivo Creusenae856f22015-09-17 16:30:16 +020069 void LogAudioPlayout(uint32_t ssrc) override {}
terelius006d93d2015-11-05 12:02:15 -080070 void LogBwePacketLossEvent(int32_t bitrate,
71 uint8_t fraction_loss,
72 int32_t total_packets) override {}
Bjorn Terelius36411852015-07-30 12:45:18 +020073};
74
ivoc14d5dbe2016-07-04 07:06:55 -070075#ifdef ENABLE_RTC_EVENT_LOG
Bjorn Terelius36411852015-07-30 12:45:18 +020076
77class RtcEventLogImpl final : public RtcEventLog {
78 public:
terelius4311ba52016-04-22 12:40:37 -070079 explicit RtcEventLogImpl(const Clock* clock);
80 ~RtcEventLogImpl() override;
terelius1adce142015-10-16 08:51:08 -070081
terelius4311ba52016-04-22 12:40:37 -070082 bool StartLogging(const std::string& file_name,
83 int64_t max_size_bytes) override;
84 bool StartLogging(rtc::PlatformFile platform_file,
85 int64_t max_size_bytes) override;
Bjorn Terelius36411852015-07-30 12:45:18 +020086 void StopLogging() override;
87 void LogVideoReceiveStreamConfig(
88 const VideoReceiveStream::Config& config) override;
89 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override;
terelius429c3452016-01-21 05:42:04 -080090 void LogRtpHeader(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +020091 MediaType media_type,
92 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -070093 size_t packet_length) override;
terelius429c3452016-01-21 05:42:04 -080094 void LogRtcpPacket(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +020095 MediaType media_type,
96 const uint8_t* packet,
97 size_t length) override;
Ivo Creusenae856f22015-09-17 16:30:16 +020098 void LogAudioPlayout(uint32_t ssrc) override;
terelius006d93d2015-11-05 12:02:15 -080099 void LogBwePacketLossEvent(int32_t bitrate,
100 uint8_t fraction_loss,
101 int32_t total_packets) override;
Bjorn Terelius36411852015-07-30 12:45:18 +0200102
103 private:
tereliusbea89592016-06-08 07:20:29 -0700104 void StoreEvent(std::unique_ptr<rtclog::Event>* event);
105
terelius4311ba52016-04-22 12:40:37 -0700106 // Message queue for passing control messages to the logging thread.
107 SwapQueue<RtcEventLogHelperThread::ControlMessage> message_queue_;
Bjorn Terelius36411852015-07-30 12:45:18 +0200108
terelius4311ba52016-04-22 12:40:37 -0700109 // Message queue for passing events to the logging thread.
110 SwapQueue<std::unique_ptr<rtclog::Event> > event_queue_;
terelius1adce142015-10-16 08:51:08 -0700111
terelius1adce142015-10-16 08:51:08 -0700112 const Clock* const clock_;
terelius4311ba52016-04-22 12:40:37 -0700113
114 RtcEventLogHelperThread helper_thread_;
115 rtc::ThreadChecker thread_checker_;
116
117 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtcEventLogImpl);
Bjorn Terelius36411852015-07-30 12:45:18 +0200118};
119
120namespace {
121// The functions in this namespace convert enums from the runtime format
122// that the rest of the WebRtc project can use, to the corresponding
123// serialized enum which is defined by the protobuf.
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
terelius4311ba52016-04-22 12:40:37 -0700154// The RTP and RTCP buffers reserve space for twice the expected number of
155// sent packets because they also contain received packets.
156static const int kEventsPerSecond = 1000;
157static const int kControlMessagesPerSecond = 10;
terelius1adce142015-10-16 08:51:08 -0700158} // namespace
159
Bjorn Terelius36411852015-07-30 12:45:18 +0200160// RtcEventLogImpl member functions.
terelius4311ba52016-04-22 12:40:37 -0700161RtcEventLogImpl::RtcEventLogImpl(const Clock* clock)
162 // Allocate buffers for roughly one second of history.
163 : message_queue_(kControlMessagesPerSecond),
164 event_queue_(kEventsPerSecond),
terelius4311ba52016-04-22 12:40:37 -0700165 clock_(clock),
166 helper_thread_(&message_queue_,
167 &event_queue_,
terelius4311ba52016-04-22 12:40:37 -0700168 clock),
169 thread_checker_() {
170 thread_checker_.DetachFromThread();
terelius1adce142015-10-16 08:51:08 -0700171}
172
terelius4311ba52016-04-22 12:40:37 -0700173RtcEventLogImpl::~RtcEventLogImpl() {
174 // The RtcEventLogHelperThread destructor closes the file
175 // and waits for the thread to terminate.
terelius1adce142015-10-16 08:51:08 -0700176}
Bjorn Terelius36411852015-07-30 12:45:18 +0200177
terelius4311ba52016-04-22 12:40:37 -0700178bool RtcEventLogImpl::StartLogging(const std::string& file_name,
179 int64_t max_size_bytes) {
180 RTC_DCHECK(thread_checker_.CalledOnValidThread());
181 RtcEventLogHelperThread::ControlMessage message;
182 message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE;
ivocc1513ee2016-05-13 08:30:39 -0700183 message.max_size_bytes = max_size_bytes <= 0
184 ? std::numeric_limits<int64_t>::max()
185 : max_size_bytes;
terelius4311ba52016-04-22 12:40:37 -0700186 message.start_time = clock_->TimeInMicroseconds();
187 message.stop_time = std::numeric_limits<int64_t>::max();
188 message.file.reset(FileWrapper::Create());
tommia6219cc2016-06-15 10:30:14 -0700189 if (!message.file->OpenFile(file_name.c_str(), false)) {
terelius43587e32016-05-27 02:22:51 -0700190 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
ivoc112a3d82015-10-16 02:22:18 -0700191 return false;
192 }
terelius4311ba52016-04-22 12:40:37 -0700193 if (!message_queue_.Insert(&message)) {
terelius43587e32016-05-27 02:22:51 -0700194 LOG(LS_ERROR) << "Message queue full. Can't start logging.";
ivoc112a3d82015-10-16 02:22:18 -0700195 return false;
196 }
tereliusbea89592016-06-08 07:20:29 -0700197 helper_thread_.SignalNewEvent();
terelius43587e32016-05-27 02:22:51 -0700198 LOG(LS_INFO) << "Starting WebRTC event log.";
ivoc112a3d82015-10-16 02:22:18 -0700199 return true;
200}
201
terelius4311ba52016-04-22 12:40:37 -0700202bool RtcEventLogImpl::StartLogging(rtc::PlatformFile platform_file,
203 int64_t max_size_bytes) {
204 RTC_DCHECK(thread_checker_.CalledOnValidThread());
205 RtcEventLogHelperThread::ControlMessage message;
206 message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE;
ivocc1513ee2016-05-13 08:30:39 -0700207 message.max_size_bytes = max_size_bytes <= 0
208 ? std::numeric_limits<int64_t>::max()
209 : max_size_bytes;
terelius4311ba52016-04-22 12:40:37 -0700210 message.start_time = clock_->TimeInMicroseconds();
211 message.stop_time = std::numeric_limits<int64_t>::max();
212 message.file.reset(FileWrapper::Create());
213 FILE* file_handle = rtc::FdopenPlatformFileForWriting(platform_file);
214 if (!file_handle) {
terelius43587e32016-05-27 02:22:51 -0700215 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
216 // Even though we failed to open a FILE*, the platform_file is still open
217 // and needs to be closed.
218 if (!rtc::ClosePlatformFile(platform_file)) {
219 LOG(LS_ERROR) << "Can't close file.";
220 }
terelius4311ba52016-04-22 12:40:37 -0700221 return false;
terelius1adce142015-10-16 08:51:08 -0700222 }
tommia6219cc2016-06-15 10:30:14 -0700223 if (!message.file->OpenFromFileHandle(file_handle)) {
terelius43587e32016-05-27 02:22:51 -0700224 LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
terelius4311ba52016-04-22 12:40:37 -0700225 return false;
Bjorn Terelius36411852015-07-30 12:45:18 +0200226 }
terelius4311ba52016-04-22 12:40:37 -0700227 if (!message_queue_.Insert(&message)) {
terelius43587e32016-05-27 02:22:51 -0700228 LOG(LS_ERROR) << "Message queue full. Can't start logging.";
terelius4311ba52016-04-22 12:40:37 -0700229 return false;
230 }
tereliusbea89592016-06-08 07:20:29 -0700231 helper_thread_.SignalNewEvent();
terelius43587e32016-05-27 02:22:51 -0700232 LOG(LS_INFO) << "Starting WebRTC event log.";
terelius4311ba52016-04-22 12:40:37 -0700233 return true;
Bjorn Terelius36411852015-07-30 12:45:18 +0200234}
235
236void RtcEventLogImpl::StopLogging() {
terelius4311ba52016-04-22 12:40:37 -0700237 RTC_DCHECK(thread_checker_.CalledOnValidThread());
238 RtcEventLogHelperThread::ControlMessage message;
239 message.message_type = RtcEventLogHelperThread::ControlMessage::STOP_FILE;
240 message.stop_time = clock_->TimeInMicroseconds();
241 while (!message_queue_.Insert(&message)) {
242 // TODO(terelius): We would like to have a blocking Insert function in the
243 // SwapQueue, but for the time being we will just clear any previous
244 // messages.
245 // Since StopLogging waits for the thread, it is essential that we don't
246 // clear any STOP_FILE messages. To ensure that there is only one call at a
247 // time, we require that all calls to StopLogging are made on the same
248 // thread.
terelius43587e32016-05-27 02:22:51 -0700249 LOG(LS_ERROR) << "Message queue full. Clearing queue to stop logging.";
terelius4311ba52016-04-22 12:40:37 -0700250 message_queue_.Clear();
251 }
terelius43587e32016-05-27 02:22:51 -0700252 LOG(LS_INFO) << "Stopping WebRTC event log.";
tereliusbea89592016-06-08 07:20:29 -0700253 helper_thread_.WaitForFileFinished();
Bjorn Terelius36411852015-07-30 12:45:18 +0200254}
255
256void RtcEventLogImpl::LogVideoReceiveStreamConfig(
257 const VideoReceiveStream::Config& config) {
terelius4311ba52016-04-22 12:40:37 -0700258 std::unique_ptr<rtclog::Event> event(new rtclog::Event());
259 event->set_timestamp_us(clock_->TimeInMicroseconds());
260 event->set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT);
Bjorn Terelius36411852015-07-30 12:45:18 +0200261
262 rtclog::VideoReceiveConfig* receiver_config =
terelius4311ba52016-04-22 12:40:37 -0700263 event->mutable_video_receiver_config();
Bjorn Terelius36411852015-07-30 12:45:18 +0200264 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc);
265 receiver_config->set_local_ssrc(config.rtp.local_ssrc);
266
267 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode));
Bjorn Terelius36411852015-07-30 12:45:18 +0200268 receiver_config->set_remb(config.rtp.remb);
269
270 for (const auto& kv : config.rtp.rtx) {
271 rtclog::RtxMap* rtx = receiver_config->add_rtx_map();
272 rtx->set_payload_type(kv.first);
273 rtx->mutable_config()->set_rtx_ssrc(kv.second.ssrc);
274 rtx->mutable_config()->set_rtx_payload_type(kv.second.payload_type);
275 }
276
277 for (const auto& e : config.rtp.extensions) {
278 rtclog::RtpHeaderExtension* extension =
279 receiver_config->add_header_extensions();
isheriff6f8d6862016-05-26 11:24:55 -0700280 extension->set_name(e.uri);
Bjorn Terelius36411852015-07-30 12:45:18 +0200281 extension->set_id(e.id);
282 }
283
284 for (const auto& d : config.decoders) {
285 rtclog::DecoderConfig* decoder = receiver_config->add_decoders();
286 decoder->set_name(d.payload_name);
287 decoder->set_payload_type(d.payload_type);
288 }
tereliusbea89592016-06-08 07:20:29 -0700289 StoreEvent(&event);
Bjorn Terelius36411852015-07-30 12:45:18 +0200290}
291
292void RtcEventLogImpl::LogVideoSendStreamConfig(
293 const VideoSendStream::Config& config) {
terelius4311ba52016-04-22 12:40:37 -0700294 std::unique_ptr<rtclog::Event> event(new rtclog::Event());
295 event->set_timestamp_us(clock_->TimeInMicroseconds());
296 event->set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT);
Bjorn Terelius36411852015-07-30 12:45:18 +0200297
terelius4311ba52016-04-22 12:40:37 -0700298 rtclog::VideoSendConfig* sender_config = event->mutable_video_sender_config();
Bjorn Terelius36411852015-07-30 12:45:18 +0200299
300 for (const auto& ssrc : config.rtp.ssrcs) {
301 sender_config->add_ssrcs(ssrc);
302 }
303
304 for (const auto& e : config.rtp.extensions) {
305 rtclog::RtpHeaderExtension* extension =
306 sender_config->add_header_extensions();
isheriff6f8d6862016-05-26 11:24:55 -0700307 extension->set_name(e.uri);
Bjorn Terelius36411852015-07-30 12:45:18 +0200308 extension->set_id(e.id);
309 }
310
311 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) {
312 sender_config->add_rtx_ssrcs(rtx_ssrc);
313 }
314 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type);
315
Bjorn Terelius36411852015-07-30 12:45:18 +0200316 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder();
317 encoder->set_name(config.encoder_settings.payload_name);
318 encoder->set_payload_type(config.encoder_settings.payload_type);
tereliusbea89592016-06-08 07:20:29 -0700319 StoreEvent(&event);
Bjorn Terelius36411852015-07-30 12:45:18 +0200320}
321
terelius429c3452016-01-21 05:42:04 -0800322void RtcEventLogImpl::LogRtpHeader(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +0200323 MediaType media_type,
324 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -0700325 size_t packet_length) {
326 // Read header length (in bytes) from packet data.
327 if (packet_length < 12u) {
328 return; // Don't read outside the packet.
329 }
330 const bool x = (header[0] & 0x10) != 0;
331 const uint8_t cc = header[0] & 0x0f;
332 size_t header_length = 12u + cc * 4u;
333
334 if (x) {
335 if (packet_length < 12u + cc * 4u + 4u) {
336 return; // Don't read outside the packet.
337 }
338 size_t x_len = ByteReader<uint16_t>::ReadBigEndian(header + 14 + cc * 4);
339 header_length += (x_len + 1) * 4;
340 }
341
terelius4311ba52016-04-22 12:40:37 -0700342 std::unique_ptr<rtclog::Event> rtp_event(new rtclog::Event());
343 rtp_event->set_timestamp_us(clock_->TimeInMicroseconds());
344 rtp_event->set_type(rtclog::Event::RTP_EVENT);
345 rtp_event->mutable_rtp_packet()->set_incoming(direction == kIncomingPacket);
346 rtp_event->mutable_rtp_packet()->set_type(ConvertMediaType(media_type));
347 rtp_event->mutable_rtp_packet()->set_packet_length(packet_length);
348 rtp_event->mutable_rtp_packet()->set_header(header, header_length);
tereliusbea89592016-06-08 07:20:29 -0700349 StoreEvent(&rtp_event);
Bjorn Terelius36411852015-07-30 12:45:18 +0200350}
351
terelius429c3452016-01-21 05:42:04 -0800352void RtcEventLogImpl::LogRtcpPacket(PacketDirection direction,
Bjorn Terelius36411852015-07-30 12:45:18 +0200353 MediaType media_type,
354 const uint8_t* packet,
355 size_t length) {
terelius4311ba52016-04-22 12:40:37 -0700356 std::unique_ptr<rtclog::Event> rtcp_event(new rtclog::Event());
357 rtcp_event->set_timestamp_us(clock_->TimeInMicroseconds());
358 rtcp_event->set_type(rtclog::Event::RTCP_EVENT);
359 rtcp_event->mutable_rtcp_packet()->set_incoming(direction == kIncomingPacket);
360 rtcp_event->mutable_rtcp_packet()->set_type(ConvertMediaType(media_type));
tereliusd66daa22015-11-06 09:00:18 -0800361
362 RTCPUtility::RtcpCommonHeader header;
363 const uint8_t* block_begin = packet;
364 const uint8_t* packet_end = packet + length;
365 RTC_DCHECK(length <= IP_PACKET_SIZE);
366 uint8_t buffer[IP_PACKET_SIZE];
367 uint32_t buffer_length = 0;
368 while (block_begin < packet_end) {
369 if (!RtcpParseCommonHeader(block_begin, packet_end - block_begin,
370 &header)) {
371 break; // Incorrect message header.
372 }
373 uint32_t block_size = header.BlockSize();
374 switch (header.packet_type) {
375 case RTCPUtility::PT_SR:
376 FALLTHROUGH();
377 case RTCPUtility::PT_RR:
378 FALLTHROUGH();
379 case RTCPUtility::PT_BYE:
380 FALLTHROUGH();
381 case RTCPUtility::PT_IJ:
382 FALLTHROUGH();
383 case RTCPUtility::PT_RTPFB:
384 FALLTHROUGH();
385 case RTCPUtility::PT_PSFB:
386 FALLTHROUGH();
387 case RTCPUtility::PT_XR:
388 // We log sender reports, receiver reports, bye messages
389 // inter-arrival jitter, third-party loss reports, payload-specific
390 // feedback and extended reports.
391 memcpy(buffer + buffer_length, block_begin, block_size);
392 buffer_length += block_size;
393 break;
394 case RTCPUtility::PT_SDES:
395 FALLTHROUGH();
396 case RTCPUtility::PT_APP:
397 FALLTHROUGH();
398 default:
399 // We don't log sender descriptions, application defined messages
400 // or message blocks of unknown type.
401 break;
402 }
403
404 block_begin += block_size;
405 }
terelius4311ba52016-04-22 12:40:37 -0700406 rtcp_event->mutable_rtcp_packet()->set_packet_data(buffer, buffer_length);
tereliusbea89592016-06-08 07:20:29 -0700407 StoreEvent(&rtcp_event);
Bjorn Terelius36411852015-07-30 12:45:18 +0200408}
409
Ivo Creusenae856f22015-09-17 16:30:16 +0200410void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) {
terelius4311ba52016-04-22 12:40:37 -0700411 std::unique_ptr<rtclog::Event> event(new rtclog::Event());
412 event->set_timestamp_us(clock_->TimeInMicroseconds());
413 event->set_type(rtclog::Event::AUDIO_PLAYOUT_EVENT);
414 auto playout_event = event->mutable_audio_playout_event();
Ivo Creusen301aaed2015-10-08 18:07:41 +0200415 playout_event->set_local_ssrc(ssrc);
tereliusbea89592016-06-08 07:20:29 -0700416 StoreEvent(&event);
Bjorn Terelius36411852015-07-30 12:45:18 +0200417}
418
terelius006d93d2015-11-05 12:02:15 -0800419void RtcEventLogImpl::LogBwePacketLossEvent(int32_t bitrate,
420 uint8_t fraction_loss,
421 int32_t total_packets) {
terelius4311ba52016-04-22 12:40:37 -0700422 std::unique_ptr<rtclog::Event> event(new rtclog::Event());
423 event->set_timestamp_us(clock_->TimeInMicroseconds());
424 event->set_type(rtclog::Event::BWE_PACKET_LOSS_EVENT);
425 auto bwe_event = event->mutable_bwe_packet_loss_event();
terelius006d93d2015-11-05 12:02:15 -0800426 bwe_event->set_bitrate(bitrate);
427 bwe_event->set_fraction_loss(fraction_loss);
428 bwe_event->set_total_packets(total_packets);
tereliusbea89592016-06-08 07:20:29 -0700429 StoreEvent(&event);
430}
431
432void RtcEventLogImpl::StoreEvent(std::unique_ptr<rtclog::Event>* event) {
433 if (!event_queue_.Insert(event)) {
434 LOG(LS_ERROR) << "WebRTC event log queue full. Dropping event.";
Bjorn Terelius36411852015-07-30 12:45:18 +0200435 }
tereliusbea89592016-06-08 07:20:29 -0700436 helper_thread_.SignalNewEvent();
Bjorn Terelius36411852015-07-30 12:45:18 +0200437}
438
439bool RtcEventLog::ParseRtcEventLog(const std::string& file_name,
440 rtclog::EventStream* result) {
441 char tmp_buffer[1024];
442 int bytes_read = 0;
kwibergb25345e2016-03-12 06:10:44 -0800443 std::unique_ptr<FileWrapper> dump_file(FileWrapper::Create());
tommia6219cc2016-06-15 10:30:14 -0700444 if (!dump_file->OpenFile(file_name.c_str(), true)) {
Bjorn Terelius36411852015-07-30 12:45:18 +0200445 return false;
446 }
447 std::string dump_buffer;
448 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) {
449 dump_buffer.append(tmp_buffer, bytes_read);
450 }
451 dump_file->CloseFile();
452 return result->ParseFromString(dump_buffer);
453}
454
455#endif // ENABLE_RTC_EVENT_LOG
456
457// RtcEventLog member functions.
terelius4311ba52016-04-22 12:40:37 -0700458std::unique_ptr<RtcEventLog> RtcEventLog::Create(const Clock* clock) {
459#ifdef ENABLE_RTC_EVENT_LOG
460 return std::unique_ptr<RtcEventLog>(new RtcEventLogImpl(clock));
461#else
462 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl());
463#endif // ENABLE_RTC_EVENT_LOG
Bjorn Terelius36411852015-07-30 12:45:18 +0200464}
terelius1adce142015-10-16 08:51:08 -0700465
ivoc14d5dbe2016-07-04 07:06:55 -0700466std::unique_ptr<RtcEventLog> RtcEventLog::CreateNull() {
467 return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl());
468}
469
Bjorn Terelius36411852015-07-30 12:45:18 +0200470} // namespace webrtc