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 void DeleteFileHelper(const base::FilePath& path) { | |
|
tommi (sloooow) - chröme
2014/05/17 10:18:37
nit: is this helper needed? (can we bind to Delete
jiayl
2014/05/19 17:32:59
Done.
| |
| 19 base::DeleteFile(path, false); | |
| 20 } | |
| 21 | |
| 22 static const size_t kMaxOngoingRtpDumpsAllowed = 5; | |
| 23 | |
| 24 // The browser process wide total number of ongoing (i.e. started and not | |
| 25 // released) RTP dumps. Incoming and outgoing in one WebRtcDumpHandler are | |
| 26 // counted as one dump. | |
| 27 // Must be accessed on the browser IO thread. | |
| 28 static size_t g_ongoing_rtp_dumps = 0; | |
| 29 | |
| 30 void FireGenericDoneCallback( | |
| 31 const WebRtcRtpDumpHandler::GenericDoneCallback& callback, | |
| 32 bool success, | |
| 33 const std::string& error_message) { | |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
|
tommi (sloooow) - chröme
2014/05/17 10:18:37
nice job on the thread checks!
| |
| 35 DCHECK(!callback.is_null()); | |
| 36 | |
| 37 content::BrowserThread::PostTask( | |
| 38 content::BrowserThread::UI, | |
| 39 FROM_HERE, | |
| 40 base::Bind(callback, success, error_message)); | |
| 41 } | |
| 42 | |
| 43 bool DumpTypeContainsIncoming(RtpDumpType type) { | |
| 44 return type == RTP_DUMP_INCOMING || type == RTP_DUMP_BOTH; | |
| 45 } | |
| 46 | |
| 47 bool DumpTypeContainsOutgoing(RtpDumpType type) { | |
| 48 return type == RTP_DUMP_OUTGOING || type == RTP_DUMP_BOTH; | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 WebRtcRtpDumpHandler::WebRtcRtpDumpHandler(const base::FilePath& dump_dir) | |
| 54 : dump_dir_(dump_dir), | |
| 55 incoming_state_(STATE_NONE), | |
| 56 outgoing_state_(STATE_NONE) { | |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 58 } | |
| 59 | |
| 60 WebRtcRtpDumpHandler::~WebRtcRtpDumpHandler() { | |
| 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 62 | |
| 63 // Reset dump writer first to stop writing. | |
| 64 if (dump_writer_) { | |
| 65 --g_ongoing_rtp_dumps; | |
| 66 dump_writer_.reset(); | |
| 67 } | |
| 68 | |
| 69 if (incoming_state_ != STATE_NONE && !incoming_dump_path_.empty()) { | |
| 70 BrowserThread::PostTask(BrowserThread::FILE, | |
| 71 FROM_HERE, | |
| 72 base::Bind(&DeleteFileHelper, incoming_dump_path_)); | |
| 73 } | |
| 74 | |
| 75 if (outgoing_state_ != STATE_NONE && !outgoing_dump_path_.empty()) { | |
| 76 BrowserThread::PostTask(BrowserThread::FILE, | |
| 77 FROM_HERE, | |
| 78 base::Bind(&DeleteFileHelper, outgoing_dump_path_)); | |
| 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 | |
| 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 ReleasedDumps result; | |
| 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 = " | |
| 183 << incoming_state_ << ", outgoing_state = " << outgoing_state_; | |
| 184 return result; | |
| 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 result.incoming_dump_path = 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 result.outgoing_dump_path = outgoing_dump_path_; | |
| 199 } | |
| 200 return result; | |
| 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; | |
|
tommi (sloooow) - chröme
2014/05/17 10:18:37
{}
jiayl
2014/05/19 17:32:59
Done.
| |
| 212 | |
| 213 dump_writer_->WriteRtpPacket( | |
| 214 packet_header, header_length, packet_length, incoming); | |
| 215 } | |
| 216 | |
| 217 void WebRtcRtpDumpHandler::SetDumpWriterForTesting( | |
| 218 scoped_ptr<WebRtcRtpDumpWriter> writer) { | |
| 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 220 | |
| 221 dump_writer_ = writer.Pass(); | |
| 222 g_ongoing_rtp_dumps++; | |
| 223 | |
| 224 incoming_dump_path_ = dump_dir_.AppendASCII("recv"); | |
| 225 outgoing_dump_path_ = dump_dir_.AppendASCII("send"); | |
| 226 } | |
| 227 | |
| 228 void WebRtcRtpDumpHandler::OnMaxDumpSizeReached() { | |
| 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 230 | |
| 231 RtpDumpType type = | |
| 232 (incoming_state_ == STATE_STARTED) | |
| 233 ? (outgoing_state_ == STATE_STARTED ? RTP_DUMP_BOTH | |
| 234 : RTP_DUMP_INCOMING) | |
| 235 : RTP_DUMP_OUTGOING; | |
| 236 StopDump(type, GenericDoneCallback()); | |
| 237 } | |
| 238 | |
| 239 void WebRtcRtpDumpHandler::OnDumpEnded(const GenericDoneCallback& callback, | |
| 240 RtpDumpType ended_type, | |
| 241 bool incoming_success, | |
| 242 bool outgoing_success) { | |
| 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 244 | |
| 245 if (DumpTypeContainsIncoming(ended_type)) { | |
| 246 DCHECK_EQ(STATE_STOPPING, incoming_state_); | |
| 247 incoming_state_ = STATE_STOPPED; | |
| 248 | |
| 249 if (!incoming_success) { | |
| 250 BrowserThread::PostTask( | |
| 251 BrowserThread::FILE, | |
| 252 FROM_HERE, | |
| 253 base::Bind(&DeleteFileHelper, incoming_dump_path_)); | |
| 254 | |
| 255 DVLOG(2) << "Deleted invalid incoming dump " | |
| 256 << incoming_dump_path_.value(); | |
| 257 incoming_dump_path_.clear(); | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 if (DumpTypeContainsOutgoing(ended_type)) { | |
| 262 DCHECK_EQ(STATE_STOPPING, outgoing_state_); | |
| 263 outgoing_state_ = STATE_STOPPED; | |
| 264 | |
| 265 if (!outgoing_success) { | |
| 266 BrowserThread::PostTask( | |
| 267 BrowserThread::FILE, | |
| 268 FROM_HERE, | |
| 269 base::Bind(&DeleteFileHelper, outgoing_dump_path_)); | |
| 270 | |
| 271 DVLOG(2) << "Deleted invalid outgoing dump " | |
| 272 << outgoing_dump_path_.value(); | |
| 273 outgoing_dump_path_.clear(); | |
| 274 } | |
| 275 } | |
| 276 | |
| 277 if (!callback.is_null()) | |
| 278 FireGenericDoneCallback(callback, true, ""); | |
| 279 | |
| 280 // Release the writer when it's no longer needed. | |
| 281 if (incoming_state_ != STATE_STOPPING && outgoing_state_ != STATE_STOPPING && | |
| 282 incoming_state_ != STATE_STARTED && outgoing_state_ != STATE_STARTED) { | |
| 283 dump_writer_.reset(); | |
| 284 g_ongoing_rtp_dumps--; | |
|
tommi (sloooow) - chröme
2014/05/17 10:18:37
nit: --g_ongoing_rtp_dumps;
jiayl
2014/05/19 17:32:59
Done.
| |
| 285 } | |
| 286 } | |
| OLD | NEW |