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_writer.h" | |
| 6 | |
| 7 #include "base/big_endian.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 #include "third_party/zlib/zlib.h" | |
| 12 | |
| 13 using content::BrowserThread; | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 static const size_t kMinimumGzipOutputBufferSize = 256; | |
|
Henrik Grunell
2014/05/26 09:51:03
Why is this needed? What's the value based on?
jiayl
2014/05/27 20:41:41
Usually we reserve the output buffer of Deflate in
| |
| 18 | |
| 19 const unsigned char kRtpDumpFileHeaderFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n"; | |
| 20 static const size_t kRtpDumpFileHeaderSize = 16; | |
| 21 | |
| 22 // A helper for writing the header of the dump file. | |
| 23 void WriteRtpDumpFileHeaderBigEndian(base::TimeTicks start, | |
| 24 std::vector<uint8>* output) { | |
| 25 size_t buffer_start_pos = output->size(); | |
| 26 output->resize(output->size() + kRtpDumpFileHeaderSize); | |
|
Henrik Grunell
2014/05/26 09:51:03
Adding the sizes written below gives 18 bytes.
jiayl
2014/05/27 20:41:41
Fixed: port should be uint16.
| |
| 27 | |
| 28 char* buffer = reinterpret_cast<char*>(output->data() + buffer_start_pos); | |
| 29 | |
| 30 base::TimeDelta delta = start - base::TimeTicks(); | |
| 31 uint32 start_sec = delta.InSeconds(); | |
| 32 base::WriteBigEndian(buffer, start_sec); | |
| 33 buffer += sizeof(start_sec); | |
| 34 | |
| 35 uint32 start_usec = | |
| 36 delta.InMilliseconds() * base::Time::kMicrosecondsPerMillisecond; | |
| 37 base::WriteBigEndian(buffer, start_usec); | |
| 38 buffer += sizeof(start_usec); | |
| 39 | |
| 40 // Network source, always 0. | |
| 41 base::WriteBigEndian(buffer, uint32(0)); | |
| 42 buffer += sizeof(uint32); | |
| 43 | |
| 44 // UDP port, always 0. | |
| 45 base::WriteBigEndian(buffer, uint32(0)); | |
| 46 buffer += sizeof(uint32); | |
| 47 | |
| 48 // 2 bytes padding. | |
| 49 base::WriteBigEndian(buffer, uint16(0)); | |
| 50 } | |
| 51 | |
| 52 // The header size for each packet dump. | |
| 53 static const size_t kPacketDumpHeaderSize = 8; | |
| 54 | |
| 55 // A helper for writing the header for each packet dump. | |
| 56 // |start| is the time when the recording is started. | |
| 57 // |dump_length| is the length of the packet dump including this header. | |
| 58 // |packet_length| is the length of the RTP packet header. | |
| 59 void WritePacketDumpHeaderBigEndian(const base::TimeTicks& start, | |
| 60 uint16 dump_length, | |
| 61 uint16 packet_length, | |
| 62 std::vector<uint8>* output) { | |
| 63 size_t buffer_start_pos = output->size(); | |
| 64 output->resize(output->size() + kPacketDumpHeaderSize); | |
| 65 | |
| 66 char* buffer = reinterpret_cast<char*>(output->data() + buffer_start_pos); | |
| 67 | |
| 68 base::WriteBigEndian(buffer, dump_length); | |
| 69 buffer += sizeof(dump_length); | |
| 70 | |
| 71 base::WriteBigEndian(buffer, packet_length); | |
| 72 buffer += sizeof(packet_length); | |
| 73 | |
| 74 base::WriteBigEndian(buffer, | |
| 75 (base::TimeTicks::Now() - start).InMilliseconds()); | |
| 76 } | |
| 77 | |
| 78 // Append |src_len| bytes from |src| to |dest|. | |
| 79 void AppendToBuffer(const uint8* src, | |
| 80 size_t src_len, | |
| 81 std::vector<uint8>* dest) { | |
| 82 size_t old_dest_size = dest->size(); | |
| 83 | |
|
Henrik Grunell
2014/05/26 09:51:03
Nit: Remove blank line.
jiayl
2014/05/27 20:41:41
Done.
| |
| 84 dest->resize(old_dest_size + src_len); | |
| 85 memcpy(dest->data() + old_dest_size, src, src_len); | |
| 86 } | |
| 87 | |
| 88 } // namespace | |
| 89 | |
| 90 // This class is running on the FILE thread for compressing and writing the | |
| 91 // dump buffer to disk. | |
| 92 class WebRtcRtpDumpWriter::FileThreadWorker { | |
| 93 public: | |
| 94 explicit FileThreadWorker(const base::FilePath& dump_path) | |
| 95 : dump_path_(dump_path), stream_initialized_(false) { | |
| 96 thread_checker_.DetachFromThread(); | |
| 97 } | |
| 98 | |
| 99 ~FileThreadWorker() { DCHECK(thread_checker_.CalledOnValidThread()); } | |
|
Henrik Grunell
2014/05/26 09:51:03
Put DCHECK on its own line.
jiayl
2014/05/27 20:41:41
Done.
| |
| 100 | |
| 101 // Compresses the data in |buffer| write to the dump file. If |end_stream| is | |
| 102 // true, the compression stream will be ended and the dump file cannot be | |
| 103 // written to any more. | |
| 104 void CompressAndWriteToFileOnFileThread( | |
| 105 scoped_ptr<std::vector<uint8> > buffer, | |
| 106 bool end_stream, | |
| 107 FlushResult* result, | |
| 108 size_t* bytes_written) { | |
| 109 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 110 | |
| 111 *result = FLUSH_RESULT_SUCCESS; | |
| 112 *bytes_written = 0; | |
| 113 | |
| 114 if (buffer->size()) { | |
|
Henrik Grunell
2014/05/26 09:51:03
buffer->size() > 0
When would this function be ca
jiayl
2014/05/27 20:41:41
When there is no RTP traffic since the last flush.
Henrik Grunell
2014/05/28 11:57:51
Why will it be called if there's no data to add?
jiayl
2014/05/28 13:48:03
To end the gzip stream. See the new DCHECK added.
| |
| 115 *bytes_written = CompressAndWriteBufferToFile(buffer.get(), result); | |
| 116 } else if (!base::PathExists(dump_path_)) { | |
|
Henrik Grunell
2014/05/26 09:51:03
I don't understand this if block.
jiayl
2014/05/27 20:41:41
Comments added.
| |
| 117 *result = FLUSH_RESULT_NO_DATA; | |
| 118 } | |
| 119 | |
| 120 if (end_stream) { | |
| 121 if (!EndDumpFile()) | |
| 122 *result = FLUSH_RESULT_FAILURE; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 private: | |
| 127 // Helper for CompressAndWriteToFileOnFileThread to compress and write one | |
| 128 // dump. | |
| 129 size_t CompressAndWriteBufferToFile(std::vector<uint8>* buffer, | |
| 130 FlushResult* result) { | |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 132 DCHECK(buffer->size()); | |
| 133 | |
| 134 *result = FLUSH_RESULT_SUCCESS; | |
| 135 | |
| 136 std::vector<uint8> compressed_buffer; | |
| 137 if (!Compress(buffer, &compressed_buffer)) { | |
| 138 DVLOG(2) << "Compressing buffer failed."; | |
| 139 *result = FLUSH_RESULT_FAILURE; | |
| 140 return 0; | |
| 141 } | |
| 142 | |
| 143 int bytes_written = -1; | |
| 144 | |
| 145 if (base::PathExists(dump_path_)) { | |
|
Henrik Grunell
2014/05/26 09:51:03
I think the user of this class should ensure that
jiayl
2014/05/27 20:41:41
The caller of the class is not running on the FILE
| |
| 146 bytes_written = base::AppendToFile( | |
| 147 dump_path_, | |
| 148 reinterpret_cast<const char*>(compressed_buffer.data()), | |
| 149 compressed_buffer.size()); | |
| 150 } else { | |
| 151 bytes_written = base::WriteFile( | |
| 152 dump_path_, | |
| 153 reinterpret_cast<const char*>(compressed_buffer.data()), | |
| 154 compressed_buffer.size()); | |
| 155 } | |
| 156 | |
| 157 if (bytes_written == -1) { | |
| 158 DVLOG(2) << "Writing file failed: " << dump_path_.value(); | |
| 159 *result = FLUSH_RESULT_FAILURE; | |
| 160 return 0; | |
| 161 } | |
| 162 | |
| 163 DCHECK_EQ(static_cast<size_t>(bytes_written), compressed_buffer.size()); | |
| 164 | |
| 165 return bytes_written; | |
| 166 } | |
| 167 | |
| 168 // Compresses |input| into |output|. | |
| 169 bool Compress(std::vector<uint8>* input, std::vector<uint8>* output) { | |
| 170 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 171 int result = Z_OK; | |
| 172 | |
| 173 output->resize(std::max(kMinimumGzipOutputBufferSize, input->size())); | |
| 174 | |
| 175 if (!stream_initialized_) { | |
|
Henrik Grunell
2014/05/26 09:51:03
Can you avoid using a flag and doing init in this
jiayl
2014/05/27 20:41:41
Done.
| |
| 176 memset(&stream_, 0, sizeof(stream_)); | |
| 177 result = deflateInit2(&stream_, | |
| 178 Z_DEFAULT_COMPRESSION, | |
| 179 Z_DEFLATED, | |
| 180 // windowBits = 15 is default, 16 is added to | |
| 181 // produce a gzip header + trailer. | |
| 182 15 + 16, | |
| 183 8, // memLevel = 8 is default. | |
| 184 Z_DEFAULT_STRATEGY); | |
| 185 DCHECK_EQ(Z_OK, result); | |
| 186 stream_initialized_ = true; | |
| 187 } | |
| 188 | |
| 189 stream_.next_in = input->data(); | |
| 190 stream_.avail_in = input->size(); | |
| 191 stream_.next_out = output->data(); | |
| 192 stream_.avail_out = output->size(); | |
| 193 | |
| 194 result = deflate(&stream_, Z_SYNC_FLUSH); | |
|
Henrik Grunell
2014/05/26 09:51:03
Nit: AFAIK, you shouldn't trust it completing all
jiayl
2014/05/27 20:41:41
I believe it's fine since we provide enough output
| |
| 195 DCHECK_EQ(Z_OK, result); | |
| 196 DCHECK_EQ(0U, stream_.avail_in); | |
| 197 | |
| 198 output->resize(output->size() - stream_.avail_out); | |
| 199 | |
| 200 stream_.next_in = NULL; | |
| 201 stream_.next_out = NULL; | |
| 202 stream_.avail_out = 0; | |
| 203 return true; | |
| 204 } | |
| 205 | |
| 206 // Ends the compression stream and completes the dump file. | |
| 207 bool EndDumpFile() { | |
| 208 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 209 | |
| 210 if (!stream_initialized_) | |
| 211 return true; | |
| 212 | |
| 213 std::vector<uint8> output_buffer; | |
| 214 output_buffer.resize(kMinimumGzipOutputBufferSize); | |
| 215 | |
| 216 stream_.next_in = NULL; | |
| 217 stream_.avail_in = 0; | |
| 218 stream_.next_out = output_buffer.data(); | |
| 219 stream_.avail_out = output_buffer.size(); | |
| 220 | |
| 221 int result = deflate(&stream_, Z_FINISH); | |
| 222 DCHECK_EQ(Z_STREAM_END, result); | |
| 223 | |
| 224 result = deflateEnd(&stream_); | |
| 225 DCHECK_EQ(Z_OK, result); | |
| 226 | |
| 227 output_buffer.resize(output_buffer.size() - stream_.avail_out); | |
| 228 | |
| 229 stream_initialized_ = false; | |
| 230 memset(&stream_, 0, sizeof(z_stream)); | |
| 231 | |
| 232 int bytes_written = | |
| 233 base::AppendToFile(dump_path_, | |
| 234 reinterpret_cast<const char*>(output_buffer.data()), | |
| 235 output_buffer.size()); | |
| 236 | |
| 237 return bytes_written > 0; | |
|
Henrik Grunell
2014/05/26 09:51:03
Will the last deflate calls for sure render output
jiayl
2014/05/27 20:41:41
I think so. Added a DCHECK.
| |
| 238 } | |
| 239 | |
| 240 const base::FilePath dump_path_; | |
| 241 | |
| 242 z_stream stream_; | |
| 243 bool stream_initialized_; | |
| 244 | |
| 245 base::ThreadChecker thread_checker_; | |
| 246 | |
| 247 DISALLOW_COPY_AND_ASSIGN(FileThreadWorker); | |
| 248 }; | |
| 249 | |
| 250 WebRtcRtpDumpWriter::WebRtcRtpDumpWriter( | |
| 251 const base::FilePath& incoming_dump_path, | |
| 252 const base::FilePath& outgoing_dump_path, | |
| 253 size_t max_dump_size, | |
| 254 const base::Closure& max_dump_size_reached_callback) | |
| 255 : max_dump_size_(max_dump_size), | |
| 256 max_dump_size_reached_callback_(max_dump_size_reached_callback), | |
| 257 total_dump_size_on_disk_(0), | |
| 258 incoming_file_thread_worker_(new FileThreadWorker(incoming_dump_path)), | |
| 259 outgoing_file_thread_worker_(new FileThreadWorker(outgoing_dump_path)), | |
| 260 weak_ptr_factory_(this) { | |
| 261 } | |
| 262 | |
| 263 WebRtcRtpDumpWriter::~WebRtcRtpDumpWriter() { | |
| 264 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 265 if (!BrowserThread::DeleteSoon(BrowserThread::FILE, | |
| 266 FROM_HERE, | |
| 267 incoming_file_thread_worker_.release())) { | |
| 268 DCHECK(false); | |
| 269 } | |
| 270 | |
| 271 if (!BrowserThread::DeleteSoon(BrowserThread::FILE, | |
| 272 FROM_HERE, | |
| 273 outgoing_file_thread_worker_.release())) { | |
| 274 DCHECK(false); | |
| 275 } | |
| 276 } | |
| 277 | |
| 278 void WebRtcRtpDumpWriter::WriteRtpPacket(const uint8* packet_header, | |
| 279 size_t header_length, | |
| 280 size_t packet_length, | |
| 281 bool incoming) { | |
| 282 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 283 | |
| 284 static const size_t kMaxInMemoryBufferSize = 65536; // 64KB | |
| 285 | |
| 286 std::vector<uint8>* dest_buffer = | |
| 287 incoming ? &incoming_buffer_ : &outgoing_buffer_; | |
| 288 | |
| 289 if (!dest_buffer->capacity()) { | |
|
Henrik Grunell
2014/05/26 09:51:03
Check empty() instead?
This is some kind of init
jiayl
2014/05/27 20:41:41
It could be empty but initialized (i.e. file heade
| |
| 290 dest_buffer->reserve(std::min(kMaxInMemoryBufferSize, max_dump_size_)); | |
| 291 | |
| 292 start_time_ = base::TimeTicks::Now(); | |
| 293 | |
| 294 // Writes the dump file header. | |
| 295 AppendToBuffer(kRtpDumpFileHeaderFirstLine, | |
| 296 arraysize(kRtpDumpFileHeaderFirstLine) - 1, | |
| 297 dest_buffer); | |
| 298 WriteRtpDumpFileHeaderBigEndian(start_time_, dest_buffer); | |
| 299 } | |
| 300 | |
| 301 size_t packet_dump_length = kPacketDumpHeaderSize + header_length; | |
| 302 | |
| 303 // Flushes the buffer to disk if the buffer is full. | |
| 304 if (dest_buffer->capacity() < dest_buffer->size() + packet_dump_length) | |
| 305 FlushBuffer(incoming, false, FlushDoneCallback()); | |
| 306 | |
| 307 WritePacketDumpHeaderBigEndian( | |
| 308 start_time_, packet_dump_length, packet_length, dest_buffer); | |
| 309 | |
| 310 // Writes the actual RTP packet header. | |
| 311 AppendToBuffer(packet_header, header_length, dest_buffer); | |
| 312 } | |
| 313 | |
| 314 void WebRtcRtpDumpWriter::EndDump(RtpDumpType type, | |
| 315 const EndDumpCallback& finished_callback) { | |
| 316 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 317 | |
| 318 bool incoming = (type == RTP_DUMP_BOTH || type == RTP_DUMP_INCOMING); | |
| 319 EndDumpContext context(type, finished_callback); | |
| 320 | |
| 321 FlushBuffer(incoming, | |
| 322 true, | |
| 323 base::Bind(&WebRtcRtpDumpWriter::OnDumpEnded, | |
| 324 weak_ptr_factory_.GetWeakPtr(), | |
| 325 context, | |
| 326 incoming)); | |
| 327 } | |
| 328 | |
| 329 size_t WebRtcRtpDumpWriter::max_dump_size() const { | |
| 330 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 331 return max_dump_size_; | |
| 332 } | |
| 333 | |
| 334 WebRtcRtpDumpWriter::EndDumpContext::EndDumpContext( | |
| 335 RtpDumpType type, | |
| 336 const EndDumpCallback& callback) | |
| 337 : type(type), | |
| 338 incoming_succeeded(false), | |
| 339 outgoing_succeeded(false), | |
| 340 callback(callback) { | |
| 341 } | |
| 342 | |
| 343 WebRtcRtpDumpWriter::EndDumpContext::~EndDumpContext() { | |
| 344 } | |
| 345 | |
| 346 void WebRtcRtpDumpWriter::FlushBuffer(bool incoming, | |
| 347 bool end_stream, | |
| 348 const FlushDoneCallback& callback) { | |
| 349 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 350 | |
| 351 scoped_ptr<std::vector<uint8> > new_buffer(new std::vector<uint8>()); | |
| 352 | |
| 353 if (incoming) { | |
| 354 new_buffer->reserve(incoming_buffer_.capacity()); | |
| 355 new_buffer->swap(incoming_buffer_); | |
| 356 } else { | |
| 357 new_buffer->reserve(outgoing_buffer_.capacity()); | |
| 358 new_buffer->swap(outgoing_buffer_); | |
| 359 } | |
| 360 | |
| 361 scoped_ptr<FlushResult> result(new FlushResult(FLUSH_RESULT_FAILURE)); | |
| 362 | |
| 363 scoped_ptr<size_t> bytes_written(new size_t(0)); | |
| 364 | |
| 365 FileThreadWorker* worker = incoming ? incoming_file_thread_worker_.get() | |
| 366 : outgoing_file_thread_worker_.get(); | |
| 367 | |
| 368 // Using "Unretained(worker)" because |worker| is owner by this object and it | |
| 369 // guaranteed to be deleted on the FILE thread before this object goes away. | |
| 370 base::Closure task = | |
| 371 base::Bind(&FileThreadWorker::CompressAndWriteToFileOnFileThread, | |
| 372 base::Unretained(worker), | |
| 373 Passed(&new_buffer), | |
| 374 end_stream, | |
| 375 result.get(), | |
| 376 bytes_written.get()); | |
| 377 | |
| 378 // OnFlushDone is necessary to avoid running the callback after this | |
| 379 // object is gone. | |
| 380 base::Closure reply = base::Bind(&WebRtcRtpDumpWriter::OnFlushDone, | |
| 381 weak_ptr_factory_.GetWeakPtr(), | |
| 382 callback, | |
| 383 Passed(&result), | |
| 384 Passed(&bytes_written)); | |
| 385 | |
| 386 // Define the task and reply outside the method call so that getting and | |
| 387 // passing the scoped_ptr does not depend on the argument evaluation order. | |
| 388 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, task, reply); | |
| 389 } | |
| 390 | |
| 391 void WebRtcRtpDumpWriter::OnFlushDone(const FlushDoneCallback& callback, | |
| 392 const scoped_ptr<FlushResult>& result, | |
| 393 const scoped_ptr<size_t>& bytes_written) { | |
| 394 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 395 | |
| 396 total_dump_size_on_disk_ += *bytes_written; | |
| 397 | |
| 398 if (total_dump_size_on_disk_ >= max_dump_size_ && | |
| 399 !max_dump_size_reached_callback_.is_null()) { | |
| 400 max_dump_size_reached_callback_.Run(); | |
| 401 } | |
| 402 | |
| 403 // Returns success for FLUSH_RESULT_MAX_SIZE_REACHED since the dump is still | |
| 404 // valid. | |
| 405 if (!callback.is_null()) { | |
| 406 callback.Run(*result != FLUSH_RESULT_FAILURE && | |
| 407 *result != FLUSH_RESULT_NO_DATA); | |
| 408 } | |
| 409 } | |
| 410 | |
| 411 void WebRtcRtpDumpWriter::OnDumpEnded(EndDumpContext context, | |
| 412 bool incoming, | |
| 413 bool success) { | |
| 414 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 415 | |
| 416 DVLOG(2) << "Dump ended, incoming = " << incoming | |
| 417 << ", succeeded = " << success; | |
| 418 | |
| 419 if (incoming) | |
| 420 context.incoming_succeeded = success; | |
| 421 else | |
| 422 context.outgoing_succeeded = success; | |
| 423 | |
| 424 // End the outgoing dump if needed. | |
| 425 if (incoming && context.type == RTP_DUMP_BOTH) { | |
| 426 FlushBuffer(false, | |
| 427 true, | |
| 428 base::Bind(&WebRtcRtpDumpWriter::OnDumpEnded, | |
| 429 weak_ptr_factory_.GetWeakPtr(), | |
| 430 context, | |
| 431 false)); | |
| 432 return; | |
| 433 } | |
| 434 | |
| 435 context.callback.Run(context.incoming_succeeded, context.outgoing_succeeded); | |
| 436 } | |
| OLD | NEW |