blob: 058879ae0b6e4b7d53efa8a381ec91feb05b8569 [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>
14
15#include "webrtc/base/checks.h"
16#include "webrtc/base/criticalsection.h"
17#include "webrtc/base/thread_annotations.h"
18#include "webrtc/call.h"
terelius2f9fd5d2015-09-04 03:39:42 -070019#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
Bjorn Terelius36411852015-07-30 12:45:18 +020020#include "webrtc/system_wrappers/interface/clock.h"
21#include "webrtc/system_wrappers/interface/file_wrapper.h"
22
23#ifdef ENABLE_RTC_EVENT_LOG
24// Files generated at build-time by the protobuf compiler.
25#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
Peter Boström5c389d32015-09-25 13:58:30 +020026#include "external/webrtc/webrtc/call/rtc_event_log.pb.h"
Bjorn Terelius36411852015-07-30 12:45:18 +020027#else
Peter Boström5c389d32015-09-25 13:58:30 +020028#include "webrtc/call/rtc_event_log.pb.h"
Bjorn Terelius36411852015-07-30 12:45:18 +020029#endif
30#endif
31
32namespace webrtc {
33
34#ifndef ENABLE_RTC_EVENT_LOG
35
36// No-op implementation if flag is not set.
37class RtcEventLogImpl final : public RtcEventLog {
38 public:
39 void StartLogging(const std::string& file_name, int duration_ms) override {}
40 void StopLogging(void) override {}
41 void LogVideoReceiveStreamConfig(
42 const VideoReceiveStream::Config& config) override {}
43 void LogVideoSendStreamConfig(
44 const VideoSendStream::Config& config) override {}
45 void LogRtpHeader(bool incoming,
46 MediaType media_type,
47 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -070048 size_t packet_length) override {}
Bjorn Terelius36411852015-07-30 12:45:18 +020049 void LogRtcpPacket(bool incoming,
50 MediaType media_type,
51 const uint8_t* packet,
52 size_t length) override {}
Ivo Creusenae856f22015-09-17 16:30:16 +020053 void LogAudioPlayout(uint32_t ssrc) override {}
Bjorn Terelius36411852015-07-30 12:45:18 +020054};
55
56#else // ENABLE_RTC_EVENT_LOG is defined
57
58class RtcEventLogImpl final : public RtcEventLog {
59 public:
60 RtcEventLogImpl();
61
62 void StartLogging(const std::string& file_name, int duration_ms) override;
63 void StopLogging() override;
64 void LogVideoReceiveStreamConfig(
65 const VideoReceiveStream::Config& config) override;
66 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override;
67 void LogRtpHeader(bool incoming,
68 MediaType media_type,
69 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -070070 size_t packet_length) override;
Bjorn Terelius36411852015-07-30 12:45:18 +020071 void LogRtcpPacket(bool incoming,
72 MediaType media_type,
73 const uint8_t* packet,
74 size_t length) override;
Ivo Creusenae856f22015-09-17 16:30:16 +020075 void LogAudioPlayout(uint32_t ssrc) override;
Bjorn Terelius36411852015-07-30 12:45:18 +020076
77 private:
78 // Stops logging and clears the stored data and buffers.
79 void StopLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_);
80 // Adds a new event to the logfile if logging is active, or adds it to the
81 // list of recent log events otherwise.
82 void HandleEvent(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_);
83 // Writes the event to the file. Note that this will destroy the state of the
84 // input argument.
85 void StoreToFile(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_);
86 // Adds the event to the list of recent events, and removes any events that
87 // are too old and no longer fall in the time window.
88 void AddRecentEvent(const rtclog::Event& event)
89 EXCLUSIVE_LOCKS_REQUIRED(crit_);
90
91 // Amount of time in microseconds to record log events, before starting the
92 // actual log.
93 const int recent_log_duration_us = 10000000;
94
95 rtc::CriticalSection crit_;
96 rtc::scoped_ptr<FileWrapper> file_ GUARDED_BY(crit_);
97 rtclog::EventStream stream_ GUARDED_BY(crit_);
98 std::deque<rtclog::Event> recent_log_events_ GUARDED_BY(crit_);
99 bool currently_logging_ GUARDED_BY(crit_);
100 int64_t start_time_us_ GUARDED_BY(crit_);
101 int64_t duration_us_ GUARDED_BY(crit_);
102 const Clock* const clock_;
103};
104
105namespace {
106// The functions in this namespace convert enums from the runtime format
107// that the rest of the WebRtc project can use, to the corresponding
108// serialized enum which is defined by the protobuf.
109
110// Do not add default return values to the conversion functions in this
111// unnamed namespace. The intention is to make the compiler warn if anyone
112// adds unhandled new events/modes/etc.
113
pbosda903ea2015-10-02 02:36:56 -0700114rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) {
Bjorn Terelius36411852015-07-30 12:45:18 +0200115 switch (rtcp_mode) {
pbosda903ea2015-10-02 02:36:56 -0700116 case RtcpMode::kCompound:
Bjorn Terelius36411852015-07-30 12:45:18 +0200117 return rtclog::VideoReceiveConfig::RTCP_COMPOUND;
pbosda903ea2015-10-02 02:36:56 -0700118 case RtcpMode::kReducedSize:
Bjorn Terelius36411852015-07-30 12:45:18 +0200119 return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE;
pbosda903ea2015-10-02 02:36:56 -0700120 case RtcpMode::kOff:
121 RTC_NOTREACHED();
122 return rtclog::VideoReceiveConfig::RTCP_COMPOUND;
Bjorn Terelius36411852015-07-30 12:45:18 +0200123 }
124 RTC_NOTREACHED();
125 return rtclog::VideoReceiveConfig::RTCP_COMPOUND;
126}
127
128rtclog::MediaType ConvertMediaType(MediaType media_type) {
129 switch (media_type) {
130 case MediaType::ANY:
131 return rtclog::MediaType::ANY;
132 case MediaType::AUDIO:
133 return rtclog::MediaType::AUDIO;
134 case MediaType::VIDEO:
135 return rtclog::MediaType::VIDEO;
136 case MediaType::DATA:
137 return rtclog::MediaType::DATA;
138 }
139 RTC_NOTREACHED();
140 return rtclog::ANY;
141}
142
143} // namespace
144
145// RtcEventLogImpl member functions.
146RtcEventLogImpl::RtcEventLogImpl()
147 : file_(FileWrapper::Create()),
148 stream_(),
149 currently_logging_(false),
150 start_time_us_(0),
151 duration_us_(0),
152 clock_(Clock::GetRealTimeClock()) {
153}
154
155void RtcEventLogImpl::StartLogging(const std::string& file_name,
156 int duration_ms) {
157 rtc::CritScope lock(&crit_);
158 if (currently_logging_) {
159 StopLoggingLocked();
160 }
161 if (file_->OpenFile(file_name.c_str(), false) != 0) {
162 return;
163 }
164 currently_logging_ = true;
165 start_time_us_ = clock_->TimeInMicroseconds();
166 duration_us_ = static_cast<int64_t>(duration_ms) * 1000;
167 // Write all the recent events to the log file, ignoring any old events.
168 for (auto& event : recent_log_events_) {
169 if (event.timestamp_us() >= start_time_us_ - recent_log_duration_us) {
170 StoreToFile(&event);
171 }
172 }
173 recent_log_events_.clear();
174 // Write a LOG_START event to the file.
175 rtclog::Event start_event;
176 start_event.set_timestamp_us(start_time_us_);
177 start_event.set_type(rtclog::Event::DEBUG_EVENT);
178 auto debug_event = start_event.mutable_debug_event();
Ivo Creusenae856f22015-09-17 16:30:16 +0200179 debug_event->set_type(rtclog::DebugEvent_EventType_LOG_START);
Bjorn Terelius36411852015-07-30 12:45:18 +0200180 StoreToFile(&start_event);
181}
182
183void RtcEventLogImpl::StopLogging() {
184 rtc::CritScope lock(&crit_);
185 StopLoggingLocked();
186}
187
188void RtcEventLogImpl::LogVideoReceiveStreamConfig(
189 const VideoReceiveStream::Config& config) {
190 rtc::CritScope lock(&crit_);
191
192 rtclog::Event event;
193 const int64_t timestamp = clock_->TimeInMicroseconds();
194 event.set_timestamp_us(timestamp);
195 event.set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT);
196
197 rtclog::VideoReceiveConfig* receiver_config =
198 event.mutable_video_receiver_config();
199 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc);
200 receiver_config->set_local_ssrc(config.rtp.local_ssrc);
201
202 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode));
203
204 receiver_config->set_receiver_reference_time_report(
205 config.rtp.rtcp_xr.receiver_reference_time_report);
206 receiver_config->set_remb(config.rtp.remb);
207
208 for (const auto& kv : config.rtp.rtx) {
209 rtclog::RtxMap* rtx = receiver_config->add_rtx_map();
210 rtx->set_payload_type(kv.first);
211 rtx->mutable_config()->set_rtx_ssrc(kv.second.ssrc);
212 rtx->mutable_config()->set_rtx_payload_type(kv.second.payload_type);
213 }
214
215 for (const auto& e : config.rtp.extensions) {
216 rtclog::RtpHeaderExtension* extension =
217 receiver_config->add_header_extensions();
218 extension->set_name(e.name);
219 extension->set_id(e.id);
220 }
221
222 for (const auto& d : config.decoders) {
223 rtclog::DecoderConfig* decoder = receiver_config->add_decoders();
224 decoder->set_name(d.payload_name);
225 decoder->set_payload_type(d.payload_type);
226 }
227 // TODO(terelius): We should use a separate event queue for config events.
228 // The current approach of storing the configuration together with the
229 // RTP events causes the configuration information to be removed 10s
230 // after the ReceiveStream is created.
231 HandleEvent(&event);
232}
233
234void RtcEventLogImpl::LogVideoSendStreamConfig(
235 const VideoSendStream::Config& config) {
236 rtc::CritScope lock(&crit_);
237
238 rtclog::Event event;
239 const int64_t timestamp = clock_->TimeInMicroseconds();
240 event.set_timestamp_us(timestamp);
241 event.set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT);
242
243 rtclog::VideoSendConfig* sender_config = event.mutable_video_sender_config();
244
245 for (const auto& ssrc : config.rtp.ssrcs) {
246 sender_config->add_ssrcs(ssrc);
247 }
248
249 for (const auto& e : config.rtp.extensions) {
250 rtclog::RtpHeaderExtension* extension =
251 sender_config->add_header_extensions();
252 extension->set_name(e.name);
253 extension->set_id(e.id);
254 }
255
256 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) {
257 sender_config->add_rtx_ssrcs(rtx_ssrc);
258 }
259 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type);
260
261 sender_config->set_c_name(config.rtp.c_name);
262
263 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder();
264 encoder->set_name(config.encoder_settings.payload_name);
265 encoder->set_payload_type(config.encoder_settings.payload_type);
266
267 // TODO(terelius): We should use a separate event queue for config events.
268 // The current approach of storing the configuration together with the
269 // RTP events causes the configuration information to be removed 10s
270 // after the ReceiveStream is created.
271 HandleEvent(&event);
272}
273
Bjorn Terelius36411852015-07-30 12:45:18 +0200274void RtcEventLogImpl::LogRtpHeader(bool incoming,
275 MediaType media_type,
276 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -0700277 size_t packet_length) {
278 // Read header length (in bytes) from packet data.
279 if (packet_length < 12u) {
280 return; // Don't read outside the packet.
281 }
282 const bool x = (header[0] & 0x10) != 0;
283 const uint8_t cc = header[0] & 0x0f;
284 size_t header_length = 12u + cc * 4u;
285
286 if (x) {
287 if (packet_length < 12u + cc * 4u + 4u) {
288 return; // Don't read outside the packet.
289 }
290 size_t x_len = ByteReader<uint16_t>::ReadBigEndian(header + 14 + cc * 4);
291 header_length += (x_len + 1) * 4;
292 }
293
Bjorn Terelius36411852015-07-30 12:45:18 +0200294 rtc::CritScope lock(&crit_);
295 rtclog::Event rtp_event;
296 const int64_t timestamp = clock_->TimeInMicroseconds();
297 rtp_event.set_timestamp_us(timestamp);
298 rtp_event.set_type(rtclog::Event::RTP_EVENT);
299 rtp_event.mutable_rtp_packet()->set_incoming(incoming);
300 rtp_event.mutable_rtp_packet()->set_type(ConvertMediaType(media_type));
terelius2f9fd5d2015-09-04 03:39:42 -0700301 rtp_event.mutable_rtp_packet()->set_packet_length(packet_length);
Bjorn Terelius36411852015-07-30 12:45:18 +0200302 rtp_event.mutable_rtp_packet()->set_header(header, header_length);
303 HandleEvent(&rtp_event);
304}
305
306void RtcEventLogImpl::LogRtcpPacket(bool incoming,
307 MediaType media_type,
308 const uint8_t* packet,
309 size_t length) {
310 rtc::CritScope lock(&crit_);
311 rtclog::Event rtcp_event;
312 const int64_t timestamp = clock_->TimeInMicroseconds();
313 rtcp_event.set_timestamp_us(timestamp);
314 rtcp_event.set_type(rtclog::Event::RTCP_EVENT);
315 rtcp_event.mutable_rtcp_packet()->set_incoming(incoming);
316 rtcp_event.mutable_rtcp_packet()->set_type(ConvertMediaType(media_type));
317 rtcp_event.mutable_rtcp_packet()->set_packet_data(packet, length);
318 HandleEvent(&rtcp_event);
319}
320
Ivo Creusenae856f22015-09-17 16:30:16 +0200321void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) {
Bjorn Terelius36411852015-07-30 12:45:18 +0200322 rtc::CritScope lock(&crit_);
323 rtclog::Event event;
324 const int64_t timestamp = clock_->TimeInMicroseconds();
325 event.set_timestamp_us(timestamp);
326 event.set_type(rtclog::Event::DEBUG_EVENT);
327 auto debug_event = event.mutable_debug_event();
Ivo Creusenae856f22015-09-17 16:30:16 +0200328 debug_event->set_type(rtclog::DebugEvent_EventType_AUDIO_PLAYOUT);
329 debug_event->set_local_ssrc(ssrc);
Bjorn Terelius36411852015-07-30 12:45:18 +0200330 HandleEvent(&event);
331}
332
333void RtcEventLogImpl::StopLoggingLocked() {
334 if (currently_logging_) {
335 currently_logging_ = false;
336 // Create a LogEnd debug event
337 rtclog::Event event;
338 int64_t timestamp = clock_->TimeInMicroseconds();
339 event.set_timestamp_us(timestamp);
340 event.set_type(rtclog::Event::DEBUG_EVENT);
341 auto debug_event = event.mutable_debug_event();
Ivo Creusenae856f22015-09-17 16:30:16 +0200342 debug_event->set_type(rtclog::DebugEvent_EventType_LOG_END);
Bjorn Terelius36411852015-07-30 12:45:18 +0200343 // Store the event and close the file
henrikg91d6ede2015-09-17 00:24:34 -0700344 RTC_DCHECK(file_->Open());
Bjorn Terelius36411852015-07-30 12:45:18 +0200345 StoreToFile(&event);
346 file_->CloseFile();
347 }
henrikg91d6ede2015-09-17 00:24:34 -0700348 RTC_DCHECK(!file_->Open());
Bjorn Terelius36411852015-07-30 12:45:18 +0200349 stream_.Clear();
350}
351
352void RtcEventLogImpl::HandleEvent(rtclog::Event* event) {
353 if (currently_logging_) {
354 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) {
355 StoreToFile(event);
356 return;
357 }
358 StopLoggingLocked();
359 }
360 AddRecentEvent(*event);
361}
362
363void RtcEventLogImpl::StoreToFile(rtclog::Event* event) {
364 // Reuse the same object at every log event.
365 if (stream_.stream_size() < 1) {
366 stream_.add_stream();
367 }
henrikg91d6ede2015-09-17 00:24:34 -0700368 RTC_DCHECK_EQ(stream_.stream_size(), 1);
Bjorn Terelius36411852015-07-30 12:45:18 +0200369 stream_.mutable_stream(0)->Swap(event);
370 // TODO(terelius): Doesn't this create a new EventStream per event?
371 // Is this guaranteed to work e.g. in future versions of protobuf?
372 std::string dump_buffer;
373 stream_.SerializeToString(&dump_buffer);
374 file_->Write(dump_buffer.data(), dump_buffer.size());
375}
376
377void RtcEventLogImpl::AddRecentEvent(const rtclog::Event& event) {
378 recent_log_events_.push_back(event);
379 while (recent_log_events_.front().timestamp_us() <
380 event.timestamp_us() - recent_log_duration_us) {
381 recent_log_events_.pop_front();
382 }
383}
384
385bool RtcEventLog::ParseRtcEventLog(const std::string& file_name,
386 rtclog::EventStream* result) {
387 char tmp_buffer[1024];
388 int bytes_read = 0;
389 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create());
390 if (dump_file->OpenFile(file_name.c_str(), true) != 0) {
391 return false;
392 }
393 std::string dump_buffer;
394 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) {
395 dump_buffer.append(tmp_buffer, bytes_read);
396 }
397 dump_file->CloseFile();
398 return result->ParseFromString(dump_buffer);
399}
400
401#endif // ENABLE_RTC_EVENT_LOG
402
403// RtcEventLog member functions.
404rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() {
405 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl());
406}
407} // namespace webrtc