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 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 13 | #include <limits> |
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" |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 17 | #include "webrtc/base/constructormagic.h" |
| 18 | #include "webrtc/base/event.h" |
| 19 | #include "webrtc/base/swap_queue.h" |
| 20 | #include "webrtc/base/thread_checker.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 21 | #include "webrtc/call.h" |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 22 | #include "webrtc/call/rtc_event_log_helper_thread.h" |
terelius | d66daa2 | 2015-11-06 09:00:18 -0800 | [diff] [blame] | 23 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 24 | #include "webrtc/modules/rtp_rtcp/source/byte_io.h" |
terelius | d66daa2 | 2015-11-06 09:00:18 -0800 | [diff] [blame] | 25 | #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 26 | #include "webrtc/system_wrappers/include/clock.h" |
| 27 | #include "webrtc/system_wrappers/include/file_wrapper.h" |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 28 | #include "webrtc/system_wrappers/include/logging.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 29 | |
| 30 | #ifdef ENABLE_RTC_EVENT_LOG |
| 31 | // Files generated at build-time by the protobuf compiler. |
| 32 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
Peter Boström | 5c389d3 | 2015-09-25 13:58:30 +0200 | [diff] [blame] | 33 | #include "external/webrtc/webrtc/call/rtc_event_log.pb.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 34 | #else |
Peter Boström | 5c389d3 | 2015-09-25 13:58:30 +0200 | [diff] [blame] | 35 | #include "webrtc/call/rtc_event_log.pb.h" |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 36 | #endif |
| 37 | #endif |
| 38 | |
| 39 | namespace webrtc { |
| 40 | |
| 41 | #ifndef ENABLE_RTC_EVENT_LOG |
| 42 | |
| 43 | // No-op implementation if flag is not set. |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 44 | class RtcEventLogNullImpl final : public RtcEventLog { |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 45 | public: |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 46 | bool StartLogging(const std::string& file_name, |
| 47 | int64_t max_size_bytes) override { |
| 48 | return false; |
| 49 | } |
| 50 | bool StartLogging(rtc::PlatformFile platform_file, |
| 51 | int64_t max_size_bytes) override { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 52 | // The platform_file is open and needs to be closed. |
| 53 | if (!rtc::ClosePlatformFile(platform_file)) { |
| 54 | LOG(LS_ERROR) << "Can't close file."; |
| 55 | } |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | void StopLogging() override {} |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 59 | void LogVideoReceiveStreamConfig( |
| 60 | const VideoReceiveStream::Config& config) override {} |
| 61 | void LogVideoSendStreamConfig( |
| 62 | const VideoSendStream::Config& config) override {} |
terelius | 429c345 | 2016-01-21 05:42:04 -0800 | [diff] [blame] | 63 | void LogRtpHeader(PacketDirection direction, |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 64 | MediaType media_type, |
| 65 | const uint8_t* header, |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 66 | size_t packet_length) override {} |
terelius | 429c345 | 2016-01-21 05:42:04 -0800 | [diff] [blame] | 67 | void LogRtcpPacket(PacketDirection direction, |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 68 | MediaType media_type, |
| 69 | const uint8_t* packet, |
| 70 | size_t length) override {} |
Ivo Creusen | ae856f2 | 2015-09-17 16:30:16 +0200 | [diff] [blame] | 71 | void LogAudioPlayout(uint32_t ssrc) override {} |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 72 | void LogBwePacketLossEvent(int32_t bitrate, |
| 73 | uint8_t fraction_loss, |
| 74 | int32_t total_packets) override {} |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | #else // ENABLE_RTC_EVENT_LOG is defined |
| 78 | |
| 79 | class RtcEventLogImpl final : public RtcEventLog { |
| 80 | public: |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 81 | explicit RtcEventLogImpl(const Clock* clock); |
| 82 | ~RtcEventLogImpl() override; |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 83 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 84 | bool StartLogging(const std::string& file_name, |
| 85 | int64_t max_size_bytes) override; |
| 86 | bool StartLogging(rtc::PlatformFile platform_file, |
| 87 | int64_t max_size_bytes) override; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 88 | void StopLogging() override; |
| 89 | void LogVideoReceiveStreamConfig( |
| 90 | const VideoReceiveStream::Config& config) override; |
| 91 | void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override; |
terelius | 429c345 | 2016-01-21 05:42:04 -0800 | [diff] [blame] | 92 | void LogRtpHeader(PacketDirection direction, |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 93 | MediaType media_type, |
| 94 | const uint8_t* header, |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 95 | size_t packet_length) override; |
terelius | 429c345 | 2016-01-21 05:42:04 -0800 | [diff] [blame] | 96 | void LogRtcpPacket(PacketDirection direction, |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 97 | MediaType media_type, |
| 98 | const uint8_t* packet, |
| 99 | size_t length) override; |
Ivo Creusen | ae856f2 | 2015-09-17 16:30:16 +0200 | [diff] [blame] | 100 | void LogAudioPlayout(uint32_t ssrc) override; |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 101 | void LogBwePacketLossEvent(int32_t bitrate, |
| 102 | uint8_t fraction_loss, |
| 103 | int32_t total_packets) override; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 104 | |
| 105 | private: |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 106 | // Message queue for passing control messages to the logging thread. |
| 107 | SwapQueue<RtcEventLogHelperThread::ControlMessage> message_queue_; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 108 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 109 | // Message queue for passing events to the logging thread. |
| 110 | SwapQueue<std::unique_ptr<rtclog::Event> > event_queue_; |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 111 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 112 | rtc::Event wake_up_; |
| 113 | rtc::Event stopped_; |
| 114 | |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 115 | const Clock* const clock_; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 116 | |
| 117 | RtcEventLogHelperThread helper_thread_; |
| 118 | rtc::ThreadChecker thread_checker_; |
| 119 | |
| 120 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtcEventLogImpl); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | namespace { |
| 124 | // The functions in this namespace convert enums from the runtime format |
| 125 | // that the rest of the WebRtc project can use, to the corresponding |
| 126 | // serialized enum which is defined by the protobuf. |
| 127 | |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 128 | rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode(RtcpMode rtcp_mode) { |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 129 | switch (rtcp_mode) { |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 130 | case RtcpMode::kCompound: |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 131 | return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 132 | case RtcpMode::kReducedSize: |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 133 | return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE; |
pbos | da903ea | 2015-10-02 02:36:56 -0700 | [diff] [blame] | 134 | case RtcpMode::kOff: |
| 135 | RTC_NOTREACHED(); |
| 136 | return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 137 | } |
| 138 | RTC_NOTREACHED(); |
| 139 | return rtclog::VideoReceiveConfig::RTCP_COMPOUND; |
| 140 | } |
| 141 | |
| 142 | rtclog::MediaType ConvertMediaType(MediaType media_type) { |
| 143 | switch (media_type) { |
| 144 | case MediaType::ANY: |
| 145 | return rtclog::MediaType::ANY; |
| 146 | case MediaType::AUDIO: |
| 147 | return rtclog::MediaType::AUDIO; |
| 148 | case MediaType::VIDEO: |
| 149 | return rtclog::MediaType::VIDEO; |
| 150 | case MediaType::DATA: |
| 151 | return rtclog::MediaType::DATA; |
| 152 | } |
| 153 | RTC_NOTREACHED(); |
| 154 | return rtclog::ANY; |
| 155 | } |
| 156 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 157 | // The RTP and RTCP buffers reserve space for twice the expected number of |
| 158 | // sent packets because they also contain received packets. |
| 159 | static const int kEventsPerSecond = 1000; |
| 160 | static const int kControlMessagesPerSecond = 10; |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 161 | } // namespace |
| 162 | |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 163 | // RtcEventLogImpl member functions. |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 164 | RtcEventLogImpl::RtcEventLogImpl(const Clock* clock) |
| 165 | // Allocate buffers for roughly one second of history. |
| 166 | : message_queue_(kControlMessagesPerSecond), |
| 167 | event_queue_(kEventsPerSecond), |
| 168 | wake_up_(false, false), |
| 169 | stopped_(false, false), |
| 170 | clock_(clock), |
| 171 | helper_thread_(&message_queue_, |
| 172 | &event_queue_, |
| 173 | &wake_up_, |
| 174 | &stopped_, |
| 175 | clock), |
| 176 | thread_checker_() { |
| 177 | thread_checker_.DetachFromThread(); |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 178 | } |
| 179 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 180 | RtcEventLogImpl::~RtcEventLogImpl() { |
| 181 | // The RtcEventLogHelperThread destructor closes the file |
| 182 | // and waits for the thread to terminate. |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 183 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 184 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 185 | bool RtcEventLogImpl::StartLogging(const std::string& file_name, |
| 186 | int64_t max_size_bytes) { |
| 187 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 188 | RtcEventLogHelperThread::ControlMessage message; |
| 189 | message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE; |
ivoc | c1513ee | 2016-05-13 08:30:39 -0700 | [diff] [blame] | 190 | message.max_size_bytes = max_size_bytes <= 0 |
| 191 | ? std::numeric_limits<int64_t>::max() |
| 192 | : max_size_bytes; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 193 | message.start_time = clock_->TimeInMicroseconds(); |
| 194 | message.stop_time = std::numeric_limits<int64_t>::max(); |
| 195 | message.file.reset(FileWrapper::Create()); |
| 196 | if (message.file->OpenFile(file_name.c_str(), false) != 0) { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 197 | LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; |
ivoc | 112a3d8 | 2015-10-16 02:22:18 -0700 | [diff] [blame] | 198 | return false; |
| 199 | } |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 200 | if (!message_queue_.Insert(&message)) { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 201 | LOG(LS_ERROR) << "Message queue full. Can't start logging."; |
ivoc | 112a3d8 | 2015-10-16 02:22:18 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 204 | LOG(LS_INFO) << "Starting WebRTC event log."; |
ivoc | 112a3d8 | 2015-10-16 02:22:18 -0700 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 208 | bool RtcEventLogImpl::StartLogging(rtc::PlatformFile platform_file, |
| 209 | int64_t max_size_bytes) { |
| 210 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 211 | RtcEventLogHelperThread::ControlMessage message; |
| 212 | message.message_type = RtcEventLogHelperThread::ControlMessage::START_FILE; |
ivoc | c1513ee | 2016-05-13 08:30:39 -0700 | [diff] [blame] | 213 | message.max_size_bytes = max_size_bytes <= 0 |
| 214 | ? std::numeric_limits<int64_t>::max() |
| 215 | : max_size_bytes; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 216 | message.start_time = clock_->TimeInMicroseconds(); |
| 217 | message.stop_time = std::numeric_limits<int64_t>::max(); |
| 218 | message.file.reset(FileWrapper::Create()); |
| 219 | FILE* file_handle = rtc::FdopenPlatformFileForWriting(platform_file); |
| 220 | if (!file_handle) { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 221 | LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; |
| 222 | // Even though we failed to open a FILE*, the platform_file is still open |
| 223 | // and needs to be closed. |
| 224 | if (!rtc::ClosePlatformFile(platform_file)) { |
| 225 | LOG(LS_ERROR) << "Can't close file."; |
| 226 | } |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 227 | return false; |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 228 | } |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 229 | if (message.file->OpenFromFileHandle(file_handle, true, false) != 0) { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 230 | LOG(LS_ERROR) << "Can't open file. WebRTC event log not started."; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 231 | return false; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 232 | } |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 233 | if (!message_queue_.Insert(&message)) { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 234 | LOG(LS_ERROR) << "Message queue full. Can't start logging."; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 235 | return false; |
| 236 | } |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 237 | LOG(LS_INFO) << "Starting WebRTC event log."; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 238 | return true; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void RtcEventLogImpl::StopLogging() { |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 242 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 243 | RtcEventLogHelperThread::ControlMessage message; |
| 244 | message.message_type = RtcEventLogHelperThread::ControlMessage::STOP_FILE; |
| 245 | message.stop_time = clock_->TimeInMicroseconds(); |
| 246 | while (!message_queue_.Insert(&message)) { |
| 247 | // TODO(terelius): We would like to have a blocking Insert function in the |
| 248 | // SwapQueue, but for the time being we will just clear any previous |
| 249 | // messages. |
| 250 | // Since StopLogging waits for the thread, it is essential that we don't |
| 251 | // clear any STOP_FILE messages. To ensure that there is only one call at a |
| 252 | // time, we require that all calls to StopLogging are made on the same |
| 253 | // thread. |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 254 | LOG(LS_ERROR) << "Message queue full. Clearing queue to stop logging."; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 255 | message_queue_.Clear(); |
| 256 | } |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 257 | LOG(LS_INFO) << "Stopping WebRTC event log."; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 258 | wake_up_.Set(); // Request the output thread to wake up. |
| 259 | stopped_.Wait(rtc::Event::kForever); // Wait for the log to stop. |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void RtcEventLogImpl::LogVideoReceiveStreamConfig( |
| 263 | const VideoReceiveStream::Config& config) { |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 264 | std::unique_ptr<rtclog::Event> event(new rtclog::Event()); |
| 265 | event->set_timestamp_us(clock_->TimeInMicroseconds()); |
| 266 | event->set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 267 | |
| 268 | rtclog::VideoReceiveConfig* receiver_config = |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 269 | event->mutable_video_receiver_config(); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 270 | receiver_config->set_remote_ssrc(config.rtp.remote_ssrc); |
| 271 | receiver_config->set_local_ssrc(config.rtp.local_ssrc); |
| 272 | |
| 273 | receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode)); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 274 | receiver_config->set_remb(config.rtp.remb); |
| 275 | |
| 276 | for (const auto& kv : config.rtp.rtx) { |
| 277 | rtclog::RtxMap* rtx = receiver_config->add_rtx_map(); |
| 278 | rtx->set_payload_type(kv.first); |
| 279 | rtx->mutable_config()->set_rtx_ssrc(kv.second.ssrc); |
| 280 | rtx->mutable_config()->set_rtx_payload_type(kv.second.payload_type); |
| 281 | } |
| 282 | |
| 283 | for (const auto& e : config.rtp.extensions) { |
| 284 | rtclog::RtpHeaderExtension* extension = |
| 285 | receiver_config->add_header_extensions(); |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 286 | extension->set_name(e.uri); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 287 | extension->set_id(e.id); |
| 288 | } |
| 289 | |
| 290 | for (const auto& d : config.decoders) { |
| 291 | rtclog::DecoderConfig* decoder = receiver_config->add_decoders(); |
| 292 | decoder->set_name(d.payload_name); |
| 293 | decoder->set_payload_type(d.payload_type); |
| 294 | } |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 295 | if (!event_queue_.Insert(&event)) { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 296 | LOG(LS_ERROR) << "Config queue full. Not logging config event."; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 297 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void RtcEventLogImpl::LogVideoSendStreamConfig( |
| 301 | const VideoSendStream::Config& config) { |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 302 | std::unique_ptr<rtclog::Event> event(new rtclog::Event()); |
| 303 | event->set_timestamp_us(clock_->TimeInMicroseconds()); |
| 304 | event->set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 305 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 306 | rtclog::VideoSendConfig* sender_config = event->mutable_video_sender_config(); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 307 | |
| 308 | for (const auto& ssrc : config.rtp.ssrcs) { |
| 309 | sender_config->add_ssrcs(ssrc); |
| 310 | } |
| 311 | |
| 312 | for (const auto& e : config.rtp.extensions) { |
| 313 | rtclog::RtpHeaderExtension* extension = |
| 314 | sender_config->add_header_extensions(); |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 315 | extension->set_name(e.uri); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 316 | extension->set_id(e.id); |
| 317 | } |
| 318 | |
| 319 | for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) { |
| 320 | sender_config->add_rtx_ssrcs(rtx_ssrc); |
| 321 | } |
| 322 | sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type); |
| 323 | |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 324 | rtclog::EncoderConfig* encoder = sender_config->mutable_encoder(); |
| 325 | encoder->set_name(config.encoder_settings.payload_name); |
| 326 | encoder->set_payload_type(config.encoder_settings.payload_type); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 327 | if (!event_queue_.Insert(&event)) { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 328 | LOG(LS_ERROR) << "Config queue full. Not logging config event."; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 329 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 330 | } |
| 331 | |
terelius | 429c345 | 2016-01-21 05:42:04 -0800 | [diff] [blame] | 332 | void RtcEventLogImpl::LogRtpHeader(PacketDirection direction, |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 333 | MediaType media_type, |
| 334 | const uint8_t* header, |
terelius | 2f9fd5d | 2015-09-04 03:39:42 -0700 | [diff] [blame] | 335 | size_t packet_length) { |
| 336 | // Read header length (in bytes) from packet data. |
| 337 | if (packet_length < 12u) { |
| 338 | return; // Don't read outside the packet. |
| 339 | } |
| 340 | const bool x = (header[0] & 0x10) != 0; |
| 341 | const uint8_t cc = header[0] & 0x0f; |
| 342 | size_t header_length = 12u + cc * 4u; |
| 343 | |
| 344 | if (x) { |
| 345 | if (packet_length < 12u + cc * 4u + 4u) { |
| 346 | return; // Don't read outside the packet. |
| 347 | } |
| 348 | size_t x_len = ByteReader<uint16_t>::ReadBigEndian(header + 14 + cc * 4); |
| 349 | header_length += (x_len + 1) * 4; |
| 350 | } |
| 351 | |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 352 | std::unique_ptr<rtclog::Event> rtp_event(new rtclog::Event()); |
| 353 | rtp_event->set_timestamp_us(clock_->TimeInMicroseconds()); |
| 354 | rtp_event->set_type(rtclog::Event::RTP_EVENT); |
| 355 | rtp_event->mutable_rtp_packet()->set_incoming(direction == kIncomingPacket); |
| 356 | rtp_event->mutable_rtp_packet()->set_type(ConvertMediaType(media_type)); |
| 357 | rtp_event->mutable_rtp_packet()->set_packet_length(packet_length); |
| 358 | rtp_event->mutable_rtp_packet()->set_header(header, header_length); |
| 359 | if (!event_queue_.Insert(&rtp_event)) { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 360 | LOG(LS_ERROR) << "RTP queue full. Not logging RTP packet."; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 361 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 362 | } |
| 363 | |
terelius | 429c345 | 2016-01-21 05:42:04 -0800 | [diff] [blame] | 364 | void RtcEventLogImpl::LogRtcpPacket(PacketDirection direction, |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 365 | MediaType media_type, |
| 366 | const uint8_t* packet, |
| 367 | size_t length) { |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 368 | std::unique_ptr<rtclog::Event> rtcp_event(new rtclog::Event()); |
| 369 | rtcp_event->set_timestamp_us(clock_->TimeInMicroseconds()); |
| 370 | rtcp_event->set_type(rtclog::Event::RTCP_EVENT); |
| 371 | rtcp_event->mutable_rtcp_packet()->set_incoming(direction == kIncomingPacket); |
| 372 | rtcp_event->mutable_rtcp_packet()->set_type(ConvertMediaType(media_type)); |
terelius | d66daa2 | 2015-11-06 09:00:18 -0800 | [diff] [blame] | 373 | |
| 374 | RTCPUtility::RtcpCommonHeader header; |
| 375 | const uint8_t* block_begin = packet; |
| 376 | const uint8_t* packet_end = packet + length; |
| 377 | RTC_DCHECK(length <= IP_PACKET_SIZE); |
| 378 | uint8_t buffer[IP_PACKET_SIZE]; |
| 379 | uint32_t buffer_length = 0; |
| 380 | while (block_begin < packet_end) { |
| 381 | if (!RtcpParseCommonHeader(block_begin, packet_end - block_begin, |
| 382 | &header)) { |
| 383 | break; // Incorrect message header. |
| 384 | } |
| 385 | uint32_t block_size = header.BlockSize(); |
| 386 | switch (header.packet_type) { |
| 387 | case RTCPUtility::PT_SR: |
| 388 | FALLTHROUGH(); |
| 389 | case RTCPUtility::PT_RR: |
| 390 | FALLTHROUGH(); |
| 391 | case RTCPUtility::PT_BYE: |
| 392 | FALLTHROUGH(); |
| 393 | case RTCPUtility::PT_IJ: |
| 394 | FALLTHROUGH(); |
| 395 | case RTCPUtility::PT_RTPFB: |
| 396 | FALLTHROUGH(); |
| 397 | case RTCPUtility::PT_PSFB: |
| 398 | FALLTHROUGH(); |
| 399 | case RTCPUtility::PT_XR: |
| 400 | // We log sender reports, receiver reports, bye messages |
| 401 | // inter-arrival jitter, third-party loss reports, payload-specific |
| 402 | // feedback and extended reports. |
| 403 | memcpy(buffer + buffer_length, block_begin, block_size); |
| 404 | buffer_length += block_size; |
| 405 | break; |
| 406 | case RTCPUtility::PT_SDES: |
| 407 | FALLTHROUGH(); |
| 408 | case RTCPUtility::PT_APP: |
| 409 | FALLTHROUGH(); |
| 410 | default: |
| 411 | // We don't log sender descriptions, application defined messages |
| 412 | // or message blocks of unknown type. |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | block_begin += block_size; |
| 417 | } |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 418 | rtcp_event->mutable_rtcp_packet()->set_packet_data(buffer, buffer_length); |
| 419 | if (!event_queue_.Insert(&rtcp_event)) { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 420 | LOG(LS_ERROR) << "RTCP queue full. Not logging RTCP packet."; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 421 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Ivo Creusen | ae856f2 | 2015-09-17 16:30:16 +0200 | [diff] [blame] | 424 | void RtcEventLogImpl::LogAudioPlayout(uint32_t ssrc) { |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 425 | std::unique_ptr<rtclog::Event> event(new rtclog::Event()); |
| 426 | event->set_timestamp_us(clock_->TimeInMicroseconds()); |
| 427 | event->set_type(rtclog::Event::AUDIO_PLAYOUT_EVENT); |
| 428 | auto playout_event = event->mutable_audio_playout_event(); |
Ivo Creusen | 301aaed | 2015-10-08 18:07:41 +0200 | [diff] [blame] | 429 | playout_event->set_local_ssrc(ssrc); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 430 | if (!event_queue_.Insert(&event)) { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 431 | LOG(LS_ERROR) << "Playout queue full. Not logging ACM playout."; |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 432 | } |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 433 | } |
| 434 | |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 435 | void RtcEventLogImpl::LogBwePacketLossEvent(int32_t bitrate, |
| 436 | uint8_t fraction_loss, |
| 437 | int32_t total_packets) { |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 438 | std::unique_ptr<rtclog::Event> event(new rtclog::Event()); |
| 439 | event->set_timestamp_us(clock_->TimeInMicroseconds()); |
| 440 | event->set_type(rtclog::Event::BWE_PACKET_LOSS_EVENT); |
| 441 | auto bwe_event = event->mutable_bwe_packet_loss_event(); |
terelius | 006d93d | 2015-11-05 12:02:15 -0800 | [diff] [blame] | 442 | bwe_event->set_bitrate(bitrate); |
| 443 | bwe_event->set_fraction_loss(fraction_loss); |
| 444 | bwe_event->set_total_packets(total_packets); |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 445 | if (!event_queue_.Insert(&event)) { |
terelius | 43587e3 | 2016-05-27 02:22:51 -0700 | [diff] [blame] | 446 | LOG(LS_ERROR) << "BWE loss queue full. Not logging BWE update."; |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, |
| 451 | rtclog::EventStream* result) { |
| 452 | char tmp_buffer[1024]; |
| 453 | int bytes_read = 0; |
kwiberg | b25345e | 2016-03-12 06:10:44 -0800 | [diff] [blame] | 454 | std::unique_ptr<FileWrapper> dump_file(FileWrapper::Create()); |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 455 | if (dump_file->OpenFile(file_name.c_str(), true) != 0) { |
| 456 | return false; |
| 457 | } |
| 458 | std::string dump_buffer; |
| 459 | while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { |
| 460 | dump_buffer.append(tmp_buffer, bytes_read); |
| 461 | } |
| 462 | dump_file->CloseFile(); |
| 463 | return result->ParseFromString(dump_buffer); |
| 464 | } |
| 465 | |
| 466 | #endif // ENABLE_RTC_EVENT_LOG |
| 467 | |
| 468 | // RtcEventLog member functions. |
terelius | 4311ba5 | 2016-04-22 12:40:37 -0700 | [diff] [blame] | 469 | std::unique_ptr<RtcEventLog> RtcEventLog::Create(const Clock* clock) { |
| 470 | #ifdef ENABLE_RTC_EVENT_LOG |
| 471 | return std::unique_ptr<RtcEventLog>(new RtcEventLogImpl(clock)); |
| 472 | #else |
| 473 | return std::unique_ptr<RtcEventLog>(new RtcEventLogNullImpl()); |
| 474 | #endif // ENABLE_RTC_EVENT_LOG |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 475 | } |
terelius | 1adce14 | 2015-10-16 08:51:08 -0700 | [diff] [blame] | 476 | |
Bjorn Terelius | 3641185 | 2015-07-30 12:45:18 +0200 | [diff] [blame] | 477 | } // namespace webrtc |