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 // |path| must be a copy of FilePath, not a reference. | |
|
Henrik Grunell
2014/05/15 14:36:34
Explain why not a reference in the comment.
jiayl
2014/05/15 23:06:22
I was wrong. It doesn't need to be a copy.
| |
| 19 void DeleteFileHelper(base::FilePath path) { | |
| 20 base::DeleteFile(path, false); | |
| 21 } | |
| 22 | |
| 23 static const size_t kMaxOngoingRtpDumpsAllowed = 5; | |
| 24 | |
| 25 // The browser process wide total number of ongoing (i.e. started and not | |
| 26 // released) RTP dumps. Incoming and outgoing in one WebRtcDumpHandler are | |
| 27 // counted as one dump. | |
| 28 // Must be accessed on the browser IO thread. | |
| 29 static size_t g_ongoing_rtp_dumps = 0; | |
| 30 | |
| 31 void FireGenericDoneCallback( | |
| 32 const WebRtcRtpDumpHandler::GenericDoneCallback& callback, | |
| 33 bool success, | |
| 34 const std::string& error_message) { | |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 36 DCHECK(!callback.is_null()); | |
| 37 | |
| 38 content::BrowserThread::PostTask( | |
| 39 content::BrowserThread::UI, | |
| 40 FROM_HERE, | |
| 41 base::Bind(callback, success, error_message)); | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 // static | |
| 47 bool WebRtcRtpDumpHandler::TypeContainsIncoming(PacketType type) { | |
| 48 return type == PACKET_TYPE_INCOMING_ONLY || type == PACKET_TYPE_BOTH; | |
| 49 } | |
| 50 | |
| 51 // static | |
| 52 bool WebRtcRtpDumpHandler::TypeContainsOutgoing(PacketType type) { | |
| 53 return type == PACKET_TYPE_OUTGOING_ONLY || type == PACKET_TYPE_BOTH; | |
| 54 } | |
| 55 | |
| 56 WebRtcRtpDumpHandler::WebRtcRtpDumpHandler(const base::FilePath& dump_dir) | |
| 57 : dump_dir_(dump_dir), | |
|
Henrik Grunell
2014/05/15 14:36:34
Is |dump_dir_| ever checked to be existing? This s
jiayl
2014/05/15 23:06:22
it's stated in the header file that the caller mus
| |
| 58 incoming_state_(STATE_NONE), | |
| 59 outgoing_state_(STATE_NONE) { | |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 61 } | |
| 62 | |
| 63 WebRtcRtpDumpHandler::~WebRtcRtpDumpHandler() { | |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 65 | |
| 66 if (incoming_state_ != STATE_NONE && !incoming_dump_path_.empty()) { | |
| 67 BrowserThread::PostTask(BrowserThread::FILE, | |
|
Henrik Grunell
2014/05/15 14:36:34
How is it guaranteed that the writer stops writing
jiayl
2014/05/15 23:06:22
Fixed. Reset the writer first will guarantee that.
| |
| 68 FROM_HERE, | |
| 69 base::Bind(&DeleteFileHelper, incoming_dump_path_)); | |
| 70 } | |
| 71 | |
| 72 if (outgoing_state_ != STATE_NONE && !outgoing_dump_path_.empty()) { | |
| 73 BrowserThread::PostTask(BrowserThread::FILE, | |
| 74 FROM_HERE, | |
| 75 base::Bind(&DeleteFileHelper, outgoing_dump_path_)); | |
| 76 } | |
| 77 | |
| 78 if (dump_writer_) | |
| 79 --g_ongoing_rtp_dumps; | |
| 80 } | |
| 81 | |
| 82 bool WebRtcRtpDumpHandler::StartDump(PacketType type, | |
| 83 std::string* error_message) { | |
| 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 85 | |
| 86 bool succeeded = false; | |
| 87 | |
| 88 if (TypeContainsIncoming(type) && incoming_state_ == STATE_NONE) { | |
| 89 incoming_state_ = STATE_STARTED; | |
| 90 succeeded = true; | |
|
Henrik Grunell
2014/05/15 14:36:34
I think this should only succeed if both types cou
jiayl
2014/05/15 23:06:22
Done.
| |
| 91 } | |
| 92 if (TypeContainsOutgoing(type) && outgoing_state_ == STATE_NONE) { | |
| 93 outgoing_state_ = STATE_STARTED; | |
| 94 succeeded = true; | |
| 95 } | |
| 96 | |
| 97 if (!dump_writer_ && g_ongoing_rtp_dumps >= kMaxOngoingRtpDumpsAllowed) { | |
|
Henrik Grunell
2014/05/15 14:36:34
Do this check before the state checks above.
jiayl
2014/05/15 23:06:22
Done.
| |
| 98 *error_message = "Max RTP dump limit reached."; | |
| 99 DVLOG(2) << *error_message; | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 if (!succeeded) { | |
| 104 *error_message = "RTP dump already started."; | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 DVLOG(2) << "Start RTP dumping: type = " << type; | |
| 109 | |
| 110 if (!dump_writer_) { | |
| 111 ++g_ongoing_rtp_dumps; | |
| 112 | |
| 113 static const char kRecvDumpFilePrefix[] = "rtpdump_recv_"; | |
| 114 static const char kSendDumpFilePrefix[] = "rtpdump_send_"; | |
| 115 static const char kDumpFileExtension[] = ".gz"; | |
| 116 static const size_t kMaxDumpSize = 5 * 1024 * 1024; // 5MB | |
| 117 | |
| 118 std::string dump_id = base::DoubleToString(base::Time::Now().ToDoubleT()); | |
| 119 incoming_dump_path_ = | |
| 120 dump_dir_.AppendASCII(std::string(kRecvDumpFilePrefix) + dump_id) | |
| 121 .AddExtension(kDumpFileExtension); | |
| 122 | |
| 123 outgoing_dump_path_ = | |
| 124 dump_dir_.AppendASCII(std::string(kSendDumpFilePrefix) + dump_id) | |
| 125 .AddExtension(kDumpFileExtension); | |
| 126 | |
| 127 // WebRtcRtpDumpWriter does not support changing the dump path after it's | |
| 128 // created. So we assign both incoming and outgoing dump path even if only | |
| 129 // one type of dumping has been started. | |
| 130 dump_writer_.reset(new WebRtcRtpDumpWriter( | |
| 131 incoming_dump_path_, | |
| 132 outgoing_dump_path_, | |
| 133 kMaxDumpSize, | |
| 134 base::Bind(&WebRtcRtpDumpHandler::OnMaxDumpSizeReached, | |
| 135 base::Unretained(this)))); | |
| 136 } | |
| 137 | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 void WebRtcRtpDumpHandler::StopDump(PacketType type, | |
| 142 const GenericDoneCallback& callback) { | |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 144 | |
| 145 bool stop_incoming = false; | |
| 146 bool stop_outgoing = false; | |
| 147 if (TypeContainsIncoming(type) && incoming_state_ == STATE_STARTED) { | |
| 148 incoming_state_ = STATE_STOPPING; | |
| 149 stop_incoming = true; | |
| 150 } | |
| 151 | |
| 152 if (TypeContainsOutgoing(type) && outgoing_state_ == STATE_STARTED) { | |
| 153 outgoing_state_ = STATE_STOPPING; | |
| 154 stop_outgoing = true; | |
| 155 } | |
| 156 | |
| 157 if (!stop_incoming && !stop_outgoing) { | |
| 158 if (!callback.is_null()) { | |
| 159 FireGenericDoneCallback( | |
| 160 callback, false, "RTP dump not started or already stopped."); | |
| 161 } | |
| 162 return; | |
| 163 } | |
| 164 | |
| 165 DVLOG(2) << "Stopping RTP dumping: type = " << type; | |
| 166 | |
| 167 if (stop_incoming) { | |
| 168 dump_writer_->EndDump( | |
|
Henrik Grunell
2014/05/15 14:36:34
Can the EndDump be changed so that it's called onc
jiayl
2014/05/15 23:06:22
Done.
| |
| 169 true, | |
| 170 base::Bind(&WebRtcRtpDumpHandler::OnDumpEnded, | |
| 171 base::Unretained(this), | |
|
Henrik Grunell
2014/05/15 14:36:34
Why is unretained safe? (Comment on this at all pl
jiayl
2014/05/15 23:06:22
Done.
| |
| 172 stop_outgoing ? GenericDoneCallback() : callback, | |
| 173 true)); | |
| 174 } | |
| 175 | |
| 176 if (stop_outgoing) { | |
| 177 dump_writer_->EndDump(false, | |
| 178 base::Bind(&WebRtcRtpDumpHandler::OnDumpEnded, | |
| 179 base::Unretained(this), | |
| 180 callback, | |
| 181 false)); | |
| 182 } | |
| 183 | |
| 184 return; | |
| 185 } | |
| 186 | |
| 187 WebRtcRtpDumpHandler::ReleasedDumps WebRtcRtpDumpHandler::ReleaseDumps() { | |
| 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 189 | |
| 190 ReleasedDumps result; | |
| 191 | |
| 192 // All types of dumps must have been stopped, or one stopped and the other not | |
| 193 // started. | |
| 194 if (incoming_state_ == STATE_STARTED || incoming_state_ == STATE_STOPPING || | |
| 195 outgoing_state_ == STATE_STARTED || outgoing_state_ == STATE_STOPPING || | |
| 196 (incoming_state_ == STATE_NONE && outgoing_state_ == STATE_NONE)) { | |
| 197 DVLOG(2) << "ReleaseDumps called in invalid state: incoming_state = " | |
| 198 << incoming_state_ << ", outgoing_state = " << outgoing_state_; | |
| 199 return result; | |
| 200 } | |
| 201 | |
| 202 if (incoming_state_ == STATE_STOPPED) { | |
| 203 DVLOG(2) << "Incoming RTP dumps released: " << incoming_dump_path_.value(); | |
| 204 | |
| 205 incoming_state_ = STATE_NONE; | |
| 206 result.incoming_dump_path = incoming_dump_path_; | |
| 207 } | |
| 208 | |
| 209 if (outgoing_state_ == STATE_STOPPED) { | |
| 210 DVLOG(2) << "Outgoing RTP dumps released: " << outgoing_dump_path_.value(); | |
| 211 | |
| 212 outgoing_state_ = STATE_NONE; | |
| 213 result.outgoing_dump_path = outgoing_dump_path_; | |
| 214 } | |
| 215 return result; | |
| 216 } | |
| 217 | |
| 218 void WebRtcRtpDumpHandler::OnRtpPacket(const uint8* packet_header, | |
| 219 size_t header_length, | |
| 220 size_t packet_length, | |
| 221 bool incoming) { | |
| 222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 223 | |
| 224 if ((incoming && incoming_state_ != STATE_STARTED) || | |
| 225 (!incoming && outgoing_state_ != STATE_STARTED)) | |
| 226 return; | |
| 227 | |
| 228 dump_writer_->WriteRtpPacket( | |
| 229 packet_header, header_length, packet_length, incoming); | |
| 230 } | |
| 231 | |
| 232 void WebRtcRtpDumpHandler::SetDumpWriterForTesting( | |
| 233 scoped_ptr<WebRtcRtpDumpWriter> writer) { | |
| 234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 235 | |
| 236 dump_writer_ = writer.Pass(); | |
| 237 g_ongoing_rtp_dumps++; | |
| 238 | |
| 239 incoming_dump_path_ = dump_dir_.AppendASCII("recv"); | |
| 240 outgoing_dump_path_ = dump_dir_.AppendASCII("send"); | |
| 241 } | |
| 242 | |
| 243 void WebRtcRtpDumpHandler::OnMaxDumpSizeReached() { | |
| 244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 245 | |
| 246 StopDump(PACKET_TYPE_BOTH, GenericDoneCallback()); | |
| 247 } | |
| 248 | |
| 249 void WebRtcRtpDumpHandler::OnDumpEnded(const GenericDoneCallback& callback, | |
| 250 bool incoming, | |
| 251 bool succeeded) { | |
| 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 253 | |
| 254 if (incoming) { | |
| 255 DCHECK_EQ(STATE_STOPPING, incoming_state_); | |
| 256 incoming_state_ = STATE_STOPPED; | |
| 257 } else { | |
| 258 DCHECK_EQ(STATE_STOPPING, outgoing_state_); | |
| 259 outgoing_state_ = STATE_STOPPED; | |
| 260 } | |
| 261 | |
| 262 if (!succeeded) { | |
| 263 base::FilePath* dump = | |
| 264 incoming ? &incoming_dump_path_ : &outgoing_dump_path_; | |
| 265 | |
| 266 BrowserThread::PostTask( | |
| 267 BrowserThread::FILE, FROM_HERE, base::Bind(&DeleteFileHelper, *dump)); | |
| 268 | |
| 269 DVLOG(2) << "Deleted invalid dump " << dump->value(); | |
| 270 dump->clear(); | |
| 271 } | |
| 272 | |
| 273 if (!callback.is_null()) | |
| 274 callback.Run(true, ""); | |
| 275 | |
| 276 // Release the writer when it's no longer needed. | |
| 277 if (incoming_state_ != STATE_STOPPING && outgoing_state_ != STATE_STOPPING && | |
| 278 incoming_state_ != STATE_STARTED && outgoing_state_ != STATE_STARTED) { | |
| 279 dump_writer_.reset(); | |
| 280 g_ongoing_rtp_dumps--; | |
| 281 } | |
| 282 } | |
| OLD | NEW |