blob: 97885cc066a609d79cf13b69beabc4874d838b57 [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_);
Ivo Creusen301aaed2015-10-08 18:07:41 +0200177 start_event.set_type(rtclog::Event::LOG_START);
Bjorn Terelius36411852015-07-30 12:45:18 +0200178 StoreToFile(&start_event);
179}
180
181void RtcEventLogImpl::StopLogging() {
182 rtc::CritScope lock(&crit_);
183 StopLoggingLocked();
184}
185
186void RtcEventLogImpl::LogVideoReceiveStreamConfig(
187 const VideoReceiveStream::Config& config) {
188 rtc::CritScope lock(&crit_);
189
190 rtclog::Event event;
191 const int64_t timestamp = clock_->TimeInMicroseconds();
192 event.set_timestamp_us(timestamp);
193 event.set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT);
194
195 rtclog::VideoReceiveConfig* receiver_config =
196 event.mutable_video_receiver_config();
197 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc);
198 receiver_config->set_local_ssrc(config.rtp.local_ssrc);
199
200 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode));
201
202 receiver_config->set_receiver_reference_time_report(
203 config.rtp.rtcp_xr.receiver_reference_time_report);
204 receiver_config->set_remb(config.rtp.remb);
205
206 for (const auto& kv : config.rtp.rtx) {
207 rtclog::RtxMap* rtx = receiver_config->add_rtx_map();
208 rtx->set_payload_type(kv.first);
209 rtx->mutable_config()->set_rtx_ssrc(kv.second.ssrc);
210 rtx->mutable_config()->set_rtx_payload_type(kv.second.payload_type);
211 }
212
213 for (const auto& e : config.rtp.extensions) {
214 rtclog::RtpHeaderExtension* extension =
215 receiver_config->add_header_extensions();
216 extension->set_name(e.name);
217 extension->set_id(e.id);
218 }
219
220 for (const auto& d : config.decoders) {
221 rtclog::DecoderConfig* decoder = receiver_config->add_decoders();
222 decoder->set_name(d.payload_name);
223 decoder->set_payload_type(d.payload_type);
224 }
225 // TODO(terelius): We should use a separate event queue for config events.
226 // The current approach of storing the configuration together with the
227 // RTP events causes the configuration information to be removed 10s
228 // after the ReceiveStream is created.
229 HandleEvent(&event);
230}
231
232void RtcEventLogImpl::LogVideoSendStreamConfig(
233 const VideoSendStream::Config& config) {
234 rtc::CritScope lock(&crit_);
235
236 rtclog::Event event;
237 const int64_t timestamp = clock_->TimeInMicroseconds();
238 event.set_timestamp_us(timestamp);
239 event.set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT);
240
241 rtclog::VideoSendConfig* sender_config = event.mutable_video_sender_config();
242
243 for (const auto& ssrc : config.rtp.ssrcs) {
244 sender_config->add_ssrcs(ssrc);
245 }
246
247 for (const auto& e : config.rtp.extensions) {
248 rtclog::RtpHeaderExtension* extension =
249 sender_config->add_header_extensions();
250 extension->set_name(e.name);
251 extension->set_id(e.id);
252 }
253
254 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) {
255 sender_config->add_rtx_ssrcs(rtx_ssrc);
256 }
257 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type);
258
259 sender_config->set_c_name(config.rtp.c_name);
260
261 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder();
262 encoder->set_name(config.encoder_settings.payload_name);
263 encoder->set_payload_type(config.encoder_settings.payload_type);
264
265 // TODO(terelius): We should use a separate event queue for config events.
266 // The current approach of storing the configuration together with the
267 // RTP events causes the configuration information to be removed 10s
268 // after the ReceiveStream is created.
269 HandleEvent(&event);
270}
271
Bjorn Terelius36411852015-07-30 12:45:18 +0200272void RtcEventLogImpl::LogRtpHeader(bool incoming,
273 MediaType media_type,
274 const uint8_t* header,
terelius2f9fd5d2015-09-04 03:39:42 -0700275 size_t packet_length) {
276 // Read header length (in bytes) from packet data.
277 if (packet_length < 12u) {
278 return; // Don't read outside the packet.
279 }
280 const bool x = (header[0] & 0x10) != 0;
281 const uint8_t cc = header[0] & 0x0f;
282 size_t header_length = 12u + cc * 4u;
283
284 if (x) {
285 if (packet_length < 12u + cc * 4u + 4u) {
286 return; // Don't read outside the packet.
287 }
288 size_t x_len = ByteReader<uint16_t>::ReadBigEndian(header + 14 + cc * 4);
289 header_length += (x_len + 1) * 4;
290 }
291
Bjorn Terelius36411852015-07-30 12:45:18 +0200292 rtc::CritScope lock(&crit_);
293 rtclog::Event rtp_event;
294 const int64_t timestamp = clock_->TimeInMicroseconds();
295 rtp_event.set_timestamp_us(timestamp);
296 rtp_event.set_type(rtclog::Event::RTP_EVENT);
297 rtp_event.mutable_rtp_packet()->set_incoming(incoming);
298 rtp_event.mutable_rtp_packet()->set_type(ConvertMediaType(media_type));
terelius2f9fd5d2015-09-04 03:39:42 -0700299 rtp_event.mutable_rtp_packet()->set_packet_length(packet_length);
Bjorn Terelius36411852015-07-30 12:45:18 +0200300 rtp_event.mutable_rtp_packet()->set_header(header, header_length);
301 HandleEvent(&rtp_event);
302}
303
304void RtcEventLogImpl::LogRtcpPacket(bool incoming,
305 MediaType media_type,
306 const uint8_t* packet,
307 size_t length) {
308 rtc::CritScope lock(&crit_);
309 rtclog::Event rtcp_event;
310 const int64_t timestamp = clock_->TimeInMicroseconds();
311 rtcp_event.set_timestamp_us(timestamp);
312 rtcp_event.set_type(rtclog::Event::RTCP_EVENT);
313 rtcp_event.mutable_rtcp_packet()->set_incoming(incoming);
314 rtcp_event.mutable_rtcp_packet()->set_type(ConvertMediaType(media_type));
315 rtcp_event.mutable_rtcp_packet()->set_packet_data(packet, length);
316 HandleEvent(&rtcp_event);
317}
318
Ivo Creusenae856f22015-09-17 16:30:16 +0200319void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) {
Bjorn Terelius36411852015-07-30 12:45:18 +0200320 rtc::CritScope lock(&crit_);
321 rtclog::Event event;
322 const int64_t timestamp = clock_->TimeInMicroseconds();
323 event.set_timestamp_us(timestamp);
Ivo Creusen301aaed2015-10-08 18:07:41 +0200324 event.set_type(rtclog::Event::AUDIO_PLAYOUT_EVENT);
325 auto playout_event = event.mutable_audio_playout_event();
326 playout_event->set_local_ssrc(ssrc);
Bjorn Terelius36411852015-07-30 12:45:18 +0200327 HandleEvent(&event);
328}
329
330void RtcEventLogImpl::StopLoggingLocked() {
331 if (currently_logging_) {
332 currently_logging_ = false;
Ivo Creusen301aaed2015-10-08 18:07:41 +0200333 // Create a LogEnd event
Bjorn Terelius36411852015-07-30 12:45:18 +0200334 rtclog::Event event;
335 int64_t timestamp = clock_->TimeInMicroseconds();
336 event.set_timestamp_us(timestamp);
Ivo Creusen301aaed2015-10-08 18:07:41 +0200337 event.set_type(rtclog::Event::LOG_END);
Bjorn Terelius36411852015-07-30 12:45:18 +0200338 // Store the event and close the file
henrikg91d6ede2015-09-17 00:24:34 -0700339 RTC_DCHECK(file_->Open());
Bjorn Terelius36411852015-07-30 12:45:18 +0200340 StoreToFile(&event);
341 file_->CloseFile();
342 }
henrikg91d6ede2015-09-17 00:24:34 -0700343 RTC_DCHECK(!file_->Open());
Bjorn Terelius36411852015-07-30 12:45:18 +0200344 stream_.Clear();
345}
346
347void RtcEventLogImpl::HandleEvent(rtclog::Event* event) {
348 if (currently_logging_) {
349 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) {
350 StoreToFile(event);
351 return;
352 }
353 StopLoggingLocked();
354 }
355 AddRecentEvent(*event);
356}
357
358void RtcEventLogImpl::StoreToFile(rtclog::Event* event) {
359 // Reuse the same object at every log event.
360 if (stream_.stream_size() < 1) {
361 stream_.add_stream();
362 }
henrikg91d6ede2015-09-17 00:24:34 -0700363 RTC_DCHECK_EQ(stream_.stream_size(), 1);
Bjorn Terelius36411852015-07-30 12:45:18 +0200364 stream_.mutable_stream(0)->Swap(event);
365 // TODO(terelius): Doesn't this create a new EventStream per event?
366 // Is this guaranteed to work e.g. in future versions of protobuf?
367 std::string dump_buffer;
368 stream_.SerializeToString(&dump_buffer);
369 file_->Write(dump_buffer.data(), dump_buffer.size());
370}
371
372void RtcEventLogImpl::AddRecentEvent(const rtclog::Event& event) {
373 recent_log_events_.push_back(event);
374 while (recent_log_events_.front().timestamp_us() <
375 event.timestamp_us() - recent_log_duration_us) {
376 recent_log_events_.pop_front();
377 }
378}
379
380bool RtcEventLog::ParseRtcEventLog(const std::string& file_name,
381 rtclog::EventStream* result) {
382 char tmp_buffer[1024];
383 int bytes_read = 0;
384 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create());
385 if (dump_file->OpenFile(file_name.c_str(), true) != 0) {
386 return false;
387 }
388 std::string dump_buffer;
389 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) {
390 dump_buffer.append(tmp_buffer, bytes_read);
391 }
392 dump_file->CloseFile();
393 return result->ParseFromString(dump_buffer);
394}
395
396#endif // ENABLE_RTC_EVENT_LOG
397
398// RtcEventLog member functions.
399rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() {
400 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl());
401}
402} // namespace webrtc