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 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 54 } | |
| 55 | |
| 56 WebRtcRtpDumpHandler::~WebRtcRtpDumpHandler() { | |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 58 | |
| 59 // Reset dump writer first to stop writing. | |
| 60 if (dump_writer_) { | |
| 61 --g_ongoing_rtp_dumps; | |
| 62 dump_writer_.reset(); | |
| 63 } | |
| 64 | |
| 65 if (incoming_state_ != STATE_NONE && !incoming_dump_path_.empty()) { | |
| 66 BrowserThread::PostTask( | |
| 67 BrowserThread::FILE, | |
| 68 FROM_HERE, | |
| 69 base::Bind( | |
| 70 base::IgnoreResult(&base::DeleteFile), incoming_dump_path_, false)); | |
| 71 } | |
| 72 | |
| 73 if (outgoing_state_ != STATE_NONE && !outgoing_dump_path_.empty()) { | |
| 74 BrowserThread::PostTask( | |
| 75 BrowserThread::FILE, | |
| 76 FROM_HERE, | |
| 77 base::Bind( | |
| 78 base::IgnoreResult(&base::DeleteFile), outgoing_dump_path_, false)); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 bool WebRtcRtpDumpHandler::StartDump(RtpDumpType type, | |
| 83 std::string* error_message) { | |
| 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 85 | |
| 86 if (!dump_writer_ && g_ongoing_rtp_dumps >= kMaxOngoingRtpDumpsAllowed) { | |
| 87 *error_message = "Max RTP dump limit reached."; | |
| 88 DVLOG(2) << *error_message; | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 // Returns an error if any type of dump specified by the caller cannot be | |
| 93 // started. | |
| 94 if ((DumpTypeContainsIncoming(type) && incoming_state_ != STATE_NONE) || | |
| 95 (DumpTypeContainsOutgoing(type) && outgoing_state_ != STATE_NONE)) { | |
| 96 *error_message = "RTP dump already started."; | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 if (DumpTypeContainsIncoming(type)) | |
| 101 incoming_state_ = STATE_STARTED; | |
| 102 | |
| 103 if (DumpTypeContainsOutgoing(type)) | |
| 104 outgoing_state_ = STATE_STARTED; | |
| 105 | |
| 106 DVLOG(2) << "Start RTP dumping: type = " << type; | |
| 107 | |
| 108 if (!dump_writer_) { | |
| 109 ++g_ongoing_rtp_dumps; | |
| 110 | |
| 111 static const char kRecvDumpFilePrefix[] = "rtpdump_recv_"; | |
| 112 static const char kSendDumpFilePrefix[] = "rtpdump_send_"; | |
| 113 static const size_t kMaxDumpSize = 5 * 1024 * 1024; // 5MB | |
| 114 | |
| 115 std::string dump_id = base::DoubleToString(base::Time::Now().ToDoubleT()); | |
| 116 incoming_dump_path_ = | |
| 117 dump_dir_.AppendASCII(std::string(kRecvDumpFilePrefix) + dump_id) | |
| 118 .AddExtension(FILE_PATH_LITERAL(".gz")); | |
| 119 | |
| 120 outgoing_dump_path_ = | |
| 121 dump_dir_.AppendASCII(std::string(kSendDumpFilePrefix) + dump_id) | |
| 122 .AddExtension(FILE_PATH_LITERAL(".gz")); | |
| 123 | |
| 124 // WebRtcRtpDumpWriter does not support changing the dump path after it's | |
|
Henrik Grunell
2014/05/23 12:17:51
That can be a source of confusion in the future. I
jiayl
2014/05/23 17:48:36
The timestamp is not intended to be human friendly
| |
| 125 // created. So we assign both incoming and outgoing dump path even if only | |
| 126 // one type of dumping has been started. | |
| 127 // For "Unretained(this)", see comments StopDump. | |
| 128 dump_writer_.reset(new WebRtcRtpDumpWriter( | |
| 129 incoming_dump_path_, | |
| 130 outgoing_dump_path_, | |
| 131 kMaxDumpSize, | |
| 132 base::Bind(&WebRtcRtpDumpHandler::OnMaxDumpSizeReached, | |
| 133 base::Unretained(this)))); | |
| 134 } | |
| 135 | |
| 136 return true; | |
| 137 } | |
| 138 | |
| 139 void WebRtcRtpDumpHandler::StopDump(RtpDumpType type, | |
| 140 const GenericDoneCallback& callback) { | |
| 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 142 | |
| 143 // Returns an error if any type of dump specified by the caller cannot be | |
| 144 // stopped. | |
| 145 if ((DumpTypeContainsIncoming(type) && incoming_state_ != STATE_STARTED) || | |
| 146 (DumpTypeContainsOutgoing(type) && outgoing_state_ != STATE_STARTED)) { | |
| 147 if (!callback.is_null()) { | |
| 148 FireGenericDoneCallback( | |
| 149 callback, false, "RTP dump not started or already stopped."); | |
| 150 } | |
| 151 return; | |
| 152 } | |
| 153 | |
| 154 DVLOG(2) << "Stopping RTP dumping: type = " << type; | |
| 155 | |
| 156 if (DumpTypeContainsIncoming(type)) | |
| 157 incoming_state_ = STATE_STOPPING; | |
| 158 | |
| 159 if (DumpTypeContainsOutgoing(type)) | |
| 160 outgoing_state_ = STATE_STOPPING; | |
| 161 | |
| 162 // Using "Unretained(this)" because the this object owns the writer and the | |
| 163 // writer is guaranteed to cancel the callback before it goes away. Same for | |
| 164 // the other posted tasks bound to the writer. | |
| 165 dump_writer_->EndDump(type, | |
| 166 base::Bind(&WebRtcRtpDumpHandler::OnDumpEnded, | |
| 167 base::Unretained(this), | |
| 168 callback, | |
| 169 type)); | |
| 170 } | |
| 171 | |
| 172 WebRtcRtpDumpHandler::ReleasedDumps WebRtcRtpDumpHandler::ReleaseDumps() { | |
| 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 174 | |
| 175 base::FilePath incoming_dump, outgoing_dump; | |
| 176 | |
| 177 // All types of dumps must have been stopped, or one stopped and the other not | |
| 178 // started. | |
| 179 if (incoming_state_ == STATE_STARTED || incoming_state_ == STATE_STOPPING || | |
| 180 outgoing_state_ == STATE_STARTED || outgoing_state_ == STATE_STOPPING || | |
| 181 (incoming_state_ == STATE_NONE && outgoing_state_ == STATE_NONE)) { | |
| 182 DVLOG(2) << "ReleaseDumps called in invalid state: incoming_state = " | |
|
Henrik Grunell
2014/05/23 12:17:51
Should this happen? If not, dcheck. It seems like
jiayl
2014/05/23 17:48:36
Changed so the caller can call ReadyToRelease to d
| |
| 183 << incoming_state_ << ", outgoing_state = " << outgoing_state_; | |
| 184 return ReleasedDumps(incoming_dump, outgoing_dump); | |
| 185 } | |
| 186 | |
| 187 if (incoming_state_ == STATE_STOPPED) { | |
| 188 DVLOG(2) << "Incoming RTP dumps released: " << incoming_dump_path_.value(); | |
| 189 | |
| 190 incoming_state_ = STATE_NONE; | |
| 191 incoming_dump = incoming_dump_path_; | |
| 192 } | |
| 193 | |
| 194 if (outgoing_state_ == STATE_STOPPED) { | |
| 195 DVLOG(2) << "Outgoing RTP dumps released: " << outgoing_dump_path_.value(); | |
| 196 | |
| 197 outgoing_state_ = STATE_NONE; | |
| 198 outgoing_dump = outgoing_dump_path_; | |
| 199 } | |
| 200 return ReleasedDumps(incoming_dump, outgoing_dump); | |
| 201 } | |
| 202 | |
| 203 void WebRtcRtpDumpHandler::OnRtpPacket(const uint8* packet_header, | |
| 204 size_t header_length, | |
| 205 size_t packet_length, | |
| 206 bool incoming) { | |
| 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 208 | |
| 209 if ((incoming && incoming_state_ != STATE_STARTED) || | |
| 210 (!incoming && outgoing_state_ != STATE_STARTED)) { | |
| 211 return; | |
|
Henrik Grunell
2014/05/23 12:17:51
IS this expected to happen?
jiayl
2014/05/23 17:48:36
I'd like to be defensive for the input to the publ
Henrik Grunell
2014/05/26 09:51:03
Is there a border case, e.g. there's a delay when
jiayl
2014/05/27 20:41:40
There is a delay in stopping the P2PSocketHost: th
| |
| 212 } | |
| 213 | |
| 214 dump_writer_->WriteRtpPacket( | |
| 215 packet_header, header_length, packet_length, incoming); | |
| 216 } | |
| 217 | |
| 218 void WebRtcRtpDumpHandler::SetDumpWriterForTesting( | |
| 219 scoped_ptr<WebRtcRtpDumpWriter> writer) { | |
| 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 221 | |
| 222 dump_writer_ = writer.Pass(); | |
| 223 ++g_ongoing_rtp_dumps; | |
|
Henrik Grunell
2014/05/23 12:17:51
Why increase the count when assigning a writer?
jiayl
2014/05/23 17:48:36
Because the goal is to limit the total disk usage
Henrik Grunell
2014/05/26 09:51:03
So, isn't start called after this? And increased a
jiayl
2014/05/27 20:41:40
It's not increased again because Start only increa
| |
| 224 | |
| 225 incoming_dump_path_ = dump_dir_.AppendASCII("recv"); | |
| 226 outgoing_dump_path_ = dump_dir_.AppendASCII("send"); | |
| 227 } | |
| 228 | |
| 229 void WebRtcRtpDumpHandler::OnMaxDumpSizeReached() { | |
| 230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 231 | |
| 232 RtpDumpType type = | |
| 233 (incoming_state_ == STATE_STARTED) | |
| 234 ? (outgoing_state_ == STATE_STARTED ? RTP_DUMP_BOTH | |
| 235 : RTP_DUMP_INCOMING) | |
| 236 : RTP_DUMP_OUTGOING; | |
| 237 StopDump(type, GenericDoneCallback()); | |
|
Henrik Grunell
2014/05/23 12:17:51
The writer calls this object, which calls back int
jiayl
2014/05/23 17:48:36
I don't think so. Writer::EndDump can be called at
| |
| 238 } | |
| 239 | |
| 240 void WebRtcRtpDumpHandler::OnDumpEnded(const GenericDoneCallback& callback, | |
| 241 RtpDumpType ended_type, | |
| 242 bool incoming_success, | |
| 243 bool outgoing_success) { | |
| 244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 245 | |
| 246 if (DumpTypeContainsIncoming(ended_type)) { | |
| 247 DCHECK_EQ(STATE_STOPPING, incoming_state_); | |
| 248 incoming_state_ = STATE_STOPPED; | |
| 249 | |
| 250 if (!incoming_success) { | |
| 251 BrowserThread::PostTask(BrowserThread::FILE, | |
| 252 FROM_HERE, | |
| 253 base::Bind(base::IgnoreResult(&base::DeleteFile), | |
| 254 incoming_dump_path_, | |
| 255 false)); | |
| 256 | |
| 257 DVLOG(2) << "Deleted invalid incoming dump " | |
| 258 << incoming_dump_path_.value(); | |
| 259 incoming_dump_path_.clear(); | |
| 260 } | |
| 261 } | |
| 262 | |
| 263 if (DumpTypeContainsOutgoing(ended_type)) { | |
|
Henrik Grunell
2014/05/23 12:17:51
I really, really would like to get rid of the inco
jiayl
2014/05/23 17:48:36
Here is the previous discussion:
https://coderevi
Henrik Grunell
2014/05/26 09:51:03
We're just storing packet headers, right? So in wh
jiayl
2014/05/27 20:41:40
Incoming and outgoing can be very asymmetric. E.g.
| |
| 264 DCHECK_EQ(STATE_STOPPING, outgoing_state_); | |
| 265 outgoing_state_ = STATE_STOPPED; | |
| 266 | |
| 267 if (!outgoing_success) { | |
| 268 BrowserThread::PostTask(BrowserThread::FILE, | |
| 269 FROM_HERE, | |
| 270 base::Bind(base::IgnoreResult(&base::DeleteFile), | |
| 271 outgoing_dump_path_, | |
| 272 false)); | |
| 273 | |
| 274 DVLOG(2) << "Deleted invalid outgoing dump " | |
| 275 << outgoing_dump_path_.value(); | |
| 276 outgoing_dump_path_.clear(); | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 if (!callback.is_null()) | |
| 281 FireGenericDoneCallback(callback, true, ""); | |
| 282 | |
| 283 // Release the writer when it's no longer needed. | |
| 284 if (incoming_state_ != STATE_STOPPING && outgoing_state_ != STATE_STOPPING && | |
| 285 incoming_state_ != STATE_STARTED && outgoing_state_ != STATE_STARTED) { | |
| 286 dump_writer_.reset(); | |
| 287 --g_ongoing_rtp_dumps; | |
| 288 } | |
| 289 } | |
| OLD | NEW |