Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 1 | /* |
| 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öm | 5c389d3 | 2015-09-25 13:58:30 +0200 | [diff] [blame] | 11 | #include "webrtc/call/rtc_event_log.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 12 | |
| 13 | #include <deque> |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame^] | 14 | #include <vector> |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 15 | |
| 16 | #include "webrtc/base/checks.h" |
| 17 | #include "webrtc/base/criticalsection.h" |
| 18 | #include "webrtc/base/thread_annotations.h" |
| 19 | #include "webrtc/call.h" |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 20 | #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 21 | #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öm | 5c389d3 | 2015-09-25 13:58:30 +0200 | [diff] [blame] | 27 | #include "external/webrtc/webrtc/call/rtc_event_log.pb.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 28 | #else |
Peter Boström | 5c389d3 | 2015-09-25 13:58:30 +0200 | [diff] [blame] | 29 | #include "webrtc/call/rtc_event_log.pb.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 30 | #endif |
| 31 | #endif |
| 32 | |
| 33 | namespace webrtc { |
| 34 | |
| 35 | #ifndef ENABLE_RTC_EVENT_LOG |
| 36 | |
| 37 | // No-op implementation if flag is not set. |
| 38 | class RtcEventLogImpl final : public RtcEventLog { |
| 39 | public: |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame^] | 40 | void SetBufferDuration(int64_t buffer_duration_us) override {} |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 41 | void StartLogging(const std::string& file_name, int duration_ms) override {} |
ivoc | 112a3d8 | 2015-10-16 02:22:18 -0700 | [diff] [blame] | 42 | bool StartLogging(rtc::PlatformFile log_file) override { return false; } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 43 | 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, |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 51 | size_t packet_length) override {} |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 52 | void LogRtcpPacket(bool incoming, |
| 53 | MediaType media_type, |
| 54 | const uint8_t* packet, |
| 55 | size_t length) override {} |
Ivo Creusen | ae856f2 | 2015-09-17 16:30:16 +0200 | [diff] [blame] | 56 | void LogAudioPlayout(uint32_t ssrc) override {} |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | #else // ENABLE_RTC_EVENT_LOG is defined |
| 60 | |
| 61 | class RtcEventLogImpl final : public RtcEventLog { |
| 62 | public: |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame^] | 63 | RtcEventLogImpl(); |
| 64 | |
| 65 | void SetBufferDuration(int64_t buffer_duration_us) override; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 66 | void StartLogging(const std::string& file_name, int duration_ms) override; |
ivoc | 112a3d8 | 2015-10-16 02:22:18 -0700 | [diff] [blame] | 67 | bool StartLogging(rtc::PlatformFile log_file) override; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 68 | 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, |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 75 | size_t packet_length) override; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 76 | void LogRtcpPacket(bool incoming, |
| 77 | MediaType media_type, |
| 78 | const uint8_t* packet, |
| 79 | size_t length) override; |
Ivo Creusen | ae856f2 | 2015-09-17 16:30:16 +0200 | [diff] [blame] | 80 | void LogAudioPlayout(uint32_t ssrc) override; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 81 | |
| 82 | private: |
ivoc | 112a3d8 | 2015-10-16 02:22:18 -0700 | [diff] [blame] | 83 | // 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 86 | // 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 99 | rtc::CriticalSection crit_; |
ivoc | 112a3d8 | 2015-10-16 02:22:18 -0700 | [diff] [blame] | 100 | 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 104 | rtclog::EventStream stream_ GUARDED_BY(crit_); |
| 105 | std::deque<rtclog::Event> recent_log_events_ GUARDED_BY(crit_); |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame^] | 106 | 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | namespace { |
| 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 | |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 125 | rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) { |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 126 | switch (rtcp_mode) { |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 127 | case RtcpMode::kCompound: |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 128 | return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 129 | case RtcpMode::kReducedSize: |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 130 | return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE; |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 131 | case RtcpMode::kOff: |
| 132 | RTC_NOTREACHED(); |
| 133 | return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 134 | } |
| 135 | RTC_NOTREACHED(); |
| 136 | return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
| 137 | } |
| 138 | |
| 139 | rtclog::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 | |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame^] | 156 | namespace { |
| 157 | bool 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 166 | // RtcEventLogImpl member functions. |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame^] | 167 | RtcEventLogImpl::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 | |
| 177 | void RtcEventLogImpl::SetBufferDuration(int64_t buffer_duration_us) { |
| 178 | rtc::CritScope lock(&crit_); |
| 179 | buffer_duration_us_ = buffer_duration_us; |
| 180 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 181 | |
| 182 | void 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 191 | start_time_us_ = clock_->TimeInMicroseconds(); |
| 192 | duration_us_ = static_cast<int64_t>(duration_ms) * 1000; |
ivoc | 112a3d8 | 2015-10-16 02:22:18 -0700 | [diff] [blame] | 193 | StartLoggingLocked(); |
| 194 | } |
| 195 | |
| 196 | bool 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 | |
| 222 | void RtcEventLogImpl::StartLoggingLocked() { |
| 223 | currently_logging_ = true; |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame^] | 224 | |
| 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 231 | for (auto& event : recent_log_events_) { |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame^] | 232 | 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 236 | 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 Creusen | 301aaed | 2015-10-08 18:07:41 +0200 | [diff] [blame] | 243 | start_event.set_type(rtclog::Event::LOG_START); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 244 | StoreToFile(&start_event); |
| 245 | } |
| 246 | |
| 247 | void RtcEventLogImpl::StopLogging() { |
| 248 | rtc::CritScope lock(&crit_); |
| 249 | StopLoggingLocked(); |
| 250 | } |
| 251 | |
| 252 | void 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 291 | HandleEvent(&event); |
| 292 | } |
| 293 | |
| 294 | void 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 326 | HandleEvent(&event); |
| 327 | } |
| 328 | |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 329 | void RtcEventLogImpl::LogRtpHeader(bool incoming, |
| 330 | MediaType media_type, |
| 331 | const uint8_t* header, |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 332 | 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 349 | 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)); |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 356 | rtp_event.mutable_rtp_packet()->set_packet_length(packet_length); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 357 | rtp_event.mutable_rtp_packet()->set_header(header, header_length); |
| 358 | HandleEvent(&rtp_event); |
| 359 | } |
| 360 | |
| 361 | void 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 Creusen | ae856f2 | 2015-09-17 16:30:16 +0200 | [diff] [blame] | 376 | void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) { |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 377 | rtc::CritScope lock(&crit_); |
| 378 | rtclog::Event event; |
| 379 | const int64_t timestamp = clock_->TimeInMicroseconds(); |
| 380 | event.set_timestamp_us(timestamp); |
Ivo Creusen | 301aaed | 2015-10-08 18:07:41 +0200 | [diff] [blame] | 381 | 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 Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 384 | HandleEvent(&event); |
| 385 | } |
| 386 | |
| 387 | void RtcEventLogImpl::StopLoggingLocked() { |
| 388 | if (currently_logging_) { |
| 389 | currently_logging_ = false; |
Ivo Creusen | 301aaed | 2015-10-08 18:07:41 +0200 | [diff] [blame] | 390 | // Create a LogEnd event |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 391 | rtclog::Event event; |
| 392 | int64_t timestamp = clock_->TimeInMicroseconds(); |
| 393 | event.set_timestamp_us(timestamp); |
Ivo Creusen | 301aaed | 2015-10-08 18:07:41 +0200 | [diff] [blame] | 394 | event.set_type(rtclog::Event::LOG_END); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 395 | // Store the event and close the file |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 396 | RTC_DCHECK(file_->Open()); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 397 | StoreToFile(&event); |
| 398 | file_->CloseFile(); |
ivoc | 112a3d8 | 2015-10-16 02:22:18 -0700 | [diff] [blame] | 399 | if (platform_file_ != rtc::kInvalidPlatformFileValue) { |
| 400 | rtc::ClosePlatformFile(platform_file_); |
| 401 | platform_file_ = rtc::kInvalidPlatformFileValue; |
| 402 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 403 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 404 | RTC_DCHECK(!file_->Open()); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 405 | stream_.Clear(); |
| 406 | } |
| 407 | |
| 408 | void 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 | |
| 419 | void 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 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 424 | RTC_DCHECK_EQ(stream_.stream_size(), 1); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 425 | 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 | |
| 433 | void RtcEventLogImpl::AddRecentEvent(const rtclog::Event& event) { |
| 434 | recent_log_events_.push_back(event); |
| 435 | while (recent_log_events_.front().timestamp_us() < |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame^] | 436 | event.timestamp_us() - buffer_duration_us_) { |
| 437 | if (IsConfigEvent(recent_log_events_.front())) { |
| 438 | config_events_.push_back(recent_log_events_.front()); |
| 439 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 440 | recent_log_events_.pop_front(); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | bool 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. |
| 463 | rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() { |
| 464 | return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl()); |
| 465 | } |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame^] | 466 | |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 467 | } // namespace webrtc |