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 // No-op implementation if flag is not set. | |
34 #ifndef ENABLE_RTC_EVENT_LOG | |
35 class RtcEventLogImpl final : public RtcEventLog { | |
36 public: | |
37 void StartLogging(const std::string& file_name, int duration_ms) override{}; | |
38 void LogVideoReceiveStreamConfig( | |
39 const VideoReceiveStream::Config& config) override{}; | |
40 void LogVideoSendStreamConfig( | |
41 const VideoSendStream::Config& config) override{}; | |
42 void LogRtpHeader(bool incoming, | |
43 MediaType media_type, | |
44 const uint8_t* header, | |
45 size_t header_length, | |
46 size_t total_length) override{}; | |
47 void LogRtcpPacket(bool incoming, | |
48 MediaType media_type, | |
49 const uint8_t* packet, | |
50 size_t length) override{}; | |
51 void LogDebugEvent(DebugEvent event_type) override{}; | |
52 }; | |
53 #else | |
54 | |
55 class RtcEventLogImpl final : public RtcEventLog { | |
56 public: | |
57 RtcEventLogImpl(); | |
58 | |
59 void StartLogging(const std::string& file_name, int duration_ms) override; | |
60 void LogVideoReceiveStreamConfig( | |
61 const VideoReceiveStream::Config& config) override; | |
62 void LogVideoSendStreamConfig(const VideoSendStream::Config& config) override; | |
63 void LogRtpHeader(bool incoming, | |
64 MediaType media_type, | |
65 const uint8_t* header, | |
66 size_t header_length, | |
67 size_t total_length) override; | |
68 void LogRtcpPacket(bool incoming, | |
69 MediaType media_type, | |
70 const uint8_t* packet, | |
71 size_t length) override; | |
72 void LogDebugEvent(DebugEvent event_type) override; | |
73 | |
74 private: | |
75 // This function is identical to LogDebugEvent, but requires holding the lock. | |
76 void LogDebugEventLocked(DebugEvent event_type) | |
pbos-webrtc
2015/07/24 11:43:40
Can this be consolidated into LogDebugEvent as mes
terelius
2015/07/27 08:27:34
Done. This required some refactoring of related fu
| |
77 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
78 // Stops logging and clears the stored data and buffers. | |
79 void Clear() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
80 // Adds a new event to the logfile if logging is active, or adds it to the | |
81 // list of recent log events otherwise. | |
82 void HandleEvent(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
83 // Writes the event to the file. Note that this will destroy the state of the | |
84 // input argument. | |
85 void StoreToFile(rtclog::Event* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
86 // Adds the event to the list of recent events, and removes any events that | |
87 // are too old and no longer fall in the time window. | |
88 void AddRecentEvent(const rtclog::Event& event) | |
89 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
90 | |
91 // Amount of time in microseconds to record log events, before starting the | |
92 // actual log. | |
93 const int recent_log_duration_us = 10000000; | |
94 | |
95 rtc::CriticalSection crit_; | |
96 rtc::scoped_ptr<FileWrapper> file_ GUARDED_BY(crit_); | |
97 rtc::scoped_ptr<rtclog::EventStream> stream_ GUARDED_BY(crit_); | |
pbos-webrtc
2015/07/24 11:43:40
Why scoped_ptr? Can you use rtclog::EventStream st
terelius
2015/07/27 08:27:34
Done. Removed scoped_ptr.
| |
98 std::deque<rtclog::Event> recent_log_events_ GUARDED_BY(crit_); | |
99 bool currently_logging_ GUARDED_BY(crit_); | |
100 int64_t start_time_us_ GUARDED_BY(crit_); | |
101 int64_t duration_us_ GUARDED_BY(crit_); | |
102 const Clock* const clock_; | |
103 }; | |
104 | |
105 namespace { | |
106 // The functions in this namespace convert enums from the runtime format | |
107 // that the rest of the WebRtc project can use, to the corresponding | |
108 // serialized enum which is defined by the protobuf. | |
109 | |
110 // Do not add default return values to the conversion functions in this | |
111 // unnamed namespace. The intention is to make the compiler warn if anyone | |
112 // adds unhandled new events/modes/etc. | |
113 | |
114 rtclog::DebugEvent_EventType ConvertDebugEvent( | |
115 RtcEventLog::DebugEvent event_type) { | |
116 switch (event_type) { | |
117 case RtcEventLog::DebugEvent::kLogStart: | |
118 return rtclog::DebugEvent::LOG_START; | |
119 case RtcEventLog::DebugEvent::kLogEnd: | |
120 return rtclog::DebugEvent::LOG_END; | |
121 case RtcEventLog::DebugEvent::kAudioPlayout: | |
122 return rtclog::DebugEvent::AUDIO_PLAYOUT; | |
123 } | |
124 RTC_NOTREACHED(); | |
125 return rtclog::DebugEvent::UNKNOWN_EVENT; | |
126 } | |
127 | |
128 rtclog::VideoReceiveConfig_RtcpMode ConvertRtcpMode( | |
129 newapi::RtcpMode rtcp_mode) { | |
130 switch (rtcp_mode) { | |
131 case newapi::kRtcpCompound: | |
132 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; | |
133 case newapi::kRtcpReducedSize: | |
134 return rtclog::VideoReceiveConfig::RTCP_REDUCEDSIZE; | |
135 } | |
136 RTC_NOTREACHED(); | |
137 return rtclog::VideoReceiveConfig::RTCP_COMPOUND; | |
138 } | |
139 | |
140 rtclog::MediaType ConvertMediaType(MediaType media_type) { | |
141 switch (media_type) { | |
142 case MediaType::ANY: | |
143 return rtclog::MediaType::ANY; | |
144 case MediaType::AUDIO: | |
145 return rtclog::MediaType::AUDIO; | |
146 case MediaType::VIDEO: | |
147 return rtclog::MediaType::VIDEO; | |
148 case MediaType::DATA: | |
149 return rtclog::MediaType::DATA; | |
150 } | |
151 RTC_NOTREACHED(); | |
152 return rtclog::ANY; | |
153 } | |
154 | |
155 } // Anonymous namespace. | |
156 | |
157 // RtcEventLogImpl member functions. | |
158 RtcEventLogImpl::RtcEventLogImpl() | |
159 : file_(FileWrapper::Create()), | |
160 stream_(new rtclog::EventStream()), | |
161 currently_logging_(false), | |
162 start_time_us_(0), | |
163 duration_us_(0), | |
164 clock_(Clock::GetRealTimeClock()) { | |
165 } | |
166 | |
167 void RtcEventLogImpl::StartLogging(const std::string& file_name, | |
168 int duration_ms) { | |
169 rtc::CritScope lock(&crit_); | |
170 Clear(); | |
171 if (file_->OpenFile(file_name.c_str(), false) != 0) { | |
172 return; | |
173 } | |
174 | |
175 // Add LOG_START event to the recent event list. This call will also remove | |
176 // any events that are too old from the recent event list. | |
177 LogDebugEventLocked(DebugEvent::kLogStart); | |
178 currently_logging_ = true; | |
179 start_time_us_ = clock_->TimeInMicroseconds(); | |
180 duration_us_ = static_cast<int64_t>(duration_ms) * 1000; | |
181 // Write all the recent events to the log file. | |
182 for (auto& event : recent_log_events_) { | |
183 StoreToFile(&event); | |
184 } | |
185 recent_log_events_.clear(); | |
186 } | |
187 | |
188 void RtcEventLogImpl::LogVideoReceiveStreamConfig( | |
189 const VideoReceiveStream::Config& config) { | |
190 rtc::CritScope lock(&crit_); | |
191 | |
192 rtclog::Event event; | |
193 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
194 event.set_timestamp_us(timestamp); | |
195 event.set_type(rtclog::Event::VIDEO_RECEIVER_CONFIG_EVENT); | |
196 | |
197 rtclog::VideoReceiveConfig* receiver_config = | |
198 event.mutable_video_receiver_config(); | |
199 receiver_config->set_remote_ssrc(config.rtp.remote_ssrc); | |
200 receiver_config->set_local_ssrc(config.rtp.local_ssrc); | |
201 | |
202 receiver_config->set_rtcp_mode(ConvertRtcpMode(config.rtp.rtcp_mode)); | |
203 | |
204 receiver_config->set_receiver_reference_time_report( | |
205 config.rtp.rtcp_xr.receiver_reference_time_report); | |
206 receiver_config->set_remb(config.rtp.remb); | |
207 | |
208 for (const auto& kv : config.rtp.rtx) { | |
209 rtclog::RtxMap* rtx = receiver_config->add_rtx_map(); | |
210 rtx->set_payload_type(kv.first); | |
211 rtx->mutable_config()->set_rtx_ssrc(kv.second.ssrc); | |
212 rtx->mutable_config()->set_rtx_payload_type(kv.second.payload_type); | |
213 } | |
214 | |
215 for (const auto& e : config.rtp.extensions) { | |
216 rtclog::RtpHeaderExtension* extension = | |
217 receiver_config->add_header_extensions(); | |
218 extension->set_name(e.name); | |
219 extension->set_id(e.id); | |
220 } | |
221 | |
222 for (const auto& d : config.decoders) { | |
223 rtclog::DecoderConfig* decoder = receiver_config->add_decoders(); | |
224 decoder->set_name(d.payload_name); | |
225 decoder->set_payload_type(d.payload_type); | |
226 } | |
227 // TODO(terelius): We should use a separate event queue for config events. | |
228 // The current approach of storing the configuration together with the | |
229 // RTP events causes the configuration information to be removed 10s | |
230 // after the ReceiveStream is created. | |
231 HandleEvent(&event); | |
232 } | |
233 | |
234 void RtcEventLogImpl::LogVideoSendStreamConfig( | |
235 const VideoSendStream::Config& config) { | |
236 rtc::CritScope lock(&crit_); | |
237 | |
238 rtclog::Event event; | |
239 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
240 event.set_timestamp_us(timestamp); | |
241 event.set_type(rtclog::Event::VIDEO_SENDER_CONFIG_EVENT); | |
242 | |
243 rtclog::VideoSendConfig* sender_config = event.mutable_video_sender_config(); | |
244 | |
245 for (const auto& ssrc : config.rtp.ssrcs) { | |
246 sender_config->add_ssrcs(ssrc); | |
247 } | |
248 | |
249 for (const auto& e : config.rtp.extensions) { | |
250 rtclog::RtpHeaderExtension* extension = | |
251 sender_config->add_header_extensions(); | |
252 extension->set_name(e.name); | |
253 extension->set_id(e.id); | |
254 } | |
255 | |
256 for (const auto& rtx_ssrc : config.rtp.rtx.ssrcs) { | |
257 sender_config->add_rtx_ssrcs(rtx_ssrc); | |
258 } | |
259 sender_config->set_rtx_payload_type(config.rtp.rtx.payload_type); | |
260 | |
261 sender_config->set_c_name(config.rtp.c_name); | |
262 | |
263 rtclog::EncoderConfig* encoder = sender_config->mutable_encoder(); | |
264 encoder->set_name(config.encoder_settings.payload_name); | |
265 encoder->set_payload_type(config.encoder_settings.payload_type); | |
266 | |
267 // TODO(terelius): We should use a separate event queue for config events. | |
268 // The current approach of storing the configuration together with the | |
269 // RTP events causes the configuration information to be removed 10s | |
270 // after the ReceiveStream is created. | |
271 HandleEvent(&event); | |
272 } | |
273 | |
274 // TODO(terelius): It is more convenient and less error prone to parse the | |
pbos-webrtc
2015/07/24 11:43:40
Not sure I agree, packet parsing inside here will
terelius
2015/07/27 08:27:34
We don't need to know which extensions are used. T
| |
275 // header length from the packet instead of relying on the caller to provide it. | |
276 void RtcEventLogImpl::LogRtpHeader(bool incoming, | |
277 MediaType media_type, | |
278 const uint8_t* header, | |
279 size_t header_length, | |
280 size_t total_length) { | |
281 rtc::CritScope lock(&crit_); | |
282 rtclog::Event rtp_event; | |
283 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
284 rtp_event.set_timestamp_us(timestamp); | |
285 rtp_event.set_type(rtclog::Event::RTP_EVENT); | |
286 rtp_event.mutable_rtp_packet()->set_incoming(incoming); | |
287 rtp_event.mutable_rtp_packet()->set_type(ConvertMediaType(media_type)); | |
288 rtp_event.mutable_rtp_packet()->set_packet_length(total_length); | |
289 rtp_event.mutable_rtp_packet()->set_header(header, header_length); | |
290 HandleEvent(&rtp_event); | |
291 } | |
292 | |
293 void RtcEventLogImpl::LogRtcpPacket(bool incoming, | |
294 MediaType media_type, | |
295 const uint8_t* packet, | |
296 size_t length) { | |
297 rtc::CritScope lock(&crit_); | |
298 rtclog::Event rtcp_event; | |
299 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
300 rtcp_event.set_timestamp_us(timestamp); | |
301 rtcp_event.set_type(rtclog::Event::RTCP_EVENT); | |
302 rtcp_event.mutable_rtcp_packet()->set_incoming(incoming); | |
303 rtcp_event.mutable_rtcp_packet()->set_type(ConvertMediaType(media_type)); | |
304 rtcp_event.mutable_rtcp_packet()->set_packet_data(packet, length); | |
305 HandleEvent(&rtcp_event); | |
306 } | |
307 | |
308 | |
309 void RtcEventLogImpl::LogDebugEvent(DebugEvent event_type) { | |
310 rtc::CritScope lock(&crit_); | |
311 LogDebugEventLocked(event_type); | |
312 } | |
313 | |
314 void RtcEventLogImpl::LogDebugEventLocked(DebugEvent event_type) { | |
315 rtclog::Event event; | |
316 int64_t timestamp = clock_->TimeInMicroseconds(); | |
317 event.set_timestamp_us(timestamp); | |
318 event.set_type(rtclog::Event::DEBUG_EVENT); | |
319 auto debug_event = event.mutable_debug_event(); | |
320 debug_event->set_type(ConvertDebugEvent(event_type)); | |
321 HandleEvent(&event); | |
322 } | |
323 | |
324 void RtcEventLogImpl::Clear() { | |
325 if (file_->Open()) { | |
326 file_->CloseFile(); | |
327 } | |
328 currently_logging_ = false; | |
329 stream_->Clear(); | |
330 } | |
331 | |
332 void RtcEventLogImpl::HandleEvent(rtclog::Event* event) { | |
333 if (currently_logging_) { | |
334 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) { | |
335 StoreToFile(event); | |
336 } else { | |
337 LogDebugEventLocked(DebugEvent::kLogEnd); | |
338 Clear(); | |
339 AddRecentEvent(*event); | |
340 } | |
341 } else { | |
342 AddRecentEvent(*event); | |
343 } | |
344 } | |
345 | |
346 void RtcEventLogImpl::StoreToFile(rtclog::Event* event) { | |
347 // Reuse the same object at every log event. | |
348 if (stream_->stream_size() < 1) { | |
349 stream_->add_stream(); | |
350 } | |
351 DCHECK_EQ(stream_->stream_size(), 1); | |
352 stream_->mutable_stream(0)->Swap(event); | |
353 | |
354 std::string dump_buffer; | |
355 stream_->SerializeToString(&dump_buffer); | |
356 file_->Write(dump_buffer.data(), dump_buffer.size()); | |
357 } | |
358 | |
359 void RtcEventLogImpl::AddRecentEvent(const rtclog::Event& event) { | |
360 recent_log_events_.push_back(event); | |
361 while (recent_log_events_.front().timestamp_us() < | |
362 event.timestamp_us() - recent_log_duration_us) { | |
363 recent_log_events_.pop_front(); | |
364 } | |
365 } | |
366 | |
367 bool RtcEventLog::ParseRtcEventLog(const std::string& file_name, | |
368 rtclog::EventStream* result) { | |
369 char tmp_buffer[1024]; | |
370 int bytes_read = 0; | |
371 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create()); | |
372 if (dump_file->OpenFile(file_name.c_str(), true) != 0) { | |
373 return false; | |
374 } | |
375 std::string dump_buffer; | |
376 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { | |
377 dump_buffer.append(tmp_buffer, bytes_read); | |
378 } | |
379 dump_file->CloseFile(); | |
380 return result->ParseFromString(dump_buffer); | |
381 } | |
382 | |
383 #endif // ENABLE_RTC_EVENT_LOG | |
384 | |
385 // RtcEventLog member functions. | |
386 rtc::scoped_ptr<RtcEventLog> RtcEventLog::Create() { | |
387 return rtc::scoped_ptr<RtcEventLog>(new RtcEventLogImpl()); | |
388 } | |
389 } // namespace webrtc | |
OLD | NEW |