| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/call/rtc_event_log.h" | 11 #include "webrtc/call/rtc_event_log.h" |
| 12 | 12 |
| 13 #include <deque> | 13 #include <limits> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/base/criticalsection.h" | 17 #include "webrtc/base/constructormagic.h" |
| 18 #include "webrtc/base/thread_annotations.h" | 18 #include "webrtc/base/event.h" |
| 19 #include "webrtc/base/thread_checker.h" |
| 19 #include "webrtc/call.h" | 20 #include "webrtc/call.h" |
| 21 #include "webrtc/call/rtc_event_log_helper_thread.h" |
| 22 #include "webrtc/common_audio/swap_queue.h" |
| 20 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | 23 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 21 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | 24 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
| 22 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" | 25 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" |
| 23 #include "webrtc/system_wrappers/include/clock.h" | 26 #include "webrtc/system_wrappers/include/clock.h" |
| 24 #include "webrtc/system_wrappers/include/file_wrapper.h" | 27 #include "webrtc/system_wrappers/include/file_wrapper.h" |
| 28 #include "webrtc/system_wrappers/include/logging.h" |
| 25 | 29 |
| 26 #ifdef ENABLE_RTC_EVENT_LOG | 30 #ifdef ENABLE_RTC_EVENT_LOG |
| 27 // Files generated at build-time by the protobuf compiler. | 31 // Files generated at build-time by the protobuf compiler. |
| 28 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD | 32 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
| 29 #include "external/webrtc/webrtc/call/rtc_event_log.pb.h" | 33 #include "external/webrtc/webrtc/call/rtc_event_log.pb.h" |
| 30 #else | 34 #else |
| 31 #include "webrtc/call/rtc_event_log.pb.h" | 35 #include "webrtc/call/rtc_event_log.pb.h" |
| 32 #endif | 36 #endif |
| 33 #endif | 37 #endif |
| 34 | 38 |
| 35 namespace webrtc { | 39 namespace webrtc { |
| 36 | 40 |
| 37 #ifndef ENABLE_RTC_EVENT_LOG | 41 #ifndef ENABLE_RTC_EVENT_LOG |
| 38 | 42 |
| 39 // No-op implementation if flag is not set. | 43 // No-op implementation if flag is not set. |
| 40 class RtcEventLogImpl final : public RtcEventLog { | 44 class RtcEventLogNullImpl final : public RtcEventLog { |
| 41 public: | 45 public: |
| 42 void SetBufferDuration(int64_t buffer_duration_us) override {} | 46 bool StartLogging(const std::string& file_name, |
| 43 void StartLogging(const std::string& file_name, int duration_ms) override {} | 47 int64_t max_size_bytes) override { |
| 44 bool StartLogging(rtc::PlatformFile log_file) override { return false; } | 48 return false; |
| 45 void StopLogging(void) override {} | 49 } |
| 50 bool StartLogging(rtc::PlatformFile platform_file, |
| 51 int64_t max_size_bytes) override { |
| 52 return false; |
| 53 } |
| 54 void StopLogging() override {} |
| 46 void LogVideoReceiveStreamConfig( | 55 void LogVideoReceiveStreamConfig( |
| 47 const VideoReceiveStream::Config& config) override {} | 56 const VideoReceiveStream::Config& config) override {} |
| 48 void LogVideoSendStreamConfig( | 57 void LogVideoSendStreamConfig( |
| 49 const VideoSendStream::Config& config) override {} | 58 const VideoSendStream::Config& config) override {} |
| 50 void LogRtpHeader(PacketDirection direction, | 59 void LogRtpHeader(PacketDirection direction, |
| 51 MediaType media_type, | 60 MediaType media_type, |
| 52 const uint8_t* header, | 61 const uint8_t* header, |
| 53 size_t packet_length) override {} | 62 size_t packet_length) override {} |
| 54 void LogRtcpPacket(PacketDirection direction, | 63 void LogRtcpPacket(PacketDirection direction, |
| 55 MediaType media_type, | 64 MediaType media_type, |
| 56 const uint8_t* packet, | 65 const uint8_t* packet, |
| 57 size_t length) override {} | 66 size_t length) override {} |
| 58 void LogAudioPlayout(uint32_t ssrc) override {} | 67 void LogAudioPlayout(uint32_t ssrc) override {} |
| 59 void LogBwePacketLossEvent(int32_t bitrate, | 68 void LogBwePacketLossEvent(int32_t bitrate, |
| 60 uint8_t fraction_loss, | 69 uint8_t fraction_loss, |
| 61 int32_t total_packets) override {} | 70 int32_t total_packets) override {} |
| 62 }; | 71 }; |
| 63 | 72 |
| 64 #else // ENABLE_RTC_EVENT_LOG is defined | 73 #else // ENABLE_RTC_EVENT_LOG is defined |
| 65 | 74 |
| 66 class RtcEventLogImpl final : public RtcEventLog { | 75 class RtcEventLogImpl final : public RtcEventLog { |
| 67 public: | 76 public: |
| 68 RtcEventLogImpl(); | 77 explicit RtcEventLogImpl(const Clock* clock); |
| 78 ~RtcEventLogImpl() override; |
| 69 | 79 |
| 70 void SetBufferDuration(int64_t buffer_duration_us) override; | 80 bool StartLogging(const std::string& file_name, |
| 71 void StartLogging(const std::string& file_name, int duration_ms) override; | 81 int64_t max_size_bytes) override; |
| 72 bool StartLogging(rtc::PlatformFile log_file) override; | 82 bool StartLogging(rtc::PlatformFile platform_file, |
| 83 int64_t max_size_bytes) override; |
| 73 void StopLogging() override; | 84 void StopLogging() override; |
| 74 void LogVideoReceiveStreamConfig( | 85 void LogVideoReceiveStreamConfig( |
| 75 const VideoReceiveStream::Config& config) override; | 86 const VideoReceiveStream::Config& config) override; |
| 76 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override; | 87 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override; |
| 77 void LogRtpHeader(PacketDirection direction, | 88 void LogRtpHeader(PacketDirection direction, |
| 78 MediaType media_type, | 89 MediaType media_type, |
| 79 const uint8_t* header, | 90 const uint8_t* header, |
| 80 size_t packet_length) override; | 91 size_t packet_length) override; |
| 81 void LogRtcpPacket(PacketDirection direction, | 92 void LogRtcpPacket(PacketDirection direction, |
| 82 MediaType media_type, | 93 MediaType media_type, |
| 83 const uint8_t* packet, | 94 const uint8_t* packet, |
| 84 size_t length) override; | 95 size_t length) override; |
| 85 void LogAudioPlayout(uint32_t ssrc) override; | 96 void LogAudioPlayout(uint32_t ssrc) override; |
| 86 void LogBwePacketLossEvent(int32_t bitrate, | 97 void LogBwePacketLossEvent(int32_t bitrate, |
| 87 uint8_t fraction_loss, | 98 uint8_t fraction_loss, |
| 88 int32_t total_packets) override; | 99 int32_t total_packets) override; |
| 89 | 100 |
| 90 private: | 101 private: |
| 91 // Starts logging. This function assumes the file_ has been opened succesfully | 102 // Message queue for passing control messages to the logging thread. |
| 92 // and that the start_time_us_ and _duration_us_ have been set. | 103 SwapQueue<RtcEventLogHelperThread::ControlMessage> message_queue_; |
| 93 void StartLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 94 // Stops logging and clears the stored data and buffers. | |
| 95 void StopLoggingLocked() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 96 // Adds a new event to the logfile if logging is active, or adds it to the | |
| 97 // list of recent log events otherwise. | |
| 98 void HandleEvent(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 99 // Writes the event to the file. Note that this will destroy the state of the | |
| 100 // input argument. | |
| 101 void StoreToFile(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 102 // Adds the event to the list of recent events, and removes any events that | |
| 103 // are too old and no longer fall in the time window. | |
| 104 void AddRecentEvent(const rtclog::Event& event) | |
| 105 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 106 | 104 |
| 107 rtc::CriticalSection crit_; | 105 // Message queues for passing events to the logging thread. |
| 108 rtc::scoped_ptr<FileWrapper> file_ GUARDED_BY(crit_) = | 106 SwapQueue<rtclog::Event> config_queue_; |
| 109 rtc::scoped_ptr<FileWrapper>(FileWrapper::Create()); | 107 SwapQueue<rtclog::Event> rtp_queue_; |
| 110 rtc::PlatformFile platform_file_ GUARDED_BY(crit_) = | 108 SwapQueue<rtclog::Event> rtcp_queue_; |
| 111 rtc::kInvalidPlatformFileValue; | 109 SwapQueue<rtclog::Event> acm_playout_queue_; |
| 112 rtclog::EventStream stream_ GUARDED_BY(crit_); | 110 SwapQueue<rtclog::Event> bwe_loss_queue_; |
| 113 std::deque<rtclog::Event> recent_log_events_ GUARDED_BY(crit_); | |
| 114 std::vector<rtclog::Event> config_events_ GUARDED_BY(crit_); | |
| 115 | 111 |
| 116 // Microseconds to record log events, before starting the actual log. | 112 rtc::Event wake_up_; |
| 117 int64_t buffer_duration_us_ GUARDED_BY(crit_); | 113 rtc::Event stopped_; |
| 118 bool currently_logging_ GUARDED_BY(crit_); | 114 |
| 119 int64_t start_time_us_ GUARDED_BY(crit_); | |
| 120 int64_t duration_us_ GUARDED_BY(crit_); | |
| 121 const Clock* const clock_; | 115 const Clock* const clock_; |
| 116 |
| 117 RtcEventLogHelperThread helper_thread_; |
| 118 rtc::ThreadChecker thread_checker_; |
| 119 |
| 120 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtcEventLogImpl); |
| 122 }; | 121 }; |
| 123 | 122 |
| 124 namespace { | 123 namespace { |
| 125 // The functions in this namespace convert enums from the runtime format | 124 // The functions in this namespace convert enums from the runtime format |
| 126 // that the rest of the WebRtc project can use, to the corresponding | 125 // that the rest of the WebRtc project can use, to the corresponding |
| 127 // serialized enum which is defined by the protobuf. | 126 // serialized enum which is defined by the protobuf. |
| 128 | 127 |
| 129 // Do not add default return values to the conversion functions in this | |
| 130 // unnamed namespace. The intention is to make the compiler warn if anyone | |
| 131 // adds unhandled new events/modes/etc. | |
| 132 | |
| 133 rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) { | 128 rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) { |
| 134 switch (rtcp_mode) { | 129 switch (rtcp_mode) { |
| 135 case RtcpMode::kCompound: | 130 case RtcpMode::kCompound: |
| 136 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; | 131 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
| 137 case RtcpMode::kReducedSize: | 132 case RtcpMode::kReducedSize: |
| 138 return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE; | 133 return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE; |
| 139 case RtcpMode::kOff: | 134 case RtcpMode::kOff: |
| 140 RTC_NOTREACHED(); | 135 RTC_NOTREACHED(); |
| 141 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; | 136 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
| 142 } | 137 } |
| 143 RTC_NOTREACHED(); | 138 RTC_NOTREACHED(); |
| 144 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; | 139 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
| 145 } | 140 } |
| 146 | 141 |
| 147 rtclog::MediaType ConvertMediaType(MediaType media_type) { | 142 rtclog::MediaType ConvertMediaType(MediaType media_type) { |
| 148 switch (media_type) { | 143 switch (media_type) { |
| 149 case MediaType::ANY: | 144 case MediaType::ANY: |
| 150 return rtclog::MediaType::ANY; | 145 return rtclog::MediaType::ANY; |
| 151 case MediaType::AUDIO: | 146 case MediaType::AUDIO: |
| 152 return rtclog::MediaType::AUDIO; | 147 return rtclog::MediaType::AUDIO; |
| 153 case MediaType::VIDEO: | 148 case MediaType::VIDEO: |
| 154 return rtclog::MediaType::VIDEO; | 149 return rtclog::MediaType::VIDEO; |
| 155 case MediaType::DATA: | 150 case MediaType::DATA: |
| 156 return rtclog::MediaType::DATA; | 151 return rtclog::MediaType::DATA; |
| 157 } | 152 } |
| 158 RTC_NOTREACHED(); | 153 RTC_NOTREACHED(); |
| 159 return rtclog::ANY; | 154 return rtclog::ANY; |
| 160 } | 155 } |
| 161 | 156 |
| 162 } // namespace | 157 // The RTP and RTCP buffers reserve space for twice the expected number of |
| 158 // sent packets because they also contain received packets. |
| 159 const int kStreamConfigsPerSecond = 64; // 16 clients w. 4 streams each. |
| 160 const int kRtpPacketsPerSecond = 500; // 125 sent video packets/s @ 1 Mbps. |
| 161 const int kRtcpPacketsPerSecond = 40; // Assume RTCP sent 20 times/s. |
| 162 const int kPlayoutsPerSecond = 100; // Playout called every 10 ms. |
| 163 const int kBweUpdatesPerSecond = 20; // One BWE update per RTCP packet. |
| 163 | 164 |
| 164 namespace { | 165 const int kControlMessagesPerSecond = 5; |
| 165 bool IsConfigEvent(const rtclog::Event& event) { | |
| 166 rtclog::Event_EventType event_type = event.type(); | |
| 167 return event_type == rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT || | |
| 168 event_type == rtclog::Event::VIDEO_SENDER_CONFIG_EVENT || | |
| 169 event_type == rtclog::Event::AUDIO_RECEIVER_CONFIG_EVENT || | |
| 170 event_type == rtclog::Event::AUDIO_SENDER_CONFIG_EVENT; | |
| 171 } | |
| 172 } // namespace | 166 } // namespace |
| 173 | 167 |
| 174 // RtcEventLogImpl member functions. | 168 // RtcEventLogImpl member functions. |
| 175 RtcEventLogImpl::RtcEventLogImpl() | 169 RtcEventLogImpl::RtcEventLogImpl(const Clock* clock) |
| 176 : file_(FileWrapper::Create()), | 170 // Allocate buffers for roughly one second of history. |
| 177 stream_(), | 171 : message_queue_(kControlMessagesPerSecond), |
| 178 buffer_duration_us_(10000000), | 172 config_queue_(kStreamConfigsPerSecond), |
| 179 currently_logging_(false), | 173 rtp_queue_(kRtpPacketsPerSecond), |
| 180 start_time_us_(0), | 174 rtcp_queue_(kRtcpPacketsPerSecond), |
| 181 duration_us_(0), | 175 acm_playout_queue_(kPlayoutsPerSecond), |
| 182 clock_(Clock::GetRealTimeClock()) { | 176 bwe_loss_queue_(kBweUpdatesPerSecond), |
| 177 wake_up_(false, false), |
| 178 stopped_(false, false), |
| 179 clock_(clock), |
| 180 helper_thread_(&message_queue_, |
| 181 &config_queue_, |
| 182 &rtp_queue_, |
| 183 &rtcp_queue_, |
| 184 &acm_playout_queue_, |
| 185 &bwe_loss_queue_, |
| 186 &wake_up_, |
| 187 &stopped_, |
| 188 clock), |
| 189 thread_checker_() { |
| 190 thread_checker_.DetachFromThread(); |
| 183 } | 191 } |
| 184 | 192 |
| 185 void RtcEventLogImpl::SetBufferDuration(int64_t buffer_duration_us) { | 193 RtcEventLogImpl::~RtcEventLogImpl() { |
| 186 rtc::CritScope lock(&crit_); | 194 // The RtcEventLogHelperThread destructor closes the file |
| 187 buffer_duration_us_ = buffer_duration_us; | 195 // and waits for the thread to terminate. |
| 188 } | 196 } |
| 189 | 197 |
| 190 void RtcEventLogImpl::StartLogging(const std::string& file_name, | 198 bool RtcEventLogImpl::StartLogging(const std::string& file_name, |
| 191 int duration_ms) { | 199 int64_t max_size_bytes) { |
| 192 rtc::CritScope lock(&crit_); | 200 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 193 if (currently_logging_) { | 201 RtcEventLogHelperThread::ControlMessage message; |
| 194 StopLoggingLocked(); | 202 message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE; |
| 195 } | 203 message.max_size_bytes = max_size_bytes; |
| 196 if (file_->OpenFile(file_name.c_str(), false) != 0) { | 204 message.start_time = clock_->TimeInMicroseconds(); |
| 197 return; | 205 message.stop_time = std::numeric_limits<int64_t>::max(); |
| 198 } | 206 message.file.reset(FileWrapper::Create()); |
| 199 start_time_us_ = clock_->TimeInMicroseconds(); | 207 if (message.file->OpenFile(file_name.c_str(), false) != 0) { |
| 200 duration_us_ = static_cast<int64_t>(duration_ms) * 1000; | |
| 201 StartLoggingLocked(); | |
| 202 } | |
| 203 | |
| 204 bool RtcEventLogImpl::StartLogging(rtc::PlatformFile log_file) { | |
| 205 rtc::CritScope lock(&crit_); | |
| 206 | |
| 207 if (currently_logging_) { | |
| 208 StopLoggingLocked(); | |
| 209 } | |
| 210 RTC_DCHECK(platform_file_ == rtc::kInvalidPlatformFileValue); | |
| 211 | |
| 212 FILE* file_stream = rtc::FdopenPlatformFileForWriting(log_file); | |
| 213 if (!file_stream) { | |
| 214 rtc::ClosePlatformFile(log_file); | |
| 215 return false; | 208 return false; |
| 216 } | 209 } |
| 217 | 210 if (!message_queue_.Insert(&message)) { |
| 218 if (file_->OpenFromFileHandle(file_stream, true, false) != 0) { | 211 LOG(LS_WARNING) << "Message queue full. Can't start logging."; |
| 219 rtc::ClosePlatformFile(log_file); | |
| 220 return false; | 212 return false; |
| 221 } | 213 } |
| 222 platform_file_ = log_file; | |
| 223 // Set the start time and duration to keep logging for 10 minutes. | |
| 224 start_time_us_ = clock_->TimeInMicroseconds(); | |
| 225 duration_us_ = 10 * 60 * 1000000; | |
| 226 StartLoggingLocked(); | |
| 227 return true; | 214 return true; |
| 228 } | 215 } |
| 229 | 216 |
| 230 void RtcEventLogImpl::StartLoggingLocked() { | 217 bool RtcEventLogImpl::StartLogging(rtc::PlatformFile platform_file, |
| 231 currently_logging_ = true; | 218 int64_t max_size_bytes) { |
| 232 | 219 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 233 // Write all old configuration events to the log file. | 220 RtcEventLogHelperThread::ControlMessage message; |
| 234 for (auto& event : config_events_) { | 221 message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE; |
| 235 StoreToFile(&event); | 222 message.max_size_bytes = max_size_bytes; |
| 223 message.start_time = clock_->TimeInMicroseconds(); |
| 224 message.stop_time = std::numeric_limits<int64_t>::max(); |
| 225 message.file.reset(FileWrapper::Create()); |
| 226 FILE* file_handle = rtc::FdopenPlatformFileForWriting(platform_file); |
| 227 if (!file_handle) { |
| 228 return false; |
| 236 } | 229 } |
| 237 // Write all recent configuration events to the log file, and | 230 if (message.file->OpenFromFileHandle(file_handle, true, false) != 0) { |
| 238 // write all other recent events to the log file, ignoring any old events. | 231 return false; |
| 239 for (auto& event : recent_log_events_) { | |
| 240 if (IsConfigEvent(event)) { | |
| 241 StoreToFile(&event); | |
| 242 config_events_.push_back(event); | |
| 243 } else if (event.timestamp_us() >= start_time_us_ - buffer_duration_us_) { | |
| 244 StoreToFile(&event); | |
| 245 } | |
| 246 } | 232 } |
| 247 recent_log_events_.clear(); | 233 if (!message_queue_.Insert(&message)) { |
| 248 // Write a LOG_START event to the file. | 234 LOG(LS_WARNING) << "Message queue full. Can't start logging."; |
| 249 rtclog::Event start_event; | 235 return false; |
| 250 start_event.set_timestamp_us(start_time_us_); | 236 } |
| 251 start_event.set_type(rtclog::Event::LOG_START); | 237 return true; |
| 252 StoreToFile(&start_event); | |
| 253 } | 238 } |
| 254 | 239 |
| 255 void RtcEventLogImpl::StopLogging() { | 240 void RtcEventLogImpl::StopLogging() { |
| 256 rtc::CritScope lock(&crit_); | 241 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 257 StopLoggingLocked(); | 242 RtcEventLogHelperThread::ControlMessage message; |
| 243 message.message_type = RtcEventLogHelperThread::ControlMessage::STOP_FILE; |
| 244 message.stop_time = clock_->TimeInMicroseconds(); |
| 245 while (!message_queue_.Insert(&message)) { |
| 246 // TODO(terelius): We would like to have a blocking Insert function in the |
| 247 // SwapQueue, but for the time being we will just clear any previous |
| 248 // messages. |
| 249 // Since StopLogging waits for the thread, it is essential that we don't |
| 250 // clear any STOP_FILE messages. To ensure that there is only one call at a |
| 251 // time, we require that all calls to StopLogging are made on the same |
| 252 // thread. |
| 253 LOG(LS_WARNING) << "Message queue full. Clearing queue to stop logging."; |
| 254 message_queue_.Clear(); |
| 255 } |
| 256 wake_up_.Set(); // Request the output thread to wake up. |
| 257 stopped_.Wait(rtc::Event::kForever); // Wait for the log to stop. |
| 258 } | 258 } |
| 259 | 259 |
| 260 void RtcEventLogImpl::LogVideoReceiveStreamConfig( | 260 void RtcEventLogImpl::LogVideoReceiveStreamConfig( |
| 261 const VideoReceiveStream::Config& config) { | 261 const VideoReceiveStream::Config& config) { |
| 262 rtc::CritScope lock(&crit_); | |
| 263 | |
| 264 rtclog::Event event; | 262 rtclog::Event event; |
| 265 event.set_timestamp_us(clock_->TimeInMicroseconds()); | 263 event.set_timestamp_us(clock_->TimeInMicroseconds()); |
| 266 event.set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); | 264 event.set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); |
| 267 | 265 |
| 268 rtclog::VideoReceiveConfig* receiver_config = | 266 rtclog::VideoReceiveConfig* receiver_config = |
| 269 event.mutable_video_receiver_config(); | 267 event.mutable_video_receiver_config(); |
| 270 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc); | 268 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc); |
| 271 receiver_config->set_local_ssrc(config.rtp.local_ssrc); | 269 receiver_config->set_local_ssrc(config.rtp.local_ssrc); |
| 272 | 270 |
| 273 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode)); | 271 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 285 receiver_config->add_header_extensions(); | 283 receiver_config->add_header_extensions(); |
| 286 extension->set_name(e.name); | 284 extension->set_name(e.name); |
| 287 extension->set_id(e.id); | 285 extension->set_id(e.id); |
| 288 } | 286 } |
| 289 | 287 |
| 290 for (const auto& d : config.decoders) { | 288 for (const auto& d : config.decoders) { |
| 291 rtclog::DecoderConfig* decoder = receiver_config->add_decoders(); | 289 rtclog::DecoderConfig* decoder = receiver_config->add_decoders(); |
| 292 decoder->set_name(d.payload_name); | 290 decoder->set_name(d.payload_name); |
| 293 decoder->set_payload_type(d.payload_type); | 291 decoder->set_payload_type(d.payload_type); |
| 294 } | 292 } |
| 295 HandleEvent(&event); | 293 if (!config_queue_.Insert(&event)) { |
| 294 LOG(LS_WARNING) << "Config queue full. Not logging config event."; |
| 295 } |
| 296 } | 296 } |
| 297 | 297 |
| 298 void RtcEventLogImpl::LogVideoSendStreamConfig( | 298 void RtcEventLogImpl::LogVideoSendStreamConfig( |
| 299 const VideoSendStream::Config& config) { | 299 const VideoSendStream::Config& config) { |
| 300 rtc::CritScope lock(&crit_); | |
| 301 | |
| 302 rtclog::Event event; | 300 rtclog::Event event; |
| 303 event.set_timestamp_us(clock_->TimeInMicroseconds()); | 301 event.set_timestamp_us(clock_->TimeInMicroseconds()); |
| 304 event.set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT); | 302 event.set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT); |
| 305 | 303 |
| 306 rtclog::VideoSendConfig* sender_config = event.mutable_video_sender_config(); | 304 rtclog::VideoSendConfig* sender_config = event.mutable_video_sender_config(); |
| 307 | 305 |
| 308 for (const auto& ssrc : config.rtp.ssrcs) { | 306 for (const auto& ssrc : config.rtp.ssrcs) { |
| 309 sender_config->add_ssrcs(ssrc); | 307 sender_config->add_ssrcs(ssrc); |
| 310 } | 308 } |
| 311 | 309 |
| 312 for (const auto& e : config.rtp.extensions) { | 310 for (const auto& e : config.rtp.extensions) { |
| 313 rtclog::RtpHeaderExtension* extension = | 311 rtclog::RtpHeaderExtension* extension = |
| 314 sender_config->add_header_extensions(); | 312 sender_config->add_header_extensions(); |
| 315 extension->set_name(e.name); | 313 extension->set_name(e.name); |
| 316 extension->set_id(e.id); | 314 extension->set_id(e.id); |
| 317 } | 315 } |
| 318 | 316 |
| 319 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) { | 317 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) { |
| 320 sender_config->add_rtx_ssrcs(rtx_ssrc); | 318 sender_config->add_rtx_ssrcs(rtx_ssrc); |
| 321 } | 319 } |
| 322 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type); | 320 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type); |
| 323 | 321 |
| 324 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder(); | 322 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder(); |
| 325 encoder->set_name(config.encoder_settings.payload_name); | 323 encoder->set_name(config.encoder_settings.payload_name); |
| 326 encoder->set_payload_type(config.encoder_settings.payload_type); | 324 encoder->set_payload_type(config.encoder_settings.payload_type); |
| 327 HandleEvent(&event); | 325 if (!config_queue_.Insert(&event)) { |
| 326 LOG(LS_WARNING) << "Config queue full. Not logging config event."; |
| 327 } |
| 328 } | 328 } |
| 329 | 329 |
| 330 void RtcEventLogImpl::LogRtpHeader(PacketDirection direction, | 330 void RtcEventLogImpl::LogRtpHeader(PacketDirection direction, |
| 331 MediaType media_type, | 331 MediaType media_type, |
| 332 const uint8_t* header, | 332 const uint8_t* header, |
| 333 size_t packet_length) { | 333 size_t packet_length) { |
| 334 // Read header length (in bytes) from packet data. | 334 // Read header length (in bytes) from packet data. |
| 335 if (packet_length < 12u) { | 335 if (packet_length < 12u) { |
| 336 return; // Don't read outside the packet. | 336 return; // Don't read outside the packet. |
| 337 } | 337 } |
| 338 const bool x = (header[0] & 0x10) != 0; | 338 const bool x = (header[0] & 0x10) != 0; |
| 339 const uint8_t cc = header[0] & 0x0f; | 339 const uint8_t cc = header[0] & 0x0f; |
| 340 size_t header_length = 12u + cc * 4u; | 340 size_t header_length = 12u + cc * 4u; |
| 341 | 341 |
| 342 if (x) { | 342 if (x) { |
| 343 if (packet_length < 12u + cc * 4u + 4u) { | 343 if (packet_length < 12u + cc * 4u + 4u) { |
| 344 return; // Don't read outside the packet. | 344 return; // Don't read outside the packet. |
| 345 } | 345 } |
| 346 size_t x_len = ByteReader<uint16_t>::ReadBigEndian(header + 14 + cc * 4); | 346 size_t x_len = ByteReader<uint16_t>::ReadBigEndian(header + 14 + cc * 4); |
| 347 header_length += (x_len + 1) * 4; | 347 header_length += (x_len + 1) * 4; |
| 348 } | 348 } |
| 349 | 349 |
| 350 rtc::CritScope lock(&crit_); | |
| 351 rtclog::Event rtp_event; | 350 rtclog::Event rtp_event; |
| 352 rtp_event.set_timestamp_us(clock_->TimeInMicroseconds()); | 351 rtp_event.set_timestamp_us(clock_->TimeInMicroseconds()); |
| 353 rtp_event.set_type(rtclog::Event::RTP_EVENT); | 352 rtp_event.set_type(rtclog::Event::RTP_EVENT); |
| 354 rtp_event.mutable_rtp_packet()->set_incoming(direction == kIncomingPacket); | 353 rtp_event.mutable_rtp_packet()->set_incoming(direction == kIncomingPacket); |
| 355 rtp_event.mutable_rtp_packet()->set_type(ConvertMediaType(media_type)); | 354 rtp_event.mutable_rtp_packet()->set_type(ConvertMediaType(media_type)); |
| 356 rtp_event.mutable_rtp_packet()->set_packet_length(packet_length); | 355 rtp_event.mutable_rtp_packet()->set_packet_length(packet_length); |
| 357 rtp_event.mutable_rtp_packet()->set_header(header, header_length); | 356 rtp_event.mutable_rtp_packet()->set_header(header, header_length); |
| 358 HandleEvent(&rtp_event); | 357 if (!rtp_queue_.Insert(&rtp_event)) { |
| 358 LOG(LS_WARNING) << "RTP queue full. Not logging RTP packet."; |
| 359 } |
| 359 } | 360 } |
| 360 | 361 |
| 361 void RtcEventLogImpl::LogRtcpPacket(PacketDirection direction, | 362 void RtcEventLogImpl::LogRtcpPacket(PacketDirection direction, |
| 362 MediaType media_type, | 363 MediaType media_type, |
| 363 const uint8_t* packet, | 364 const uint8_t* packet, |
| 364 size_t length) { | 365 size_t length) { |
| 365 rtc::CritScope lock(&crit_); | |
| 366 rtclog::Event rtcp_event; | 366 rtclog::Event rtcp_event; |
| 367 rtcp_event.set_timestamp_us(clock_->TimeInMicroseconds()); | 367 rtcp_event.set_timestamp_us(clock_->TimeInMicroseconds()); |
| 368 rtcp_event.set_type(rtclog::Event::RTCP_EVENT); | 368 rtcp_event.set_type(rtclog::Event::RTCP_EVENT); |
| 369 rtcp_event.mutable_rtcp_packet()->set_incoming(direction == kIncomingPacket); | 369 rtcp_event.mutable_rtcp_packet()->set_incoming(direction == kIncomingPacket); |
| 370 rtcp_event.mutable_rtcp_packet()->set_type(ConvertMediaType(media_type)); | 370 rtcp_event.mutable_rtcp_packet()->set_type(ConvertMediaType(media_type)); |
| 371 | 371 |
| 372 RTCPUtility::RtcpCommonHeader header; | 372 RTCPUtility::RtcpCommonHeader header; |
| 373 const uint8_t* block_begin = packet; | 373 const uint8_t* block_begin = packet; |
| 374 const uint8_t* packet_end = packet + length; | 374 const uint8_t* packet_end = packet + length; |
| 375 RTC_DCHECK(length <= IP_PACKET_SIZE); | 375 RTC_DCHECK(length <= IP_PACKET_SIZE); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 FALLTHROUGH(); | 407 FALLTHROUGH(); |
| 408 default: | 408 default: |
| 409 // We don't log sender descriptions, application defined messages | 409 // We don't log sender descriptions, application defined messages |
| 410 // or message blocks of unknown type. | 410 // or message blocks of unknown type. |
| 411 break; | 411 break; |
| 412 } | 412 } |
| 413 | 413 |
| 414 block_begin += block_size; | 414 block_begin += block_size; |
| 415 } | 415 } |
| 416 rtcp_event.mutable_rtcp_packet()->set_packet_data(buffer, buffer_length); | 416 rtcp_event.mutable_rtcp_packet()->set_packet_data(buffer, buffer_length); |
| 417 HandleEvent(&rtcp_event); | 417 if (!rtcp_queue_.Insert(&rtcp_event)) { |
| 418 LOG(LS_WARNING) << "RTCP queue full. Not logging RTCP packet."; |
| 419 } |
| 418 } | 420 } |
| 419 | 421 |
| 420 void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) { | 422 void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) { |
| 421 rtc::CritScope lock(&crit_); | |
| 422 rtclog::Event event; | 423 rtclog::Event event; |
| 423 event.set_timestamp_us(clock_->TimeInMicroseconds()); | 424 event.set_timestamp_us(clock_->TimeInMicroseconds()); |
| 424 event.set_type(rtclog::Event::AUDIO_PLAYOUT_EVENT); | 425 event.set_type(rtclog::Event::AUDIO_PLAYOUT_EVENT); |
| 425 auto playout_event = event.mutable_audio_playout_event(); | 426 auto playout_event = event.mutable_audio_playout_event(); |
| 426 playout_event->set_local_ssrc(ssrc); | 427 playout_event->set_local_ssrc(ssrc); |
| 427 HandleEvent(&event); | 428 if (!acm_playout_queue_.Insert(&event)) { |
| 429 LOG(LS_WARNING) << "Playout queue full. Not logging ACM playout."; |
| 430 } |
| 428 } | 431 } |
| 429 | 432 |
| 430 void RtcEventLogImpl::LogBwePacketLossEvent(int32_t bitrate, | 433 void RtcEventLogImpl::LogBwePacketLossEvent(int32_t bitrate, |
| 431 uint8_t fraction_loss, | 434 uint8_t fraction_loss, |
| 432 int32_t total_packets) { | 435 int32_t total_packets) { |
| 433 rtc::CritScope lock(&crit_); | |
| 434 rtclog::Event event; | 436 rtclog::Event event; |
| 435 event.set_timestamp_us(clock_->TimeInMicroseconds()); | 437 event.set_timestamp_us(clock_->TimeInMicroseconds()); |
| 436 event.set_type(rtclog::Event::BWE_PACKET_LOSS_EVENT); | 438 event.set_type(rtclog::Event::BWE_PACKET_LOSS_EVENT); |
| 437 auto bwe_event = event.mutable_bwe_packet_loss_event(); | 439 auto bwe_event = event.mutable_bwe_packet_loss_event(); |
| 438 bwe_event->set_bitrate(bitrate); | 440 bwe_event->set_bitrate(bitrate); |
| 439 bwe_event->set_fraction_loss(fraction_loss); | 441 bwe_event->set_fraction_loss(fraction_loss); |
| 440 bwe_event->set_total_packets(total_packets); | 442 bwe_event->set_total_packets(total_packets); |
| 441 HandleEvent(&event); | 443 if (!bwe_loss_queue_.Insert(&event)) { |
| 442 } | 444 LOG(LS_WARNING) << "BWE loss queue full. Not logging BWE update."; |
| 443 | |
| 444 void RtcEventLogImpl::StopLoggingLocked() { | |
| 445 if (currently_logging_) { | |
| 446 currently_logging_ = false; | |
| 447 // Create a LogEnd event | |
| 448 rtclog::Event event; | |
| 449 event.set_timestamp_us(clock_->TimeInMicroseconds()); | |
| 450 event.set_type(rtclog::Event::LOG_END); | |
| 451 // Store the event and close the file | |
| 452 RTC_DCHECK(file_->Open()); | |
| 453 StoreToFile(&event); | |
| 454 file_->CloseFile(); | |
| 455 if (platform_file_ != rtc::kInvalidPlatformFileValue) { | |
| 456 rtc::ClosePlatformFile(platform_file_); | |
| 457 platform_file_ = rtc::kInvalidPlatformFileValue; | |
| 458 } | |
| 459 } | |
| 460 RTC_DCHECK(!file_->Open()); | |
| 461 stream_.Clear(); | |
| 462 } | |
| 463 | |
| 464 void RtcEventLogImpl::HandleEvent(rtclog::Event* event) { | |
| 465 if (currently_logging_) { | |
| 466 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) { | |
| 467 StoreToFile(event); | |
| 468 return; | |
| 469 } | |
| 470 StopLoggingLocked(); | |
| 471 } | |
| 472 AddRecentEvent(*event); | |
| 473 } | |
| 474 | |
| 475 void RtcEventLogImpl::StoreToFile(rtclog::Event* event) { | |
| 476 // Reuse the same object at every log event. | |
| 477 if (stream_.stream_size() < 1) { | |
| 478 stream_.add_stream(); | |
| 479 } | |
| 480 RTC_DCHECK_EQ(stream_.stream_size(), 1); | |
| 481 stream_.mutable_stream(0)->Swap(event); | |
| 482 // TODO(terelius): Doesn't this create a new EventStream per event? | |
| 483 // Is this guaranteed to work e.g. in future versions of protobuf? | |
| 484 std::string dump_buffer; | |
| 485 stream_.SerializeToString(&dump_buffer); | |
| 486 file_->Write(dump_buffer.data(), dump_buffer.size()); | |
| 487 } | |
| 488 | |
| 489 void RtcEventLogImpl::AddRecentEvent(const rtclog::Event& event) { | |
| 490 recent_log_events_.push_back(event); | |
| 491 while (recent_log_events_.front().timestamp_us() < | |
| 492 event.timestamp_us() - buffer_duration_us_) { | |
| 493 if (IsConfigEvent(recent_log_events_.front())) { | |
| 494 config_events_.push_back(recent_log_events_.front()); | |
| 495 } | |
| 496 recent_log_events_.pop_front(); | |
| 497 } | 445 } |
| 498 } | 446 } |
| 499 | 447 |
| 500 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, | 448 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, |
| 501 rtclog::EventStream* result) { | 449 rtclog::EventStream* result) { |
| 502 char tmp_buffer[1024]; | 450 char tmp_buffer[1024]; |
| 503 int bytes_read = 0; | 451 int bytes_read = 0; |
| 504 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create()); | 452 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create()); |
| 505 if (dump_file->OpenFile(file_name.c_str(), true) != 0) { | 453 if (dump_file->OpenFile(file_name.c_str(), true) != 0) { |
| 506 return false; | 454 return false; |
| 507 } | 455 } |
| 508 std::string dump_buffer; | 456 std::string dump_buffer; |
| 509 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { | 457 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { |
| 510 dump_buffer.append(tmp_buffer, bytes_read); | 458 dump_buffer.append(tmp_buffer, bytes_read); |
| 511 } | 459 } |
| 512 dump_file->CloseFile(); | 460 dump_file->CloseFile(); |
| 513 return result->ParseFromString(dump_buffer); | 461 return result->ParseFromString(dump_buffer); |
| 514 } | 462 } |
| 515 | 463 |
| 516 #endif // ENABLE_RTC_EVENT_LOG | 464 #endif // ENABLE_RTC_EVENT_LOG |
| 517 | 465 |
| 518 // RtcEventLog member functions. | 466 // RtcEventLog member functions. |
| 519 rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() { | 467 rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create(const Clock* clock) { |
| 520 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl()); | 468 #ifdef ENABLE_RTC_EVENT_LOG |
| 469 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl(clock)); |
| 470 #else |
| 471 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogNullImpl()); |
| 472 #endif // ENABLE_RTC_EVENT_LOG |
| 521 } | 473 } |
| 522 | 474 |
| 523 } // namespace webrtc | 475 } // namespace webrtc |
| OLD | NEW |