OLD | NEW |
(Empty) | |
| 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 |
| 11 #include "webrtc/video/rtc_event_log.h" |
| 12 |
| 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" |
| 19 #include "webrtc/system_wrappers/interface/clock.h" |
| 20 #include "webrtc/system_wrappers/interface/file_wrapper.h" |
| 21 |
| 22 #ifdef ENABLE_RTC_EVENT_LOG |
| 23 // Files generated at build-time by the protobuf compiler. |
| 24 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
| 25 #include "external/webrtc/webrtc/video/rtc_event_log.pb.h" |
| 26 #else |
| 27 #include "webrtc/video/rtc_event_log.pb.h" |
| 28 #endif |
| 29 #endif |
| 30 |
| 31 namespace webrtc { |
| 32 |
| 33 #ifndef ENABLE_RTC_EVENT_LOG |
| 34 |
| 35 // No-op implementation if flag is not set. |
| 36 class RtcEventLogImpl final : public RtcEventLog { |
| 37 public: |
| 38 void StartLogging(const std::string& file_name, int duration_ms) override {} |
| 39 void StopLogging(void) override {} |
| 40 void LogVideoReceiveStreamConfig( |
| 41 const VideoReceiveStream::Config& config) override {} |
| 42 void LogVideoSendStreamConfig( |
| 43 const VideoSendStream::Config& config) override {} |
| 44 void LogRtpHeader(bool incoming, |
| 45 MediaType media_type, |
| 46 const uint8_t* header, |
| 47 size_t header_length, |
| 48 size_t total_length) override {} |
| 49 void LogRtcpPacket(bool incoming, |
| 50 MediaType media_type, |
| 51 const uint8_t* packet, |
| 52 size_t length) override {} |
| 53 void LogDebugEvent(DebugEvent event_type) override {} |
| 54 }; |
| 55 |
| 56 #else // ENABLE_RTC_EVENT_LOG is defined |
| 57 |
| 58 class 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, |
| 70 size_t header_length, |
| 71 size_t total_length) override; |
| 72 void LogRtcpPacket(bool incoming, |
| 73 MediaType media_type, |
| 74 const uint8_t* packet, |
| 75 size_t length) override; |
| 76 void LogDebugEvent(DebugEvent event_type) override; |
| 77 |
| 78 private: |
| 79 // Stops logging and clears the stored data and buffers. |
| 80 void StopLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 81 // Adds a new event to the logfile if logging is active, or adds it to the |
| 82 // list of recent log events otherwise. |
| 83 void HandleEvent(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 84 // Writes the event to the file. Note that this will destroy the state of the |
| 85 // input argument. |
| 86 void StoreToFile(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 87 // Adds the event to the list of recent events, and removes any events that |
| 88 // are too old and no longer fall in the time window. |
| 89 void AddRecentEvent(const rtclog::Event& event) |
| 90 EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 91 |
| 92 // Amount of time in microseconds to record log events, before starting the |
| 93 // actual log. |
| 94 const int recent_log_duration_us = 10000000; |
| 95 |
| 96 rtc::CriticalSection crit_; |
| 97 rtc::scoped_ptr<FileWrapper> file_ GUARDED_BY(crit_); |
| 98 rtclog::EventStream stream_ GUARDED_BY(crit_); |
| 99 std::deque<rtclog::Event> recent_log_events_ GUARDED_BY(crit_); |
| 100 bool currently_logging_ GUARDED_BY(crit_); |
| 101 int64_t start_time_us_ GUARDED_BY(crit_); |
| 102 int64_t duration_us_ GUARDED_BY(crit_); |
| 103 const Clock* const clock_; |
| 104 }; |
| 105 |
| 106 namespace { |
| 107 // The functions in this namespace convert enums from the runtime format |
| 108 // that the rest of the WebRtc project can use, to the corresponding |
| 109 // serialized enum which is defined by the protobuf. |
| 110 |
| 111 // Do not add default return values to the conversion functions in this |
| 112 // unnamed namespace. The intention is to make the compiler warn if anyone |
| 113 // adds unhandled new events/modes/etc. |
| 114 |
| 115 rtclog::DebugEvent_EventType ConvertDebugEvent( |
| 116 RtcEventLog::DebugEvent event_type) { |
| 117 switch (event_type) { |
| 118 case RtcEventLog::DebugEvent::kLogStart: |
| 119 return rtclog::DebugEvent::LOG_START; |
| 120 case RtcEventLog::DebugEvent::kLogEnd: |
| 121 return rtclog::DebugEvent::LOG_END; |
| 122 case RtcEventLog::DebugEvent::kAudioPlayout: |
| 123 return rtclog::DebugEvent::AUDIO_PLAYOUT; |
| 124 } |
| 125 RTC_NOTREACHED(); |
| 126 return rtclog::DebugEvent::UNKNOWN_EVENT; |
| 127 } |
| 128 |
| 129 rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode( |
| 130 newapi::RtcpMode rtcp_mode) { |
| 131 switch (rtcp_mode) { |
| 132 case newapi::kRtcpCompound: |
| 133 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
| 134 case newapi::kRtcpReducedSize: |
| 135 return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE; |
| 136 } |
| 137 RTC_NOTREACHED(); |
| 138 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
| 139 } |
| 140 |
| 141 rtclog::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 |
| 156 } // namespace |
| 157 |
| 158 // RtcEventLogImpl member functions. |
| 159 RtcEventLogImpl::RtcEventLogImpl() |
| 160 : file_(FileWrapper::Create()), |
| 161 stream_(), |
| 162 currently_logging_(false), |
| 163 start_time_us_(0), |
| 164 duration_us_(0), |
| 165 clock_(Clock::GetRealTimeClock()) { |
| 166 } |
| 167 |
| 168 void RtcEventLogImpl::StartLogging(const std::string& file_name, |
| 169 int duration_ms) { |
| 170 rtc::CritScope lock(&crit_); |
| 171 if (currently_logging_) { |
| 172 StopLoggingLocked(); |
| 173 } |
| 174 if (file_->OpenFile(file_name.c_str(), false) != 0) { |
| 175 return; |
| 176 } |
| 177 currently_logging_ = true; |
| 178 start_time_us_ = clock_->TimeInMicroseconds(); |
| 179 duration_us_ = static_cast<int64_t>(duration_ms) * 1000; |
| 180 // Write all the recent events to the log file, ignoring any old events. |
| 181 for (auto& event : recent_log_events_) { |
| 182 if (event.timestamp_us() >= start_time_us_ - recent_log_duration_us) { |
| 183 StoreToFile(&event); |
| 184 } |
| 185 } |
| 186 recent_log_events_.clear(); |
| 187 // Write a LOG_START event to the file. |
| 188 rtclog::Event start_event; |
| 189 start_event.set_timestamp_us(start_time_us_); |
| 190 start_event.set_type(rtclog::Event::DEBUG_EVENT); |
| 191 auto debug_event = start_event.mutable_debug_event(); |
| 192 debug_event->set_type(ConvertDebugEvent(DebugEvent::kLogStart)); |
| 193 StoreToFile(&start_event); |
| 194 } |
| 195 |
| 196 void RtcEventLogImpl::StopLogging() { |
| 197 rtc::CritScope lock(&crit_); |
| 198 StopLoggingLocked(); |
| 199 } |
| 200 |
| 201 void RtcEventLogImpl::LogVideoReceiveStreamConfig( |
| 202 const VideoReceiveStream::Config& config) { |
| 203 rtc::CritScope lock(&crit_); |
| 204 |
| 205 rtclog::Event event; |
| 206 const int64_t timestamp = clock_->TimeInMicroseconds(); |
| 207 event.set_timestamp_us(timestamp); |
| 208 event.set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); |
| 209 |
| 210 rtclog::VideoReceiveConfig* receiver_config = |
| 211 event.mutable_video_receiver_config(); |
| 212 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc); |
| 213 receiver_config->set_local_ssrc(config.rtp.local_ssrc); |
| 214 |
| 215 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode)); |
| 216 |
| 217 receiver_config->set_receiver_reference_time_report( |
| 218 config.rtp.rtcp_xr.receiver_reference_time_report); |
| 219 receiver_config->set_remb(config.rtp.remb); |
| 220 |
| 221 for (const auto& kv : config.rtp.rtx) { |
| 222 rtclog::RtxMap* rtx = receiver_config->add_rtx_map(); |
| 223 rtx->set_payload_type(kv.first); |
| 224 rtx->mutable_config()->set_rtx_ssrc(kv.second.ssrc); |
| 225 rtx->mutable_config()->set_rtx_payload_type(kv.second.payload_type); |
| 226 } |
| 227 |
| 228 for (const auto& e : config.rtp.extensions) { |
| 229 rtclog::RtpHeaderExtension* extension = |
| 230 receiver_config->add_header_extensions(); |
| 231 extension->set_name(e.name); |
| 232 extension->set_id(e.id); |
| 233 } |
| 234 |
| 235 for (const auto& d : config.decoders) { |
| 236 rtclog::DecoderConfig* decoder = receiver_config->add_decoders(); |
| 237 decoder->set_name(d.payload_name); |
| 238 decoder->set_payload_type(d.payload_type); |
| 239 } |
| 240 // TODO(terelius): We should use a separate event queue for config events. |
| 241 // The current approach of storing the configuration together with the |
| 242 // RTP events causes the configuration information to be removed 10s |
| 243 // after the ReceiveStream is created. |
| 244 HandleEvent(&event); |
| 245 } |
| 246 |
| 247 void RtcEventLogImpl::LogVideoSendStreamConfig( |
| 248 const VideoSendStream::Config& config) { |
| 249 rtc::CritScope lock(&crit_); |
| 250 |
| 251 rtclog::Event event; |
| 252 const int64_t timestamp = clock_->TimeInMicroseconds(); |
| 253 event.set_timestamp_us(timestamp); |
| 254 event.set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT); |
| 255 |
| 256 rtclog::VideoSendConfig* sender_config = event.mutable_video_sender_config(); |
| 257 |
| 258 for (const auto& ssrc : config.rtp.ssrcs) { |
| 259 sender_config->add_ssrcs(ssrc); |
| 260 } |
| 261 |
| 262 for (const auto& e : config.rtp.extensions) { |
| 263 rtclog::RtpHeaderExtension* extension = |
| 264 sender_config->add_header_extensions(); |
| 265 extension->set_name(e.name); |
| 266 extension->set_id(e.id); |
| 267 } |
| 268 |
| 269 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) { |
| 270 sender_config->add_rtx_ssrcs(rtx_ssrc); |
| 271 } |
| 272 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type); |
| 273 |
| 274 sender_config->set_c_name(config.rtp.c_name); |
| 275 |
| 276 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder(); |
| 277 encoder->set_name(config.encoder_settings.payload_name); |
| 278 encoder->set_payload_type(config.encoder_settings.payload_type); |
| 279 |
| 280 // TODO(terelius): We should use a separate event queue for config events. |
| 281 // The current approach of storing the configuration together with the |
| 282 // RTP events causes the configuration information to be removed 10s |
| 283 // after the ReceiveStream is created. |
| 284 HandleEvent(&event); |
| 285 } |
| 286 |
| 287 // TODO(terelius): It is more convenient and less error prone to parse the |
| 288 // header length from the packet instead of relying on the caller to provide it. |
| 289 void RtcEventLogImpl::LogRtpHeader(bool incoming, |
| 290 MediaType media_type, |
| 291 const uint8_t* header, |
| 292 size_t header_length, |
| 293 size_t total_length) { |
| 294 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)); |
| 301 rtp_event.mutable_rtp_packet()->set_packet_length(total_length); |
| 302 rtp_event.mutable_rtp_packet()->set_header(header, header_length); |
| 303 HandleEvent(&rtp_event); |
| 304 } |
| 305 |
| 306 void 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 |
| 321 void RtcEventLogImpl::LogDebugEvent(DebugEvent event_type) { |
| 322 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(); |
| 328 debug_event->set_type(ConvertDebugEvent(event_type)); |
| 329 HandleEvent(&event); |
| 330 } |
| 331 |
| 332 void RtcEventLogImpl::StopLoggingLocked() { |
| 333 if (currently_logging_) { |
| 334 currently_logging_ = false; |
| 335 // Create a LogEnd debug event |
| 336 rtclog::Event event; |
| 337 int64_t timestamp = clock_->TimeInMicroseconds(); |
| 338 event.set_timestamp_us(timestamp); |
| 339 event.set_type(rtclog::Event::DEBUG_EVENT); |
| 340 auto debug_event = event.mutable_debug_event(); |
| 341 debug_event->set_type(ConvertDebugEvent(DebugEvent::kLogEnd)); |
| 342 // Store the event and close the file |
| 343 DCHECK(file_->Open()); |
| 344 StoreToFile(&event); |
| 345 file_->CloseFile(); |
| 346 } |
| 347 DCHECK(!file_->Open()); |
| 348 stream_.Clear(); |
| 349 } |
| 350 |
| 351 void RtcEventLogImpl::HandleEvent(rtclog::Event* event) { |
| 352 if (currently_logging_) { |
| 353 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) { |
| 354 StoreToFile(event); |
| 355 return; |
| 356 } |
| 357 StopLoggingLocked(); |
| 358 } |
| 359 AddRecentEvent(*event); |
| 360 } |
| 361 |
| 362 void RtcEventLogImpl::StoreToFile(rtclog::Event* event) { |
| 363 // Reuse the same object at every log event. |
| 364 if (stream_.stream_size() < 1) { |
| 365 stream_.add_stream(); |
| 366 } |
| 367 DCHECK_EQ(stream_.stream_size(), 1); |
| 368 stream_.mutable_stream(0)->Swap(event); |
| 369 // TODO(terelius): Doesn't this create a new EventStream per event? |
| 370 // Is this guaranteed to work e.g. in future versions of protobuf? |
| 371 std::string dump_buffer; |
| 372 stream_.SerializeToString(&dump_buffer); |
| 373 file_->Write(dump_buffer.data(), dump_buffer.size()); |
| 374 } |
| 375 |
| 376 void RtcEventLogImpl::AddRecentEvent(const rtclog::Event& event) { |
| 377 recent_log_events_.push_back(event); |
| 378 while (recent_log_events_.front().timestamp_us() < |
| 379 event.timestamp_us() - recent_log_duration_us) { |
| 380 recent_log_events_.pop_front(); |
| 381 } |
| 382 } |
| 383 |
| 384 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, |
| 385 rtclog::EventStream* result) { |
| 386 char tmp_buffer[1024]; |
| 387 int bytes_read = 0; |
| 388 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create()); |
| 389 if (dump_file->OpenFile(file_name.c_str(), true) != 0) { |
| 390 return false; |
| 391 } |
| 392 std::string dump_buffer; |
| 393 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { |
| 394 dump_buffer.append(tmp_buffer, bytes_read); |
| 395 } |
| 396 dump_file->CloseFile(); |
| 397 return result->ParseFromString(dump_buffer); |
| 398 } |
| 399 |
| 400 #endif // ENABLE_RTC_EVENT_LOG |
| 401 |
| 402 // RtcEventLog member functions. |
| 403 rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() { |
| 404 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl()); |
| 405 } |
| 406 } // namespace webrtc |
OLD | NEW |