| 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/modules/audio_coding/main/acm2/acm_dump.h" | |
| 12 | |
| 13 #include <deque> | |
| 14 | |
| 15 #include "webrtc/base/checks.h" | |
| 16 #include "webrtc/base/thread_annotations.h" | |
| 17 #include "webrtc/system_wrappers/interface/clock.h" | |
| 18 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" | |
| 19 #include "webrtc/system_wrappers/interface/file_wrapper.h" | |
| 20 | |
| 21 #ifdef RTC_AUDIOCODING_DEBUG_DUMP | |
| 22 // Files generated at build-time by the protobuf compiler. | |
| 23 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD | |
| 24 #include "external/webrtc/webrtc/modules/audio_coding/dump.pb.h" | |
| 25 #else | |
| 26 #include "webrtc/audio_coding/dump.pb.h" | |
| 27 #endif | |
| 28 #endif | |
| 29 | |
| 30 namespace webrtc { | |
| 31 | |
| 32 // Noop implementation if flag is not set | |
| 33 #ifndef RTC_AUDIOCODING_DEBUG_DUMP | |
| 34 class AcmDumpImpl final : public AcmDump { | |
| 35 public: | |
| 36 void StartLogging(const std::string& file_name, int duration_ms) override{}; | |
| 37 void LogRtpPacket(bool incoming, | |
| 38 const uint8_t* packet, | |
| 39 size_t length) override{}; | |
| 40 void LogDebugEvent(DebugEvent event_type, | |
| 41 const std::string& event_message) override{}; | |
| 42 void LogDebugEvent(DebugEvent event_type) override{}; | |
| 43 }; | |
| 44 #else | |
| 45 | |
| 46 class AcmDumpImpl final : public AcmDump { | |
| 47 public: | |
| 48 AcmDumpImpl(); | |
| 49 | |
| 50 void StartLogging(const std::string& file_name, int duration_ms) override; | |
| 51 void LogRtpPacket(bool incoming, | |
| 52 const uint8_t* packet, | |
| 53 size_t length) override; | |
| 54 void LogDebugEvent(DebugEvent event_type, | |
| 55 const std::string& event_message) override; | |
| 56 void LogDebugEvent(DebugEvent event_type) override; | |
| 57 | |
| 58 private: | |
| 59 // This function is identical to LogDebugEvent, but requires holding the lock. | |
| 60 void LogDebugEventLocked(DebugEvent event_type, | |
| 61 const std::string& event_message) | |
| 62 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 63 // Stops logging and clears the stored data and buffers. | |
| 64 void Clear() EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 65 // Adds a new event to the logfile if logging is active, or adds it to the | |
| 66 // list of recent log events otherwise. | |
| 67 void HandleEvent(ACMDumpEvent* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 68 // Writes the event to the file. Note that this will destroy the state of the | |
| 69 // input argument. | |
| 70 void StoreToFile(ACMDumpEvent* event) EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 71 // Adds the event to the list of recent events, and removes any events that | |
| 72 // are too old and no longer fall in the time window. | |
| 73 void AddRecentEvent(const ACMDumpEvent& event) | |
| 74 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
| 75 | |
| 76 // Amount of time in microseconds to record log events, before starting the | |
| 77 // actual log. | |
| 78 const int recent_log_duration_us = 10000000; | |
| 79 | |
| 80 rtc::scoped_ptr<webrtc::CriticalSectionWrapper> crit_; | |
| 81 rtc::scoped_ptr<webrtc::FileWrapper> file_ GUARDED_BY(crit_); | |
| 82 rtc::scoped_ptr<ACMDumpEventStream> stream_ GUARDED_BY(crit_); | |
| 83 std::deque<ACMDumpEvent> recent_log_events_ GUARDED_BY(crit_); | |
| 84 bool currently_logging_ GUARDED_BY(crit_); | |
| 85 int64_t start_time_us_ GUARDED_BY(crit_); | |
| 86 int64_t duration_us_ GUARDED_BY(crit_); | |
| 87 const webrtc::Clock* const clock_; | |
| 88 }; | |
| 89 | |
| 90 namespace { | |
| 91 | |
| 92 // Convert from AcmDump's debug event enum (runtime format) to the corresponding | |
| 93 // protobuf enum (serialized format). | |
| 94 ACMDumpDebugEvent_EventType convertDebugEvent(AcmDump::DebugEvent event_type) { | |
| 95 switch (event_type) { | |
| 96 case AcmDump::DebugEvent::kLogStart: | |
| 97 return ACMDumpDebugEvent::LOG_START; | |
| 98 case AcmDump::DebugEvent::kLogEnd: | |
| 99 return ACMDumpDebugEvent::LOG_END; | |
| 100 case AcmDump::DebugEvent::kAudioPlayout: | |
| 101 return ACMDumpDebugEvent::AUDIO_PLAYOUT; | |
| 102 } | |
| 103 return ACMDumpDebugEvent::UNKNOWN_EVENT; | |
| 104 } | |
| 105 | |
| 106 } // Anonymous namespace. | |
| 107 | |
| 108 // AcmDumpImpl member functions. | |
| 109 AcmDumpImpl::AcmDumpImpl() | |
| 110 : crit_(webrtc::CriticalSectionWrapper::CreateCriticalSection()), | |
| 111 file_(webrtc::FileWrapper::Create()), | |
| 112 stream_(new webrtc::ACMDumpEventStream()), | |
| 113 currently_logging_(false), | |
| 114 start_time_us_(0), | |
| 115 duration_us_(0), | |
| 116 clock_(webrtc::Clock::GetRealTimeClock()) { | |
| 117 } | |
| 118 | |
| 119 void AcmDumpImpl::StartLogging(const std::string& file_name, int duration_ms) { | |
| 120 CriticalSectionScoped lock(crit_.get()); | |
| 121 Clear(); | |
| 122 if (file_->OpenFile(file_name.c_str(), false) != 0) { | |
| 123 return; | |
| 124 } | |
| 125 // Add LOG_START event to the recent event list. This call will also remove | |
| 126 // any events that are too old from the recent event list. | |
| 127 LogDebugEventLocked(DebugEvent::kLogStart, ""); | |
| 128 currently_logging_ = true; | |
| 129 start_time_us_ = clock_->TimeInMicroseconds(); | |
| 130 duration_us_ = static_cast<int64_t>(duration_ms) * 1000; | |
| 131 // Write all the recent events to the log file. | |
| 132 for (auto&& event : recent_log_events_) { | |
| 133 StoreToFile(&event); | |
| 134 } | |
| 135 recent_log_events_.clear(); | |
| 136 } | |
| 137 | |
| 138 void AcmDumpImpl::LogRtpPacket(bool incoming, | |
| 139 const uint8_t* packet, | |
| 140 size_t length) { | |
| 141 CriticalSectionScoped lock(crit_.get()); | |
| 142 ACMDumpEvent rtp_event; | |
| 143 const int64_t timestamp = clock_->TimeInMicroseconds(); | |
| 144 rtp_event.set_timestamp_us(timestamp); | |
| 145 rtp_event.set_type(webrtc::ACMDumpEvent::RTP_EVENT); | |
| 146 rtp_event.mutable_packet()->set_direction( | |
| 147 incoming ? ACMDumpRTPPacket::INCOMING : ACMDumpRTPPacket::OUTGOING); | |
| 148 rtp_event.mutable_packet()->set_rtp_data(packet, length); | |
| 149 HandleEvent(&rtp_event); | |
| 150 } | |
| 151 | |
| 152 void AcmDumpImpl::LogDebugEvent(DebugEvent event_type, | |
| 153 const std::string& event_message) { | |
| 154 CriticalSectionScoped lock(crit_.get()); | |
| 155 LogDebugEventLocked(event_type, event_message); | |
| 156 } | |
| 157 | |
| 158 void AcmDumpImpl::LogDebugEvent(DebugEvent event_type) { | |
| 159 CriticalSectionScoped lock(crit_.get()); | |
| 160 LogDebugEventLocked(event_type, ""); | |
| 161 } | |
| 162 | |
| 163 void AcmDumpImpl::LogDebugEventLocked(DebugEvent event_type, | |
| 164 const std::string& event_message) { | |
| 165 ACMDumpEvent event; | |
| 166 int64_t timestamp = clock_->TimeInMicroseconds(); | |
| 167 event.set_timestamp_us(timestamp); | |
| 168 event.set_type(webrtc::ACMDumpEvent::DEBUG_EVENT); | |
| 169 auto debug_event = event.mutable_debug_event(); | |
| 170 debug_event->set_type(convertDebugEvent(event_type)); | |
| 171 debug_event->set_message(event_message); | |
| 172 HandleEvent(&event); | |
| 173 } | |
| 174 | |
| 175 void AcmDumpImpl::Clear() { | |
| 176 if (file_->Open()) { | |
| 177 file_->CloseFile(); | |
| 178 } | |
| 179 currently_logging_ = false; | |
| 180 stream_->Clear(); | |
| 181 } | |
| 182 | |
| 183 void AcmDumpImpl::HandleEvent(ACMDumpEvent* event) { | |
| 184 if (currently_logging_) { | |
| 185 if (clock_->TimeInMicroseconds() < start_time_us_ + duration_us_) { | |
| 186 StoreToFile(event); | |
| 187 } else { | |
| 188 LogDebugEventLocked(DebugEvent::kLogEnd, ""); | |
| 189 Clear(); | |
| 190 AddRecentEvent(*event); | |
| 191 } | |
| 192 } else { | |
| 193 AddRecentEvent(*event); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 void AcmDumpImpl::StoreToFile(ACMDumpEvent* event) { | |
| 198 // Reuse the same object at every log event. | |
| 199 if (stream_->stream_size() < 1) { | |
| 200 stream_->add_stream(); | |
| 201 } | |
| 202 DCHECK_EQ(stream_->stream_size(), 1); | |
| 203 stream_->mutable_stream(0)->Swap(event); | |
| 204 | |
| 205 std::string dump_buffer; | |
| 206 stream_->SerializeToString(&dump_buffer); | |
| 207 file_->Write(dump_buffer.data(), dump_buffer.size()); | |
| 208 } | |
| 209 | |
| 210 void AcmDumpImpl::AddRecentEvent(const ACMDumpEvent& event) { | |
| 211 recent_log_events_.push_back(event); | |
| 212 while (recent_log_events_.front().timestamp_us() < | |
| 213 event.timestamp_us() - recent_log_duration_us) { | |
| 214 recent_log_events_.pop_front(); | |
| 215 } | |
| 216 } | |
| 217 | |
| 218 bool AcmDump::ParseAcmDump(const std::string& file_name, | |
| 219 ACMDumpEventStream* result) { | |
| 220 char tmp_buffer[1024]; | |
| 221 int bytes_read = 0; | |
| 222 rtc::scoped_ptr<FileWrapper> dump_file(FileWrapper::Create()); | |
| 223 if (dump_file->OpenFile(file_name.c_str(), true) != 0) { | |
| 224 return false; | |
| 225 } | |
| 226 std::string dump_buffer; | |
| 227 while ((bytes_read = dump_file->Read(tmp_buffer, sizeof(tmp_buffer))) > 0) { | |
| 228 dump_buffer.append(tmp_buffer, bytes_read); | |
| 229 } | |
| 230 dump_file->CloseFile(); | |
| 231 return result->ParseFromString(dump_buffer); | |
| 232 } | |
| 233 | |
| 234 #endif // RTC_AUDIOCODING_DEBUG_DUMP | |
| 235 | |
| 236 // AcmDump member functions. | |
| 237 rtc::scoped_ptr<AcmDump> AcmDump::Create() { | |
| 238 return rtc::scoped_ptr<AcmDump>(new AcmDumpImpl()); | |
| 239 } | |
| 240 } // namespace webrtc | |
| OLD | NEW |