Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/media/webrtc_rtp_dump_handler.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "base/time/time.h" | |
| 11 #include "chrome/browser/media/webrtc_rtp_dump_writer.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 | |
| 14 using content::BrowserThread; | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 static const size_t kMaxOngoingRtpDumpsAllowed = 5; | |
| 19 | |
| 20 // The browser process wide total number of ongoing (i.e. started and not | |
| 21 // released) RTP dumps. Incoming and outgoing in one WebRtcDumpHandler are | |
| 22 // counted as one dump. | |
| 23 // Must be accessed on the browser IO thread. | |
| 24 static size_t g_ongoing_rtp_dumps = 0; | |
| 25 | |
| 26 void FireGenericDoneCallback( | |
| 27 const WebRtcRtpDumpHandler::GenericDoneCallback& callback, | |
| 28 bool success, | |
| 29 const std::string& error_message) { | |
| 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 31 DCHECK(!callback.is_null()); | |
| 32 | |
| 33 content::BrowserThread::PostTask( | |
| 34 content::BrowserThread::UI, | |
| 35 FROM_HERE, | |
| 36 base::Bind(callback, success, error_message)); | |
| 37 } | |
| 38 | |
| 39 bool DumpTypeContainsIncoming(RtpDumpType type) { | |
| 40 return type == RTP_DUMP_INCOMING || type == RTP_DUMP_BOTH; | |
| 41 } | |
| 42 | |
| 43 bool DumpTypeContainsOutgoing(RtpDumpType type) { | |
| 44 return type == RTP_DUMP_OUTGOING || type == RTP_DUMP_BOTH; | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 WebRtcRtpDumpHandler::WebRtcRtpDumpHandler(const base::FilePath& dump_dir) | |
| 50 : dump_dir_(dump_dir), | |
| 51 incoming_state_(STATE_NONE), | |
| 52 outgoing_state_(STATE_NONE), | |
| 53 weak_ptr_factory_(this) { | |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 55 } | |
| 56 | |
| 57 WebRtcRtpDumpHandler::~WebRtcRtpDumpHandler() { | |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 59 | |
| 60 // Reset dump writer first to stop writing. | |
| 61 if (dump_writer_) { | |
| 62 --g_ongoing_rtp_dumps; | |
| 63 dump_writer_.reset(); | |
| 64 } | |
| 65 | |
| 66 if (incoming_state_ != STATE_NONE && !incoming_dump_path_.empty()) { | |
| 67 BrowserThread::PostTask( | |
| 68 BrowserThread::FILE, | |
| 69 FROM_HERE, | |
| 70 base::Bind( | |
| 71 base::IgnoreResult(&base::DeleteFile), incoming_dump_path_, false)); | |
| 72 } | |
| 73 | |
| 74 if (outgoing_state_ != STATE_NONE && !outgoing_dump_path_.empty()) { | |
| 75 BrowserThread::PostTask( | |
| 76 BrowserThread::FILE, | |
| 77 FROM_HERE, | |
| 78 base::Bind( | |
| 79 base::IgnoreResult(&base::DeleteFile), outgoing_dump_path_, false)); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 bool WebRtcRtpDumpHandler::StartDump(RtpDumpType type, | |
| 84 std::string* error_message) { | |
| 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 86 | |
| 87 if (!dump_writer_ && g_ongoing_rtp_dumps >= kMaxOngoingRtpDumpsAllowed) { | |
| 88 *error_message = "Max RTP dump limit reached."; | |
| 89 DVLOG(2) << *error_message; | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 // Returns an error if any type of dump specified by the caller cannot be | |
| 94 // started. | |
| 95 if ((DumpTypeContainsIncoming(type) && incoming_state_ != STATE_NONE) || | |
| 96 (DumpTypeContainsOutgoing(type) && outgoing_state_ != STATE_NONE)) { | |
| 97 *error_message = "RTP dump already started."; | |
|
vrk (LEFT CHROMIUM)
2014/05/28 23:55:07
Add type to the error message, like "RTP dump alre
| |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 if (DumpTypeContainsIncoming(type)) | |
| 102 incoming_state_ = STATE_STARTED; | |
| 103 | |
| 104 if (DumpTypeContainsOutgoing(type)) | |
| 105 outgoing_state_ = STATE_STARTED; | |
| 106 | |
| 107 DVLOG(2) << "Start RTP dumping: type = " << type; | |
| 108 | |
| 109 if (!dump_writer_) { | |
| 110 ++g_ongoing_rtp_dumps; | |
| 111 | |
| 112 static const char kRecvDumpFilePrefix[] = "rtpdump_recv_"; | |
| 113 static const char kSendDumpFilePrefix[] = "rtpdump_send_"; | |
| 114 static const size_t kMaxDumpSize = 5 * 1024 * 1024; // 5MB | |
| 115 | |
| 116 std::string dump_id = base::DoubleToString(base::Time::Now().ToDoubleT()); | |
| 117 incoming_dump_path_ = | |
| 118 dump_dir_.AppendASCII(std::string(kRecvDumpFilePrefix) + dump_id) | |
| 119 .AddExtension(FILE_PATH_LITERAL(".gz")); | |
| 120 | |
| 121 outgoing_dump_path_ = | |
| 122 dump_dir_.AppendASCII(std::string(kSendDumpFilePrefix) + dump_id) | |
| 123 .AddExtension(FILE_PATH_LITERAL(".gz")); | |
| 124 | |
| 125 // WebRtcRtpDumpWriter does not support changing the dump path after it's | |
| 126 // created. So we assign both incoming and outgoing dump path even if only | |
| 127 // one type of dumping has been started. | |
| 128 // For "Unretained(this)", see comments StopDump. | |
| 129 dump_writer_.reset(new WebRtcRtpDumpWriter( | |
| 130 incoming_dump_path_, | |
| 131 outgoing_dump_path_, | |
| 132 kMaxDumpSize, | |
| 133 base::Bind(&WebRtcRtpDumpHandler::OnMaxDumpSizeReached, | |
| 134 base::Unretained(this)))); | |
| 135 } | |
| 136 | |
| 137 return true; | |
| 138 } | |
| 139 | |
| 140 void WebRtcRtpDumpHandler::StopDump(RtpDumpType type, | |
| 141 const GenericDoneCallback& callback) { | |
| 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 143 | |
| 144 // Returns an error if any type of dump specified by the caller cannot be | |
| 145 // stopped. | |
| 146 if ((DumpTypeContainsIncoming(type) && incoming_state_ != STATE_STARTED) || | |
| 147 (DumpTypeContainsOutgoing(type) && outgoing_state_ != STATE_STARTED)) { | |
| 148 if (!callback.is_null()) { | |
| 149 FireGenericDoneCallback( | |
| 150 callback, false, "RTP dump not started or already stopped."); | |
| 151 } | |
| 152 return; | |
| 153 } | |
| 154 | |
| 155 DVLOG(2) << "Stopping RTP dumping: type = " << type; | |
| 156 | |
| 157 if (DumpTypeContainsIncoming(type)) | |
| 158 incoming_state_ = STATE_STOPPING; | |
| 159 | |
| 160 if (DumpTypeContainsOutgoing(type)) | |
| 161 outgoing_state_ = STATE_STOPPING; | |
| 162 | |
| 163 // Using "Unretained(this)" because the this object owns the writer and the | |
| 164 // writer is guaranteed to cancel the callback before it goes away. Same for | |
| 165 // the other posted tasks bound to the writer. | |
| 166 dump_writer_->EndDump( | |
| 167 type, | |
| 168 base::Bind(&WebRtcRtpDumpHandler::OnDumpEnded, | |
| 169 base::Unretained(this), | |
| 170 callback.is_null() | |
| 171 ? base::Closure() | |
| 172 : base::Bind(&FireGenericDoneCallback, callback, true, ""), | |
| 173 type)); | |
| 174 } | |
| 175 | |
| 176 bool WebRtcRtpDumpHandler::ReadyToRelease() const { | |
| 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 178 | |
| 179 return incoming_state_ != STATE_STARTED && | |
| 180 incoming_state_ != STATE_STOPPING && | |
| 181 outgoing_state_ != STATE_STARTED && outgoing_state_ != STATE_STOPPING; | |
| 182 } | |
| 183 | |
| 184 WebRtcRtpDumpHandler::ReleasedDumps WebRtcRtpDumpHandler::ReleaseDumps() { | |
| 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 186 DCHECK(ReadyToRelease()); | |
| 187 | |
| 188 base::FilePath incoming_dump, outgoing_dump; | |
| 189 | |
| 190 if (incoming_state_ == STATE_STOPPED) { | |
| 191 DVLOG(2) << "Incoming RTP dumps released: " << incoming_dump_path_.value(); | |
| 192 | |
| 193 incoming_state_ = STATE_NONE; | |
| 194 incoming_dump = incoming_dump_path_; | |
| 195 } | |
| 196 | |
| 197 if (outgoing_state_ == STATE_STOPPED) { | |
| 198 DVLOG(2) << "Outgoing RTP dumps released: " << outgoing_dump_path_.value(); | |
| 199 | |
| 200 outgoing_state_ = STATE_NONE; | |
| 201 outgoing_dump = outgoing_dump_path_; | |
| 202 } | |
| 203 return ReleasedDumps(incoming_dump, outgoing_dump); | |
| 204 } | |
| 205 | |
| 206 void WebRtcRtpDumpHandler::OnRtpPacket(const uint8* packet_header, | |
| 207 size_t header_length, | |
| 208 size_t packet_length, | |
| 209 bool incoming) { | |
| 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 211 | |
| 212 if ((incoming && incoming_state_ != STATE_STARTED) || | |
| 213 (!incoming && outgoing_state_ != STATE_STARTED)) { | |
| 214 return; | |
| 215 } | |
| 216 | |
| 217 dump_writer_->WriteRtpPacket( | |
| 218 packet_header, header_length, packet_length, incoming); | |
| 219 } | |
| 220 | |
| 221 void WebRtcRtpDumpHandler::StopOngoingDumps(const base::Closure& callback) { | |
| 222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 223 DCHECK(!callback.is_null()); | |
| 224 | |
| 225 // No ongoing dumps, return directly. | |
| 226 if ((incoming_state_ == STATE_NONE || incoming_state_ == STATE_STOPPED) && | |
| 227 (outgoing_state_ == STATE_NONE || outgoing_state_ == STATE_STOPPED)) { | |
| 228 callback.Run(); | |
| 229 return; | |
| 230 } | |
| 231 | |
| 232 // If the FILE thread is working on stopping the dumps, wait for the FILE | |
| 233 // thread to return and check the states again. | |
| 234 if (incoming_state_ == STATE_STOPPING || outgoing_state_ == STATE_STOPPING) { | |
| 235 BrowserThread::PostTaskAndReply( | |
| 236 BrowserThread::FILE, | |
| 237 FROM_HERE, | |
| 238 base::Bind(&base::DoNothing), | |
| 239 base::Bind(&WebRtcRtpDumpHandler::StopOngoingDumps, | |
| 240 weak_ptr_factory_.GetWeakPtr(), | |
| 241 callback)); | |
| 242 return; | |
| 243 } | |
| 244 | |
| 245 // Either incoming or outgoing dump must be ongoing. | |
| 246 RtpDumpType type = | |
| 247 (incoming_state_ == STATE_STARTED) | |
| 248 ? (outgoing_state_ == STATE_STARTED ? RTP_DUMP_BOTH | |
| 249 : RTP_DUMP_INCOMING) | |
| 250 : RTP_DUMP_OUTGOING; | |
| 251 | |
| 252 if (incoming_state_ == STATE_STARTED) | |
| 253 incoming_state_ = STATE_STOPPING; | |
| 254 | |
| 255 if (outgoing_state_ == STATE_STARTED) | |
| 256 outgoing_state_ = STATE_STOPPING; | |
| 257 | |
| 258 DVLOG(2) << "Stopping ongoing dumps: type = " << type; | |
| 259 | |
| 260 dump_writer_->EndDump(type, | |
| 261 base::Bind(&WebRtcRtpDumpHandler::OnDumpEnded, | |
| 262 base::Unretained(this), | |
| 263 callback, | |
| 264 type)); | |
| 265 } | |
| 266 | |
| 267 void WebRtcRtpDumpHandler::SetDumpWriterForTesting( | |
| 268 scoped_ptr<WebRtcRtpDumpWriter> writer) { | |
| 269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 270 | |
| 271 dump_writer_ = writer.Pass(); | |
| 272 ++g_ongoing_rtp_dumps; | |
| 273 | |
| 274 incoming_dump_path_ = dump_dir_.AppendASCII("recv"); | |
| 275 outgoing_dump_path_ = dump_dir_.AppendASCII("send"); | |
| 276 } | |
| 277 | |
| 278 void WebRtcRtpDumpHandler::OnMaxDumpSizeReached() { | |
| 279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 280 | |
| 281 RtpDumpType type = | |
| 282 (incoming_state_ == STATE_STARTED) | |
| 283 ? (outgoing_state_ == STATE_STARTED ? RTP_DUMP_BOTH | |
| 284 : RTP_DUMP_INCOMING) | |
| 285 : RTP_DUMP_OUTGOING; | |
| 286 StopDump(type, GenericDoneCallback()); | |
| 287 } | |
| 288 | |
| 289 void WebRtcRtpDumpHandler::OnDumpEnded(const base::Closure& callback, | |
| 290 RtpDumpType ended_type, | |
| 291 bool incoming_success, | |
| 292 bool outgoing_success) { | |
| 293 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 294 | |
| 295 if (DumpTypeContainsIncoming(ended_type)) { | |
| 296 DCHECK_EQ(STATE_STOPPING, incoming_state_); | |
| 297 incoming_state_ = STATE_STOPPED; | |
| 298 | |
| 299 if (!incoming_success) { | |
| 300 BrowserThread::PostTask(BrowserThread::FILE, | |
| 301 FROM_HERE, | |
| 302 base::Bind(base::IgnoreResult(&base::DeleteFile), | |
| 303 incoming_dump_path_, | |
| 304 false)); | |
| 305 | |
| 306 DVLOG(2) << "Deleted invalid incoming dump " | |
| 307 << incoming_dump_path_.value(); | |
| 308 incoming_dump_path_.clear(); | |
| 309 } | |
| 310 } | |
| 311 | |
| 312 if (DumpTypeContainsOutgoing(ended_type)) { | |
| 313 DCHECK_EQ(STATE_STOPPING, outgoing_state_); | |
| 314 outgoing_state_ = STATE_STOPPED; | |
| 315 | |
| 316 if (!outgoing_success) { | |
| 317 BrowserThread::PostTask(BrowserThread::FILE, | |
| 318 FROM_HERE, | |
| 319 base::Bind(base::IgnoreResult(&base::DeleteFile), | |
| 320 outgoing_dump_path_, | |
| 321 false)); | |
| 322 | |
| 323 DVLOG(2) << "Deleted invalid outgoing dump " | |
| 324 << outgoing_dump_path_.value(); | |
| 325 outgoing_dump_path_.clear(); | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 if (!callback.is_null()) | |
| 330 callback.Run(); | |
| 331 | |
| 332 // Release the writer when it's no longer needed. | |
| 333 if (incoming_state_ != STATE_STOPPING && outgoing_state_ != STATE_STOPPING && | |
| 334 incoming_state_ != STATE_STARTED && outgoing_state_ != STATE_STARTED) { | |
| 335 dump_writer_.reset(); | |
| 336 --g_ongoing_rtp_dumps; | |
| 337 } | |
| 338 } | |
| OLD | NEW |