| 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; // In bytes. | |
| 18 | |
| 19 const unsigned char kRtpDumpFileHeaderFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n"; | |
| 20 static const size_t kRtpDumpFileHeaderSize = 16; // In bytes. | |
| 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); | |
| 27 | |
| 28 char* buffer = reinterpret_cast<char*>(&(*output)[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, uint16(0)); | |
| 46 buffer += sizeof(uint16); | |
| 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; // In bytes. | |
| 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)[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 dest->resize(old_dest_size + src_len); | |
| 84 memcpy(&(*dest)[old_dest_size], src, src_len); | |
| 85 } | |
| 86 | |
| 87 } // namespace | |
| 88 | |
| 89 // This class is running on the FILE thread for compressing and writing the | |
| 90 // dump buffer to disk. | |
| 91 class WebRtcRtpDumpWriter::FileThreadWorker { | |
| 92 public: | |
| 93 explicit FileThreadWorker(const base::FilePath& dump_path) | |
| 94 : dump_path_(dump_path) { | |
| 95 thread_checker_.DetachFromThread(); | |
| 96 | |
| 97 memset(&stream_, 0, sizeof(stream_)); | |
| 98 int result = deflateInit2(&stream_, | |
| 99 Z_DEFAULT_COMPRESSION, | |
| 100 Z_DEFLATED, | |
| 101 // windowBits = 15 is default, 16 is added to | |
| 102 // produce a gzip header + trailer. | |
| 103 15 + 16, | |
| 104 8, // memLevel = 8 is default. | |
| 105 Z_DEFAULT_STRATEGY); | |
| 106 DCHECK_EQ(Z_OK, result); | |
| 107 } | |
| 108 | |
| 109 ~FileThreadWorker() { | |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 111 } | |
| 112 | |
| 113 // Compresses the data in |buffer| and write to the dump file. If |end_stream| | |
| 114 // is true, the compression stream will be ended and the dump file cannot be | |
| 115 // written to any more. | |
| 116 void CompressAndWriteToFileOnFileThread( | |
| 117 scoped_ptr<std::vector<uint8> > buffer, | |
| 118 bool end_stream, | |
| 119 FlushResult* result, | |
| 120 size_t* bytes_written) { | |
| 121 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 122 | |
| 123 // This is called either when the in-memory buffer is full or the dump | |
| 124 // should be ended. | |
| 125 DCHECK(!buffer->empty() || end_stream); | |
| 126 | |
| 127 *result = FLUSH_RESULT_SUCCESS; | |
| 128 *bytes_written = 0; | |
| 129 | |
| 130 // There may be nothing to compress/write if there is no RTP packet since | |
| 131 // the last flush. | |
| 132 if (!buffer->empty()) { | |
| 133 *bytes_written = CompressAndWriteBufferToFile(buffer.get(), result); | |
| 134 } else if (!base::PathExists(dump_path_)) { | |
| 135 // If the dump does not exist, it means there is no RTP packet recorded. | |
| 136 // Return FLUSH_RESULT_NO_DATA to indicate no dump file created. | |
| 137 *result = FLUSH_RESULT_NO_DATA; | |
| 138 } | |
| 139 | |
| 140 if (end_stream && !EndDumpFile()) | |
| 141 *result = FLUSH_RESULT_FAILURE; | |
| 142 } | |
| 143 | |
| 144 private: | |
| 145 // Helper for CompressAndWriteToFileOnFileThread to compress and write one | |
| 146 // dump. | |
| 147 size_t CompressAndWriteBufferToFile(std::vector<uint8>* buffer, | |
| 148 FlushResult* result) { | |
| 149 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 150 DCHECK(buffer->size()); | |
| 151 | |
| 152 *result = FLUSH_RESULT_SUCCESS; | |
| 153 | |
| 154 std::vector<uint8> compressed_buffer; | |
| 155 if (!Compress(buffer, &compressed_buffer)) { | |
| 156 DVLOG(2) << "Compressing buffer failed."; | |
| 157 *result = FLUSH_RESULT_FAILURE; | |
| 158 return 0; | |
| 159 } | |
| 160 | |
| 161 int bytes_written = -1; | |
| 162 | |
| 163 if (base::PathExists(dump_path_)) { | |
| 164 bytes_written = base::AppendToFile( | |
| 165 dump_path_, | |
| 166 reinterpret_cast<const char*>(&compressed_buffer[0]), | |
| 167 compressed_buffer.size()); | |
| 168 } else { | |
| 169 bytes_written = base::WriteFile( | |
| 170 dump_path_, | |
| 171 reinterpret_cast<const char*>(&compressed_buffer[0]), | |
| 172 compressed_buffer.size()); | |
| 173 } | |
| 174 | |
| 175 if (bytes_written == -1) { | |
| 176 DVLOG(2) << "Writing file failed: " << dump_path_.value(); | |
| 177 *result = FLUSH_RESULT_FAILURE; | |
| 178 return 0; | |
| 179 } | |
| 180 | |
| 181 DCHECK_EQ(static_cast<size_t>(bytes_written), compressed_buffer.size()); | |
| 182 return bytes_written; | |
| 183 } | |
| 184 | |
| 185 // Compresses |input| into |output|. | |
| 186 bool Compress(std::vector<uint8>* input, std::vector<uint8>* output) { | |
| 187 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 188 int result = Z_OK; | |
| 189 | |
| 190 output->resize(std::max(kMinimumGzipOutputBufferSize, input->size())); | |
| 191 | |
| 192 stream_.next_in = &(*input)[0]; | |
| 193 stream_.avail_in = input->size(); | |
| 194 stream_.next_out = &(*output)[0]; | |
| 195 stream_.avail_out = output->size(); | |
| 196 | |
| 197 result = deflate(&stream_, Z_SYNC_FLUSH); | |
| 198 DCHECK_EQ(Z_OK, result); | |
| 199 DCHECK_EQ(0U, stream_.avail_in); | |
| 200 | |
| 201 output->resize(output->size() - stream_.avail_out); | |
| 202 | |
| 203 stream_.next_in = NULL; | |
| 204 stream_.next_out = NULL; | |
| 205 stream_.avail_out = 0; | |
| 206 return true; | |
| 207 } | |
| 208 | |
| 209 // Ends the compression stream and completes the dump file. | |
| 210 bool EndDumpFile() { | |
| 211 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 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[0]; | |
| 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 memset(&stream_, 0, sizeof(z_stream)); | |
| 230 | |
| 231 DCHECK(!output_buffer.empty()); | |
| 232 int bytes_written = | |
| 233 base::AppendToFile(dump_path_, | |
| 234 reinterpret_cast<const char*>(&output_buffer[0]), | |
| 235 output_buffer.size()); | |
| 236 | |
| 237 return bytes_written > 0; | |
| 238 } | |
| 239 | |
| 240 const base::FilePath dump_path_; | |
| 241 | |
| 242 z_stream stream_; | |
| 243 | |
| 244 base::ThreadChecker thread_checker_; | |
| 245 | |
| 246 DISALLOW_COPY_AND_ASSIGN(FileThreadWorker); | |
| 247 }; | |
| 248 | |
| 249 WebRtcRtpDumpWriter::WebRtcRtpDumpWriter( | |
| 250 const base::FilePath& incoming_dump_path, | |
| 251 const base::FilePath& outgoing_dump_path, | |
| 252 size_t max_dump_size, | |
| 253 const base::Closure& max_dump_size_reached_callback) | |
| 254 : max_dump_size_(max_dump_size), | |
| 255 max_dump_size_reached_callback_(max_dump_size_reached_callback), | |
| 256 total_dump_size_on_disk_(0), | |
| 257 incoming_file_thread_worker_(new FileThreadWorker(incoming_dump_path)), | |
| 258 outgoing_file_thread_worker_(new FileThreadWorker(outgoing_dump_path)), | |
| 259 weak_ptr_factory_(this) { | |
| 260 } | |
| 261 | |
| 262 WebRtcRtpDumpWriter::~WebRtcRtpDumpWriter() { | |
| 263 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 264 | |
| 265 bool success = BrowserThread::DeleteSoon( | |
| 266 BrowserThread::FILE, FROM_HERE, incoming_file_thread_worker_.release()); | |
| 267 DCHECK(success); | |
| 268 | |
| 269 success = BrowserThread::DeleteSoon( | |
| 270 BrowserThread::FILE, FROM_HERE, outgoing_file_thread_worker_.release()); | |
| 271 DCHECK(success); | |
| 272 } | |
| 273 | |
| 274 void WebRtcRtpDumpWriter::WriteRtpPacket(const uint8* packet_header, | |
| 275 size_t header_length, | |
| 276 size_t packet_length, | |
| 277 bool incoming) { | |
| 278 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 279 | |
| 280 static const size_t kMaxInMemoryBufferSize = 65536; | |
| 281 | |
| 282 std::vector<uint8>* dest_buffer = | |
| 283 incoming ? &incoming_buffer_ : &outgoing_buffer_; | |
| 284 | |
| 285 // We use the capacity of the buffer to indicate if the buffer has been | |
| 286 // initialized and if the dump file header has been created. | |
| 287 if (!dest_buffer->capacity()) { | |
| 288 dest_buffer->reserve(std::min(kMaxInMemoryBufferSize, max_dump_size_)); | |
| 289 | |
| 290 start_time_ = base::TimeTicks::Now(); | |
| 291 | |
| 292 // Writes the dump file header. | |
| 293 AppendToBuffer(kRtpDumpFileHeaderFirstLine, | |
| 294 arraysize(kRtpDumpFileHeaderFirstLine) - 1, | |
| 295 dest_buffer); | |
| 296 WriteRtpDumpFileHeaderBigEndian(start_time_, dest_buffer); | |
| 297 } | |
| 298 | |
| 299 size_t packet_dump_length = kPacketDumpHeaderSize + header_length; | |
| 300 | |
| 301 // Flushes the buffer to disk if the buffer is full. | |
| 302 if (dest_buffer->size() + packet_dump_length > dest_buffer->capacity()) | |
| 303 FlushBuffer(incoming, false, FlushDoneCallback()); | |
| 304 | |
| 305 WritePacketDumpHeaderBigEndian( | |
| 306 start_time_, packet_dump_length, packet_length, dest_buffer); | |
| 307 | |
| 308 // Writes the actual RTP packet header. | |
| 309 AppendToBuffer(packet_header, header_length, dest_buffer); | |
| 310 } | |
| 311 | |
| 312 void WebRtcRtpDumpWriter::EndDump(RtpDumpType type, | |
| 313 const EndDumpCallback& finished_callback) { | |
| 314 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 315 DCHECK(type == RTP_DUMP_OUTGOING || incoming_file_thread_worker_ != NULL); | |
| 316 DCHECK(type == RTP_DUMP_INCOMING || outgoing_file_thread_worker_ != NULL); | |
| 317 | |
| 318 bool incoming = (type == RTP_DUMP_BOTH || type == RTP_DUMP_INCOMING); | |
| 319 EndDumpContext context(type, finished_callback); | |
| 320 | |
| 321 // End the incoming dump first if required. OnDumpEnded will continue to end | |
| 322 // the outgoing dump if necessary. | |
| 323 FlushBuffer(incoming, | |
| 324 true, | |
| 325 base::Bind(&WebRtcRtpDumpWriter::OnDumpEnded, | |
| 326 weak_ptr_factory_.GetWeakPtr(), | |
| 327 context, | |
| 328 incoming)); | |
| 329 } | |
| 330 | |
| 331 size_t WebRtcRtpDumpWriter::max_dump_size() const { | |
| 332 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 333 return max_dump_size_; | |
| 334 } | |
| 335 | |
| 336 WebRtcRtpDumpWriter::EndDumpContext::EndDumpContext( | |
| 337 RtpDumpType type, | |
| 338 const EndDumpCallback& callback) | |
| 339 : type(type), | |
| 340 incoming_succeeded(false), | |
| 341 outgoing_succeeded(false), | |
| 342 callback(callback) { | |
| 343 } | |
| 344 | |
| 345 WebRtcRtpDumpWriter::EndDumpContext::~EndDumpContext() { | |
| 346 } | |
| 347 | |
| 348 void WebRtcRtpDumpWriter::FlushBuffer(bool incoming, | |
| 349 bool end_stream, | |
| 350 const FlushDoneCallback& callback) { | |
| 351 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 352 | |
| 353 scoped_ptr<std::vector<uint8> > new_buffer(new std::vector<uint8>()); | |
| 354 | |
| 355 if (incoming) { | |
| 356 new_buffer->reserve(incoming_buffer_.capacity()); | |
| 357 new_buffer->swap(incoming_buffer_); | |
| 358 } else { | |
| 359 new_buffer->reserve(outgoing_buffer_.capacity()); | |
| 360 new_buffer->swap(outgoing_buffer_); | |
| 361 } | |
| 362 | |
| 363 scoped_ptr<FlushResult> result(new FlushResult(FLUSH_RESULT_FAILURE)); | |
| 364 | |
| 365 scoped_ptr<size_t> bytes_written(new size_t(0)); | |
| 366 | |
| 367 FileThreadWorker* worker = incoming ? incoming_file_thread_worker_.get() | |
| 368 : outgoing_file_thread_worker_.get(); | |
| 369 | |
| 370 // Using "Unretained(worker)" because |worker| is owner by this object and it | |
| 371 // guaranteed to be deleted on the FILE thread before this object goes away. | |
| 372 base::Closure task = | |
| 373 base::Bind(&FileThreadWorker::CompressAndWriteToFileOnFileThread, | |
| 374 base::Unretained(worker), | |
| 375 Passed(&new_buffer), | |
| 376 end_stream, | |
| 377 result.get(), | |
| 378 bytes_written.get()); | |
| 379 | |
| 380 // OnFlushDone is necessary to avoid running the callback after this | |
| 381 // object is gone. | |
| 382 base::Closure reply = base::Bind(&WebRtcRtpDumpWriter::OnFlushDone, | |
| 383 weak_ptr_factory_.GetWeakPtr(), | |
| 384 callback, | |
| 385 Passed(&result), | |
| 386 Passed(&bytes_written)); | |
| 387 | |
| 388 // Define the task and reply outside the method call so that getting and | |
| 389 // passing the scoped_ptr does not depend on the argument evaluation order. | |
| 390 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, task, reply); | |
| 391 | |
| 392 if (end_stream) { | |
| 393 bool success = BrowserThread::DeleteSoon( | |
| 394 BrowserThread::FILE, | |
| 395 FROM_HERE, | |
| 396 incoming ? incoming_file_thread_worker_.release() | |
| 397 : outgoing_file_thread_worker_.release()); | |
| 398 DCHECK(success); | |
| 399 } | |
| 400 } | |
| 401 | |
| 402 void WebRtcRtpDumpWriter::OnFlushDone(const FlushDoneCallback& callback, | |
| 403 const scoped_ptr<FlushResult>& result, | |
| 404 const scoped_ptr<size_t>& bytes_written) { | |
| 405 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 406 | |
| 407 total_dump_size_on_disk_ += *bytes_written; | |
| 408 | |
| 409 if (total_dump_size_on_disk_ >= max_dump_size_ && | |
| 410 !max_dump_size_reached_callback_.is_null()) { | |
| 411 max_dump_size_reached_callback_.Run(); | |
| 412 } | |
| 413 | |
| 414 // Returns success for FLUSH_RESULT_MAX_SIZE_REACHED since the dump is still | |
| 415 // valid. | |
| 416 if (!callback.is_null()) { | |
| 417 callback.Run(*result != FLUSH_RESULT_FAILURE && | |
| 418 *result != FLUSH_RESULT_NO_DATA); | |
| 419 } | |
| 420 } | |
| 421 | |
| 422 void WebRtcRtpDumpWriter::OnDumpEnded(EndDumpContext context, | |
| 423 bool incoming, | |
| 424 bool success) { | |
| 425 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 426 | |
| 427 DVLOG(2) << "Dump ended, incoming = " << incoming | |
| 428 << ", succeeded = " << success; | |
| 429 | |
| 430 if (incoming) | |
| 431 context.incoming_succeeded = success; | |
| 432 else | |
| 433 context.outgoing_succeeded = success; | |
| 434 | |
| 435 // End the outgoing dump if needed. | |
| 436 if (incoming && context.type == RTP_DUMP_BOTH) { | |
| 437 FlushBuffer(false, | |
| 438 true, | |
| 439 base::Bind(&WebRtcRtpDumpWriter::OnDumpEnded, | |
| 440 weak_ptr_factory_.GetWeakPtr(), | |
| 441 context, | |
| 442 false)); | |
| 443 return; | |
| 444 } | |
| 445 | |
| 446 context.callback.Run(context.incoming_succeeded, context.outgoing_succeeded); | |
| 447 } | |
| OLD | NEW |